s01 wrote:Question: I'm a bit confused now, the values that are displayed on the lines on screen mean nothing then, how do you decide how to find the values if there are more than 3 or 6? Is it a progressive thing were the first two values since x is always -1 would be 1 & -1 the next would be 2 or -2?
Steve
In this particular indicator, there are 5 buffers being used. Three are being handled "naturally" and 2 are done with normal arrays that are modified to look like series arrays via the ArraySetAsSeries() function.
I'll try to walk through the buffer logic for this indicator. The properties section defines 2 buffers via the statement
This tells Empty4 that we will be drawing two "lines" on the chart.
Later, in the init() function, we make the following calls:
Code: Select all
IndicatorBuffers(3); // orig 3
SetIndexBuffer(0, Uptrend);
// ArraySetAsSeries(Uptrend, true); // orig was //'d out
SetIndexBuffer(1, Dntrend);
// ArraySetAsSeries(Dntrend, true); // orig was //'d out
SetIndexBuffer(2, ExtMapBuffer);
ArraySetAsSeries(ExtMapBuffer, true);
The call to IndicatorBuffers() tells Empty4 to actually reserve memory to track three buffers. Since we are only able to display 2 buffers on the chart (because of our setting indicator_buffers to 2), the third buffer will be hidden (it won't even appear in the data window) but Empty4 will deal with resizing it as new bars are formed.
The two buffers Uptrend[] and Dntrend[] are really only displaying a single line. We use two buffers because we want to display them in the appropriate color depending on whether it is in an up trend or a down trend. Metrader isn't smart enough to allow us to switch colors on the fly.
In start(), we have the following code to create two additional buffers for our calculations:
Code: Select all
double vect[], trend[];
if(e > Bars)
e = Bars;
ArrayResize(vect, e);
ArraySetAsSeries(vect, true);
ArrayResize(trend, e);
ArraySetAsSeries(trend, true);
When using our own arrays (not those specified as indicator buffers via the SetIndexBuffer() function) we have to deal with sizing them correctly and usually calling the ArraySetAsSeries() function. The ASAS() function reverses the order we access the array so that it is logically aligned with the way indicator buffers are accessed (the last element in the array has index 0).
Next, we have three loops. The first prefills our vect[] array:
Code: Select all
for(x = 0; x < e; x++)
{
vect[x] = 2*WMA(x, period/2) - WMA(x, period);
// Print("Bar date/time: ", TimeToStr(Time[x]), " close: ", Close[x], " vect[", x, "] = ", vect[x], // orig was //'d out
// " 2*WMA(p/2) = ", 2*WMA(x, period/2), " WMA(p) = ", WMA(x, period)); // orig was //'d out
}
The next loops fills our third "natural" buffer (the invisible one) with a smoothed version of vect[]
Code: Select all
for(x = 0; x < e-period; x++)
ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, method, x);
And finally, in our third loop we calculate the trend (is our smoothed value in ExtManBuffer[] rising or falling) and set the appropriate visible indicator buffer value.
Code: Select all
for(x = e-period; x >= 0; x--)
{
trend[x] = trend[x+1];
if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;
if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;
if (trend[x]>0)
{ Uptrend[x] = ExtMapBuffer[x];
if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];
Dntrend[x] = EMPTY_VALUE;
}
else
if (trend[x]<0)
{
Dntrend[x] = ExtMapBuffer[x];
if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];
Uptrend[x] = EMPTY_VALUE;
}
I'm not sure why the original author didn't use "natural" buffers throughout the program. Since we are allowed to have 8, the two additional ones shouldn't have been an issue.
George