EA Opening One Position Per Signal & Setting A Trailing Stop Loss To The Same

 

Hey guys,

I have a problem with my code and I am not sure how to resolve this.

I want to open a single position every time a condition is met. This I have been able to accomplish.

The other part is to set a trailing stop loss on this open position. Here is where the problem lies.

My current code sets the trailing stop loss to the position but only once and does not adjust it accordingly there after.

Please shed some light on where I am failing and how I can resolve this.

//Import Trade Library
#include<Trade\Trade.mqh>

//Create an instance for Ctrade
CTrade trade;

//Get account balance
      double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
      
      //Get account equity
      double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
      
      //Get Ask Price
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      
      //Get Bid Price
      double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      
      //Create string for the signal
      string signal="";

int OpenBuy()
  {
      trade.Buy(0.01,NULL,Ask,(Ask-30000 * _Point),(Ask+30000 * _Point),NULL);
      
      //Set desired stop loss to 5000 points
      double SL=NormalizeDouble(Ask-5000*_Point,_Digits);
      
      //Check all open positions for the current symbol
      for(int i=PositionsTotal()-1; i>=0; i--)
      {
         string symbol=PositionGetSymbol(i);
         
         if (_Symbol==symbol)
         {
            //Get position ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
            
            //Get current stoploss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
            
            if(CurrentStopLoss<SL)
            {
               //Print("Trail SL Buy");
               trade.PositionModify(PositionTicket,(CurrentStopLoss+10000*_Point),0);
            }
         }
      }
      return(1);
  }
  
  int OpenSell()
  {
      trade.Sell(0.01,NULL,Bid,(Bid+30000 * _Point),(Bid-30000 * _Point),NULL);
      
      //Set desired stop loss to 5000 points
      double SL=NormalizeDouble(Bid+5000*_Point,_Digits);
      
      //Check all open positions for the current symbol
      for(int i=PositionsTotal()-1; i>=0; i--)
      {
         string symbol=PositionGetSymbol(i);
         
         if (_Symbol==symbol)
         {
            //Get position ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
            
            //Get current stoploss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
            
            if(CurrentStopLoss>SL)
            {
               //Print("Trail SL Sell");
               trade.PositionModify(PositionTicket,(CurrentStopLoss-10000*_Point),0);
            }
         }
      }
      return(1);
  }

void OnTick()
  {
      //Check if a single order has been executed for signal
      static bool Buy_Once=false,Sell_Once=false;
      
      //Price Array
      double SmallMovingAverageArray[],BigMovingAverageArray[];
      
      //Define properties for Small MA
      int SmallMovingAverageArrayDefinition = iMA (_Symbol,_Period,5,0,MODE_SMA,PRICE_CLOSE);
      
      //Define properties for Big MA
      int BigMovingAverageArrayDefinition = iMA (_Symbol,_Period,300,0,MODE_SMA,PRICE_CLOSE);
      
      //Define MA1, one line, current candle, 3 candles, store result
      CopyBuffer(SmallMovingAverageArrayDefinition,0,0,3,SmallMovingAverageArray);
      
      //Define MA2, one line, current candle, 3 candles, store result
      CopyBuffer(BigMovingAverageArrayDefinition,0,0,3,BigMovingAverageArray);
      
      //Check buy signal
      if (BigMovingAverageArray[1] < SmallMovingAverageArray[1])
      {
         signal="BUY";
      }
            
      //Check sell signal
      if (BigMovingAverageArray[1] > SmallMovingAverageArray[1])
      {
         signal="SELL";
      }
                
      //Buy 1 Microlot on buy signal
      if (signal =="BUY" && Equity>=Balance && PositionsTotal()==0 && !Buy_Once)
      {
         int Buy=OpenBuy();
         
         //Check if a buy order has been opened. If so, does not open another   
         if (Buy>0)
         {
            Comment("An order opened. Current signal is: ",signal);
            Buy_Once=true;
            Sell_Once=false;
         }
      }
        
      //Check for buy positions to close on sell signal     
      if (signal =="SELL" && PositionsTotal()>0)
      CloseAllBuyPositionsThisPair();
          
      //Sell 1 Microlot on sell signal
      if (signal =="SELL" && Equity>=Balance && PositionsTotal()==0 && !Sell_Once)
      {
         int Sell=OpenSell();
         
         //Check if a sell order has been opened. If so, does not open another
         if (Sell>0)
         {
            Comment("An order opened. Current signal is: ",signal);
            Buy_Once=false;
            Sell_Once=true;
          }
      }
      
      //Check for sell positions to close on buy signal     
      if (signal =="BUY" && PositionsTotal()>0)
      CloseAllSellPositionsThisPair();
  }

void CloseAllBuyPositionsThisPair()
  {
     //Go through all positions until there are no positions left       
     for(int b=PositionsTotal()-1; b>=0; b--)
       {
          //Get ticket number for the current position
          int ticket=PositionGetTicket(b);
           
          //Identify current currency pair
          string CurrentPair=PositionGetSymbol(b);
            
          //Get position direction
          int PositionDirection=PositionGetInteger(POSITION_TYPE);
            
          //If it is a buy position
          if (PositionDirection==POSITION_TYPE_BUY)
            
          //If symbol on chart equals position symbol
          if (_Symbol==CurrentPair)
          {
             //Close current position
             trade.PositionClose(ticket);
          }
       }  
  }
  
void CloseAllSellPositionsThisPair()
  {
     //Go through all positions until there are no positions left       
     for(int s=PositionsTotal()-1; s>=0; s--)
       {
          //Get ticket number for the current position
          int ticket=PositionGetTicket(s);
           
          //Identify current currency pair
          string CurrentPair=PositionGetSymbol(s);
            
          //Get position direction
          int PositionDirection=PositionGetInteger(POSITION_TYPE);
            
          //If it is a buy position
          if (PositionDirection==POSITION_TYPE_SELL)
            
          //If symbol on chart equals position symbol
          if (_Symbol==CurrentPair)
          {
             //Close current position
             trade.PositionClose(ticket);
          }
       }
  }


 

Reason: