variables losing value

 

Hi all MQL4 experts,

i'm going crazy on this one! I want to make one trade per candle so use the code below to set 'newcandle' to Time[0] once a trade has been made. 'Newcandle' variable loses its' value and returns to 0 when the routine is exited - so on the next tick the routine is entered again because 'newcandle = 0!!. Why does it lose its' value

thanks for your help.

int start;

{

int i;
datetime newcandle;
double buyprice,sellprice;
double sh, sl,high,low,lots,spread;
double highstop, lowstop;
double hightp,lowtp;

Print("enter candle =", newcandle);

if(newcandle!= Time[0]) //check for new candle
{
buyprice=high+1*Point;

sellprice=low-1*Point;

hightp=Ask + tp * Point;
lowtp=Bid - tp * Point;

highstop=High[1];
lowstop=Low[1];
if(Ask>=buyprice) buyprice=Ask;
if(Bid<=sellprice)sellprice=Bid;

OrderSend(Symbol(),OP_BUY,lots,buyprice,3,lowstop,hightp); //open BUY order
OrderSend(Symbol(),OP_SELL,lots,sellprice,3,highstop,lowtp); //open SELL order

if(OrderType()==OP_BUY || OrderType()==OP_SELL) newcandle=Time[0]; //Set current time to old time for next check
return(0);
}

Print("exit candle =", newcandle);

return(0);

}

 
sd59:

Hi all MQL4 experts,

i'm going crazy on this one! I want to make one trade per candle so use the code below to set 'newcandle' to Time[0] once a trade has been made. 'Newcandle' variable loses its' value and returns to 0 when the routine is exited - so on the next tick the routine is entered again because 'newcandle = 0!!. Why does it lose its' value

thanks for your help.


Because you declare the variable in start so every tick, the variable is re initialised. Either make it a static or declare it on the global scope.

https://book.mql4.com/variables/types


V


 
Viffer:

Because you declare the variable in start so every tick, the variable is re initialised. Either make it a static or declare it on the global scope.

https://book.mql4.com/variables/types


V



thanks Viffer - makes a lot of sense!!
Reason: