Backtesting EA - Moving Stoploss to Breakeven

 
      //for LongEntry Condition
      if(glongEntry == true)
        {
         if(PositionsTotal() == 0)
           {
            double buyStopLoss = BuyStopLoss(_Symbol,StopLoss);
            double buyTakeProfit = BuyTakeProfit(_Symbol,TakeProfit);

            gbuyTicket1 = Trade.Buy(_Symbol,PositionSize,buyStopLoss,buyTakeProfit);
           }

         if(PositionsTotal() == 1)
           {
            double buyStopLoss = BuyStopLoss(_Symbol,StopLoss);
            double buyTakeProfit = BuyTakeProfit(_Symbol,TakeProfit);
            Print(buyTakeProfit);
            
            if(gbuyTicket2 == 0)
             {
               gbuyTicket2 = Trade.Buy(_Symbol,PositionSize,buyStopLoss);                             
             }
            if(gbuyTicket2 > 0 && bid >= buyTakeProfit)
              {
               //Once price hits initial TP, move SL to BE
               bool breakeven = Trailing.BreakEven(gbuyTicket2,buyTakeProfit);
               Print("buyTakeProfit = "+ buyTakeProfit);
               Print("Is breakeven successful = "+ breakeven);               
              }
            
           }

Hi, I am having trouble in the EA I am making.


To give a brief background:

My EA will open two positions every time a signal is given.

- One position has both TP and SL (Position 1)

- One position has SL only  (Position 2)

When position 1 hits its TP, Position 2 SL will move to its Entry Price.


I've attached the code for reference. But I am having issue on the strategy tester because eventhough the TP of Position 1 is hit, the SL did not moved to the entry price.

Any advice on how to solve this issue will be really appreciated. Thank you very much.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 

I've attached the code for reference. But I am having issue on the strategy tester because eventhough the TP of Position 1 is hit, the SL did not moved to the entry price.

You have not posted any code, use the code button (Alt+S) when pasting code.


The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
Keith Watford:

I've attached the code for reference. But I am having issue on the strategy tester because eventhough the TP of Position 1 is hit, the SL did not moved to the entry price.

You have not posted any code, use the code button (Alt+S) when pasting code.


Apologies Keith. I have updated the code.  I thought it was added yesterday. I am using a class in an include file for the the opening and modification position.
// Trade opening shortcuts
ulong CTrade::Buy(string pSymbol,double pVolume,double pStop=0.000000,double pProfit=0.000000,string pComment=NULL)
  {
   ulong ticket = OpenPosition(pSymbol,ORDER_TYPE_BUY,pVolume,pStop,pProfit,pComment);
   return(ticket);
  }


bool CTrailing::BreakEven(ulong pTicket,double pBreakStart)
{
        if(PositionSelectByTicket(pTicket) == true)
        {
                request.action = TRADE_ACTION_SLTP;
                request.position = pTicket;
                
                long posType = PositionGetInteger(POSITION_TYPE);
                double currentSL = PositionGetDouble(POSITION_SL);
                double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
                string symbol = PositionGetString(POSITION_SYMBOL);
                
                double point = SymbolInfoDouble(symbol,SYMBOL_POINT);
                int digits = (int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
                
                double breakEvenStop = openPrice;
                breakEvenStop = NormalizeDouble(breakEvenStop, digits);
                
                double bid = 0, ask = 0;
                
                if(posType == POSITION_TYPE_BUY)
                {
                        bid = SymbolInfoDouble(symbol,SYMBOL_BID);
                                                
                        if(bid >= pBreakStart) 
                        {
                                request.sl = breakEvenStop;
                                bool sent = OrderSend(request,result);
                        }
                        else return(false);
                }
                else if(posType == POSITION_TYPE_SELL)
                {
                        ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
                        
                        if(pBreakStart >= ask)
                        {
                                request.sl = breakEvenStop;
                                bool sent = OrderSend(request,result);
                        }
                        else return(false);
                }
        }               
        else return(false);
        return(false);
}