Signals "blink"

I plotted out both “short” and the conditions under which the short signal would be triggered. However when the condition’s true, the short signal stays false. As I moved the bar back and forth on the chart, the plots changed. And I only used reference price of O,H,L & C. Can anyone tell me what types of errors in my code might cause this problem?

I also used the exploration interface to dispaly the condition and signal columns. It shows that while the condition’s true, the signal is false.

for (i=0;i<BarCount;i++)
{
	if  (i==0)
	{
		Highafterentry[i]=0;
		Lowafterentry[i]=0;
		Longin[i]=0;
		Shortin[i]=0;
		Buy[i]=0;
		Short[i]=0;
		Cover[i]=0;
		Sell[i]=0;
	}

	else
	{
		if (Buysig[i])// AND !Longin[i-1] AND !Shortin[i-1])
		{
			Buy[i]=1;
			BuyPrice[i]=O[i];
			priceatbuy[i]=BuyPrice[i];
			Highafterentry[i]=H[i];
			Lowafterentry[i]=L[i];
			Longin[i]=1;
			
		}
		
	
		else if (Shortsig[i])// AND  !Longin[i-1] AND !Shortin[i-1])
		{
			Short[i]=1;
 			ShortPrice[i]=O[i];
			priceatshort[i]=ShortPrice[i];
			Highafterentry[i]=H[i];
			Lowafterentry[i]=L[i];
			Shortin[i]=1;
		}
		
		else if (Sellsig[i] AND Longin[i-1])
			{
				Sell[i]=1;
				SellPrice[i]=O[i];
				Longin[i]=0;
			}

		else if (Coversig[i] AND Shortin[i-1])
			{
				Cover[i]=1;
				CoverPrice[i]=O[i];
				Shortin[i]=0;
			}

		else if (Longin[i-1])
		{
			priceatbuy[i]=priceatbuy[i-1];
			Highafterentry[i]=Max(H[i],Highafterentry[i-1]);
			Lowafterentry[i]=Min(L[i],Lowafterentry[i-1]);

			longstop3[i]=(Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro3) AND Low[i]<= Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro3);
			longstop2[i]=(Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro2) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro2);
			longstop1[i]=(Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro1) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro1);
			Longstoploss[i]=(Low[i]<=priceatbuy[i]*(1-StopLoss*0.01));


			if (Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro3) AND Low[i]<= Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro3)
			{
				Sell[i]=1;
				SellPrice[i]=Min(Open[i],Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro3);
				Longin[i]=0;

			}

			else if (Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro2) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro2)
			{
				Sell[i]=1;
				SellPrice[i]=Min(Open[i],Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro2);
				Longin[i]=0;
			}

			else if (Highafterentry[i-1]>=priceatbuy[i]*(1+0.01*StartPro1) AND Low[i]<=Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro1)
			{
				Sell[i]=1;
				SellPrice[i]=Min(Open[i],Highafterentry[i-1]-(Highafterentry[i-1]-priceatbuy[i])*0.01*StopPro1);
				Longin[i]=0;
			}
		
			else if (Low[i]<=priceatbuy[i]*(1-StopLoss*0.01))
			{
				Sell[i]=1;
				SellPrice[i]=Min(Open[i],priceatbuy[i]*(1-StopLoss*0.01));
				Longin[i]=0;
			}

			else
			{
				Sell[i]=0;
				Longin[i]=1;
			}

		}

		else if (Shortin[i-1])
		{
			priceatshort[i]=priceatshort[i-1];
			Highafterentry[i]=Max(H[i],Highafterentry[i-1]);
			Lowafterentry[i]=Min(L[i],Lowafterentry[i-1]);

			Shortstop3[i]=( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro3) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro3);
			Shortstop2[i]=( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro2) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro2);
			Shortstop1[i]=( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro1) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro1);
			Shortstoploss[i]=(High[i]>=priceatshort[i]*(1+StopLoss*0.01));

			



			if ( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro3) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro3)
			{
				Cover[i]=1;
				CoverPrice[i]=Max(Open[i],Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro3);
				Shortin[i]=0;
			}
		
			else if ( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro2) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro2)
			{
				Cover[i]=1;
				CoverPrice[i]=Max(Open[i],Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro2);
				Shortin[i]=0;
			}

			else if ( Lowafterentry[i-1]<=priceatshort[i]*(1-0.01*StartPro1) AND High[i]>=Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro1)
			{
				Cover[i]=1;
				CoverPrice[i]=Max(Open[i],Lowafterentry[i-1]+(priceatshort[i]-Lowafterentry[i-1])*0.01*StopPro1);
				Shortin[i]=0;
			}

			else if (High[i]>=priceatshort[i]*(1+StopLoss*0.01))
			{
				Cover[i]=1;
				CoverPrice[i]=Max(Open[i],priceatshort[i]*(1+StopLoss*0.01));
				Shortin[i]=0;
			}
	

			else
			{
				Cover[i]=0;
				Shortin[i]=1;
			}
		}

		else
		{
			Shortin[i]=0;
			Longin[i]=0;
		}

	}

}


Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
SetPositionSize(10,spsShares);

//Plot(Cover,"cover",colorRed);
//Plot(Buysig,"buysig",colorRed);
//Plot(Buy,"buy",colorWhite);
Plot(Shortsig,"shortsig",colorYellow);
Plot(Short,"short",colorBlue);

Filter=1;

