Conditions met, but no signals

Hi Ami community,

I found that sometimes when a certain buy/short/sell/cover condition is met, there’s no actual signal or any trade. I plotted out “buy/short” on the chart. As I move the bar back and forth on the chart, the plots would change. Can anyone tell me why this is happening ? In case I didn’t describe the situation clear enough, I recorded a short gif of what’s actually happening.

I’d appreciate any hint from you. Thanks a lot.

Joanna


function Avgtr(Length)
{
	Truehigh=IIf(H>Ref(C,-1),H,Ref(C,-1));
	Truelow=IIf(L<Ref(C,-1),L,Ref(C,-1));
	Truerange=Truehigh-Truelow;
	myatr=MA(Truerange,Length);
	return myatr;
}



NMA=100;
N=20;    
StartPro=5;
StopPro=8; 
StopLoss=3;


ATR1=Avgtr(14);

MAO=MA(O,NMA);
MAC=MA(C,NMA);
HHN=HHV(H,N);
LLN=LLV(L,N);


//Plot(MAO,"MAO",colorRed);
//Plot(MAC,"MAC",colorWhite);
//Plot(HHN,"HHN",colorBlue);
//Plot(LLN,"LLN",colorYellow);

Buy=0;
Sell=0;
Short=0;
Cover=0;



for (i=1; i<BarCount; i++)
{
	if (i==1)
	{
		ValidTrade[i]=0;
		longin[i]=0;
		shortin[i]=0;
		highafterentry[i]=0;
		lowafterentry[i]=0;
		priceatbuy[i]=0;
		priceatshort[i]=0;
	}

	else
	{
		if (ValidTrade[i-1]==0)
		{
			if(MAO[i-1]<MAC[i-1] AND H[i-1]>MAC[i-1]) 
			{
				ValidTrade[i]=1;
			}

			else if  (MAO[i-1]>MAC[i-1] AND L[i-1]<MAC[i-1])
	   		{
				ValidTrade[i]=-1;
			}
			
			else
			{
				ValidTrade[i]=0;
			}
		}

		else if(ValidTrade[i-1]==1)
		{
			if(MAO[i-1]>MAC[i-1] AND L[i-1]<MAC[i-1]) 
			{
				ValidTrade[i]=0;   
			}
			
			else
			{
				validtrade[i]=1;
			}
		}
	
		else if(ValidTrade[i-1] == -1)
		{
			if(MAO[i-1]<MAC[i-1]  AND H[i-1]>MAC[i-1] )
			{
				ValidTrade[i]=0;		     
			}
			
			else
			{
				validtrade[i]=-1;
			}
		}
	
	
		if (validtrade[i-1]==1 AND H[i-1]>=HHN[i-1] AND longin[i-1]!=1)
		{
			Buy[i]=1;
			BuyPrice[i]=O[i];
			highafterentry[i]=H[i];
			lowafterentry[i]=L[i];
			priceatbuy[i]=BuyPrice[i];
			longin[i]=1;
			shortin[i]=0;
		}
		
		else if (validtrade[i-1]==-1 AND L[i-1]<=LLN[i-1] AND shortin[i-1]!=1)
		{
			Short[i]=1;
			ShortPrice[i]=O[i];
			highafterentry[i]=H[i];
			lowafterentry[i]=L[i];
			priceatshort[i]=ShortPrice[i];
			shortin[i]=1;
			longin[i]=0;
		}
		
		else if (longin[i-1]==1)
		{
			priceatbuy[i]=priceatbuy[i-1];
			highafterentry[i]=Max(highafterentry[i-1],H[i]);
			lowafterentry[i]=Min(lowafterentry[i-1],L[i-1]);
			
			if (highafterentry[i-1]>=priceatbuy[i]+startpro*ATR1[i-1] AND L[i-1] <= highafterentry[i-1] - StopPro*ATR1[i-1])
			{
				Sell[i]=1;
				SellPrice[i]=O[i];
				Short[i]=1;
				ShortPrice[i]=O[i];
				longin[i]=0;
				shortin[i]=1;
				highafterentry[i]=H[i];
				lowafterentry[i]=L[i];
				priceatshort[i]=ShortPrice[i];
			}
			
			else if (L[i]<=priceatbuy[i]*(1-StopLoss*0.01))
			{
				Sell[i]=1;
				SellPrice[i]=Min(priceatbuy[i]*(1-StopLoss*0.01),O[i]);
				longin[i]=0;
			}
			
			else
			{
				longin[i]=longin[i-1];
				shortin[i]=shortin[i-1];
			}
		}
		
		else if (shortin[i-1]==1)
		{
			priceatshort[i]=priceatshort[i-1];
			highafterentry[i]=Max(highafterentry[i-1],H[i]);
			lowafterentry[i]=Min(lowafterentry[i-1],L[i-1]);
			
			if(lowafterentry[i-1]<= priceatshort[i] - StartPro*ATR1[i-1] AND H[i-1]>= Lowafterentry[i-1] + StopPro*ATR1[i-1])
			{
				_TRACE("short to buy triggered at bar "+i);
				Cover[i]=1;
				CoverPrice[i]=O[i];
				shortin[i]=0;
				Buy[i]=1;
				BuyPrice[i]=O[i];
				priceatbuy[i]=BuyPrice[i];
				longin[i]=1;
				highafterentry[i]=H[i];
				lowafterentry[i]=L[i];
			}
			
			else if (H[i]>=priceatshort[i]*(1+StopLoss*0.01))
			{
				Cover[i]=1;
				CoverPrice[i]=Max(priceatshort[i]*(1+StopLoss*0.01),O[i]);
				shortin[i]=0;
				
			}
			
			else
			{
				
				longin[i]=longin[i-1];
				shortin[i]=shortin[i-1];
			}
		}
		
		else
		{
			longin[i]=0;
			shortin[i]=0;
		}
	
	}
}


//Plot(ValidTrade,"ValidTrade",colorRed);
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
SetPositionSize(10,spsShares);

//Plot(Buy,"buy",colorWhite);
//Plot(Short,"short",colorRed);


PlotShapes(Buy*shapeUpArrow,colorWhite);
PlotShapes(Sell*shapeHollowDownArrow,colorWhite);
PlotShapes(Short*shapeDownArrow,colorRed);
PlotShapes(Cover*shapeHollowUpArrow,colorRed);

Filter=1;
AddColumn(BarIndex(),"BarIndex");
//AddColumn(MAO,"MAO");
//AddColumn(MAC,"MAC");
//AddColumn(HHN,"HHN");
//AddColumn(LLN,"LLN");
//AddColumn(validtrade,"validtrade");
AddColumn(longin,"longin");
AddColumn(shortin,"shortin");
AddColumn((Ref(validtrade,-1)==1 AND Ref(H,-1)>=Ref(HHN,-1) AND Ref(longin,-1)!=1),"Buy open");
AddColumn((Ref(shortin,-1) AND Ref(lowafterentry,-1)<= priceatshort - StartPro*Ref(ATR1,-1) AND Ref(H,-1)>= Ref(Lowafterentry,-1) + StopPro*Ref(ATR1,-1)),"short reverse to Buy");

AddColumn((Ref(validtrade,-1)==-1 AND Ref(L,-1)<=Ref(LLN,-1) AND Ref(shortin,-1)!=1),"short open");
AddColumn(Ref(longin,-1) AND Ref(highafterentry,-1)>=priceatbuy+startpro*Ref(ATR1,-1) AND Ref(L,-1) <= Ref(highafterentry,-1) - StopPro*Ref(ATR1,-1),"buy reverse to short");

