Hi Seniors,
Can you rectify this AFL code, which I am using for exploration? But result was not coming as expected.
Please check below code and guide why result was incorrect.
//www.stockbangladesh.com
// Mehedi Arafat
SetChartBkGradientFill( ParamColor("BgTop", colorBlack),ParamColor("BgBottom", colorBlack));
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("Long R/S");
farback = Param("How Far back to go", 100, 50, 5000, 10);
nBars = Param("Number of bars", 12, 5, 40);
aHPivs = H - H;
aLPivs = L - L;
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;
aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);
aVisBars = Status("barvisible");
nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
curBar = (BarCount - 1);
curTrend = "";
if (aLLVBars[curBar] < aHHVBars[curBar])
{
curTrend = "D";
}
else
{
curTrend = "U";
}
for (i = 0; i < farback; i++)
{
curBar = (BarCount - 1) - i;
if (aLLVBars[curBar] < aHHVBars[curBar])
{
if (curTrend == "U")
{
curTrend = "D";
curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}
}
else
{
if (curTrend == "D")
{
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}
}
}
curBar = (BarCount - 1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx)
{
candIdx = curBar - aHHVBars[curBar];
candPrc = aHHV[curBar];
if (lastHPH < candPrc AND candIdx > lastLPIdx AND candIdx < curBar)
{
aHPivs[candIdx] = 1;
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
{
candIdx = curBar - aLLVBars[curBar];
candPrc = aLLV[curBar];
if (lastLPL > candPrc AND candIdx > lastHPIdx AND candIdx < curBar)
{
aLPivs[candIdx] = 1;
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++;
}
}
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]);
}
a1 = ahpivs == 1;
a2 = alpivs == 1;
Para = ParamToggle("Plot Parallel Lines","Off,On");
ColorS= ParamColor("Support",colorLime);
ColorR= ParamColor("Resistance",colorRed);
x = Cum(1);
s1 = L;
s11 = H;
pS = a2 == 1;
endt = LastValue(ValueWhen(ps,x,1));
startt = LastValue(ValueWhen(ps,x,2));
ends = LastValue(ValueWhen(ps,S1,1));
starts = LastValue(ValueWhen(ps,S1,2));
dtS = endt - startt;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS *(x - endt) + bS;
g3 = IIf(x > startt - 10, trendlineS, Null);
Plot(g3, "", colors, styleLine+styleDashed+styleNoRescale+styleNoLabel);
pR = a1 == 1;
endt1 = LastValue(ValueWhen(pr,x,1));
startt1 = LastValue(ValueWhen(pr,x,2));
endr = LastValue(ValueWhen(pr,S11,1));
startr = LastValue(ValueWhen(pr,S11,2));
dtR = endt1 - startt1;
aR = (endR - startR) / dtR;
bR = endR;
trendlineR = aR *(x - endt1) + bR;
g4 = IIf(x > startT1 - 10, trendlineR, Null);
Plot(g4, "", colorr, styleLine+styleDashed+styleNoRescale+styleNoLabel);
_SECTION_END();
MyBH = pS==1;
MySH = pR==1;
MyBH = ExRem(MyBH,MySH);
MySH = ExRem(MySH,MyBH);
MyBP = ValueWhen(MyBH,C);
MySP = ValueWhen(MySH,C);
MyBSL = ValueWhen(MyBH,L + (C-O));
MySSL = ValueWhen(MySH,H - (O-C));
MyBHD = ValueWhen(MyBH,MyBP-MyBSL);
MySHD = ValueWhen(MySH,MySSL-MySP);
xBT = ValueWhen(MyBH,DateTime());
xST = ValueWhen(MySH,DateTime());
Long=Flip(MyBH,MySH);
Shrt=Flip(MySH,MyBH);
MYxT= WriteIf(xBT>=xST, "LONG HERE", WriteIf(xBT<=xST, "SELL HERE","WAIT HERE"));
MYxP= NumToStr(IIf(xBT>=xST, MyBP, MySP),1.1,0);
Q = IIf(RoundLotSize==0,1000,RoundLotSize);
Filter = pS==1 OR pR==1;
SetOption( "NoDefaultColumns", True );
AddTextColumn( Name(), "Full Name",formatChar, colorDefault,colorDefault,105);
AddColumn( DateTime(), "Date / Time", formatDateTime,colorDefault,colorDefault,105);
AddTextColumn(MYxT,"Action",1.6,colorDefault,colorDefault,105);
AddColumn(MyBH,"LONG",1,colorDefault,colorDefault,105);
AddColumn(MySH,"SELL",1,colorDefault,colorDefault,105);
//AddtextColumn(MYxP,"Close",1.4,colorDefault,colorDefault,105);
_SECTION_END();