AddColumn(barssofartoday,"barsofartoday");
AddColumn(Buysig,"buysig");
AddColumn(Shortsig,"shortsig");
AddColumn(Sellsig,"sellsig");
AddColumn(Coversig,"coversig");
AddColumn(Buy,"buy");
AddColumn(Short,"short");
AddColumn(Sell,"sell");
AddColumn(Cover,"cover");
AddColumn(Longin,"longin");
AddColumn(Shortin,"shortin");

Your code is incorrect (incomplete). You are not initializing arrays to zero. You are just initializing first element and that is wrong. To initialize you should move first part of your code outside the loop and remove all those subscripts. Also all those ExRems are wrong (should be removed) and you are using loops but don’t tell AB how many bars you need (QuickAFL)

See this: How do I debug my formula? and see this http://www.amibroker.com/kb/2008/07/03/quickafl/

1 Like

@Tomasz Thanks a lot!

AB is a powerful tool. Thanks for your effort.

Hi

When i run analysis windows with some specific range of dates,my charts with signals plotted also get impacted by the analysis windows range of dates. Basically the signals on the chart disappear for the dates outside range of analysis window.I want to run analysis in window independent of my charts getting impacted.

Moderator: You should SEARCH BEFORE posting duplicates

@Tomasz Thanks

I appreciate you for pointing me to a appropriate post, but i don’t use quick AFL in my code. And i have setbarsrequired condition to sbrall. Still the plotting on chart appears to be linked with range of dates i run my analysis window. I want my analysis window range to be independent of plotting signals on the chart.
All i do in my script is generate buy and sell signals and plot them through arrows . So i have a chart which has this afl script plotted and simultaneously i also sometimes run an analysis window. So whatever the date range i set in analysis window, signals on the chart appear only for the that range. Signals on chart should be independent of what i do in analysis window.

I understand the problem with my plotting was due to equity(1) default range parameter being -1. So i now i have set it 0 and everything works fine. Sorry to bother you.

I am having a similar problem in that my signal arrows and trailing stop line seem to randomly appear and disappear. I have no idea why, even after reading the thread.

Here is an image with the signals and trailing stop (in white).

Signals

If I just click to the left three times, then all the signals and trailing stop disappear.

No Signals

Below is the code.

// System Parameters

SetOption("InitialEquity",1000000);
SetOption("AllowSameBarExit",False);
SetOption("FuturesMode",True);
SetTradeDelays(1,1,1,1);
PositionSize=MarginDeposit=1;
BuyPrice = Open;
SellPrice = Open;
ShortPrice = Open;
CoverPrice = Open;

SetChartOptions(0,chartShowArrows|chartShowDates);

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%0.1%%) {{VALUES}}",O,H,L,C,SelectedValue(ROC(C,1))));

// Variables
HighestHi = Ref(HHV(High,80),-1);
LowestLo = Ref(LLV(Low,80),-1);

BuyOpenStop = Open - 3*ATR(20);
BuyHighStop = High - (ATR(20) + 5.5*StDev(ATR(1),20,True));

ShortOpenStop = Open + 3*ATR(20);
ShortLowStop = Low + (ATR(20) + 5.5*StDev(ATR(1),20,True));

// Position Size

// Buy-Sell Rules
Buy = Cross(High,HighestHi);
Sell = 0;
BuyTrailArray = Null;
BuyTrailStop = 0;

for(i=1; i<BarCount; i++)
{
	if(BuyTrailStop == 0 AND Buy[i])
	{
		BuyTrailStop = Max(BuyOpenStop[i],BuyHighStop[i]);
	}
	else Buy[i] = 0; // remove excess buy signals
		
	if(BuyTrailStop>0 AND Low[i]<BuyTrailStop)
	{
		Sell[i]=1;
		BuyTrailStop=0;
	}
	
	if(BuyTrailStop>0)
	{
		BuyTrailStop = Max(Max(BuyOpenStop[i],BuyHighStop[i]),BuyTrailStop);
		BuyTrailArray[i]=BuyTrailStop;
	}
}

// Short-Cover Rules
Short = Cross(LowestLo,Low);
Cover = 0;
ShortTrailArray = Null;
ShortTrailStop = 0;

for(i=1; i<BarCount; i++)
{
	if(ShortTrailStop == 0 AND Short[i])
	{
		ShortTrailStop = Min(ShortOpenStop[i],ShortLowStop[i]);
	}
	else Short[i] = 0; // remove excess short signals
		
	if(ShortTrailStop>0 AND High[i]>ShortTrailStop)
	{
		Cover[i]=1;
		ShortTrailStop=0;
	}
	
	if(ShortTrailStop>0)
	{
		ShortTrailStop = Min(Min(ShortOpenStop[i],ShortLowStop[i]),ShortTrailStop);
		ShortTrailArray[i]=ShortTrailStop;
	}
}

//Plot chart, indicators, stops, arrows
Plot(Close,"Price",colorDefault,styleCandle);

Plot(HighestHi,"Buy",colorLime,styleStaircase|styleThick|styleDashed);
Plot(LowestLo,"Short",colorLime,styleStaircase|styleThick|styleDashed);

Plot(BuyTrailArray,"Buy Trailing Stop",colorWhite,styleStaircase|styleThick);
Plot(ShortTrailArray,"Short Trailing Stop",colorWhite,styleStaircase|styleThick);

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorWhite,0,H,offset=15);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorWhite,0,L,offset=15);
PlotShapes(IIf(Short,shapeDownArrow,shapeNone),colorWhite,0,L,offset=15);
PlotShapes(IIf(Cover,shapeUpArrow,shapeNone),colorWhite,0,H,offset=15);

Must-read for you: http://www.amibroker.com/kb/2008/07/03/quickafl/
(the link was already provided as SOLUTION, so you would do yourself a favor if you read solution before re-asking the same question again)

2 Likes