Results 1 to 2 of 2
 0 Attachment(s)    

Thread: Code Problem with my order modify EA

  1. #1

    Default Code Problem with my order modify EA

    Hi guys,

    I have an EA based on a code template I purchased that has a problem with the TrailingStop function.

    The problem is that when the price is above the breakeven amount, it continually spams the ordermodify to adjust to breakeven again. When the trailingstop kicks in, the breakeven will once again overwrite the stoploss back to Breakeven. This results in a constant flood of ordermodify requests.

    I've tried a work-around using a 'hasbrokeneven' bool which only seemed to work for the first trade or so.
    Can anyone give me an idea as to what I need todo to fix this? It seems like the breakeven feature is a hack.

    Code:
    int TrailingStopEngine()
      {
      // candle ts
      int Return = 0;
      if (CandleTrailingStop != 0)
        {
         if (OrderType()==OP_BUY) 
           {
            TrailingStop = MathAbs((Ask-Low[CandleTrailingStop])/Point);
            if (Ask<Low[CandleTrailingStop]) 
               {
                if (OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue)) {trace("Candle trailing stop closed position.", 1);}
               }
           }
         if (OrderType()==OP_SELL) 
           {
            TrailingStop = MathAbs((Bid-High[CandleTrailingStop])/Point);
            if (Bid>High[CandleTrailingStop]) 
               {
                if (OrderClose(OrderTicket(),OrderLots(),Ask,3,Red)) {trace("Candle trailing stop closed position.", 1);}
               }
           }
        }
        
        // break even ts
       double Up = (Bid - OrderOpenPrice())/Point/Pip;
       double Dn = (OrderOpenPrice() - Ask)/Point/Pip;
       RefreshRates();
       double Spread = MarketInfo(Symbol(), MODE_SPREAD)/Pip;
       double BuyBreakEvenSL = OrderOpenPrice() + Spread*Point*Pip;
       double SellBreakEvenSL = OrderOpenPrice() - Spread*Point*Pip;
       double ExistingSL = OrderStopLoss();
        
       double BreakEvenRate = 0.01*BreakEvenPct;
       if (BreakEven == True )
         {
         if (OrderType()==OP_BUY) 
           {
           if ((BuyTakeProfit != 0) && (BreakEvenRate != 0) && (NormalizeDouble(BuyBreakEvenSL,5) != NormalizeDouble(ExistingSL,5)))
             { if (Up/BuyTakeProfit > BreakEvenRate || Up > BreakEvenPips) {
             if (0 < BreakEvenRate && BreakEvenRate < 1)
               {
               if (OrderModify(OrderTicket(), 0, BuyBreakEvenSL, TPPrice, 0, 0))
                 {
                  Return = 1;
                  OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                  trace(StringConcatenate("BuyBreakEvenSL= ", BuyBreakEvenSL), 1);
                 }
               else 
                  {
                   trace(StringConcatenate("Error when modifying order for break even: ", GetLastError()), 1);
                  }  
               }
             else if (BreakEvenPips > Spread + 1)
               {
               if(OrderModify(OrderTicket(), 0, BuyBreakEvenSL, TPPrice, 0, 0))
                 {
                 Return = 1;
                 OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                 trace(StringConcatenate("BuyBreakEvenSL= ", BuyBreakEvenSL), 1);
                 }
               else 
                  {
                   trace(StringConcatenate("Error when modifying order for break even: ", GetLastError()), 1);
                  }    
               }
             else
               {
               trace("BreakEvenPct and BreakEvenPips is not set! Set at least one of them.", 1);
               }
             }}
           else if (NormalizeDouble(BuyBreakEvenSL,5) != NormalizeDouble(ExistingSL,5) && Up > BreakEvenPips) //when tp=0
             {
             if (BreakEvenPips > Spread + 1)
               {
               if (OrderModify(OrderTicket(), 0, BuyBreakEvenSL, TPPrice, 0, 0))
                 {
                 Return = 1;
                 OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                 trace(StringConcatenate("BuyBreakEvenSL= ", BuyBreakEvenSL), 1);
                 }
               else 
                  {
                   trace(StringConcatenate("Error when modifying order for break even: ", GetLastError()), 1);
                  }  
               }
             else
               {
               trace("Take Profit and BreakEvenPips is not set! Set at least one of them.", 1);
               }
             }
           }
           
         if (OrderType()==OP_SELL) 
           {
           if ((SellTakeProfit != 0) && (BreakEvenRate != 0) && (NormalizeDouble(SellBreakEvenSL,5) != NormalizeDouble(ExistingSL,5)))
             { if (Dn/SellTakeProfit > BreakEvenRate || Dn > BreakEvenPips) {
             if (0 < BreakEvenRate && BreakEvenRate < 1)
               {
               if (OrderModify(OrderTicket(), 0, SellBreakEvenSL, TPPrice, 0, 0))
                 {
                 Return = 1;
                 OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                 trace(StringConcatenate("SellBreakEvenSL= ", SellBreakEvenSL), 1);
                 }
               else 
                  {
                   trace(StringConcatenate("Error when modifying order for break even: ", GetLastError()), 1);
                  }  
               }
             else if (BreakEvenPips > Spread + 1)
               {
               if (OrderModify(OrderTicket(), 0, SellBreakEvenSL, TPPrice, 0, 0))
                 {
                 Return = 1;
                 OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                 trace(StringConcatenate("SellBreakEvenSL= ", SellBreakEvenSL), 1);
                 }
               else 
                  {
                   trace(StringConcatenate("Error when modifying order for break even: ", GetLastError()), 1);
                  }  
               }
             else
               {
               trace("BreakEvenPct and BreakEvenPips is not set! Set at least one of them.", 1);
               }
             }}
           else if (NormalizeDouble(SellBreakEvenSL,5) != NormalizeDouble(ExistingSL,5) && Dn > BreakEvenPips) //when tp=0
             {
             if (BreakEvenPips > Spread + 1)
               {
               if (OrderModify(OrderTicket(), 0, SellBreakEvenSL, TPPrice, 0, 0))
                 {
                 Return = 1;
                 OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                 trace(StringConcatenate("SellBreakEvenSL= ", SellBreakEvenSL), 1);
                 }
               else 
                  {
                   trace(StringConcatenate("Error when modifying order for break even: ", GetLastError()), 1);
                  }  
               }
             else
               {
               trace("Take Profit and BreakEvenPips is not set! Set at least one of them.", 1);
               }
             }
           }
        }
          
       
        // simple (pips) ts
          if(OrderType() == OP_BUY)
            {   //((Up)>0.001) && 
             if ((((Bid-TrailingStop*Point*Pip) - OrderStopLoss())>(TrailingStopStep*Point*Pip)) && (OrderStopLoss()<(Bid-TrailingStop*Point*Pip)))
                {
                if(TrailingStop!=0)
                  {
                  if (BuyTakeProfit!=0)
                     {
                     if (TrailingStop > MarketInfo(Symbol(), MODE_STOPLEVEL))
                        {
                        if (OrderModify(OrderTicket(), 0, Bid-TrailingStop*Point*Pip, TPPrice, 0, 0))
                          {
                          Return = 1;
                          trace(StringConcatenate("TrailingStop = ", TrailingStop/Pip), 3);
                          trace(StringConcatenate("MODE_STOPLEVEL = ", MarketInfo(Symbol(), MODE_STOPLEVEL)), 999);
                          OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                          }
                         }
                      else {trace(StringConcatenate("Trailing stop not set: too close to price. TrailingStop= ", TrailingStop/Pip), 2);} 
                      if (CandleTrailingStop!=0) {TrailingStop=0;}
                     }
                     else
                         {if (TrailingStop > MarketInfo(Symbol(), MODE_STOPLEVEL))
                           {
                            if (OrderModify(OrderTicket(), 0, Bid-TrailingStop*Point*Pip, 0, 0, 0))
                              {
                              Return = 1;
                              trace(StringConcatenate("TrailingStop = ", TrailingStop/Pip), 3);
                              trace(StringConcatenate("MODE_STOPLEVEL = ", MarketInfo(Symbol(), MODE_STOPLEVEL)), 999);
                              OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                              }
                            }
                           else {trace(StringConcatenate("Trailing stop not set: too close to price. TrailingStop= ", TrailingStop/Pip), 2);}   
                         if (CandleTrailingStop!=0) {TrailingStop=0;}
                         }
                  }
               }
            }
          if (OrderType() == OP_SELL)
             {
             if(((OrderStopLoss() - (Ask+TrailingStop*Point*Pip))>(TrailingStopStep*Point*Pip)) && (OrderStopLoss()>(Ask+TrailingStop*Point*Pip)))
               {
                if(TrailingStop!=0)
                  {
                  if (SellTakeProfit!=0)
                      {
                      if (TrailingStop > MarketInfo(Symbol(), MODE_STOPLEVEL))
                        {
                         if (OrderModify(OrderTicket(), 0, Ask+TrailingStop*Point*Pip, TPPrice, 0, 0))
                           {
                           Return = 1;
                           trace(StringConcatenate("TrailingStop= ", TrailingStop/Pip), 3);
                           trace(StringConcatenate("MODE_STOPLEVEL = ", MarketInfo(Symbol(), MODE_STOPLEVEL)), 999);
                           OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                           }
                         }
                      else {trace(StringConcatenate("Trailing stop not set: too close to price. TrailingStop = ", TrailingStop/Pip), 2);} 
                      if (CandleTrailingStop!=0) {TrailingStop=0;}
                      }
                      else
                         {if (TrailingStop > MarketInfo(Symbol(), MODE_STOPLEVEL))
                           {
                            if (OrderModify(OrderTicket(), 0, Ask+TrailingStop*Point*Pip, 0, 0, 0))
                              {
                              Return = 1;
                              trace(StringConcatenate("TrailingStop= ", TrailingStop/Pip), 3);
                              trace(StringConcatenate("MODE_STOPLEVEL = ", MarketInfo(Symbol(), MODE_STOPLEVEL)), 999);
                              OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES);
                              }
                            }
                           else {trace(StringConcatenate("Trailing stop not set: too close to price. TrailingStop = ", TrailingStop/Pip), 2);}
                           if (CandleTrailingStop!=0) {TrailingStop=0;}
                         } 
                  }
               }
            }
          return(Return);  
     }

  2. #2

    Default

    This was a strategy based on the EA Creator - create Forex Expert Advisor without programming code template. Please ignore this thread now. I will update with funyoo's template.

Similar Threads

  1. Replies: 2
    Last Post: 08-04-2014, 19:03
  2. Help- SL problem
    By shabeer.a100 in forum Softwares
    Replies: 1
    Last Post: 09-12-2013, 15:00
  3. Open oppsite order if order don't take profit
    By luisneves in forum MQL programming
    Replies: 0
    Last Post: 12-06-2012, 17:09
  4. Code needed to track ea in order to hedge
    By snkpool in forum MQL programming
    Replies: 0
    Last Post: 09-28-2010, 03:57

Tags for this Thread

advisor, breakeven, candle, expert, expert advisor, forex, forex expert advisor, high, low, pips, programming, simple, spread, stoploss, strategy, trailingstop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •