Here are a few checks before invoking PositionModify:
bool CheckStopLoss_Takeprofit(ENUM_ORDER_TYPE type,double SL,double TP) { //--- get the SYMBOL_TRADE_STOPS_LEVEL level int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL); if(stops_level!=0) { PrintFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss and TakeProfit must"+ " not be nearer than %d points from the closing price",stops_level,stops_level); } //--- bool SL_check=false,TP_check=false; //--- check only two order types switch(type) { //--- Buy operation case ORDER_TYPE_BUY: { //--- check the StopLoss SL_check=(Bid-SL>stops_level*_Point); if(!SL_check) PrintFormat("For order %s StopLoss=%.5f must be less than %.5f"+ " (Bid=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type),SL,Bid-stops_level*_Point,Bid,stops_level); //--- check the TakeProfit TP_check=(TP-Bid>stops_level*_Point); if(!TP_check) PrintFormat("For order %s TakeProfit=%.5f must be greater than %.5f"+ " (Bid=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type),TP,Bid+stops_level*_Point,Bid,stops_level); //--- return the result of checking return(SL_check&&TP_check); } //--- Sell operation case ORDER_TYPE_SELL: { //--- check the StopLoss SL_check=(SL-Ask>stops_level*_Point); if(!SL_check) PrintFormat("For order %s StopLoss=%.5f must be greater than %.5f "+ " (Ask=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type),SL,Ask+stops_level*_Point,Ask,stops_level); //--- check the TakeProfit TP_check=(Ask-TP>stops_level*_Point); if(!TP_check) PrintFormat("For order %s TakeProfit=%.5f must be less than %.5f "+ " (Ask=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)", EnumToString(type),TP,Ask-stops_level*_Point,Ask,stops_level); //--- return the result of checking return(TP_check&&SL_check); } break; } //--- a slightly different function is required for pending orders return false; }
Had to disable position types and position symbols but I still get invalid stops for sell modifying.
for(int Loop=PositionsTotal()-1;Loop>=0;Loop--) { if(m_position.SelectByIndex(Looop)) { TPBuy=Bid+TakeProfit*Pips(); SLBuy=Bid-StopLoss*Pips(); bool ModBuy=trade.PositionModify(m_position.Ticket(),SLBuy,TPBuy); TPSell=Bid-TakeProfit*Pips(); SLSell=Bid+StopLoss*Pips(); bool ModSell=trade.PositionModify(m_position.Ticket(),SLSell,TPSell); } }
I still get Invalid stops even when Trade stop levels is being added:
int StopLevels=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL); for(int Loop=PositionsTotal()-1;Loop>=0;Loop--) { if(m_position.SelectByIndex(Loop)) { TPBuy=Bid+TakeProfit*Pips(); SLBuy=Bid-StopLoss*Pips(); TPBuy=TPBuy-Bid>StopLevels*Pips(); SLBuy=Bid-SLBuy>StopLevels*Pips(); bool ModBuy=trade.PositionModify(m_position.Ticket(),SLBuy,SLBuy); TPSell=Bid-TakeProfit*Pips(); SLSell=Bid+StopLoss*Pips(); TPSell=TPSell-Ask>StopLevels*Pips(); SLSell=Ask-SLSell>StopLevels*Pips(); bool ModSell=trade.PositionModify(m_position.Ticket(),SLSell,TPSell); }
I tried using Round to tick size but I still get invalid stops why is this?
double RoundToTickSize(double Price) { double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE); return(round(Price/TickSize)*TickSize); } void OnStart() { double TPBuy=0; double SLBuy=0; double TPSell=0; double SLSell=0; double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID); for(int Loop=PositionsTotal()-1;Loop>=0;Loop--) { if(m_position.SelectByIndex(Loop)) { TPBuy=RoundTickSize(Bid+TakeProfit*Pips()); SLBuy=RoundTickSize(Bid-StopLoss*Pips()); trade.PositionModify(m_position.Ticket(),SLBuy,TPBuy); TPSell=RoundTickSize(Bid-TakeProfit*Pips()); SLSell=RoundTickSize(Bid+StopLoss*Pips()); trade.PositionModify(m_position.Ticket(),SLSell,TPSell); } } }
have you tried using a bigger value for the StopLoss input? many brokers don't allow the stop loss to be set so narrow and the minimum SL can change just as the slippage can change when the market is volatile
I know on the first post my inputs are 0 SL and 0 TP but these are my default inputs for StopLoss and TakeProfit.
input double TakeProfit=5; input double StopLoss=10;
I know on the first post my inputs are 0 SL and 0 TP but these are my default inputs for StopLoss and TakeProfit.
right but what are you setting SL to? try 10, 100, 1000, and see how it looks in the strategy tester visual mode. One pip size in EURJPY is 0.01 I think, and the SL has to be set accordingly
- 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 All
My modify TP/SL code below won't modify sell positions instead I get modify errors. With Buy positions everything modifies as normal, please assist me as to why sell positions will not modify.