need help with some simple coding!

 

Hi all,

i am trying to code this part on "at the start of say, m5 candle, check the trend". how do I code it?

I tried to test code -- at the start of every 5 min candle, enter a buy trade.

CurrentTimeM5 = iTime(NULL, PERIOD_M5,1);
CurrentTimeM1 = iTime (NULL, PERIOD_M1, 1);
if( CurrentTimeM5 == CurrentTimeM1) /* used to denote start of every 5 min candle*/

OrderSend (NULL,OP_BUY, 3, Ask,0,0,Ask+ 30*Point,NULL,CLR_NONE);

I use " if( CurrentTimeM5 == CurrentTimeM1)" to denote the start of the m5 candle since both M5 and M1 still show the same timing.

However when I tried to run the above, no buy trades is entered!,

is there something wrong with my "f( CurrentTimeM5 == CurrentTimeM1)" or is there something wrong with my ordersend?

appreciate some help. Thanks.

 

What chart timeframe are you using?

If going backwards in time just keep incrementing the shift value by one in CurrentTimeM5 = iTime(NULL, PERIOD_M5,1); to get the previous value.

Otherwise not enough code to be able to answer your question.

 
yuan83:
i am trying to code this part on "at the start of say, m5 candle, check the trend". how do I code it?

I tried to test code -- at the start of every 5 min candle, enter a buy trade.

CurrentTimeM5 = iTime(NULL, PERIOD_M5,1);
CurrentTimeM1 = iTime (NULL, PERIOD_M1, 1);
if( CurrentTimeM5 == CurrentTimeM1) /* used to denote start of every 5 min candle*/

  1. iTime(M5,1) is the start of previous 5 minute candle. iTime(M1,1) is the start of the previous 1 minute candle. The two will never be equal. Had you used (M5,0) and (M1,0) then your code would have worked except it would be true for one minute and you would have opened multiple orders (one per tick.)

    To check for the start of a new candle on whatever timeframe the chart is on is easier

    int start(){
       static datetime Time0; if (Time0 == Time[0]) return(0); Time0 = Time[0];
       : // Start of a new bar.

 

Hi both,

thanks alot. hmm i used 0 as the shift actually. but actually nth happened i think, no buy order was started.

anyway i was opening it on a 5 min chart.

WHRoeder i will try your recommendation as per the code you showed here.

However if i use 2 different time frame, say start of 5 min candle and start of 1 hr candle, while i was using on a 5 min chart, how do i code for the 1 hr candle ? could you advise?

Thanks!

 

by the way, WHRoeder do u mind sharing with me, how does the code works. why do u need

return(0); Time0 = Time[0];

 
yuan83:

by the way, WHRoeder do u mind sharing with me, how does the code works. why do u need

return(0); Time0 = Time[0];

Read, learn, understand.

This will help: Documentation

 
yuan83:

by the way, WHRoeder do u mind sharing with me, how does the code works. why do u need

return(0); Time0 = Time[0];

return(0); // Same bar as last tick
Time0 = Time[0]; // Remember this new bar for next tick
 
yuan83:

WHRoeder i will try your recommendation as per the code you showed here.

However if i use 2 different time frame, say start of 5 min candle and start of 1 hr candle, while i was using on a 5 min chart, how do i code for the 1 hr candle ? could you advise?

bool newH1Bar = Time[0] % (PERIOD_H1 * 60) == 0;
Reason: