Convert EA for Live Trading

 

I have created one dynamic trailing stoploss but it's only working in backtesting how can I modify it to live trading. Also can anyone check whether I am doing right or not

input int      InpTrailingStopPoints      =  500;  
input double InpVolume = 0.01;
input int InpMagicNumber = 212121;
double         StopLoss;
int OnInit(){
   StopLoss = SymbolInfoDouble(_Symbol, SYMBOL_POINT)*InpTrailingStopPoints;
   return INIT_SUCCEEDED;
   

}
void OnTick(){
   
   CreateTrades(Symbol(), InpVolume, InpMagicNumber);      
   ApplyTrailingStop(Symbol(), InpMagicNumber, StopLoss);
}


void CreateTrades(string symbol, double volume, int magicNumber){
   int buycnt = 0;
   int sellcnt = 0;
   int cnt = OrdersTotal();
   
   for(int i = cnt - 1; i >= 0; i--){
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
      if(OrderSymbol() == symbol && OrderMagicNumber() == magicNumber){
         if(OrderType() == ORDER_TYPE_BUY) buycnt++;
         if(OrderType() == ORDER_TYPE_SELL) sellcnt++;
      }
    }  
   }
   if(buycnt == 0){
      if(OrderSend(symbol, ORDER_TYPE_BUY, volume, Ask, 0, 0, 0, NULL, magicNumber)){}
      
   }
   if(sellcnt == 0){
      if(OrderSend(symbol, ORDER_TYPE_SELL, volume, Bid, 0, 0, 0, NULL, magicNumber)){}
      
   }
}


void  ApplyTrailingStop(string symbol, int magicNumber, double stopLoss) {
   static int     digits   =  (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
   // Trailing from the close prices
   double   buyStopLoss    =  NormalizeDouble(SymbolInfoDouble(symbol, SYMBOL_BID)-stopLoss, digits);
   double   sellStopLoss   =  NormalizeDouble(SymbolInfoDouble(symbol, SYMBOL_ASK)+stopLoss, digits);;
   int      count          =  OrdersTotal();
   for (int i=count-1; i>=0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol()==symbol && OrderMagicNumber()==magicNumber) {
            if (OrderType()==ORDER_TYPE_BUY && buyStopLoss>OrderOpenPrice() && (OrderStopLoss()==0 || buyStopLoss>OrderStopLoss())) {
               if (OrderModify(OrderTicket(), OrderOpenPrice(), buyStopLoss, OrderTakeProfit(), OrderExpiration())) {}
            } else
            if (OrderType()==ORDER_TYPE_SELL && sellStopLoss<OrderOpenPrice() && (OrderStopLoss()==0 || sellStopLoss<OrderStopLoss())) {
               if (OrderModify(OrderTicket(), OrderOpenPrice(), sellStopLoss, OrderTakeProfit(), OrderExpiration())) {}
            }
         }
      }
   }
    
}
 

There is so much about trailing stops: https://www.mql5.com/en/search#!keyword=traling%20stop

Select one example and amend it according to your ideas.

 
  1.          if(OrderType() == ORDER_TYPE_BUY) buycnt++;
             if(OrderType() == ORDER_TYPE_SELL) sellcnt++;

    OrderSelect loop only has pending orders. Position select loop has open positions. Your counts will always be zero.

  2.       if(OrderSend(symbol, ORDER_TYPE_BUY, volume, Ask, 0, 0, 0, NULL, magicNumber)){}

    Invalid MT5 call. Invalid MT4 order type. MT5 has no predefined Ask.

  3. Stop using ChatGPT.
              Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

 
William Roeder #:
  1. OrderSelect loop only has pending orders. Position select loop has open positions. Your counts will always be zero.

  2. Invalid MT5 call. Invalid MT4 order type. MT5 has no predefined Ask.

  3. Stop using ChatGPT.
              Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

Using MT4 not MT5