Calculate Keltner Channel for EA

 

Hello , 

i'd like to calculate Keltner channel for my EA , i only need the calculations not to make it as an indicator to display on the terminal 

i have calculated the (Moving average & Average true rage )  but can't get to calculate Keltner channel right

Ketlner channel calculations that i'd like to make :

MiddleLine=EMAUpperChannelLine=EMA+2ATRLowerChannelLine=EMA -2ATR


This is my attempt , please help find the error!!


// ATR INDICATOR 

    double ATRvalues[];

    int ATRdef = iATR(_Symbol,_Period,10);

    ArraySetAsSeries(ATRvalues,true);

    CopyBuffer(ATRdef,0,0,3,ATRvalues);

    double ATRV = NormalizeDouble(ATRvalues[0],5);



//MOVING AVERAGE INDICATOR

     double ma_array [];

     int MAdef = iMA(_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE);

     CopyBuffer(MAdef,0,0,3,ma_array);

     float ma0 = ma_array[0];



     //KETLNER CHANNEL INDICATOR

     double upperk [];

     double middlek[];

     double lowerk [];

     

     ArraySetAsSeries(upperk,true);

     ArraySetAsSeries(middlek,true);

     ArraySetAsSeries(lowerk,true);

     

     middlek = ma_array;

     upperk = middlek + (2 * ATRvalues);

     lowerk = middlek - (2 * ATRvalues);

 

     

 
andrw11: i'd like to calculate Keltner channel for my EA , i only need the calculations not to make it as an indicator
  1. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

    Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.

    You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 programming forum #33 2017.05.23

  2.     int ATRdef = iATR(_Symbol,_Period,10);
    
        ArraySetAsSeries(ATRvalues,true);
    
        CopyBuffer(ATRdef,0,0,3,ATRvalues);
    

    Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

Reason: