- doing different activities depending TP or SL accrued
- Can I know the order be stoploss in OnTrade Even?
- "wrong" arrows in indicator
Is there any way to know that SL was hit? I can use OnTrade(), but I won't be able to know the reason why the order closed or am I missing something?
You're right - there doesn't seem to be an order reason code. You'd probably be able to infer it from the fact that you got an OnTrade event when your EA didn't send an order, but the code would be a little complex.
Paul
You're right - there doesn't seem to be an order reason code. You'd probably be able to infer it from the fact that you got an OnTrade event when your EA didn't send an order, but the code would be a little complex.
Paul
Try this for Market Stops. May not always be the exact Market Stop, but should be very close.
if(PositionsTotal() > 0) { if(PositionSelect(_Symbol)) { if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { if(SymbolInfoDouble(Symbol(), SYMBOL_BID) >= PositionGetDouble(POSITION_TP) ) { Print("Buy Position Closed at TP ",DoubleToString(PositionGetDouble(POSITION_TP),_Digits)); } else if(SymbolInfoDouble(Symbol(), SYMBOL_BID) <= PositionGetDouble(POSITION_SL) ) { Print("Buy Position Closed at SL ",DoubleToString(PositionGetDouble(POSITION_SL),_Digits)); } } if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) { if(SymbolInfoDouble(Symbol(), SYMBOL_ASK) <= PositionGetDouble(POSITION_TP) ) { Print("Sell Position Closed at TP ",DoubleToString(PositionGetDouble(POSITION_TP),_Digits)); } else if(SymbolInfoDouble(Symbol(), SYMBOL_ASK) >= PositionGetDouble(POSITION_SL) ) { Print("Sell Position Closed at SL ",DoubleToString(PositionGetDouble(POSITION_SL),_Digits)); } } } }
Wait, but
PositionsTotal()
will return 0 if it's already closed, won't it?
Wait, but
will return 0 if it's already closed, won't it?
While PositionsTotal() > 0, assigned TP and SL to double variables. Then values will be available when PositionsTotal() == 0. You could also use HistorySelect() feature to find last order TP and SL.
double BuyTP, SellTP, BuySL, SellSL; uint total=0; ulong ticket; HistorySelect(0,TimeCurrent()); total=HistoryOrdersTotal(); // This number is last order filed. if(PositionsTotal() > 0) ticket=total; else ticket=total-1; // Skip last order when PositionsTotal()=0 for(uint i=0;i<ticket;i++) { if( HistoryOrderGetString(i,ORDER_SYMBOL)==_Symbol ) { if(HistoryOrderGetInteger(i,ORDER_TYPE)==ORDER_TYPE_BUY) { BuyTP=HistoryOrderGetDouble(i,ORDER_TP); BuySL=HistoryOrderGetDouble(i,ORDER_SL); } if(HistoryOrderGetInteger(ticket,ORDER_TYPE)==ORDER_TYPE_SELL) { SellTP=HistoryOrderGetDouble(i,ORDER_TP); SellSL=HistoryOrderGetDouble(i,ORDER_SL); } } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use