Just wonder if anyone can help me on this code
Does anyone know how and where PoshBuffer is getting its value from
Nope... but in other news.... SetIndexBuffer should start at 0 rather than 1 and there is no need to declare sumn as =0.0. The default value is zero.
V
Ah, you're trying to trick me by posting the code in a different thread :)
https://forum.mql4.com/33806#351358
.... well not so fast... posbuffer gets set here...
//---- smoothed moving average rel=Close[i]-Close[i+1]; if(rel>0) sump=rel; else sumn=-rel; positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod; negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod; } PosBuffer[i]=positive; NegBuffer[i]=negative;
V
Ah, you're trying to trick me by posting the code in a different thread :)
https://forum.mql4.com/33806#351358
.... well not so fast... posbuffer gets set here...
V
Thanks Viffer
I was trying to simplify the code on this thread so someone could explain how this setindex buffer works.
This is the full code what I am trying to analyse
//+------------------------------------------------------------------+ //| RSI.mq4 | //| Copyright © 2004, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2004, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property indicator_separate_window #property indicator_minimum 0 #property indicator_maximum 100 #property indicator_buffers 1 #property indicator_color1 DodgerBlue //---- input parameters extern int RSIPeriod=14; //---- buffers double RSIBuffer[]; double PosBuffer[]; double NegBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { string short_name; //---- 2 additional buffers are used for counting. IndicatorBuffers(3); SetIndexBuffer(1,PosBuffer); SetIndexBuffer(2,NegBuffer); //---- indicator line SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,RSIBuffer); //---- name for DataWindow and indicator subwindow label short_name="RSI("+RSIPeriod+")"; IndicatorShortName(short_name); SetIndexLabel(0,short_name); //---- SetIndexDrawBegin(0,RSIPeriod); //---- return(0); } //+------------------------------------------------------------------+ //| Relative Strength Index | //+------------------------------------------------------------------+ int start() { int i,counted_bars=IndicatorCounted(); double rel,negative,positive; //---- if(Bars<=RSIPeriod) return(0); //---- initial zero if(counted_bars<1) for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0; //---- i=Bars-RSIPeriod-1; if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1; while(i>=0) { double sumn=0.0,sump=0.0; if(i==Bars-RSIPeriod-1) { int k=Bars-2; //---- initial accumulation while(k>=i) { rel=Close[k]-Close[k+1]; if(rel>0) sump+=rel; else sumn-=rel; k--; } positive=sump/RSIPeriod; negative=sumn/RSIPeriod; } else { //---- smoothed moving average rel=Close[i]-Close[i+1]; if(rel>0) sump=rel; else sumn=-rel; positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod; negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod; } PosBuffer[i]=positive; NegBuffer[i]=negative; if(negative==0.0) RSIBuffer[i]=0.0; else RSIBuffer[i]=100.0-100.0/(1+positive/negative); i--; } //---- return(0); } //+------------------------------------------------------------------+
This bit of the code below is what I am having trouble understanding because if the PosBuffer is adding up all the positives in the RSIPeriod the value its giving in the PosBuffer does not match my Manual calculation that I have done in a spreadsheet.
//---- smoothed moving average rel=Close[i]-Close[i+1]; if(rel>0) sump=rel; else sumn=-rel; positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod; negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
PosBuffer is holding the average of the positives not the total.
positive = the latest average multiplied by the number of elements-1 to give a total. The latest rel is then added to that and the whole lot is divided by the total number of elements to give a new average.
next bar, the whole thing is repeated with the new average that is now stored in posBuffer[i+1]
Does that make sense / is that what your spreadsheet is doing?
PosBuffer is holding the average of the positives not the total.
positive = the latest average multiplied by the number of elements-1 to give a total. The latest rel is then added to that and the whole lot is divided by the total number of elements to give a new average.
next bar, the whole thing is repeated with the new average that is now stored in posBuffer[i+1]
Does that make sense / is that what your spreadsheet is doing?
Thanks for that info I inderstand it now so PosBuffer does the average for that RSI period automatically
So because the PosBuffer had a setindexbuffer any value that goes in the PoBuffer will come out an average .
SetIndexBuffer(1,PosBuffer);
PosBuffer[i]=positive;
puts that value into the buffer in position i. When we move onto the next bar, all the indexes increase. The value we just put into position i is now +1 bar ago so when we want to reference it we need posBuffer[i+1].
This line
positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
is doing the average calculations. Because the actual individual elements are irrelivent it simply multiplies the last known average by the number of elements (RSIPeriod-1). This gets us to the same place mathimatically that adding each individual rel would and becasue we are only interested in the average, holding the individual values is pointless. We can now add the latest rel value and divide the sum by the total number of elements to calculate the new average. We store it in PosBuffer[i]. When the chart moves onto the next bar, that location is now i+1 and the procees starts again.
hth
v
A buffer is just an indicators way of saying array. It stores values in elements that can be referenced by index. The buffer index will start at zero and increases as you go back in bars. i+1 is just the element before the one we are currently looking at. The calculation of "positive" is the calculation of the average and
puts that value into the buffer in position i. When we move onto the next bar, all the indexes increase. The value we just put into position i is now +1 bar ago so when we want to reference it we need posBuffer[i+1].
This line
is doing the average calculations. Because the actual individual elements are irrelivent it simply multiplies the last known average by the number of elements (RSIPeriod-1). This gets us to the same place mathimatically that adding each individual rel would and becasue we are only interested in the average, holding the individual values is pointless. We can now add the latest rel value and divide the sum by the total number of elements to calculate the new average. We store it in PosBuffer[i]. When the chart moves onto the next bar, that location is now i+1 and the procees starts again.
hth
v
Thanks for that info I understand it a lot more now.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Just wonder if anyone can help me on this code
Does anyone know how and where PoshBuffer is getting its value from