Dear sr members,
Few days back i have taken following AFL from some threads (i dont know right now) it works fine but when i tried it for auto analysis it gives following error message..similarly it gives error message for back test as under-----
****************
// -- Have we identified a pivot? If trend is down...
if (aLLVBars[curBar]
-------------------^
Error 10.
Subscript out of range.
You must not access array elements outside 0..(BarCount-1) range.
**********
// -- Have we identified a pivot? If trend is down...
if (aLLVBars[curBar]
-------------------^
Error 10.
Subscript out of range.
You must not access array elements outside 0..(BarCount-1) range.
I am simply pasting said AFL seniors are requested to plz see and rectify the error...
********
_SECTION_BEGIN("BUY-SELL");
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorLavender ), styleNoTitle | ParamStyle( "Style", styleHistogram | styleOwnScale | styleThick | styleNoLabel, maskHistogram ), 2 );
_SECTION_END();
_SECTION_BEGIN("cord");
Cord = 0; // initialize
for( Counter = 52; Counter >= 26; Counter-- )
{
Cord = Max( Cord, LinearReg(Close,Counter) );
}
ProportionalATR = ATR(13)*2.5 / (Sum(C, 52) /13);
Factor = 2.618 * Cord * ProportionalATR;
LD = Cord - Factor;
LD = IIf(Cord < LD, Cord, LD);
// "The upper deviation (UD) is the Cord plus a factor of 2.618 times the 52 week proportional ATR
UD = Cord + Factor;
// Plots
GraphXSpace = 5;
Plot(Cord, "Cord", colorBlue);
Plot(LD, "LD", colorRed);
Plot(UD, "UD", colorGreen);
_SECTION_END();
_SECTION_BEGIN("pivots using arrows");
/* **********************************
Code to automatically identify pivots
********************************** */
// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",100,50,5000,10);
nBars = Param("Number of bars", 12, 5, 40);
// -- Title.
// -- Plot basic candle chart
PlotOHLC(Open, High, Low, Close,
"\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L
+ "\n"+"C ",
colorBlack, styleCandle);
GraphXSpace=7;
// -- Create 0-initialized arrays the size of barcount
aHPivs = H - H;
aLPivs = L - L;
// -- More for future use, not necessary for basic plotting
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;
// -- looking back from the current bar, how many bars
// back were the hhv and llv values of the previous
// n bars, etc.?
aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);
// -- Would like to set this up so pivots are calculated back from
// last visible bar to make it easy to "go back" and see the pivots
// this code would find. However, the first instance of
// _Trace output will show a value of 0
aVisBars = Status("barvisible");
nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
// -- Initialize value of curTrend
curBar = (BarCount-1);
curTrend = "";
if (aLLVBars[curBar] <
aHHVBars[curBar]) {
curTrend = "D";
}
else {
curTrend = "U";
}
// -- Loop through bars. Search for
// entirely array-based approach
// in future version
for (i=0; i<farback; i++) {
curBar = (BarCount - 1) - i;
// -- Have we identified a pivot? If trend is down...
if (aLLVBars[curBar] < aHHVBars[curBar]) {
// ... and had been up, this is a trend change
if (curTrend == "U") {
curTrend = "D";
// -- Capture pivot information
curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}
// -- or current trend is up
} else {
if (curTrend == "D") {
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}
// -- If curTrend is up...else...
}
// -- loop through bars
}
// -- Basic attempt to add a pivot this logic may have missed
// -- OK, now I want to look at last two pivots. If the most
// recent low pivot is after the last high, I could
// still have a high pivot that I didn't catch
// -- Start at last bar
curBar = (BarCount-1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx) {
// -- Bar and price info for candidate pivot
candIdx = curBar - aHHVBars[curBar];
candPrc = aHHV[curBar];
if (
lastHPH < candPrc AND
candIdx > lastLPIdx AND
candIdx < curBar) {
// -- OK, we'll add this as a pivot...
aHPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-
(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];
}
aHPivHighs[0] = candPrc ;
aHPivIdxs[0] = candIdx;
nHPivs++;
}
} else {
// -- Bar and price info for candidate pivot
candIdx = curBar - aLLVBars[curBar];
candPrc = aLLV[curBar];
if (
lastLPL > candPrc AND
candIdx > lastHPIdx AND
candIdx < curBar) {
// -- OK, we'll add this as a pivot...
aLPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = candPrc;
aLPivIdxs[0] = candIdx;
nLPivs++;
}
}
// -- Dump inventory of high pivots for debugging
/*
for (k=0; k<nHPivs; k++) {
_TRACE("High pivot no. " + k
+ " at barindex: " + aHPivIdxs[k] + ", "
+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],
DateTime(), 1), formatDateTime)
+ ", " + aHPivHighs[k]);
}
*/
// -- OK, let's plot the pivots using arrows
PlotShapes(
IIf(aHPivs==1, shapeDownArrow, shapeNone), colorRed, 0,
High, Offset=-8);
PlotShapes(
IIf(aLPivs==1, shapeUpArrow , shapeNone), colorGreen, 0,
Low, Offset=-8);
_SECTION_END();
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
THIS SECTION DRAWS TD TREND LINES */
_SECTION_BEGIN("Trend Line");
percent = 0.01 * 1; /* Adjust this percent as necessary, I use .01 because FOREX is a 0.0000 number */
firstpointL = 2;
firstpointH = 2;
y0=LastValue(Trough(L,percent,firstpointL));
y1=LastValue(Trough(Ref(L,-1),percent,1));
for( i = 1; i < BarCount AND y0 >= y1; i++ )
{
firstpointL++;
y0=LastValue(Trough(L,percent,firstpointL));
}
x0=BarCount - 1 - LastValue(TroughBars(L,percent,firstpointL));
x1=BarCount - 1 - LastValue(TroughBars(Ref(L,-1),percent,1));
LineL = LineArray( x0, y0, x1, y1, 1 );
/*
Plot(C, "C", colorBlack, styleCandle);
*/
Plot( LineL, " Support Trend line", colorBrown,4 +8 );
yt0=LastValue(Peak(H,percent,firstpointH));
yt1=LastValue(Peak(Ref(H,-1),percent,1));
for(i = 1; i < BarCount AND yt0 <= yt1; i++ )
{
firstpointH++;
yt0=LastValue(Peak(H,percent,firstpointH));
}
xt0=BarCount - 1 - LastValue(PeakBars(H,percent,firstpointH));
xt1=BarCount - 1 - LastValue(PeakBars(Ref(H,-1),percent,1));
LineH = LineArray( xt0, yt0, xt1, yt1, 1 );
Plot( LineH, "Resistance Trend line", colorBrown,4 + 8 );
_SECTION_END();
/*-----------------------------------------------------------------------*/
_SECTION_BEGIN("Giving price");
GraphXSpace = 20;
dist = 1.1*ATR(20);
T1 = 0; T2 = 0; B1 = 0; B2 = 0;
for(m=0;m<=BarCount-1;m++){
if(aHPivs[m] == 1){
PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, H[m]+dist[m],colorRed);
if (T1 != 0) T2 = T1;
T1 = H[m];
}
if(aLPivs[m] == 1){
PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, L[m]-dist[m], colorGreen);
if(B1!=0) B2 = B1;
B1 = L[m];
}
}
Buy = C > T1 AND T1 > T2 AND B1 > B2 ;
Sell = C < B1 ;
_SECTION_END();
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
_SECTION_END();