Script: Invalid Stops Take Profit & Stop Loss (Sell Positions) - page 2

 
Conor Mcnamara #:

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

I tried all numbers 10 to 1000 I still get invalid stops instead of using my pips function I assumed that might be the problem

double Pips()
 {
  double PipPoint=0;
  int Digit=(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);
  if(Digit==2||Digit==3||Digit==5){PipPoint=Point()*10;}
  return(PipPoint);
 }

switched to "_point" function but I still encounter the same problem invalid stops so that's not the problem. 

 
Scalper8 #:

I tried using Round to tick size but I still get invalid stops why is this?

Use ASK for modifying buy positions, use BID for modifying sell positions. You have it the other way around here. The Ask price is the available buy price. The Bid is the price you sell at. switch it around (even though it might not resolve this issue)

I don't use this function "SelectByIndex", try this instead:


   for(int i=0; i<PositionsTotal(); i++)
     {
      if(PositionSelectByTicket(PositionGetTicket(i)))
        {
 
Conor Mcnamara #:

Use ASK for modifying buy positions, use BID for modifying sell positions. You have it the other way around here. The Ask price is the available buy price. The Bid is the price you sell at. switch it around (even though it might not resolve this issue)

I don't use this function "SelectByIndex", try this instead:


Don't you count down when looping? I tried using position get ticket and I still get invalid stops

double RoundToTickSize(double Price)
 {
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  
  return(round(Price/TickSize)*TickSize);
 }

for(int Loop=PositionsTotal()-1;Loop>=0;Loop--)
   {
    if(PositionSelectByTicket(PositionGetTicket(Loop)))
    {
     TPBuy=RoundToTickSize(Bid+TakeProfit*Pips());
     SLBuy=RoundToTickSize(Bid-StopLoss*Pips());
     
     trade.PositionModify(m_position.Ticket(),SLBuy,TPBuy);
     
     TPSell=RoundToTickSize(Bid-TakeProfit*Pips());
     SLSell=RoundToTickSize(Bid+StopLoss*Pips());
     
     trade.PositionModify(m_position.Ticket(),SLSell,TPSell);
     
    }
   }
 
Scalper8 #:

Don't you count down when looping? I tried using position get ticket and I still get invalid stops

I still get invalid stops even when checking stop levels

double RoundToTickSize(double Price)
 {
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  
  return(round(Price/TickSize)*TickSize);
 } 

bool StopLevelCheck(ENUM_POSITION_TYPE Type,double SL,double TP)
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  bool TPCheck,SLCheck=false;
  
  switch(Type)
  {
   case POSITION_TYPE_BUY:
   TPCheck=TP-Bid>StopLevel*Pips();
   
   SLCheck=Bid-SL>StopLevel*Pips();
   
   case POSITION_TYPE_SELL:
   TPCheck=Ask-TP>StopLevel*Pips();
   
   SLCheck=SL-Ask>StopLevel*Pips();
  }
  
  return(false);
 } 

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);
  
  int Spread=(int)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD);
  
  TPBuy=RoundToTickSize(Bid+TakeProfit*Pips());
  SLBuy=RoundToTickSize(Bid-StopLoss*Pips());
  
  TPSell=RoundToTickSize(Bid-TakeProfit*Pips());
  SLSell=RoundToTickSize(Bid+StopLoss*Pips());
  
  for(int Loop=PositionsTotal()-1;Loop>=0;Loop--)
  {
   if(m_position.SelectByIndex(Loop))
   {
    if(StopLevelCheck(POSITION_TYPE_BUY,SLBuy,TPBuy))
    {
     trade.PositionModify(m_position.Ticket(),SLBuy,TPBuy);
    }
  
    if(StopLevelCheck(POSITION_TYPE_SELL,SLSell,TPSell))
    {
     trade.PositionModify(m_position.Ticket(),SLSell,TPSell);
    }
   }
  }  
}
 

