Seniors, need your help to rectify this AFL exploration result

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();

expected

MYxT= WriteIf(xBT>=xST, "LONG HERE", WriteIf(xBT<=xST, "SELL HERE","WAIT HERE"));
// And this
MYxP= NumToStr(IIf(xBT>=xST, MyBP, MySP),1.1,0);

Both those lines should mean the same thing. Look at them closer.

WriteIf(xBT>=xST…. in a comparison, there can only be three states, Two of which are already stated in this line.
Adding another WriteIf technically is not required, but even if you add one as 2nd Arg, then "WAIT HERE" will never be triggered so your structure is incorrect.

The fix would be to use the same code as in MYxP and it should work fine because the Buy/Sell under the numerical Columns is matching your required output.

Sir,
Thank you for prompt reply.
I try same but the exploration result was not as per my expectation.

If possible the necessary changes in my code and post me. it will be great help for me.

Thank you, once again.

MYxT = WriteIf ( xBT >= xST, "LONG HERE", "SELL HERE" );

Why will this not work ?

@RUPEEXN, please review this KB article and see if it applies to your case.

The easiest solution is to use the AddMultiTextColumn() function that is available from AmiBroker version 6.20.

1 Like

But the context needs to be checked, if you're not passing an Array, you're probably fetching the last value (or a single value) for each row.
You can try and move the WriteIf condition to the AddTextColumn(), will run the code at my end.

:+1: @beppe that's what I thought, never crossed that KB, makes sense but that's what I was thinking too.

In one of my codes it's working, but the case is fine I think where there is one latest bar which I usually use.
Historical bars will obviously pose a problem with AddText, MultiText is the way to go.

or something like this

AddColumn( IIf( xBT >= xST, 'B', 'S' ), "Signal", formatChar );

and remove the MYxT variable altogether.

1 Like

Thank you, for your guidance.. let me check and confirm you.

Sir,

Thank you for reference..
Just checked your code, i very sure that this will help me .

Thank you once again.

Did u get the solution?

can you share here, as I am interested in this---please.