counting of hourly bars for current daily session in the code.... help

Questions about MultiCharts and user contributed studies.
auato
Posts: 89
Joined: 23 Nov 2016
Has thanked: 18 times

counting of hourly bars for current daily session in the code.... help

Postby auato » 19 Aug 2017

Hi,
I would like to create a code in which I use the counting of hourly bars. For example:

Code: Select all

if <I am in the first bar of the current day> AND h[4]<h[5] AND h[3]<h[4] then sellshort ("entry_1") 1 contracts next bar...
if <I am in the second bar of the current day> AND h[4]<h[5] AND h[1]<h[4] then sellshort ("entry_2") 1 contracts next bar...
I am not able to realize the fisrt conditions:
if I am in the first/second bar...
and, at the same time, I also want avoid to use commands based on the time.

Could you suggest something or address me to a workaround?

Thank you,
auato

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: counting of hourly bars for current daily session in the code.... help

Postby JoshM » 19 Aug 2017

I would like to create a code in which I use the counting of hourly bars. At the same time, I also want avoid to use commands based on the time.

Could you suggest something or address me to a workaround?
I don't think this is possible. Because without the PowerLanguage keywords for time, date, or sessions, there doesn't seem to be a way in which your script can know how many bars the current bar is from the start of the session. It simply has to know that in order to start counting from that point.

It's the same as when you want to count the number of bars since the recent highest high. In order to do that, you'll first need to calculate the highest high. And then you can count the bars since that point. Does this make sense? (Or did I misunderstood what you're saying here?)

auato
Posts: 89
Joined: 23 Nov 2016
Has thanked: 18 times

Re: counting of hourly bars for current daily session in the code.... help

Postby auato » 19 Aug 2017

Thank you JoshM.
OK, it's clear. I would like to avoid the time based commands because with my better settings in Quote Manager (sessions start from 8:59am to 8:34pm) and with hourly resolution (60minutes or 1hour) I notice that all events occur at 9:59 (first bar) or 10:59 (second bar) or 11:59 (third bar) and so on.. produce badly entries on the next bar on the chart. For example in this picture I attach below the entry "Tmp_V2_1" should be on the first bar (bar 9:59) and not on the second one:
Capture.PNG
(4.84 KiB) Downloaded 671 times
Indeed the part of code for this entry is here:

Code: Select all

if time > 900 and time <= 959 then Begin //if I am in the first hourly bar (9:59)
if h[4]<h[5] AND h[3]<h[4] AND c[1]<DEMA and marketposition=0 and daily_entries=0 then Begin
if ((o[3]-c[3])/o[3])<0.002 then Begin
sellshort ("TmpV2_1") 1 contracts next bar at valenter limit;
condEntry=1;
End;
End;
End;
The same happens for the other bars if all conditions are verified at the 59th minute. Analysing with print debugging the strange behaviour I realized that if I remove "=" from the code changing into:
if time > 900 and time <959 then Begin..., the change seems to correct the problem but I will lose all 59' which means a sensitive decreasing of number of entries in my study. It's really odd :? because hh:59 should be in the previous bar as I already mentioned that the my session starts at 8:59 and resolution is @60 minutes. That's why I was searching a workaround to bypass time.

Thank you

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: counting of hourly bars for current daily session in the code.... help

Postby TJ » 19 Aug 2017

...
I am not able to realize the fisrt conditions:
if I am in the first/second bar...


First bar of the current day:

Code: Select all


var:
First.Bar( false );

First.Bar = Date > Date[1];


If First.Bar = true then
Begin
// your trading logic here
End;

auato
Posts: 89
Joined: 23 Nov 2016
Has thanked: 18 times

Re: counting of hourly bars for current daily session in the code.... help

Postby auato » 22 Aug 2017

OK, anyway it seems I have some issues with time aggregation or some incorrect configuration in my MC to resolve... but I am experiencig it's really hard to get out from this problem (it looks like there is a mismatching between the time used in the study and checked by print command and the time used on the chart).
So I am thinking to bypass all my trubles in another way and that is why I am asking if in your opinion it is possible to use the same instrument (symbol) with different configuration settings in quote manager. To better explain: I would like to operate with two different studies on the same symbol but with different time resolutions (5 minutes and hourly) and different time sessions.


Return to “MultiCharts”