what symbol are you trading on? Invalid stops simply means that it's not being set at an allowed stop loss level. Different securities have different point systems. It's down to the broker to provide the correct data specifications for the symbols.

Go to the market watch window, right click the symbol you're trading on, and click "Specification". None of those fields should be 0. The "Initial Margin" should be 100000 for forex pairs. 

I think your "Pips" function might be the problem.  Use _Point (predefined variable).

Please change it to:

  TPBuy = Ask+TakeProfit*_Point;
  SLBuy = Ask-StopLoss*_Point;
  
  TPSell = Bid-TakeProfit*_Point;
  SLSell = Bid+StopLoss*_Point;
 
Conor Mcnamara #:

what symbol are you trading on? Invalid stops simply means that it's not being set at an allowed stop loss level. Different securities have different point systems. It's down to the broker to provide the correct data specifications for the symbols.

Go to the market watch window, right click the symbol you're trading on, and click "Specification". None of those fields should be 0. The "Initial Margin" should be 100000 for forex pairs. 

I think your "Pips" function might be the problem.  Use _Point (predefined variable).

Please change it to:

I don't think that's the problem as I've tried it before. I found a way to use a double function that includes stop levels , I don't experience invalid stops with buy orders its only with sell orders this time they don't modify at all.

double AdjustAboveLevel(double Price)
 {
  double NewPrice=0;
  
  double StopLevelPoints=StopLevel*Pips();
  double StopPrice=Ask+StopLevel;
  double StopLevelAddPips=1*Pips();
  
  if(Price>StopPrice+StopLevelAddPips)return(Price);
  else
  {
   NewPrice=StopPrice+StopLevelAddPips;
  }
  
  return(NewPrice);
 } 
 
double AdjustBelowLevel(double Price)
 {
  double NewPrice=0;
  
  double StopLevelPoints=StopLevel*Pips();
  double StopPrice=Bid-StopLevel;
  double StopLevelAddPips=1*Pips();
  
  if(Price<StopPrice-StopLevelAddPips)return(Price);
  else
  {
   NewPrice=StopPrice-StopLevelAddPips;
  }
  
  return(NewPrice);
 } 
 
void OnStart()
 {
  double TPBuy=NormalizeDouble(Bid+TakeProfit*Pips(),Digits());
  double SLBuy=NormalizeDouble(Bid-StopLoss*Pips(),Digits());
  
  double TPSell=NormalizeDouble(Bid-TakeProfit*Pips(),Digits());
  double SLSell=NormalizeDouble(Bid+StopLoss*Pips(),Digits());
  
  for(int Loop=PositionsTotal()-1;Loop>=0;Loop--)
  {
   if(m_position.SelectByIndex(Loop))
   {
    if(TPBuy>0&&SLBuy>0)
    {
     trade.PositionModify(m_position.Ticket(),AdjustBelowLevel(SLBuy),AdjustAboveLevel(TPBuy));
    }

    if(TPSell>0&&SLSell>0)
    {
     trade.PositionModify(m_position.Ticket(),AdjustBelowLevel(SLSell),AdjustAboveLevel(TPSell));
    }
   }
   
  } 

 }


 

 

I don't know why you use OnStart(), it's for scripts. I use OnTick() in expert advisors. I hope you're not learning from AI, as it will teach one how to do things incorrectly. I suggest to read through the "how to" guides for expert advisors 

https://www.mql5.com/en/articles


https://www.mql5.com/en/articles/15299

MQL5 Articles
MQL5 Articles
  • www.mql5.com
MQL5 Programming Articles
 
Conor Mcnamara #:

I don't know why you use OnStart(), it's for scripts. I use OnTick() in expert advisors. I hope you're not learning from AI, as it will teach one how to do things incorrectly. I suggest to read through the "how to" guides for expert advisors 

https://www.mql5.com/en/articles


https://www.mql5.com/en/articles/15299

I'm using Scripts for manual trading when I want to modify my positions not just one positions but multiple positions. As for AI I don't use it at all, it would be a waste of my time and everybody's time.