My EMA array keeps showing a negative value, is that normal? - page 2

 
Alain Verleyen:

Exactly I changed the variable name. But in the code you posted after, it's again 2 times EMA5_Handle.

By the way, you still don't show the code where we can see you get negative values. Which values ? Which array, indexes ?

Sure. Code is below and the output is below that.  To me the handle command looks OK but I'm wondering if I got the CopyBuffer command wrong?


int OnInit()
  {
 
   RSIHandle=iRSI(Symbol(),0,RSIPeriod,InpAppliedPrice);
   if(RSIHandle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator RSI");
      return(INIT_FAILED);
     } 
//---     
   EMA5_Handle=iMA(Symbol(),0,5,0,MODE_EMA,PRICE_CLOSE);
   if(EMA5_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     }
//---     
   EMA10_Handle=iMA(Symbol(),0,10,0,MODE_EMA,PRICE_CLOSE);
   if(EMA10_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     }
//---     
   SMA50_Handle=iMA(Symbol(),0,50,0,MODE_SMA,PRICE_CLOSE);
   if(SMA50_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     }
//---     
   SMA100_Handle=iMA(Symbol(),0,100,0,MODE_SMA,PRICE_CLOSE);
   if(SMA100_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     }                      

   CopyBuffer(MACDHandle,MAIN_LINE,0,9,MACDMainArray);
   ArraySetAsSeries(MACDMainArray,true);

   CopyBuffer(MACDHandle,SIGNAL_LINE,0,9,MACDSignalArray);
   ArraySetAsSeries(MACDSignalArray,true);
  
   CopyBuffer(EMA5_Handle,MODE_EMA,0,17,EMA5_ARRAY);
   ArraySetAsSeries(EMA5_ARRAY,true);
  
   CopyBuffer(EMA10_Handle,MODE_EMA,0,17,EMA10_ARRAY);
   ArraySetAsSeries(EMA10_ARRAY,true);
  
   CopyBuffer(SMA50_Handle,MODE_SMA,0,17,SMA50_ARRAY);
   ArraySetAsSeries(SMA50_ARRAY,true);
  
   CopyBuffer(SMA100_Handle,MODE_SMA,0,17,SMA100_ARRAY);
   ArraySetAsSeries(SMA100_ARRAY,true);

 for(int x=0; x<8 && PrintToJournalValues==true; x++)
     {
      Print("MACD Main Array [ ",x," ] = ",NormalizeDouble(MACDMainArray[x],_Digits));
      Print("MACD Signal Array [ ",x," ] = ",NormalizeDouble(MACDSignalArray[x],_Digits));
      Print("EMA 5 Array [ ",x," ] = ",NormalizeDouble(EMA5_ARRAY[x],_Digits));
      Print("EMA 10 Array [ ",x," ] = ",NormalizeDouble(EMA10_ARRAY[x],_Digits));
      Print("SMA 50 Array [ ",x," ] = ",NormalizeDouble(SMA50_ARRAY[x],_Digits));
      Print("SMA 100 Array [ ",x," ] = ",NormalizeDouble(SMA100_ARRAY[x],_Digits));
      Print("RSI Array[ ",x," ] = ",NormalizeDouble(RSI_ARRAY[x],_Digits));
     }

2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 10 Array [ 5 ] = 0.0
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 5 Array [ 5 ] = -9.223464270575145e+018
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Signal Array [ 5 ] = -0.00092
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Main Array [ 5 ] = 6e-005.0
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    RSI Array[ 4 ] = 57.85432
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 100 Array [ 4 ] = 1.04294
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 50 Array [ 4 ] = 1.03867
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 10 Array [ 4 ] = 0.0
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 5 Array [ 4 ] = -9.223464270575145e+018
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Signal Array [ 4 ] = -0.00083
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Main Array [ 4 ] = 0.00017
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    RSI Array[ 3 ] = 59.21015
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 100 Array [ 3 ] = 1.04291
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 50 Array [ 3 ] = 1.0387
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 10 Array [ 3 ] = 0.0
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 5 Array [ 3 ] = -9.223464270575145e+018
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Signal Array [ 3 ] = -0.00074
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Main Array [ 3 ] = 0.00029
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    RSI Array[ 2 ] = 54.92844
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 100 Array [ 2 ] = 1.04289
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 50 Array [ 2 ] = 1.03867
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 10 Array [ 2 ] = 0.0
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    EMA 5 Array [ 2 ] = -9.223464270575145e+018
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Signal Array [ 2 ] = -0.00065
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    MACD Main Array [ 2 ] = 0.00034
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    RSI Array[ 1 ] = 56.52017
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 100 Array [ 1 ] = 1.04285
2015.06.30 14:25:03.713    ForExpertsV1 (EURCHF,H1)    SMA 50 Array [ 1 ] = 1.03866

 
flwilliams87:

Thanks Alan and I will include more error handling in my code. Do you think the problem might be my CopyBuffer command? Or could it be something else?


You have to check the return value of CopyBuffer(), please check the documentation.

You can't use MODE_EMA in CopyBuffer, an MA only have 1 buffer, the index should be 0. MODE_EMA value is 1. 

Your SMA values are good by chance, as MODE_SMA is 0.

Reason: