Invalid Stop Loss && Take profit

 

Please can anyone help me with this error 

Ive tried using the CTrade class to modify sl and TP using the

trade method, and PositionModify method but it keeps throwing me this error on the tester


Heres the src code

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, test"
#property link      ""
#property version   "1.00"

#include  <Trade/Trade.mqh>

CTrade trade;

input ulong  ExpertMagic = 21341;
input int Nbr_Periods = 14;
input int History_Bars = 20000;
input double Multiplier = 2;

input double CM_TakeProfit = 0;
input double CM_LostSize = 0.02;
input double CM_StopLoss = 0;

int handleSuperTrend;
int handlePattern;
int barsTotal;
ulong positionTicket;

datetime    glTimeBarOpen;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   glTimeBarOpen = D'1971.01.01 00:00';
   handleSuperTrend = iCustom(_Symbol,PERIOD_CURRENT,"KT-SuperTrend-Indicator.ex5",History_Bars,Nbr_Periods,Multiplier);
   handlePattern = iCustom(_Symbol,PERIOD_CURRENT,"Candlestick Pattern Detector.ex5",
                           "",
                           "",
                           "",
                           500,
                           true,
                           true,
                           false,
                           "",
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           true,
                           true,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           true,
                           true,
                           "",
                           8,
                           clrGreen,
                           clrRed,
                           clrNONE,
                           "",
                           false,
                           false,
                           false,
                           false,
                           "",
                           20,
                           20,
                           "",
                           true);

   barsTotal = iBars(_Symbol,PERIOD_CURRENT);
   if(handleSuperTrend && handlePattern == INVALID_HANDLE)
     {
      return -1;
      Print("There was an error creating indicators handle: ", GetLastError());
     }
   else
     {
      Print("Indicators initialized successfully");
     }

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   bool newBar = false;

//Check for New Bar
   if(glTimeBarOpen != iTime(Symbol(),PERIOD_CURRENT,0))
     {
      newBar = true;
      glTimeBarOpen = iTime(Symbol(),PERIOD_CURRENT,0);
     }

   if(newBar == true)
     {


      double close1 = iClose(_Symbol,PERIOD_CURRENT,1);
      double close2 = iClose(_Symbol,PERIOD_CURRENT,2);
      double open1 = iOpen(_Symbol,PERIOD_CURRENT,1);


      //---dpStoplosssell = lowerBand and dpStopLossBuy = upperband

      double dpBuy[], dpSell[], dpStopLossSell[], dpStopLossBuy[], patternDirection[];
      CopyBuffer(handleSuperTrend,2,0,3,dpBuy);
      CopyBuffer(handleSuperTrend,3,0,3,dpSell);
      CopyBuffer(handleSuperTrend,1,0,3,dpStopLossSell);
      CopyBuffer(handleSuperTrend,0,0,3,dpStopLossBuy);
      CopyBuffer(handlePattern,1,0,3,patternDirection);

      double slBuy = dpStopLossBuy[0];
      slBuy = NormalizeDouble(slBuy,_Digits);

      double slSell = dpStopLossSell[0];
      slSell = NormalizeDouble(slSell,_Digits);

      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      ask = NormalizeDouble(ask,_Digits);

      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      bid = NormalizeDouble(bid,_Digits);




      // check if buy signal is given and make a trade
      //if lower band is true and pattern direction for candle ID 1 is >= 1 make a buy trade
      if(ArraySize(dpStopLossBuy) > 0 && dpStopLossBuy[1] != EMPTY_VALUE && dpStopLossBuy[1] != 0)
        {

         Print(__FUNCTION__, " New Buy Signal ", dpStopLossBuy[1]);


         if(patternDirection[1] != -1 && patternDirection[1] == 1)
           {
            trade.Buy(CM_LostSize,_Symbol,ask,0,0,"Placed By Expert");

           }


        }

      //Check if sell signal is given and make a trade
      //if upper band is true and pattern direction for candle ID 1 is <= -1 make a sell trade
      if(ArraySize(dpStopLossSell) > 0 && dpStopLossSell[1] != EMPTY_VALUE && patternDirection[1] != 0)
        {

         Print(__FUNCTION__, " New Sell Signal ", dpStopLossSell[1]);
         if(patternDirection[1] != 1 && patternDirection[1] == -1)
           {
            trade.Sell(CM_LostSize,_Symbol,bid,0,0,"Placed By Expert");


           }

        }
      for(int i=PositionsTotal()-1; i>=0; i--)
        {

         ulong posTicket = PositionGetTicket(i);
         trade.PositionModify(posTicket,CM_StopLoss,CM_TakeProfit);

         Print("MOD");
        }


     }

  }
//+------------------------------------------------------------------+
//External Functions
Files:
CTRade.PNG  77 kb
 

Is it not obvious why the stops are invalid?

They are both the same! Stop-Loss is the same as the Take-Profit! That is invalid!

 
Fernando Carreiro #:

Is it not obvious why the stops are invalid?

They are both the same! Stop-Loss is the same as the Take-Profit! That is invalid!

Even in situations where they are different it does the same thing.

I made little updates to the code but still same issue


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, test"
#property link      ""
#property version   "1.00"

#include  <Trade/Trade.mqh>

CTrade trade;

input ulong  ExpertMagic = 21341;
input int Nbr_Periods = 14;
input int History_Bars = 20000;
input double Multiplier = 2;

input double CM_TakeProfit = 0;
input double CM_LostSize = 0.02;
input double CM_StopLoss = 0;

int handleSuperTrend;
int handlePattern;
int barsTotal;
ulong positionTicket;

