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);
    }
   }
  }  
}