in my code I found that OrderProfit() just returned zero if the profit was less than 1 (0.02 or -0.04 etc.)
Then there is something wrong with your code.
Maybe you are using an int instead of a double?
This is the begging of my code
void OnTick() { for(int i=OrdersTotal()-1; i>=0; i--) if(OrderSymbol()==Symbol()) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) and this is the end } else Print("OrderSelect returned the error of ",GetLastError()); if(print){Print("Symbol:"+OrderSymbol()+" Profit="+OrderProfit()+" OrderLots="+OrderLots());}
I changed the begging of my code to be:
void OnTick() { for(int i=OrdersTotal()-1; i>=0; i--) {OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderSymbol()==Symbol()) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
but this didn't solve it too
void OnTick() { for(int i=OrdersTotal()-1; i>=0; i--) {OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderSymbol()==Symbol()) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
Why are you selecting the order twice?
Please show the complete code for the loop as it is quite likely that you are selecting an order that does not exist or that your print is not even in the loop.
I am selecting the order twice for the check in between the 2 selects
if(OrderSymbole()==Symbol()) <<<<<
Yes the print is not in the loop, this loop is to select the order which is currently having the ea running on it (or there is better way to do this)
Here is the code:
void OnTick() { //Print("OrdersTotal: "+OrdersTotal()); for(int i=OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); Print("Order"+i+" symbole"+OrderSymbol()); if(OrderSymbol()==Symbol()) OrderSelect(i,SELECT_BY_POS,MODE_TRADES); } // { if(oldProfit==-999)//start { lossSteps=0; profitSteps=0; profitStepsTotal=0; oldProfit=OrderProfit(); } else { if(OrderProfit()<0) { /////////////////////////////////lossing case profitSteps=0; profitStepsTotal=0; if(oldProfit>OrderProfit()) { profitSteps=0; lossSteps++; if(anyStepBackStop) anyStepBackStopExecute=true; } else if(oldProfit<OrderProfit()) { lossSteps=0; //profitSteps++; } if(oldProfit!=OrderProfit()) { if(print) Print("Symbol:"+OrderSymbol()+" Profit="+OrderProfit() + " lossSteps="+lossSteps); fullHistory+="\n"+"Profit="+OrderProfit() + " lossSteps="+lossSteps; } oldProfit=OrderProfit(); }///////////////////////////////// end of losing case else////////////////////// no profit no loss case if(OrderProfit()==0) { profitSteps=0; lossSteps=0; }/////////////////// end of no profit no loss case else if(OrderProfit()>0) { /////////////////////////// profitable case lossSteps=0; if(oldProfit>OrderProfit()) { profitSteps=0; // lossSteps++; if(anyStepBackStop) anyStepBackStopExecute=true; } else if(oldProfit<OrderProfit()) { lossSteps=0; profitStepsTotal++; profitSteps++; } if(oldProfit!=OrderProfit()) { if(print) Print("Symbol:"+OrderSymbol()+ " Profit="+OrderProfit() + " profitSteps="+profitSteps +" ProfitStepsTotal="+profitStepsTotal); fullHistory+="\n"+"Profit="+OrderProfit() + " profitSteps="+profitSteps +" ProfitStepsTotal="+profitStepsTotal; } oldProfit=OrderProfit(); }////////////////////////// end of profitable case } if(profitSteps>=Profit_steps||profitStepsTotal>=Profit_steps_total||OrderProfit()>Take_Profit_Over) anyStepBackStop=true; //close trade upon steps if(lossSteps>=Loss_steps) //close trade for continuse lossing { if(print) { OrderPrint(); Print("Symbol:"+OrderSymbol()+" close on loss"); Print("Close on loss, lossSteps="+lossSteps); } SendMail("Trade closed on loss",fullHistory+"\nClose on loss, lossSteps="+lossSteps); OrderClose(OrderTicket(),OrderLots(),Ask,1000,Red); oldProfit=-999; profitSteps=0; lossSteps=0; fullHistory=""; anyStepBackStop=false; anyStepBackStopExecute=false; } else//close trade on profit if(anyStepBackStopExecute) //close trade after continuse profit steps { if(print) { OrderPrint(); Print("Symbol:"+OrderSymbol()+" Close on profit, profitSteps="+profitSteps+" and profitStepsTotal="+profitStepsTotal+ " Take profit over="+Take_Profit_Over); } SendMail("Trade closed on Profit",fullHistory+"\nClose on profit, profitSteps="+profitSteps+" and profitStepsTotal="+profitStepsTotal+ " Take profit over="+Take_Profit_Over); OrderClose(OrderTicket(),OrderLots(),Ask,1000,Red); oldProfit=-999; profitSteps=0; lossSteps=0; fullHistory=""; anyStepBackStop=false; anyStepBackStopExecute=false; } // } // else Print("OrderSelect returned the error of ",GetLastError()); // if(print){Print("Symbol:"+OrderSymbol()+" Profit="+DoubleToStr(OrderProfit())+" OrderLots="+OrderLots());} Print("Symbol: "+Symbol()+ " OrerSymbol: "+OrderSymbol()); }
I am selecting the order twice for the check in between the 2 selects
if(OrderSymbole()==Symbol()) <<<<<
Yes the print is not in the loop, this loop is to select the order which is currently having the ea running on it (or there is better way to do this)
Here is the code:
void OnTick() { //Print("OrdersTotal: "+OrdersTotal()); for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { Print("Order"+i+" symbole"+OrderSymbol()); if(OrderSymbol()!= Symbol()) continue; // { if(oldProfit==-999)//start { lossSteps=0; profitSteps=0; profitStepsTotal=0; oldProfit=OrderProfit(); } else { if(OrderProfit()<0) { /////////////////////////////////lossing case profitSteps=0; profitStepsTotal=0; if(oldProfit>OrderProfit()) { profitSteps=0; lossSteps++; if(anyStepBackStop) anyStepBackStopExecute=true; } else if(oldProfit<OrderProfit()) { lossSteps=0; //profitSteps++; } if(oldProfit!=OrderProfit()) { if(print) Print("Symbol:"+OrderSymbol()+" Profit="+OrderProfit() + " lossSteps="+lossSteps); fullHistory+="\n"+"Profit="+OrderProfit() + " lossSteps="+lossSteps; } oldProfit=OrderProfit(); }///////////////////////////////// end of losing case else////////////////////// no profit no loss case if(OrderProfit()==0) { profitSteps=0; lossSteps=0; }/////////////////// end of no profit no loss case else if(OrderProfit()>0) { /////////////////////////// profitable case lossSteps=0; if(oldProfit>OrderProfit()) { profitSteps=0; // lossSteps++; if(anyStepBackStop) anyStepBackStopExecute=true; } else if(oldProfit<OrderProfit()) { lossSteps=0; profitStepsTotal++; profitSteps++; } if(oldProfit!=OrderProfit()) { if(print) Print("Symbol:"+OrderSymbol()+ " Profit="+OrderProfit() + " profitSteps="+profitSteps +" ProfitStepsTotal="+profitStepsTotal); fullHistory+="\n"+"Profit="+OrderProfit() + " profitSteps="+profitSteps +" ProfitStepsTotal="+profitStepsTotal; } oldProfit=OrderProfit(); }////////////////////////// end of profitable case } if(profitSteps>=Profit_steps||profitStepsTotal>=Profit_steps_total||OrderProfit()>Take_Profit_Over) anyStepBackStop=true; //close trade upon steps if(lossSteps>=Loss_steps) //close trade for continuse lossing { if(print) { OrderPrint(); Print("Symbol:"+OrderSymbol()+" close on loss"); Print("Close on loss, lossSteps="+lossSteps); } SendMail("Trade closed on loss",fullHistory+"\nClose on loss, lossSteps="+lossSteps); OrderClose(OrderTicket(),OrderLots(),Ask,1000,Red); oldProfit=-999; profitSteps=0; lossSteps=0; fullHistory=""; anyStepBackStop=false; anyStepBackStopExecute=false; } else//close trade on profit if(anyStepBackStopExecute) //close trade after continuse profit steps { if(print) { OrderPrint(); Print("Symbol:"+OrderSymbol()+" Close on profit, profitSteps="+profitSteps+" and profitStepsTotal="+profitStepsTotal+ " Take profit over="+Take_Profit_Over); } SendMail("Trade closed on Profit",fullHistory+"\nClose on profit, profitSteps="+profitSteps+" and profitStepsTotal="+profitStepsTotal+ " Take profit over="+Take_Profit_Over); OrderClose(OrderTicket(),OrderLots(),Ask,1000,Red); oldProfit=-999; profitSteps=0; lossSteps=0; fullHistory=""; anyStepBackStop=false; anyStepBackStopExecute=false; } // } // else Print("OrderSelect returned the error of ",GetLastError()); // if(print){Print("Symbol:"+OrderSymbol()+" Profit="+DoubleToStr(OrderProfit())+" OrderLots="+OrderLots());} Print("Symbol: "+Symbol()+ " OrerSymbol: "+OrderSymbol()); } } }
You might have to study the differences in both code, you were selecting order twice which has no meaningful impact, yet you pushed all codes that relied on the order selection outside the for loop, that's buggy and have unpredictable consequences if no order selected.
In fact, reading the code once again made me feel so bad you might not be getting what you are expecting with the whole thing I even modified, your else if being in a different line doesn't make any sense regard to what you want it to do, I'm sure you need the help of freelancers to achieve the same goal you hoped for...
- www.mql5.com
Yes the print is not in the loop, this loop is to select the order which is currently having the ea running on it (or there is better way to do this)
Never work with a selected order outside of the loop where it is selected (except maybe in a function called from within the loop after the order select, though still unadvisable.)
Thank you Keith Watford
Thank you Racheal Samson, this works
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello Everyone,
in my code I found that OrderProfit() just returned zero if the profit was less than 1 (0.02 or -0.04 etc.)
So is there a way to let it show the exact profit value of a trade just like it showing up in the Trade tab?
Thanks in advance