datetime    glTimeBarOpen;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   glTimeBarOpen = D'1971.01.01 00:00';
   handleSuperTrend = iCustom(_Symbol,PERIOD_CURRENT,"KT-SuperTrend-Indicator.ex5",History_Bars,Nbr_Periods,Multiplier);
   handlePattern = iCustom(_Symbol,PERIOD_CURRENT,"Candlestick Pattern Detector.ex5",
                           "",
                           "",
                           "",
                           500,
                           true,
                           true,
                           false,
                           "",
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           true,
                           true,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           false,
                           true,
                           true,
                           "",
                           8,
                           clrGreen,
                           clrRed,
                           clrNONE,
                           "",
                           false,
                           false,
                           false,
                           false,
                           "",
                           20,
                           20,
                           "",
                           true);

   barsTotal = iBars(_Symbol,PERIOD_CURRENT);
   if(handleSuperTrend && handlePattern == INVALID_HANDLE)
     {
      return -1;
      Print("There was an error creating indicators handle: ", GetLastError());
     }
   else
     {
      Print("Indicators initialized successfully");
     }
     
     trade.SetExpertMagicNumber(ExpertMagic);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   bool newBar = false;

//Check for New Bar
   if(glTimeBarOpen != iTime(Symbol(),PERIOD_CURRENT,0))
     {
      newBar = true;
      glTimeBarOpen = iTime(Symbol(),PERIOD_CURRENT,0);
     }

   if(newBar == true)
     {


      double close1 = iClose(_Symbol,PERIOD_CURRENT,1);
      double close2 = iClose(_Symbol,PERIOD_CURRENT,2);
      double open1 = iOpen(_Symbol,PERIOD_CURRENT,1);


      //---dpStoplosssell = lowerBand and dpStopLossBuy = upperband

      double dpBuy[], dpSell[], dpStopLossSell[], dpStopLossBuy[], patternDirection[];
      CopyBuffer(handleSuperTrend,2,0,3,dpBuy);
      CopyBuffer(handleSuperTrend,3,0,3,dpSell);
      CopyBuffer(handleSuperTrend,1,0,3,dpStopLossSell);
      CopyBuffer(handleSuperTrend,0,0,3,dpStopLossBuy);
      CopyBuffer(handlePattern,1,0,3,patternDirection);

      double NCM_SL = NormalizeDouble(CM_StopLoss,_Digits);

     double NCM_TP =  NormalizeDouble(CM_TakeProfit,_Digits);

      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      ask = NormalizeDouble(ask,_Digits);

      double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      bid = NormalizeDouble(bid,_Digits);




      // check if buy signal is given and make a trade
      //if lower band is true and pattern direction for candle ID 1 is >= 1 make a buy trade
      if(ArraySize(dpStopLossBuy) > 0 && dpStopLossBuy[1] != EMPTY_VALUE && dpStopLossBuy[1] != 0)
        {

         Print(__FUNCTION__, " New Buy Signal ", dpStopLossBuy[1]);


         if(patternDirection[1] != -1 && patternDirection[1] == 1)
           {
            trade.Buy(CM_LostSize,_Symbol,ask,0,0,"Placed By Expert");

           }


        }

      //Check if sell signal is given and make a trade
      //if upper band is true and pattern direction for candle ID 1 is <= -1 make a sell trade
      if(ArraySize(dpStopLossSell) > 0 && dpStopLossSell[1] != EMPTY_VALUE && patternDirection[1] != 0)
        {

         Print(__FUNCTION__, " New Sell Signal ", dpStopLossSell[1]);
         if(patternDirection[1] != 1 && patternDirection[1] == -1)
           {
            trade.Sell(CM_LostSize,_Symbol,bid,0,0,"Placed By Expert");


           }

        }



      // MODIFY SL AND TP OF BUY POSITIONS :
      for(int i=PositionsTotal()-1; i>=0; --i)
        {
         ulong ticket=PositionGetTicket(i);
         if(PositionGetSymbol(i)==Symbol() && PositionGetInteger(POSITION_TYPE)==0)
           {
            trade.PositionModify(ticket,NCM_SL,NCM_TP);
           }
        }

      // MODIFY SL AND TP OF SELL POSITIONS :
      for(int i=PositionsTotal()-1; i>=0; --i)
        {
         ulong ticket=PositionGetTicket(i);
         if(PositionGetSymbol(i)==Symbol() && PositionGetInteger(POSITION_TYPE)==1)
           {
            trade.PositionModify(ticket,NCM_SL,NCM_TP);
           }
        }
        

     }

  }
//+------------------------------------------------------------------+
//External Functions
Files:
CTRade2.PNG  110 kb
 
Fela Fomonyuy #: Even in situations where they are different it does the same thing. I made little updates to the code but still same issue

And what are the market prices at the time of the modification?

Print the current ask/bid price, so you can make sure that the stop-loss is in the adverse region and the take-profit is in the favourable region when compared to the current quote prices.

 

For a Buy: S/L < Bid - StopLevel, T/P > Bid + StopLevel

For a Sell: S/L > Ask + StopLevel, T/P < Ask - StopLevel

 
Fernando Carreiro #:

Thank you very much. Let me study this and implement it 
 
Fernando Carreiro #:

     //define stop loss and take profits
      double NCM_TP = bid + CM_TakeProfit*Point();   
      double NCM_SL = ask - CM_StopLoss*Point(); 
     

This is what i was missing. Thank you very much

 
Fela Fomonyuy #: This is what i was missing. Thank you very much

No. Please read my posts again.

For a Buy the S/L must be less than the Bid, not the Ask, and for a Sell ...

// For a buy ...
   double NCM_TP = bid + CM_TakeProfit * _Point,
          NCM_SL = bid - CM_StopLoss   * _Point; 

// For a sell ...
   double NCM_TP = ask - CM_TakeProfit * _Point,
          NCM_SL = ask + CM_StopLoss   * _Point; 
Reason: