Please help

 

I had tried to write the ATR trailing stop EA code (MQL4) from "Orchard Forex" as below.

Compiling the code was passing, but I could not run this EA actually.

Please anyone make comments where are the possible mistake.


input int      InpATRPeriods     = 10;             // ATR Periods



input double   InpATRMultiplier  = 2.5;            // ATR Multiplier







input double   InpVolume         = 0.01;           // Trade volume



input int      InpMagicNumber    = 212121;         // Magic Number







void OnTick() {



   



   static double stopLoss = 0.0;



   



   if(NewBar() || stopLoss==0.0) {



  stopLoss = ATRStopLoss(Symbol(),(ENUM_TIMEFRAMES)Period(), InpATRPeriods, InpATRMultiplier);



  }



  



  CreateTrades(Symbol(), InpVolume, InpMagicNumber);



  



  ApplyTrailingStop(Symbol(), InpMagicNumber, stopLoss);







}







bool NewBar() {







   static datetime   previousTime   = 0;



   datetime          now            = iTime(Symbol(), Period(), 0);



   if (now!=previousTime) {



      previousTime   =  now;



      return(true);



     }



     return(false);



}







double   ATRStopLoss(string symbol,ENUM_TIMEFRAMES timeframe,int periods, double multiplier) {







   double   atr      = iATR(symbol, timeframe, periods, 1);   // Don't use period 0



   double   stopLoss = atr * multiplier;



   return(stopLoss);



}







void  ApplyTrailingStop(string symbol, int magicNumber, double stopLoss) {







   static   int  digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);



   



   // Trailing from the close price



   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())) {}



           }



        }



     }



  } 



}







void CreateTrades(string symbol, double volume, int magicNumber) {







   int   buyCount    = 0;



   int   sellCount   = 0;



   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)    buyCount++;



             if (OrderType()==ORDER_TYPE_SELL)   sellCount++;



            }



         }  



   }



   if(buyCount==0) {



      if(OrderSend(symbol, ORDER_TYPE_BUY, volume,0,0,0,0,0,magicNumber)) { }



     }  



   if(sellCount==0) {



      if(OrderSend(symbol, ORDER_TYPE_SELL , volume,0,0,0,0,0,magicNumber)) { }



     }   



}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Ssupitchakul: but I could not run this EA actually.
  1. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)