Sometimes correct sometimes incorrect results

I am trying to get a buy/sell signals on the chart on below conditions:
st = supertrend(10,3)

buysig= c>st
sellsig=c<st

Buy = Cross(H,Ref(H,BuySig))
Sell = Cross(Ref(L,SellSig),L)

however, sometimes I am getting correct and sometimes incorrect signals.
Please help in correcting the code.
Below is the entire code:

_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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("Try");

function SuperTrend(lenATR, width)

{
	nATR = ATR(lenATR);
	pAvg = (H+L) / 2;
	
	upperBand = pAvg + width * nATR;
	lowerBand = pAvg - width * nATR;
	isUpTrend = True;
	dn = DateNum();
	
	for (i=lenATR; i<BarCount; ++i)
	{
		if (C[i] > upperBand[i-1])
			isUpTrend[i] = True;
		else if (C[i] < lowerBand[i-1])
			isUpTrend[i] = False;
		else
			isUpTrend[i] = isUpTrend[i-1];
			
		if (isUpTrend[i])
			lowerBand[i] = Max(lowerBand[i], lowerBand[i-1]);
		else
			upperBand[i] = Min(upperBand[i], upperBand[i-1]);
	}
	
	super = IIf(isUpTrend, lowerBand, upperBand); 	
	return super;
}
 
st = SuperTrend(10,3);

Plot(st, "STOP", IIf(C>st,colorBrightGreen,colorRed),styleLine|styleStaircase|styleThick);

BuySig = C > st;
SellSig = C < st;

Buy = Cross(H,Ref(H,BuySig));
Sell = Cross(Ref(L,SellSig),L);

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

Short = Sell;
Cover = Buy;

Cover = ExRem(Buy, Sell);
Short = ExRem(Sell, Buy);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorBlueGrey, 0,L, offset=-80);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0,H, offset=-90);

PlotShapes(IIf(Cover, shapeUpArrow, shapeNone), colorGreen, 0,L, offset=-60);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone), colorOrange, 0,H, offset=-70);


_SECTION_END();


@Yogi Please explain precisely what you wanted to achieve with these two lines of code because I think they do not do what you expect.

Buy = Cross(H,Ref(H,BuySig))
Sell = Cross(Ref(L,SellSig),L)

(You are passing to Ref() as "period" an array instead of the more commonly used constant parameter).

1 Like

@beppe

Buy = Cross(H,Ref(H,BuySig)) // > This line of code is to generate a buy signal on the chart.
Strategy is to generate buy if prices crosses the high of crossing candle or (Signal candle)

BuySig = close price > supertrend(10,3) /// > this line of code gives crossing candle
Buy = Cross(H,Ref(H,BuySig))

vice versa for the sell.

@yogi, BuySig and SellSig are boolean arrays, meaning that they hold values of 0 (false) and 1 (true). Therefore, when you do this:

Buy = Cross(H,Ref(H,BuySig));
Sell = Cross(Ref(L,SellSig),L);

You are looking 1 bar into the future whenever BuySig or SellSig is true. That is almost certainly not what you want to do, as @beppe has already pointed out.

I think it's likely that what you really wanted to use is the ValueWhen() function. Perhaps something like this would be closer to what you want:

Buy = H > ValueWhen(BuySig,H);
Sell = L < ValueWhen(SellSig,L);
2 Likes

@mradtke, not giving desired results.
as i stated before some times correct sometimes incorrect on using below codes

Buy = Cross(H,Ref(H,BuySig));
Sell = Cross(Ref(L,SellSig),L);

on observing incorrect signals, it seems they are giving signal above/below the previous BuySig/SellSig candle and not on the recent one.

is there a way to refresh/re-initializing Bigsig/Sellsig.

@yogi I suggest you do an extra effort to understand what your code is doing truly:

Buy = Cross(H,Ref(H,BuySig));
Sell = Cross(Ref(L,SellSig),L);

@mradtke already explained it to you, but it seems that you have not yet realized why you get some random results.

Let's say it differently: these two lines of code are fundamentally WRONG (while they are syntactically correct, they will NOT produce anything useful in your formula).

You should use an exploration or the debugger to examine all the arrays used in your code, to see their values and then proceed to fix tour Buy/Sell lines.

I suppose that you have already seen references to this must-read document:

If its content is not very clear in your mind, please, take your time to review it since it is essential to figure out how to deal with the arrays in AmiBroker.

C, H, L, st, BuySig, SellSig are some of the arrays used in your formula, so it is critical to combine them correctly (with operators or using functions) to produce meaningful results.

And if you are still struggling, please, details your Buy/Sell using some pseudo-code and/or provide an accurate description, so that we can understand what you really wanted to get.

2 Likes

@mradtke I am trying to explain my strategy with the help of a chart, pls ref the uploaded chart.BuySell

@yogi, the goal of this forum is to help you learn to use AFL effectively, not to simply provide you with free coding services. I've given you a working SuperTrend function. I've pointed out your errors, and given you possible alternative code to try. @beppe has reinforced that message, told you flat out that your Buy and Sell logic is incorrect, and pointed you in the direction of some essential learning material. Your response is to post a pretty chart. If you want to learn AFL, then you're going to have to try harder. If you don't want to learn AFL, then I suggest you hire a consultant.

1 Like

@yogi, HINTS:
What do you mean by "Ref(H,BuySig)"?
What does AFL reference tell you about 'Ref('?

@KeithMcC
Ref( = High of the crossing candle ==BuySig.

Is that what the AFL reference is telling you about 'Ref"???

1 Like

@yogi NO Hint. I'll be more direct.
You are not using Ref correctly! In editor, place cursor on Ref, press F1, read how Ref really works, especially second parameter, 'period'.

1 Like

below line solved my problem

up = ValueWhen( Cross ( Close , st) , High);

``

thanks @mradtke @beppe @KeithMcC for your guidance..