AddColumn(Buy,"Buy");
AddColumn(Short,"Short");
AddColumn(highafterentry,"highafterentry");
AddColumn(lowafterentry,"lowafterentry");
AddColumn(priceatbuy,"priceatbuy");
AddColumn(priceatshort,"priceatshort");
AddColumn(ATR1,"ATR");


Hi @Joanna, maybe let us take the ‘baby steps’ approach - could you describe what is your trading system in simple words, like

  1. Entry - this
  2. Exit - this and that

As oppose to putting that loop - which (I fell) can be a bit simplified to array type conditions. From there one can look why certain signals are not working as you expected.

1 Like

thanks for your kind suggestions. I’ll edit the post and make it more clear.

It seems that the original post cannot be edited now…

The idea behind is a “band-breakout” strategy.

First I calculated the average open and close prices of the last ,say, 100 bars(here donoted as MAO & MAC), as well as the highest high(HHN) and lowest low(LLN) in the last 20 bars. When the last bar’s high is greater than last bar’s MAC, and last bar’s MAC is greater than last bar’s MAO, we are allowed to take a long position, donoted as validtrade=1. When the last bar’s low is smaller than last bar’s MAC, and last bar’s MAC is smaller than last bar’s MAO, we are allowed to take a short position, donoted as validtrade=-1. Otherwise validtrade=0.

Then the breakout rules- When validtrade==1, which means we could take a long position, and when last bar’s high is greater than last bar’s HHN(break up though the HHN line), we take a long position; validtrade==-1, and low break down through LLN, we take a short position. When holding a long position, if our profit of the trade reaches a certain amout and then drop down to a certain amount we set, then close the long position and take a short position at the same time; and vice versa. And we also set a stoploss level at which we exit trades when single trade loss reaches.

The signal failed to show up as I referred earlier is the one when holing a short position and experiencing an up breaking through.

Actually I recorded a short gif. But I failed like dozens of times to upload it. I’ll change the network and try again later on.

Thanks again!

1 Like

This is by no means a fully developed solution but it shows the concept - bottom line ‘no unnecessary loops’.

// arrays 
// "(...) First I calculated the average open and close prices of the last ,say, 100 bars(here donoted as MAO & MAC), as well as the highest high(HHN) and lowest low(LLN) in the last 20 bars. "

period_one = 20;

avg_open    = ma(o, period_one);
avg_close   = ma(c, period_one);
avg_high    = ma(h, period_one);
avg_low     = ma(l, period_one);

// conditions
// "(...) When the last bar’s high is greater than last bar’s MAC" 
condition_one_long = REF(H > avg_close,-1);

// "and last bar’s MAC is greater than last bar’s MAO" validtrade=1
condition_two_long = REF(avg_open < avg_close,-1);

// validtrade=1
validate_long = condition_one_long and condition_two_long;

// When the last bar’s low is smaller than last bar’s MAC
condition_one_short = ref(l < avg_close,-1 );

// and when last bar’s high is greater than last bar’s HHN(break up though the HHN line)
condition_two_short = ref( h > avg_high, -1 );

// we are allowed to take a short position, donoted as validtrade=-1
validate_short = condition_one_short and condition_two_short;

// buy  =       = validate_long;
// sell =       = validate_short;
// short        = cover = 0;

As for the rest:

Then the breakout rules- When validtrade==1, which means we could take a long position, and when last bar’s high is greater than last bar’s HHN(break up though the HHN line), we take a long position; validtrade==-1, and low break down through LLN, we take a short position. When holding a long position, if our profit of the trade reaches a certain amout and then drop down to a certain amount we set, then close the long position and take a short position at the same time; and vice versa. And we also set a stoploss level at which we exit trades when single trade loss reaches.

I recommend the following reading:

https://www.amibroker.com/guide/afl/applystop.html
https://www.amibroker.com/guide/afl/flip.html

3 Likes

Thanks so much for the “bottom line” , the simplified and elegant code, and the suggested reading materials. Feel grateful to know good programming habits and see good examples as a newbie.

You’ve been very helpful! Thanks !

1 Like