Why does the scan show only the morning signals?

The below code's purpose is to:

  1. Calculate and plot Heikin Ashi Price
  2. And on the (heikin ashi) chart, look for the following conditions in the hourly chart:
    image

The code works fine when applied like an indicator (i.e. on a chart) but as a scanner, it only returns the morning signals.
Here's the chart (indicator works normally):
The white circle is for SELL, yellow is for BUY.
image

And here's the scanner:
image

Clearly, there are much more trades in chart than in the analysis window. Why is this happening?

_SECTION_BEGIN("Price Heikin Ashi");
SetChartOptions(0,chartShowArrows | chartShowDates);

TimeFrameSet(inHourly);
 
HaClose = (O + H + L + C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
xDiff = (HaHigh - Halow) * 10000;
barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", barcolor, styleCandle );



// 3 GREEN CANDLES AFTER 1 RED
Gtrig0 = IIf(Ref(HaClose,-2)<=Ref(HaOpen,-2),True,False); // RED CANDLE BEFORE GREEN
Gtrig1 = IIf(HaClose>=HaOpen,True,False); // 2ND GREEN CANDLE
Gtrig2 = IIf(Ref(HaClose,-1)>=Ref(HaOpen,-1),True,False); // 1ST GREEN CANDLE
//Gtrig3 = IIf(Ref(HaClose,-3)>=Ref(HaOpen,-3),True,False);
Gtrig4 = IIf(HaClose>Ref(HaClose,-1) AND Ref(HaClose,-1)>Ref(HaClose,-2), True, False);

//StockName = FullName();
//BankN = StockName == "BANKNIFTY";

Buy = Gtrig0 AND Gtrig1 AND Gtrig2 AND Gtrig4; // AND Gtrig4;

PlotShapes(IIf(Buy, shapeCircle, shapeNone), colorYellow, 0);

// 3 RED CANDLES AFTER 1 GREEN
Rtrig0 = IIf(Ref(HaClose,-2)>=Ref(HaOpen,-2),True,False); //GREEN CANDLE BEFORE RED
Rtrig1 = IIf(HaOpen>=HaClose,True,False); // 2ND RED CANDLE
Rtrig2 = IIf(Ref(HaOpen,-1)>=Ref(HaClose,-1),True,False); // 1ST RED CANDLE
//Rtrig3 = IIf(Ref(HaOpen,-3)<=Ref(HaClose,-3),True,False);
Rtrig4 = IIf(HaClose<Ref(HaClose,-1) AND Ref(HaClose,-1)<Ref(HaClose,-2), True, False);

Sell = Rtrig0 AND Rtrig1 AND Rtrig2 AND Rtrig4;

PlotShapes(IIf(Sell, shapeCircle, shapeNone), colorWhite, 0);

Filter = Buy OR Sell;

TimeFrameRestore();

_SECTION_END();

Help would be appreciated!

You do not use TimeFrame functions properly.
You should expand after restore.

Also if you just want to plot analysis range then you have to add barinrange Status code to Plotshapes.

/// @link https://forum.amibroker.com/t/why-does-the-scan-show-only-the-morning-signals/6584/2
/// OP code did not expand, so fixed code below has TimeFrameExpand added
/// in addition added Status( "BarInRange" ) to plot shapes only within analysis range
_SECTION_BEGIN("Price Heikin Ashi");
SetChartOptions(0,chartShowArrows | chartShowDates);

tmfrm = inHourly;
mode = expandFirst;

TimeFrameSet(tmfrm); 
	HaClose = (O + H + L + C)/4; 
	HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
	HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
	HaLow = Min( L, Min( HaClose, HaOpen ) ); 	

	// 3 GREEN CANDLES AFTER 1 RED
	Gtrig0 = IIf(Ref(HaClose,-2)<=Ref(HaOpen,-2),True,False); // RED CANDLE BEFORE GREEN
	Gtrig1 = IIf(HaClose>=HaOpen,True,False); // 2ND GREEN CANDLE
	Gtrig2 = IIf(Ref(HaClose,-1)>=Ref(HaOpen,-1),True,False); // 1ST GREEN CANDLE
	//Gtrig3 = IIf(Ref(HaClose,-3)>=Ref(HaOpen,-3),True,False);
	Gtrig4 = IIf(HaClose>Ref(HaClose,-1) AND Ref(HaClose,-1)>Ref(HaClose,-2), True, False);

	// 3 RED CANDLES AFTER 1 GREEN
	Rtrig0 = IIf(Ref(HaClose,-2)>=Ref(HaOpen,-2),True,False); //GREEN CANDLE BEFORE RED
	Rtrig1 = IIf(HaOpen>=HaClose,True,False); // 2ND RED CANDLE
	Rtrig2 = IIf(Ref(HaOpen,-1)>=Ref(HaClose,-1),True,False); // 1ST RED CANDLE
	//Rtrig3 = IIf(Ref(HaOpen,-3)<=Ref(HaClose,-3),True,False);
	Rtrig4 = IIf(HaClose<Ref(HaClose,-1) AND Ref(HaClose,-1)<Ref(HaClose,-2), True, False);
TimeFrameRestore();

HaClose = TimeFrameExpand( HAClose, tmfrm, mode ); 
HaOpen = TimeFrameExpand( HaOpen, tmfrm, mode );
HaHigh = TimeFrameExpand( HaHigh, tmfrm, mode ); 
HaLow = TimeFrameExpand( HaLow, tmfrm, mode );

barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);
xDiff = (HaHigh - Halow) * 10000;
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", barcolor, styleCandle );

