How to Plot volume in a loop

#1
Hi,

I have a MT4 indicator, which I translated into AFL. While plotting volume, it gives error that I cannot use Plot for more than 500 times. I found this solution:


But I am not sure, how to make it work with my code due to my limited skills.
Any help will be highly appreciated.

Code for AFL is given below:


Code:
_SECTION_BEGIN("EMA");
P = Volume;
Periods = Param("PeriodsEMA", 100);
Plot( EMA( P, Periods ), _DEFAULT_NAME(), colorBlack, styleLine | styleThick );
_SECTION_END();

// Param( ''name'', defaultval, min, max, step, sincr = 0 )  
period = Param("period", 2);;
//LowColor = colorLightGrey ;
//ClimaxColor = colorGreen;
//ChurnColor = colorRed;
//ClimaxChurnColor=colorBlue;
//LowChurnColor= colorBlack ;
// dynamicrsi = IIf( Close > MA(C,10), RSI(9), RSI(14) );  



for( i = 0; i < BarCount; i++ )
{
Value2=0;
Value3=0;
HiValue2=0;
HiValue3=0;
LoValue3=99999999;
tempv2=0;
tempv3=0;
tempv=0;
Range=(high[i]-low[i]);
Value2=Volume[i]*Range;
if(Range != 0 )
         Value3 = Volume[i]/Range;
         

for( n = 0; n < period; n++ ) 
{
tempv2=Volume[n]*((high[n]-low[n]));
if( tempv2 >= HiValue2 )
HiValue2 = tempv2;

if(Volume[n]*((high[n]-low[n]))!=0)
           {
            tempv3=Volume[n]/((high[n]-low[n]));
            if(tempv3>HiValue3)
               HiValue3=tempv3;
            if(tempv3<LoValue3)
               LoValue3=tempv3;
           }

if(Value2==HiValue2 && close[i]>(high[i]+low[i])/2)
        {
         BarColor[i] = colorRed;
        }
//---
      if(Value3==HiValue3)
        {
         BarColor[i] = colorGreen;

        }
      if(Value2==HiValue2 && Value3==HiValue3)
        {
         BarColor[i] = colorBlue;

        }
      if(Value2==HiValue2 && close[i]<=(high[i]+low[i])/2)
        {
         BarColor[i] = colorYellow;
        }    
        
     }

_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), BarColor, ParamStyle( "Style", styleHistogram | styleThick, maskHistogram ), 2 );
_SECTION_END(); 
}
Thanks
 

trash

Well-Known Member
#2
Unfortunately your code is wrong on many levels. Read in the AB help on Understanding how AFL works, looping, also read coding mistakes there etc.

Secondly the thing you want to do is creating Better Volume indicator. I have programmed it few years ago for AmiBroker (and no it is not on the Internet, those ones are wrong) and there isn't any barcount loop required. Just pure array processing.

 
Last edited:

trash

Well-Known Member
#4
No, as I said in my first post those ones to be found on the Internet are all wrong (the wisestocktrader ones).

The tradingview one is not AFL. I didn't check that one on top of that.
 
#5
ok. I got it working and did the translation into Amibroker from MQL4. It is working now but something is still wrong as the Volume Colors are different than what I see in MT4.

Code:
lookback=2;


for( i = 0; i < BarCount-1; i++ )
//for( i = BarCount-1; i >=0; i-- )
{
Value2=0;
Value3=0;
HiValue2=0;
HiValue3=0;
LoValue3=99999999;
tempv2=0;
tempv3=0;
tempv=0;

Range=(high[i]-low[i]);
Value2=Volume[i]*Range;
if(Range != 0 )
Value3 = Volume[i]/Range;         

for( n = i; n < (i+lookback) ; n++ ) 
{
tempv2=Volume[n]*((high[n]-low[n]));
if( tempv2 >= HiValue2 )
HiValue2 = tempv2;

if((Volume[n]*((high[n]-low[n])))!=0)
           {
            tempv3=Volume[n]/(high[n]-low[n]);
            if(tempv3>HiValue3)
               HiValue3=tempv3;
            if(tempv3<LoValue3)
               LoValue3=tempv3;
           }
}
if(Value2==HiValue2 && close[i]>(high[i]+low[i])/2)
        {
         BarColor[i] = colorRed;
        }
//---
      if(Value3 == HiValue3)
        {
         BarColor[i] = colorGreen;
        }
      if(Value2==HiValue2 && Value3==HiValue3)
        {
         BarColor[i] = colorBlue;
        }
      if(Value2==HiValue2 && close[i]<=(high[i]+low[i])/2)
        {
         BarColor[i] = colorYellow;
        }     
     }



_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), BarColor, ParamStyle( "Style", styleHistogram | styleThick, maskHistogram ), 2 );
_SECTION_END();
 
Last edited:
#6
MT4 Code is given below:
Code:
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 clrGreen          
#property indicator_color2 clrLightGray
#property indicator_color3 clrLightGray
#property indicator_color4 clrLightGray
#property indicator_color5 clrCrimson      
#property indicator_color6 clrMagenta      
#property indicator_color7 clrBlack    
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 2

