Few hints;
- you dont need to RefreshRates too often, better refresh it once right before first "for".
- You should not trust OrdersHistoryTotal()-1 to be last trade. I dont think its reliable fact, i would first create a for loop which finds latest trade. All depends what is for you latest trade. It can be OrderID, EntryTime, ExitTime, you have to judge it yourself what is for you "last". Then i would compare just this ticket.
bool CheckHistory() { RefreshRates(); int lastTrade=-1; // Find latest Trade, this assmes, last OrderID is last ticket for(int i=0;i<OrdersHistoryTotal();i++) if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) if (OrderTicket()>lastTrade) lastTrade=OrderTicket(); // Now lets load this last Trade and decide if it hit SL if (lastTrade>-1 && OrderSelect(lastTrade,SELECT_BY_TICKET,MODE_HISTORY)) { if( OrderMagicNumber() == MagicNumber1 ) if(OrderSymbol() == Symbol()) { if( OrderType() == OP_BUY && OrderClosePrice() <= OrderStopLoss() ) { //H1_Buy_Touch = "None"; HistoryBarTime = OrderCloseTime(); return(true); } if( OrderType() == OP_SELL && OrderClosePrice() >= OrderStopLoss()) { //H1_Sell_Touch = "None"; HistoryBarTime = OrderCloseTime(); return(true); } } } HistoryBarTime = 0; return(false); }
You don't do any checks to see if it was actually the last trade.
What is the last trade? Last by open time, last by close time?
If your code does find the last trade and it hit TP, it will be ignored and your loop will continue until it finds a trade that matches the criteria .
If a single trade in History has the same magic number and Symbol and also OP_BUY && OrderStopLoss() >= OrderClosePrice() or OP_SELL && OrderClosePrice() >= OrderStopLoss() is true
the function will return true
if( OrderType() == OP_BUY && OrderStopLoss() >= OrderClosePrice())
double OCP = OrderClosePrice(); bool isCloseBySL = MathAbs( OCP - OrderStopLoss() ) < MathAbs( OCP - OrderTakeProfit() );
Thanks for your inputs everyone. Just to be clear, what I mean by "Last Trade" is, I want to check the LAST closed trade on Symbol == OrderSymbol() && OrderMagicNumber() == MagicNumber and find it if it hit it's stop. If it has not hit it's stop, then the Bool CheckHistory
returns(false);
When it gets called again, then check if it hit stop, if so return(true). I am only interested in the very last trade on this pair and it's outcome.
I'll have a play with the feedback you guys have given, thanks!
//+------------------------------------------------------------------+ //| Check History to see if trade hit stop | //+------------------------------------------------------------------+ bool CheckHistory() { RefreshRates(); bool isCloseBySL; int lastTrade=-1; for(int i=0;i<OrdersHistoryTotal();i++) // < --- Is this bit definitely right? if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) if (OrderTicket()>lastTrade) lastTrade=OrderTicket(); if (lastTrade>-1 && OrderSelect(lastTrade,SELECT_BY_TICKET,MODE_HISTORY)) { if( OrderMagicNumber() == MagicNumber1 ) if(OrderSymbol() == Symbol()) { if( OrderType() == OP_BUY ) { double OCP = OrderClosePrice(); isCloseBySL = MathAbs( OCP - OrderStopLoss() ) < MathAbs( OCP - OrderTakeProfit() ); if( OrderStopLoss() >= OrderClosePrice() && isCloseBySL ) { HistoryBarTime = OrderCloseTime(); return(true); } } if( OrderType() == OP_SELL ) { double OCP = OrderClosePrice(); isCloseBySL = MathAbs( OrderStopLoss() - OCP ) < MathAbs( OrderTakeProfit() - OCP ); if( OrderClosePrice() >= OrderStopLoss() && isCloseBySL) { HistoryBarTime = OrderCloseTime(); return(true); } } } } HistoryBarTime = 0; return(false); }Thanks for your help everyone. I have a question regarding the above though; do I increment or decrement when select historical trades? (highlighted line above?)
I completely forgot to ask - as this function is under "OnTick()" is there a crafty way so that rather than check this function on EVERY SINGLE TICK (which is a waste of resources), I want to run this function once (but I don't know how to determine "once" I cannot use OrderOpenThis == 0"), check the last trade, "did it hit stop?", if yes or no, then you do not need to run it again until next trade is OPENED.
I am finding this part to be slowing down my back-testing a little bit. (sorry if thats vague).
DomGilberto: is there a crafty way so that rather than check this function on EVERY SINGLE TICK (which is a waste of resources), I want to run this function once |
OnTick(){ static int OOC=0; int OOCprev = OOC; OOC = OrdersTotal(); if(OOC < OOCprev){ CheckHistory(); // An order closed |
DomGilberto: is there a crafty way so that rather than check this function on EVERY SINGLE TICK (which is a waste of resources), I want to run this function once |
OnTick(){ static int OOC=0; int OOCprev = OOC; OOC = OrdersTotal(); if(OOC < OOCprev){ CheckHistory(); // An order closed
What if one or more orders were opened and less or the same amount of orders closed in between executions of this code?
Would it be better to see if OrdersHistoryTotal had increased?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Don't want this conflicting with other pairs and also looking at wrong trade...