Too little raw entry signals generated

Hi there, I've just started off with coding so everything is new to me. I am playing around with a simple BO strategy based on an EMA lineup and 30 day high.
The code is below

// Backtest Setup
SetOption( "InitialEquity", 250000);								
SetOption("MaxOpenPositions", 1000);								
SetOption("AccountMargin", 100);									
SetOption("CommissionAmount", 0.5);        							
BuyPrice 	= O;                        							
SellPrice	= O;                        							
Short = Cover = 0;

// Trading Rules 
MA5		        = EMA(C,5);										   
MA15			= EMA(C,15);	
MA30			= EMA(C,30);	
MA60		    = EMA(C,60);
BuyRule1		= C==HHV(H,30); 									
MLU1			= C > MA5;
MLU2			= MA5 > MA15;
MLU3			= MA15 > MA30;
MLU4			= MA30 > MA60;
Green			= C>O;
SellRule1		= 0;
shareprice		= C>0.02;

Buy 			= MALineup AND BuyRule1 AND Green AND shareprice;			
Sell 			= 0;										
amount = 25; // 25% loss (trailing)
ApplyStop( stopTypeTrailing, stopModePercent, amount, True );

RiskPerShare = O - (O*0.75);									

PercentRiskPerTrade = 2;
PctSize = PercentRiskPerTrade * BuyPrice / RiskPerSharetype 

After running the backtest on my chosen samples I wanted to investigate whether the system was recognizing the entry signal. I was basically after the raw signals. I have noticed that the entry signal was absent in quite a few instances. Below is a screen capture where a signal (green arrow next to purple vertical line) is present on the 26/08/16. However, I was expecting for it to be generated already on the bar circled in red. I can't figure out why the signal was generated on the 26/08/19 and not earlier as the condition had been met unless which is probably the reason my code is wrong.


Great to see your efforts !

So, Buy when MALineup AND BuyRule1 AND Green AND shareprice. Let's investigate each one of them.

Drawing clues from the image shared by you, does this mean you want to capture the moment when Close crosses above MA5 while MLU2 & MLU3 & MLU4. BTW Cross() and comparision operator (>) are not same. Otherwise, if your logic is to capture the bars when Close is greater than MA5 while MLU2 & MLU3 & MLU4, then you can ignore this interpretation.

Same doubt like previous interpretation! :unamused:

This will be rare because BuyRule1 will be True only when Close is equal to HHV(H,30). Is that what you really mean?

Fine !

This will be always True !

May I suggest you to first write an Algorithm in plain English before starting to code - you'll notice the difference. And also please refer to all that is mentioned in this post:

1 Like

Thank you for you detailed answer and explanation. I'll go back to the drawing board and heed your advise. I'll post an update later

Thank you for your help. I have made the relevant adjustments and it works fine.