extern int     NumberOfBars=500; // 0 ; 500;
extern string  Note="0 means Display all bars";
extern int     MAPeriod = 100;
extern int     LookBack = 2;
extern int     width1 = 2;
extern int     width2 = 2;

double red[],blue[],yellow[],green[],white[],magenta[],v4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,red);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,width2);
   SetIndexLabel(0,"Climax High ");

   SetIndexBuffer(1,blue);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,width2);
   SetIndexLabel(1,"Neutral");

   SetIndexBuffer(2,yellow);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,width2);
   SetIndexLabel(2,"Low ");

   SetIndexBuffer(3,green);
   SetIndexStyle(3,DRAW_HISTOGRAM,0,width2);
   SetIndexLabel(3,"HighChurn ");

   SetIndexBuffer(4,white);
   SetIndexStyle(4,DRAW_HISTOGRAM,0,width2);
   SetIndexLabel(4,"Climax Low ");

   SetIndexBuffer(5,magenta);
   SetIndexStyle(5,DRAW_HISTOGRAM,0,width2);
   SetIndexLabel(5,"ClimaxChurn ");

   SetIndexBuffer(6,v4);
   SetIndexStyle(6,DRAW_LINE,0,2);
   SetIndexLabel(6,"Average("+(string)MAPeriod+")");

   IndicatorShortName("Better Volume");
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double VolLowest,Range,Value2,Value3,HiValue2,HiValue3,LoValue3,tempv2,tempv3,tempv;
   if(NumberOfBars==0 || NumberOfBars>rates_total)
      NumberOfBars=rates_total;  //
//---
   int begin=(prev_calculated<1)?NumberOfBars-MAPeriod:rates_total-prev_calculated;
//---
   for(int i=begin;i>=0;i--)
     {
      red[i]= 0; blue[i] = (double)tick_volume[i]; yellow[i] = 0; green[i] = 0; white[i] = 0; magenta[i] = 0;
      Value2=0;Value3=0;HiValue2=0;HiValue3=0;LoValue3=99999999;tempv2=0;tempv3=0;tempv=0;

      VolLowest=(double)tick_volume[ArrayMinimum(tick_volume,20,i)];
      if(tick_volume[i]==VolLowest)
        {
         yellow[i]=(double)tick_volume[i];
         blue[i]=0;
        }
//---
      Range=(high[i]-low[i]);
      Value2=tick_volume[i]*Range;

      if(Range != 0 )
         Value3 = tick_volume[i]/Range;

      for(int n=i;n<i+MAPeriod;n++)
        {
         tempv=tick_volume[n]+tempv;
        }
      v4[i]=NormalizeDouble(tempv/MAPeriod,0);
//---
      for(int n=i;n<i+LookBack;n++)
        {
         tempv2=tick_volume[n]*((high[n]-low[n]));
         if( tempv2 >= HiValue2 )
            HiValue2 = tempv2;

         if(tick_volume[n]*((high[n]-low[n]))!=0)
           {
            tempv3=tick_volume[n]/((high[n]-low[n]));
            if(tempv3>HiValue3)
               HiValue3=tempv3;
            if(tempv3<LoValue3)
               LoValue3=tempv3;
           }
        }
//---
      if(Value2==HiValue2 && close[i]>(high[i]+low[i])/2)
        {
         red[i] = (double)tick_volume[i];
         blue[i]=0;
         yellow[i]=0;
        }
//---
      if(Value3==HiValue3)
        {
         green[i]= (double)tick_volume[i];
         blue[i] =0;
         yellow[i]=0;
         red[i]=0;
        }
      if(Value2==HiValue2 && Value3==HiValue3)
        {
         magenta[i]=(double)tick_volume[i];
         blue[i]=0;
         red[i]=0;
         green[i]=0;
         yellow[i]=0;
        }
      if(Value2==HiValue2 && close[i]<=(high[i]+low[i])/2)
        {
         white[i]=(double)tick_volume[i];
         magenta[i]=0;
         blue[i]=0;
         red[i]=0;
         green[i]=0;
         yellow[i]=0;
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

trash

Well-Known Member
#7
Your code is wrong (technically and as a result. And again: no looping required) and the MT4 code is wrong too. Both are wrong compared to the original BetterVolume.
 
#8
May be if you are specific about the part which is wrong that would definitely help me. For me, MQL4 indicator just works fine and I am trying to translate that into AFL. Not trying to reproduce original Better Volume Indicator. If I am using loops, is there something wrong with the results because of this ? How looping with index variables is technically wrong as compared to array processing ? May be I am missing something here.
 
Last edited:

trash

Well-Known Member
#9
Your version is wrong.
In addition I said in second post to read in manual sections "Understanding how AFL works". Have you done it? No.

Anyway, I'm repeating myself... loop is not required.
The AFL version of your posted MQ4 version is just 20 lines of code.

And of course here is proof that properly coding in AFL will show up same result as posted MT4 version.



 
#10
I read it multiple times and I tried to do it with arrays. I already have the code that I tried with Arrays but I am definitely missing something. Back to the drawing board but thanks a lot for showing me that it works that way :thumb:
 

Similar threads