Indicators with different time/period to the chart, in EA's

 

Hi all,

Below is the (working) code to open a buy order if the close price of the previous bar is above the 20 moving average. However, i use M15 charts but want the moving average to be calculated on the D1 chart/time frame. How do i go about this?

TIA

#include <Trade/Trade.mqh>
CTrade trade;

input double cLot=25;
input char     MA_Period=20;

int maHandle;
double maVal[];
double p_close;

int OnInit()
  {
   maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
   if(maHandle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
     }
  return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{
   IndicatorRelease(maHandle);
}

void OnTick()
  {  
    MqlRates mrate[];  
    MqlTick latest_price; 
    
    ArraySetAsSeries(mrate,true);    
    ArraySetAsSeries(maVal,true);
    
    if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      return;
     }
    
    if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

   if(CopyBuffer(maHandle,0,0,2,maVal)<0)
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      return;
     }
   
   p_close=mrate[1].close;  
     
    int    digits=(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
    double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);
    double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
    double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    double SL=bid-5000*point;
    double TP=bid+5000*point; 

   bool Buy_Condition_1 = (p_close > maVal[1]); 
   bool Buy_Condition_2 = (PositionSelect(_Symbol) == false); //check theres no open positions
  
   if(Buy_Condition_1) // && Buy_Condition_2 && Buy_Condition_3)
     {
      if(Buy_Condition_2)
         {
           trade.Buy(cLot,_Symbol,ask,SL,TP,"");
         }
     }
  }
 
JimboDiggity: t want the moving average to be calculated on the D1 chart/time frame. How do i go about this?

Perhaps you should read the manual. What are the first two parameters in iMA?
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

 
William Roeder #:

Perhaps you should read the manual. What are the first two parameters in iMA?
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

Terse as your response was its actually very helpful as i wasn't sure where to start.. So to be clear the below would be correct, right? I assume none of the rest of the code needs to be changed to accommodate?

TIA

maHandle=iMA(_Symbol,PERIOD_D1,MA_Period,0,MODE_EMA,PRICE_CLOSE);
Reason: