iMAonArray - Coding help

#1
Hi All,

I have been learning more MQL4 and have been trying to get the hang of arrays, which is going OK but I have noticed a problem with iMAonArray. I have scoured the internet for most of the weekend and I can't find anybody else reporting the problem so I am guessing I am doing something wrong but for my life I cannot work out what that might be.

1st is the HiLoDeltaAV doesn't work properly at all

The problem is, when I apply the iMAonArray to the whole array for either MA, it doesn't calculate it properly. I am looking for a 4 Period SMA, and when I set the iMAonArray to only cover the last four bars of the array, it calculates it correctly. However when I set the the iMAonArray to use the entire array I *think* it is calculating the average against all bars, rather than a 4-period SMA, but either way it isn't calculating correctly. I have attached the work I have done so far.

You will notice that I have multiple loops, this was an attempt to fix the problem, as well as initializing the array and setting it as series (I have tried it in both directions),

If anybody could spare a minute to give me any tips I would be grateful.

Many thanks in advance

Code: Select all

//+------------------------------------------------------------------+
//|                                             Candle Price Action.mq4 |
//|                                                           itsbigyellow |
//+------------------------------------------------------------------+
#property  copyright "itsbigyellow"

#property  indicator_separate_window
#property  indicator_buffers 6
#property  indicator_color1  DarkGray
#property  indicator_color2  Red
#property  indicator_color3  DarkGray
#property  indicator_color4  Green
#property  indicator_color5  Green
#property  indicator_color6  Red
#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width4  2
#property  indicator_width5  4
#property  indicator_width6  4


//
//
//
//
//


double Buffer1[];
double OpClDeltaAv[10000];
double Buffer3[];
double HiLoDeltaAv[10000];
double Buffer5[];
double Buffer6[];
double Multiplier=0;

double CalculateNormalizedDigits()
{
   // If there are 3 or fewer digits (JPY, for example), then return 0.01, which is the pip value.
   if (Digits == 5){
      return(0.0001);
   }
   // If there are 4 or more digits, then return 0.0001, which is the pip value.
   else if (Digits ==4 ){
      return(0.001);
   }
   else if (Digits ==3 ){
      return(0.01);
   }
   else if (Digits ==2 ){
      return(0.1);
   }
   // In all other cases, return 0.
   else return(1);
}

double nDigits = CalculateNormalizedDigits();


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,Buffer1);
   SetIndexBuffer(1,OpClDeltaAv);
   SetIndexBuffer(2,Buffer3);
   SetIndexBuffer(3,HiLoDeltaAv);
   SetIndexBuffer(4,Buffer5);
   SetIndexBuffer(5,Buffer6);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_HISTOGRAM);
   SetIndexStyle(5,DRAW_HISTOGRAM);
   SetIndexLabel(1,"OpClDeltaAv");
   SetIndexLabel(3,"HiLoDeltaAv");


   IndicatorShortName("Candle Price Action");
   IndicatorDigits(2);
        
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
 
     





int start()
{
   int countedBars = IndicatorCounted();
   int limit, i;
 
   if(countedBars<0) return(-1);
   if(countedBars>0) countedBars--;
      limit = Bars-countedBars;
      
   double HiLoDelta[10000]; 
   double OpClDelta[10000];
   
   ArrayInitialize(HiLoDeltaAv,EMPTY_VALUE);
   
   ArraySetAsSeries (OpClDeltaAv,true);
   ArraySetAsSeries (HiLoDeltaAv,true);
   
   

   for(i=limit; i>=0; i--)
   {
      if (Open[i]>=Close[i]) {OpClDelta[i]=(Open[i]-Close[i])/nDigits;}
      if (Close[i]>Open[i])  {OpClDelta[i]=(Close[i]-Open[i])/nDigits;}
      
   }
   
   for(i=limit; i>=0; i--)
   {      
      HiLoDelta[i]   = (High[i]-Low[i])/nDigits;
            
   }
   
   for(i=limit; i>=0; i--)
   {      
      OpClDeltaAv[i] = iMAOnArray(OpClDelta,0,4,0,MODE_SMA,i);

    
      //Print("[0] "+HiLoDelta[0]+" [1] "+HiLoDelta[1]+" [2] "+HiLoDelta[2]+" [3] "+HiLoDelta[3]+ " [4] "+HiLoDelta[4] );
   }

   for(i=limit; i>=0; i--)
   {  
      HiLoDeltaAv[i] = iMAOnArray(HiLoDelta,0,4,0,MODE_SMA,i);

      
      //Print("[0] "+HiLoDelta[0]+" [1] "+HiLoDelta[1]+" [2] "+HiLoDelta[2]+" [3] "+HiLoDelta[3]+ " [4] "+HiLoDelta[4] );
    
   }
   
         
      for(i=limit; i>=0; i--)
      {   
   
          if (OpClDelta[i+1] > (OpClDeltaAv[i+1]*1.25) && HiLoDelta[i+1] > (HiLoDeltaAv[i+1]*1.25)   ){
               Buffer5[i] = HiLoDelta[i];
               Buffer1[i] = OpClDelta[i];
                 Buffer3[i] = 0;
               } else {
               Buffer5[i] = 0;
               Buffer1[i] = OpClDelta[i];
               Buffer3[i] = HiLoDelta[i];
               }
       }
            
    
       for(i=limit; i>=0; i--)
       {
        
         if (OpClDelta[i+1] > (OpClDeltaAv[i+1]*1.25) && HiLoDelta[i+1] > (HiLoDeltaAv[i+1]*1.25)   ){
            Buffer6[i] = HiLoDelta[i];
            Buffer1[i] = OpClDelta[i];
            Buffer3[i] = 0;
            } else {
            Buffer6[i] = 0;
            Buffer1[i] = OpClDelta[i];
            Buffer3[i] = HiLoDelta[i];
            }
        }
      
   return(0);
}



  1. Similar Topics

    1. iMAonArray - Am I doing something wrong ?

      0 Replies 843 Views

      by rogerha, Mon Sep 20, 2021 6:28 pm in Coding Help

      0 Replies
      843 Views
    2. CODING HELP

      0 Replies 1304 Views

      by slyfer91, Sun Apr 25, 2021 6:43 am in Coding Help

      0 Replies
      1304 Views
    3. Help me to calculate slope

      2 Replies 493 Views

      by Eddie_83, Fri Apr 23, 2021 3:08 am in Coding Help

      2 Replies
      493 Views

Return to “Coding Help”

Users viewing this forum: CommonCrawl [Bot] and 1 guest