Need Help: EA with multiple Timeframe

 

I am new in coding and hope find helps and insights with respect to my problem.

I downloades an EA using ADX as indicator for buy and sell signal. I added a second indicator (still ADX) but with different timeframes (5 minutes). The idea is when both ADX (1 minutes and 5 minutes) are higher than a threshold then we can send and order.

Is there any possibility to use two or more timeframes for the same indicator in EA? If yes how can we do it?

I appreciate if you have any reference for that.

Thank a lot

Files:
MyFirstEA.mq5  12 kb
 

yes you can define two timefames like this :

static input ENUM_TIMEFRAMES timeframe_1=PERIOD_D1;// Timeframe 1
static input ENUM_TIMEFRAMES timeframe_2=PERIOD_W1;// Timeframe 2
 
amine.mounir:

I am new in coding and hope find helps and insights with respect to my problem.

I downloades an EA using ADX as indicator for buy and sell signal. I added a second indicator (still ADX) but with different timeframes (5 minutes). The idea is when both ADX (1 minutes and 5 minutes) are higher than a threshold then we can send and order.

Is there any possibility to use two or more timeframes for the same indicator in EA? If yes how can we do it?

I appreciate if you have any reference for that.

Thank a lot

Hi amine,

Nice work for not being too familiar with the language. I would like to point out that while it's nice to have an understanding of low-level API features like how to copy buffers to C-style arrays and such... MQL5 is really intended to be used with the standard library classes. Not using the standard library is like programming on hard mode. Here is an example of your EA refactored to avoid the pain and frustration of dealing directly with low-level abstractions. 


extern int     inpStopLoss    = 100;         // Stop Loss Pips
extern int     inpTakeProfit  = 100;        // Take Profit Pips
input int      inpAdxPeriod   = 14;        // ADX Period
input ulong    inpMagic       = 12345;    // EA Magic Number
input double   inpAdxMin      = 22.0;    // Minimum ADX Value
input double   inpLot         = 0.1;    // Lots to Trade

#include <Indicators\Indicators.mqh>
#include <Trade\Trade.mqh>

CiADX          *adx_m1; 
CiADX          *adx_m5;
CIndicators    indicators;
CSymbolInfo    sym_info;
CPositionInfo  pos_info;
CTrade         trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   if(_Digits == 5 || _Digits == 3){
      inpStopLoss *= 10;
      inpTakeProfit *= 10;
   }
   adx_m1 = new CiADX();
   adx_m5 = new CiADX();
   if(!adx_m1.Create(_Symbol, PERIOD_M1, inpAdxPeriod)
      || !adx_m5.Create(_Symbol, PERIOD_M5, inpAdxPeriod)
      || !indicators.Add(adx_m1)
      || !indicators.Add(adx_m5)
   ){
      return INIT_FAILED;
   }
   sym_info.Name(_Symbol);
   trade.LogLevel(LOG_LEVEL_ALL);
   trade.SetExpertMagicNumber(inpMagic);
   trade.SetDeviationInPoints(10);
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   indicators.Refresh(); //refresh indicator buffers
   sym_info.RefreshRates();
   if(adx_m1.Main(0) > inpAdxMin && adx_m5.Main(0) > inpAdxMin){
      if(!(pos_info.SelectByMagic(_Symbol, inpMagic) 
         && pos_info.PositionType() == POSITION_TYPE_BUY)
      ){
         double sl = NormalizeDouble(sym_info.Ask() - inpStopLoss * _Point, _Digits);
         double tp = NormalizeDouble(sym_info.Ask() + inpTakeProfit * _Point, _Digits);
         if(!trade.Buy(inpLot, _Symbol, sym_info.Ask(), sl, tp))
            Alert("The Buy order request could not be completed -error:", _LastError);
      }
   }
   else if(adx_m1.Main(0) < inpAdxMin && adx_m5.Main(0) < inpAdxMin){
      if(!(pos_info.SelectByMagic(_Symbol, inpMagic) 
         && pos_info.PositionType() == POSITION_TYPE_SELL)
      ){
         double sl = NormalizeDouble(sym_info.Bid() + inpStopLoss * _Point, _Digits);
         double tp = NormalizeDouble(sym_info.Bid() - inpTakeProfit * _Point, _Digits);
         if(!trade.Sell(inpLot, _Symbol, sym_info.Bid(), sl, tp))
            Alert("The Sell order request could not be completed -error:", _LastError);
      }
   }
}
//+------------------------------------------------------------------+
 

Thanks a lot guys I will see the output and keep you posted.

 

Thanks a lot nicholi shen I used your EA and it seems working fine. I even Added other indicators, great work. I would like to know if there is any possibilty to create a text file or asci file with all computed data (Time, O,H,L,C, ADX_1 minute, ADX_5 minutes) after running the EA.

Thank for your assitance