for( i = 0; i < 5; i++ ) {
	VarSet( "Gtrig" + i, TimeFrameExpand( VarGet( "Gtrig" + i), tmfrm, mode ) ); 
	VarSet( "Rtrig" + i, TimeFrameExpand( VarGet( "Rtrig" + i), tmfrm, mode ) ); 
}

//StockName = FullName();
//BankN = StockName == "BANKNIFTY";

bir = Status( "BarinRange" );

Buy = Gtrig0 AND Gtrig1 AND Gtrig2 AND Gtrig4;
PlotShapes(IIf(Buy AND bir, shapeCircle, shapeNone), colorYellow, 0);

Sell = Rtrig0 AND Rtrig1 AND Rtrig2 AND Rtrig4;
PlotShapes(IIf(Sell AND bir, shapeCircle, shapeNone), colorWhite, 0);

Filter = Buy OR Sell;

_SECTION_END();
3 Likes

Thank you so much @fxshrat for this correction. I'm kinda still not sure how to use TimeFrame functions properly. However, I'm curious to what the below code does, well I know what it does, but why is it used:

for( i = 0; i < 5; i++ ) {
	VarSet( "Gtrig" + i, TimeFrameExpand( VarGet( "Gtrig" + i), tmfrm, mode ) ); 
	VarSet( "Rtrig" + i, TimeFrameExpand( VarGet( "Rtrig" + i), tmfrm, mode ) ); 
}

And unfortunately, the main issue is still there (with a slight difference):
image

It still shows only the morning signals. What else am I doing wrong?

No, you don't know what it does as you would not have asked otherwise.

Well, it saves from writing repeated code lines. Instead of writing 10 lines of Gtrig + Rtrig TimeFrameExpand (Gtrig0=... to Gtrig4=... and Rtrig0=... to Rtrig4=...) it only requires two lines since the variable names differ only by numbering. Two lines versus 10...50...100 lines. What would you prefer? Saving time or wasting time?

You apparently have not set to intraday interval in analysis settings but seem to have set EOD interval instead. Read the paragraph above of the 3rd picture of below link (and look at that 3rd picture).

As you can see I am getting multiple signals per day if setting to e.g. Hourly in Analysis.

2 Likes

PERFECT. Thanks a lot @fxshrat! It worked perfectly when I changed the periodicity to hourly.
image

Multiple questions, however:

Will changing the periodicity in the backtester settings influence the periodicity in all analysis code? Probably, yes.

What if I want to scan for signals in hourly for one strategy and then in 5 minute interval for another in the same code? Is that possible?

If you have opened multiple analysis windows you can set independent periodicity settings for each opened one of them. So no, they will not influence each other.

One thing to note... If you close all analysis windows then the last closed one will set new default analysis settings for analysis window(s) getting opened next time.

If you want to save analysis settings (analysis toolbar settings and analysis backtest settings) for a code then create analysis project file (.apx extension) via File - Save or File - Save as. (Analysis projects also save applied AFL).

1 Like

Got it. One other question.

Some strategies work on some stocks but not all. Some strategies are universal. For the former case, is there any way to make sure that a particular strategy (code) looks for opportunities only in a particular group of stock(s)?

Use Filter settings of analysis (and save to analysis project file if you want to keep settings per code. Then next time open the analysis project file.).

41

2 Likes

Thank you! You've been very helpful. May I ask how you achieved your level of expertise in AFL?

No problem. You're welcome.

Studying a lot and exercising a lot. That's basically it (mixed with little bit of talent, vision / imaginary level, tenacity to get things done. Also being eagle-eyed may be helpful).

3 Likes