I have the snippet below in a function. To detect if a closed bar lies completely above a trade for buy or below for sell. Problem is that the global variable gets set immediately the trade opens ignoring the set conditions/rules that must be present for it to be set. Is this a mt4 bug or am I missing something?
It's not possible to answer from this little code snippet. If the GV is set, that means your condition is true, why you think it's false is not clear.
My bet is it's a bug in your code but I can't prove it without more information.
It's not possible to answer from this little code snippet. If the GV is set, that means your condition is true, why you think it's false is not clear.
My bet is it's a bug in your code but I can't prove it without more information.
GV gets set even when the open price is above previous bar low(Low[1]) for buy and vice versa for sell. Even adding more conditions to the if makes no change. I have provided the whole function below.
void GVsetter(int tsMagicNumber) { if(OrdersTotal()>0) { for(int tsi=1; tsi<=OrdersTotal(); tsi++) { // Cycle searching in orders if(OrderSelect(tsi-1,SELECT_BY_POS)==true && OrderMagicNumber()==tsMagicNumber && OrderSymbol()==Symbol()) { // If the next is available if(OrderType()==OP_BUY && OrderOpenTime()<Time[0])// for buy trades { string gvname1=(string)OrderTicket(); if(Low[1]>=OrderOpenPrice() ) { datetime setbuy=GlobalVariableSet(gvname1,Low[1]); } } if(OrderType()==OP_SELL && OrderOpenTime()<Time[0])// for sell trades { string gvname2=(string)OrderTicket(); if(High[1]<=OrderOpenPrice() ) { datetime setsell=GlobalVariableSet(gvname2,High[1]); } } //--------Trailing stop------------- } // if available } // cycle }// orders total } //------------------------------------------------------------------------------------
GV gets set even when the open price is above previous bar low(Low[1]) for buy and vice versa for sell. Even adding more conditions to the if makes no change. I have provided the whole function below.
The code seems correct.
You need to debug it, add some print the check the values in the log :
datetime setbuy=GlobalVariableSet(gvname1,Low[1]); printf("GV %s set, Low[1]=%f OOP=%f",gvname1,Low[1],OrderOpenPrice());
The code seems correct.
You need to debug it, add some print the check the values in the log :
If i set anything else to be executed in place of place of GlobalVariableSet() it works fine but with the GlobalVariableSet() the conditions get ignored.
The code seems correct.
You need to debug it, add some print the check the values in the log :
Just did that and nothing gets printed. Adding this comment below confirms the GV has been set because it shows some value as a comment immediately a trade opens even before the conditions are met.
Comment(GlobalVariableGet((string)OrderTicket()));
As in the image above at the top left corner you can see the value of the GV and as you can see visually the rules aren't met at all.
Check your whole code, the snippet you posted can not lead to this behaviour.
Its the only place that GVs are set. Perhaps its mt4 bug? Because the Print, Printf, Alert and other functions don't work because the condition hasn't been met. Create a dummy ea with the function and see.
Its the only place that GVs are set. Perhaps its mt4 bug? Because the Print, Printf, Alert and other functions don't work because the condition hasn't been met. Create a dummy ea with the function and see.
Silly question, but did you press F3 in the terminal and check global variables before, during and after you run your EA?
Is it possible that another copy of the EA, running in another window, sets GV values?
Also, global variables are not destroyed after your EA exits. As it is stated in a documentation: "Global variables exist in the client terminal during 4 weeks since their last use, then they are automatically deleted."
If all above checks, than listen what Alain told you and debug your code.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have the snippet below in a function. To detect if a closed bar lies completely above a trade for buy or below for sell. Problem is that the global variable gets set immediately the trade opens ignoring the set conditions/rules that must be present for it to be set. Is this a mt4 bug or am I missing something?