How MQL5 Adds Two Values. iATR + iMA = Crazy!

 

Question#1:

Can someone explain why two values are not able to add?

iATR     +   iMA     =  9.881312916824931e-32

5.9125  +  2691.3  =  9.881312916824931e-32

I have attached code for review. 

Great Thanks!

Files:
TEST2.mq5  3 kb
TEST2.ex5  11 kb
 

MQL5 indicators are a bit more complicated, there are handles, copying to arrays and only then you can manipulate the values.

Look up iATR and iMA in the code base and you'll figure it out.

 
cowmodee:

Can someone explain why two values are not able to add?

In your code, you have:

double ATR_plus_KC_Definition=iATR(_Symbol,_Period,20)+iMA(_Symbol,_Period,20,0,MODE_SMA,PRICE_CLOSE);

You are adding two indicator handles together, storing them in a double, then using that double as an indicator handle?

Indicators handles don't work that way.

Get the values for ATR and MA like you are doing. Store them in buffers as you are doing. Then . . . add the respective values for ATR and MA.

ATR_plus_KC_Array[0] = ATR_Array[0] + KC_Array[0];

ATR_plus_KC_Array[1] = ATR_Array[1] + KC_Array[1];

. . . 
 
Anthony Garot:

In your code, you have:

You are adding two indicator handles together, storing them in a double, then using that double as an indicator handle?

Indicators handles don't work that way.

Get the values for ATR and MA like you are doing. Store them in buffers as you are doing. Then . . . add the respective values for ATR and MA.

With GREAT Thanks!

this works.

// calculate for the last candle
      
      double ATR_Value=NormalizeDouble(ATR_Array[0],5);
      double KC_Value=NormalizeDouble(KC_Array[0],5);
      double ATR_Plus_KC_Value=NormalizeDouble((ATR_Array[0]+KC_Array[0]),5);

      // calculate for the previous candle
      double ATR_Value_Previous=ATR_Array[1];
      double KC_Value_Previous=KC_Array[1];
      double ATR_Plus_KC_Value_Previous=NormalizeDouble((ATR_Array[1]+KC_Array[1]),5);
 
It is more correct to connect indicators.
int   KC_Definition,
      ATR_Definition;
      
// Define Average True Range
// create Array for several prices
double   ATR_Array[],
         KC_Array[];  

void OnInit()
  {
      // define the properties of the ATR, KC
      KC_Definition  = iMA(_Symbol,_Period,20,0,MODE_SMA,PRICE_CLOSE);
      ATR_Definition = iATR(_Symbol,_Period,20);
      
      // sort the price array from the current candle downwards
      ArraySetAsSeries(ATR_Array,true);
      ArraySetAsSeries(KC_Array,true);
  }

void OnTick()
  {    
      ArrayFree(ATR_Array);
      ArrayFree(KC_Array);

      // Copy price info into the array
      if( CopyBuffer(ATR_Definition,0,0,2,ATR_Array) !=2 )  return;
      if( CopyBuffer(KC_Definition,0,0,2,KC_Array) !=2 )  return;

      // calculation
  }
And make the necessary calculations
 
Konstantin Nikitin:
It is more correct to connect indicators.
And make the necessary calculations
more efficient!
good suggestion! 
thank you!