Trailing stop/ Break Even stop

 
Hi, I have a problem with my MT5 break-even and take profit Bolinger bands trailing stop, for some reason, it does not work. After placing the TP or Sl it change once and it won't move further.


please see the code, and If you could tell me what is wrong with it I would really appreciate it.





#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#include <Trade/Trade.mqh>



CTrade trade;

double Equity = AccountInfoDouble(ACCOUNT_EQUITY);

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

input int eamagic = 2;

input double lotssize = 0.1;

input int TakeProfitInPips = 350;//takeprofit

input int StopLossInPips = 150; //stop loss

input string TradeSymbols = "Current";

double DynamicPositionSize = NormalizeDouble(((Equity*0.01)/StopLossInPips*ask/10),2);

int OnInit()

  {

   return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)

  {

   

  }

void OnTick()

  {



      MqlRates PriceInformtion[];

   

      double myLOWERBAND[], myHIGHERBAND[], myMIDDLEBAND[],myRSI[];

      

      ArraySetAsSeries(PriceInformtion,true);

      ArraySetAsSeries(myLOWERBAND,true);

      ArraySetAsSeries(myHIGHERBAND,true);

      ArraySetAsSeries(myMIDDLEBAND,true);

      ArraySetAsSeries(myRSI,true);

   

      int BAND = iBands(_Symbol,PERIOD_M15,20,0,2,PRICE_CLOSE);

      int RSI6 = iRSI(_Symbol,PERIOD_M15,6,PRICE_CLOSE);





      CopyBuffer(BAND,2,0,4,myLOWERBAND);

      CopyBuffer(BAND,1,0,4,myHIGHERBAND);

      CopyBuffer(BAND,0,0,4,myMIDDLEBAND);

      CopyBuffer(RSI6,0,0,1,myRSI); 



      double myMA200[];

      

      ArraySetAsSeries(myMA200,true);



      int MA200 = iMA(_Symbol,PERIOD_M15,200,0,MODE_EMA,PRICE_CLOSE);

      

      CopyBuffer(MA200,0,0,4,myMA200);

   

      int data = CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),PriceInformtion);



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

      double myLOWERBANDValue = NormalizeDouble(myLOWERBAND[0],_Digits);

      double myHIGHERBANDValue = NormalizeDouble(myHIGHERBAND[0],_Digits);

      double myMIDDLEBANDValue = NormalizeDouble(myMIDDLEBAND[0],_Digits);

      double TakeProfitBuy = ask + TakeProfitInPips * _Point;

      double StopLossBuy = ask - StopLossInPips * _Point;

      double TakeProfitSell = ask - TakeProfitInPips * _Point;

      double StopLossSell = ask + StopLossInPips * _Point;

 



      

      if(ask < myMA200[1]){ 

         if(myRSI[2] > 80);        

           if(PriceInformtion[2].close > myHIGHERBAND[2] && PriceInformtion[1].close < myHIGHERBAND[1]){ 

              if(PriceInformtion[0].open <= PriceInformtion[1].close){ 

                  if(PositionsTotal()  < 3){

                     trade.Sell(lotssize,_Symbol,ask,StopLossSell,TakeProfitSell,NULL);

                     

                     CheckBBTakeProfit(myLOWERBANDValue);

                  }     

               }        

            }           

         }      

      if(ask > myMA200[0]){

         if(myRSI[2] < 20);        

            if(PriceInformtion[2].close < myLOWERBAND[2] && PriceInformtion[1].close > myLOWERBAND[1]){ 

               if(PriceInformtion[0].open >= PriceInformtion[1].close){ 

                  if(PositionsTotal()  < 1){

                     trade.Buy(lotssize,_Symbol,ask,StopLossBuy,TakeProfitBuy,NULL);

                     

                     CheckBBTakeProfit(myHIGHERBANDValue);

                     CheckBBStopLoos(StopLossBuy);

                            

                  }            

               }               

            }                  

         }                     

                                  

      Comment("\n Middleband - ",NormalizeDouble(myMIDDLEBANDValue,_Digits),

              "\n Higherband - ",NormalizeDouble(myHIGHERBANDValue,_Digits),

              "\n Lowerband - ",NormalizeDouble(myLOWERBANDValue,_Digits),

              "\n TP - ",PositionGetDouble(POSITION_TP)

              );

              



}





void CheckBBTakeProfit(double myHIGHERBANDValue)

{

 

   for(int i = PositionsTotal()-1; i>=0; i--){ //check all open position for the current symbol, count all currency pair positions

   

      string symbol = PositionGetSymbol(i); // get position symbol

      if(_Symbol==symbol){ // if chart symbol = position symbol

      

         ulong PositionTicket=PositionGetInteger(POSITION_TICKET); //get the ticket number

         

         double CurrentStopLoss = PositionGetDouble(POSITION_SL); //get the current stoploss

         double PositionBuyPrice = PositionGetDouble(POSITION_PRICE_OPEN);

         double CurrentTakeProfit = PositionGetDouble(POSITION_TP);

         double CurrentVolume = PositionGetDouble(POSITION_VOLUME);

         double PositionType = PositionGetInteger(POSITION_TYPE);

         

         if(PositionType == POSITION_TYPE_BUY){

            if(myHIGHERBANDValue < CurrentTakeProfit){

               trade.PositionModify(PositionTicket,0,myHIGHERBANDValue);  

               

            }   

         }

      }            

   }

}   

void CheckBBStopLoos(double StopLossBuy)

{

   for(int i = PositionsTotal()-1; i>=0; i--){ //check all open position for the current symbol, count all currency pair positions

   

      string symbol = PositionGetSymbol(i); // get position symbol

      if(_Symbol==symbol){ // if chart symbol = position symbol

      

         ulong PositionTicket=PositionGetInteger(POSITION_TICKET); //get the ticket number

         

         double CurrentStopLoss = PositionGetDouble(POSITION_SL); //get the current stoploss

         double PositionBuyPrice = PositionGetDouble(POSITION_PRICE_OPEN);

         double CurrentTakeProfit = PositionGetDouble(POSITION_TP);

         double CurrentVolume = PositionGetDouble(POSITION_VOLUME);

         double PositionType = PositionGetInteger(POSITION_TYPE);

         

         if(PositionType == POSITION_TYPE_BUY){

            if(ask > (PositionBuyPrice + TakeProfitInPips/2)){

               trade.PositionModify(PositionTicket,PositionBuyPrice,0);

            }   

         }        

      }

   }

}


Hi, I have a problem with my MT5 break-even and take profit Bolinger bands trailing stop, for some reason, it does not work. After placing the TP or Sl it change once and it won't move further.


please see the code, and If you could tell me what is wrong with it I would really appreciate it.





#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#include <Trade/Trade.mqh>



CTrade trade;

double Equity = AccountInfoDouble(ACCOUNT_EQUITY);

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

input int eamagic = 2;

input double lotssize = 0.1;

input int TakeProfitInPips = 350;//takeprofit

input int StopLossInPips = 150; //stop loss

input string TradeSymbols = "Current";

double DynamicPositionSize = NormalizeDouble(((Equity*0.01)/StopLossInPips*ask/10),2);

int OnInit()

  {

   return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)

  {

   

  }

void OnTick()

  {



      MqlRates PriceInformtion[];

   

      double myLOWERBAND[], myHIGHERBAND[], myMIDDLEBAND[],myRSI[];

      

      ArraySetAsSeries(PriceInformtion,true);

      ArraySetAsSeries(myLOWERBAND,true);

      ArraySetAsSeries(myHIGHERBAND,true);

      ArraySetAsSeries(myMIDDLEBAND,true);

      ArraySetAsSeries(myRSI,true);

   

      int BAND = iBands(_Symbol,PERIOD_M15,20,0,2,PRICE_CLOSE);

      int RSI6 = iRSI(_Symbol,PERIOD_M15,6,PRICE_CLOSE);





      CopyBuffer(BAND,2,0,4,myLOWERBAND);

      CopyBuffer(BAND,1,0,4,myHIGHERBAND);

      CopyBuffer(BAND,0,0,4,myMIDDLEBAND);

      CopyBuffer(RSI6,0,0,1,myRSI); 



      double myMA200[];

      

      ArraySetAsSeries(myMA200,true);



      int MA200 = iMA(_Symbol,PERIOD_M15,200,0,MODE_EMA,PRICE_CLOSE);

      

      CopyBuffer(MA200,0,0,4,myMA200);

   

      int data = CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),PriceInformtion);



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

      double myLOWERBANDValue = NormalizeDouble(myLOWERBAND[0],_Digits);

      double myHIGHERBANDValue = NormalizeDouble(myHIGHERBAND[0],_Digits);

      double myMIDDLEBANDValue = NormalizeDouble(myMIDDLEBAND[0],_Digits);

      double TakeProfitBuy = ask + TakeProfitInPips * _Point;

      double StopLossBuy = ask - StopLossInPips * _Point;

      double TakeProfitSell = ask - TakeProfitInPips * _Point;

      double StopLossSell = ask + StopLossInPips * _Point;

 



      

      if(ask < myMA200[1]){ 

         if(myRSI[2] > 80);        

           if(PriceInformtion[2].close > myHIGHERBAND[2] && PriceInformtion[1].close < myHIGHERBAND[1]){ 

              if(PriceInformtion[0].open <= PriceInformtion[1].close){ 

                  if(PositionsTotal()  < 3){

                     trade.Sell(lotssize,_Symbol,ask,StopLossSell,TakeProfitSell,NULL);

                     

                     CheckBBTakeProfit(myLOWERBANDValue);

                  }     

               }        

            }           

         }      

      if(ask > myMA200[0]){

         if(myRSI[2] < 20);        

            if(PriceInformtion[2].close < myLOWERBAND[2] && PriceInformtion[1].close > myLOWERBAND[1]){ 

               if(PriceInformtion[0].open >= PriceInformtion[1].close){ 

                  if(PositionsTotal()  < 1){

                     trade.Buy(lotssize,_Symbol,ask,StopLossBuy,TakeProfitBuy,NULL);

                     

                     CheckBBTakeProfit(myHIGHERBANDValue);

                     CheckBBStopLoos(StopLossBuy);

                            

                  }            

               }               

            }                  

         }                     

                                  

      Comment("\n Middleband - ",NormalizeDouble(myMIDDLEBANDValue,_Digits),

              "\n Higherband - ",NormalizeDouble(myHIGHERBANDValue,_Digits),

              "\n Lowerband - ",NormalizeDouble(myLOWERBANDValue,_Digits),

              "\n TP - ",PositionGetDouble(POSITION_TP)

              );

              



}





void CheckBBTakeProfit(double myHIGHERBANDValue)

{

 

   for(int i = PositionsTotal()-1; i>=0; i--){ //check all open position for the current symbol, count all currency pair positions

   

      string symbol = PositionGetSymbol(i); // get position symbol

      if(_Symbol==symbol){ // if chart symbol = position symbol

      

         ulong PositionTicket=PositionGetInteger(POSITION_TICKET); //get the ticket number

         

         double CurrentStopLoss = PositionGetDouble(POSITION_SL); //get the current stoploss

         double PositionBuyPrice = PositionGetDouble(POSITION_PRICE_OPEN);

         double CurrentTakeProfit = PositionGetDouble(POSITION_TP);

         double CurrentVolume = PositionGetDouble(POSITION_VOLUME);

         double PositionType = PositionGetInteger(POSITION_TYPE);

         

         if(PositionType == POSITION_TYPE_BUY){

            if(myHIGHERBANDValue < CurrentTakeProfit){

               trade.PositionModify(PositionTicket,0,myHIGHERBANDValue);  

               

            }   

         }

      }            

   }

}   

void CheckBBStopLoos(double StopLossBuy)

{

   for(int i = PositionsTotal()-1; i>=0; i--){ //check all open position for the current symbol, count all currency pair positions

   

      string symbol = PositionGetSymbol(i); // get position symbol

      if(_Symbol==symbol){ // if chart symbol = position symbol

      

         ulong PositionTicket=PositionGetInteger(POSITION_TICKET); //get the ticket number

         

         double CurrentStopLoss = PositionGetDouble(POSITION_SL); //get the current stoploss

         double PositionBuyPrice = PositionGetDouble(POSITION_PRICE_OPEN);

         double CurrentTakeProfit = PositionGetDouble(POSITION_TP);

         double CurrentVolume = PositionGetDouble(POSITION_VOLUME);

         double PositionType = PositionGetInteger(POSITION_TYPE);

         

         if(PositionType == POSITION_TYPE_BUY){

            if(ask > (PositionBuyPrice + TakeProfitInPips/2)){

               trade.PositionModify(PositionTicket,PositionBuyPrice,0);

            }   

         }        

      }

   }

}


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2021.01.24
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.