Trailing Stop with Commission - page 2

 
I really want to help you, but your code is so messy and I don't know what exactly you are trying to accomplish...
 
Chad Magruder:
I really want to help you, but your code is so messy and I don't know what exactly you are trying to accomplish...

Thanks Chad!


Below in the pic is what Im trying to achieve

TrailingStop-To include commission

 
void TrailStop(string pair, double trailing, double sureprofit)
{
              
      double SLoss    = 0                           ;
      double SLossCk  = 0                           ;     
      bool   error    = false                       ;      
      double pt       = MarketInfo(pair,MODE_POINT) ;
      double sprd     = MarketInfo(pair,MODE_SPREAD);
      
      for(int i=0; i<OrdersTotal(); i++)
      {
            if(OrderSelect(i,SELECT_BY_POS)  && OrderSymbol()==pair)
            {                         
               
            
                  
                  if(OrderType()==OP_BUY) //Buy               
                    {                       
                           if(trailing<MathMax(MarketInfo(pair, MODE_STOPLEVEL),MarketInfo(pair, MODE_SPREAD)))
                           {
                                    SLoss=NormalizeDouble(MarketInfo(pair,MODE_BID)-(MathMax(MarketInfo(pair, MODE_STOPLEVEL),MarketInfo(pair, MODE_SPREAD)))*pt,(int)MarketInfo(pair,MODE_DIGITS));
                                    SLossCk=NormalizeDouble(MarketInfo(pair,MODE_BID)-(MathMax(MarketInfo(pair, MODE_STOPLEVEL),MarketInfo(pair, MODE_SPREAD))+SurePips)*pt,(int)MarketInfo(pair,MODE_DIGITS));
                           }
                           else
                           {
                                    SLoss=NormalizeDouble(MarketInfo(pair,MODE_BID)-(trailing)*pt,(int)MarketInfo(pair,MODE_DIGITS));
                                    SLossCk=NormalizeDouble(MarketInfo(pair,MODE_BID)-(trailing+sureprofit)*pt,(int)MarketInfo(pair,MODE_DIGITS));
                                    
                           }
                          
                           if((SLossCk>OrderStopLoss() || OrderStopLoss()==0) && SLossCk>OrderOpenPrice())
                             {                        
                                    if(NormalizeDouble(SLoss,(int)MarketInfo(pair,MODE_DIGITS))!=OrderStopLoss())
                                    {     error=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLoss,(int)MarketInfo(pair,MODE_DIGITS)),OrderTakeProfit(),0,White);
                                          Sleep(500);                                                                     
                                    }                       
                             }
                    }
                  if(OrderType()==OP_SELL)              
                    {              
                           if(trailing<(MathMax(MarketInfo(pair, MODE_STOPLEVEL),MarketInfo(pair, MODE_SPREAD))))
                           {
                                    SLoss=NormalizeDouble(MarketInfo(pair,MODE_ASK)+(MathMax(MarketInfo(pair, MODE_STOPLEVEL),MarketInfo(pair, MODE_SPREAD)))*pt,(int)MarketInfo(pair,MODE_DIGITS));
                                    SLossCk=NormalizeDouble(MarketInfo(pair,MODE_ASK)+(MathMax(MarketInfo(pair, MODE_STOPLEVEL),MarketInfo(pair, MODE_SPREAD))+SurePips)*pt,(int)MarketInfo(pair,MODE_DIGITS));
                           }
                           else
                           {
                                    SLoss=NormalizeDouble(MarketInfo(pair,MODE_ASK)+(trailing+sprd)*pt,(int)MarketInfo(pair,MODE_DIGITS));
                                    SLossCk=NormalizeDouble(MarketInfo(pair,MODE_ASK)+(trailing+sureprofit+sprd)*pt,(int)MarketInfo(pair,MODE_DIGITS));
                           }
                          
                           
                           if((SLossCk<OrderStopLoss() || OrderStopLoss()==0) && SLossCk<OrderOpenPrice())
                             {                              
                                    if(NormalizeDouble(SLoss,(int)MarketInfo(pair,MODE_DIGITS))!=OrderStopLoss())
                                    {
                                          error=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLoss,(int)MarketInfo(pair,MODE_DIGITS)),OrderTakeProfit(),0,White);                        
                                          Sleep(500);                                                           
                                    }
                             }
                    }
            
                 
            }
      }
}
    
 
Joel Protusada:

Thanks @Joel Protusada for your code!

I'm trying to inject this on my code, but missing these parameter values when passing, are these defined with values?  Also for "SurePips" ?   

string pair, double trailing, double sureprofit
 
sathyaicm:

Thanks @Joel Protusada for your code!

I'm trying to inject this on my code, but missing these parameter values when passing, are these defined with values?  Also for "SurePips" ?   

That code is full of race conditions which could result in bugs. Since the order pool is a shared resource, you need to always iterate in reverse or use recursion due to the race conditions between multiple EAs and even raw market conditions. Order processing is [blocking] and can take seconds to complete; during which time orders can/will be added/removed by other EAs and TP/SL which will cause serious issues if you're iterating in the direction of the push-back/pop. It would also help to break down your logic into smaller reusable functions. Example:


bool trailing_after_breakeven(int trailing_points, int magic=0, string symbol=NULL)
{
   bool result = true;
   if (symbol == NULL)
      symbol = Symbol();
   double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
   for (int i=OrdersTotal()-1; i>=0; --i) {
      if (position_select(i, magic, symbol)) {
         RefreshRates();
         double fee_points = fee_recovery_points(OrderTicket()) * point;
         if (fee_points < 0.0)
            continue;
         bool is_break_even = false;
         bool is_move = false;
         double new_stop = DBL_MAX;
         switch(OrderType()) {
            case OP_BUY: {
               new_stop = round_tick(OrderClosePrice() - trailing_points * point);
               is_break_even = (new_stop >= OrderOpenPrice() + fee_points);
               is_move = (is_break_even && OrderStopLoss() < new_stop);
               break;
            }
            case OP_SELL: {
               new_stop = round_tick(OrderClosePrice() + trailing_points * point);
               is_break_even = (new_stop <= OrderOpenPrice() - fee_points);
               is_move = (is_break_even && new_stop < OrderStopLoss());
               break;  
            }
         }
         if (is_move) {
            result &= OrderModify(OrderTicket(), OrderOpenPrice(), new_stop, OrderTakeProfit(), 0);
         }
      }
   }
   return result;
}

bool position_select(int index, int magic=0, string symbol=NULL) {
   if (symbol == NULL)
      symbol = Symbol();
   return (
         OrderSelect(index, SELECT_BY_POS)
      && OrderType() < 2
      && OrderMagicNumber() == magic
      && OrderSymbol() == symbol
   );
}

double round_tick(double price)
{
   double tick_size = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
   return round(price / tick_size) * tick_size;
}

int fee_recovery_points(int ticket_number) 
{
   if (!OrderSelect(ticket_number, SELECT_BY_TICKET))
      return -1;
   double fees = fabs(OrderCommission() + OrderSwap());
   string symbol = OrderSymbol();
   double tick_size = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
   double tick_value = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
   double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
   double recovery = fees / tick_value * tick_size / point;
   return int(ceil(recovery));
}
 
nicholi shen:

That code is full of race conditions which could result in bugs. Since the order pool is a shared resource, you need to always iterate in reverse or use recursion due to the race conditions between multiple EAs and even raw market conditions. Order processing is [blocking] and can take seconds to complete; during which time orders can/will be added/removed by other EAs and TP/SL which will cause serious issues if you're iterating in the direction of the push-back/pop. It would also help to break down your logic into smaller reusable functions. Example:

Thanks @nicholi shen san!

I'm passing,

trailing_points=150

For

bool trailing_after_breakeven(int trailing_points, int magic=0, string symbol=NULL
Hope this is fine! Also I'm working to fix bunch of OrderModify errors I'm getting.
 
sathyaicm:

Thanks @Joel Protusada for your code!

I'm trying to inject this on my code, but missing these parameter values when passing, are these defined with values?  Also for "SurePips" ?   

double symbol = Symbol();  //Currency Pair
double trail  = 200     ;  //200 points = 20 pips
double sure   = 100     ;  //100 points = 10 pips

// when your open trade moves forward and reaches 200+100=300 points it will move the SL 200 points beyond the current price

TrailStop(symbol,trail,sure);

I've been using this for more than 3 years already. No problem so far :) 

Reason: