Download MetaTrader 5

Arrays [i-2]

To add comments, please log in or register
Unable to find the right application? Order it!
Abraham
89
Abraham 2010.12.15 11:04 

Sorry if this is a nube question but it is driving me crazy. The first thing I'd like to say is that the time period system is annoying! I don't see why we are restricted to a limited time set. Second, I am struggling with an array issue. Why can't I write (for example) buffer[i-2]? eg consider the OBV function:

void CalculateOBV(int StartPosition,
                  int RatesCount,
                  const double &ClBuffer[],
                  const long &VolBuffer[])
  {
   for(int i=StartPosition;i<RatesCount;i++)
     {
      //--- get some data
      double Volume=VolBuffer[i];
      double PrevClose=ClBuffer[i-1];
      double CurrClose=ClBuffer[i];
      //--- fill ExtOBVBuffer
      if(CurrClose<PrevClose) ExtOBVBuffer[i]=ExtOBVBuffer[i-1]-Volume;
      else
        {
         if(CurrClose>PrevClose) ExtOBVBuffer[i]=ExtOBVBuffer[i-1]+Volume;
         else                    ExtOBVBuffer[i]=ExtOBVBuffer[i-1];
        }
     }
  }


Why can't I write:

if(CurrClose>PrevClose) ExtOBVBuffer[i]=ExtOBVBuffer[i-2]+Volume;

         else                    ExtOBVBuffer[i]=ExtOBVBuffer[i-2];


or:

double PrevClose=ClBuffer[i-2];


What  am trying to do is deal with the timeframe issue by starting with PERIOD_M1 and creating my own time spans with multiples of the 1 minute graph.

Thanks for any help...

Alexey Da
Moderator
6606
Alexey Da 2010.12.15 12:38  
Please explain your problem. Are you getting error?
Abraham
89
Abraham 2010.12.15 16:50  
alexvd:
Please explain your problem. Are you getting error?
I've tried this kind of approach many times in many contexts, [i-1] works but any other larger integer leads to the result that the graph disappears (although there is no error).
Alexey Da
Moderator
6606
Alexey Da 2010.12.15 20:48  
Abraham:
I've tried this kind of approach many times in many contexts, [i-1] works but any other larger integer leads to the result that the graph disappears (although there is no error).
Could you provide full source code?
Abraham
89
Abraham 2010.12.16 01:50  

Ok, this is basically the OBV indicator with some of the arrays in the function set to [i-2]:


//+------------------------------------------------------------------+
//|                                                          OBV.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2009, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "On Balance Volume"
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  DodgerBlue
#property indicator_label1  "OBV"
//--- input parametrs
input ENUM_APPLIED_VOLUME InpVolumeType=VOLUME_TICK; // Volumes
//---- indicator buffer
double                    ExtOBVBuffer[];
//+------------------------------------------------------------------+
//| On Balance Volume initialization function                        |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- define indicator buffer
   SetIndexBuffer(0,ExtOBVBuffer);
//--- set indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//---- OnInit done
  }
//+------------------------------------------------------------------+
//| On Balance Volume                                                |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,
                const datetime &Time[],
                const double &Open[],
                const double &High[],
                const double &Low[],
                const double &Close[],
                const long &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
//--- variables
   int    pos;
//--- check for bars count
   if(rates_total<2)
      return(0);
//--- starting calculation
   pos=prev_calculated-1;
//--- correct position, when it's first iteration
   if(pos<1)
     {
      pos=1;
      if(InpVolumeType==VOLUME_TICK)
         ExtOBVBuffer[0]=TickVolume[0];
      else ExtOBVBuffer[0]=Volume[0];
     }
    
//--- main cycle
   if(InpVolumeType==VOLUME_TICK)
    
      CalculateOBV(pos,rates_total,Close,TickVolume);

  
   else
      CalculateOBV(pos,rates_total,Close,Volume);
     
//---- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Calculate OBV by volume argument                                 |
//+------------------------------------------------------------------+
void CalculateOBV(int StartPosition,
                  int RatesCount,
                  const double &ClBuffer[],
                  const long &VolBuffer[])
  {
   for(int i=StartPosition;i<RatesCount;i++)
     {
      //--- get some data
      double Volume=VolBuffer[i];
      double PrevClose=ClBuffer[i-2];
      double CurrClose=ClBuffer[i];
      //--- fill ExtOBVBuffer
      if(CurrClose<PrevClose) ExtOBVBuffer[i]=ExtOBVBuffer[i-2]-Volume;
      else
        {
         if(CurrClose>PrevClose) ExtOBVBuffer[i]=ExtOBVBuffer[i-2]+Volume;
         else                    ExtOBVBuffer[i]=ExtOBVBuffer[i-1];
        }
     }
  }
//+------------------------------------------------------------------+


Alexey Da
Moderator
6606
Alexey Da 2010.12.21 18:33  

I've tried to attach your indicator to chart. There are error.

 

ZenForex.com
217
ZenForex.com 2010.12.22 00:31  

I assume in order for you to be on the safe side, the loop should be:

for(int i=StartPosition+2;i<RatesCount;i++)
     {
      //--- get some data
      double Volume=VolBuffer[i];
      double PrevClose=ClBuffer[i-2];
      double CurrClose=ClBuffer[i];...

 for every start position>=0.

To add comments, please log in or register