Hello,
I'm facing some problems when trying to implement a bar stop, which exits a trade after e.g. 2 Bar in the higher-TimeFrame (e.g. Daily).
The base TimeFrame is for example 15 minutes.
(I know, the simple suggestion is: set the Base-TF also to Daily. But this is not possible. I need this granularity for several reasons.)
The first and easiest solution therefore would be to go to higher TF (Daily) via TimeFrameSet and code the stop there via Applystop. But in my attempts, it doesn't work.
It exited every time after 2 Bars in the Base-TF but not in the higher TF ( it should have been approximately 100-140 Base-TF-Bars (15 minutes) ) , as it should do.
Maybe I must use a variable for the Applystop and then expand it, but otherwise I'm not able to expand the Applystop. I don't see an other attempt to fix this try.
Sure, I thought about another really simple solution: determine the number of bars of the higher TF (e.g. Daily) in terms of the Base TF. But as far as I know, the relation beetween for example Daily and 15 minutes is fixed and therefore leads to a false solution (if the market isn't open 24 hours).
https://www.amibroker.com/guide/afl/timeframeset.html
(Align to market hours isn't an option. I want to test through different markets with the same formula.)
Ok. Now my solution attempt:
Filter = 1 ;
Buy_Cond = TimeNum() >= 160000 ; // AND DayOfWeek() == 5 ;
Buy = Buy_Cond ;
BarStop_TF_long = inDaily ;
Count_Bars_long = Param("Count Bars Stop", 2, 0, 100, 1) ;
Incr_Close_long = ParamToggle("Incr_Close?", "No|Yes", 1) ;
Count_Bars_long = TimeFrameCompress(Count_Bars_long, BarStop_TF_long, compressOpen) ;
jjj = TimeFrameCompress( Buy, BarStop_TF_long, compressHigh ); // compressOpen
TimeFrameSet(BarStop_TF_long) ;
Holdlength_Bars_long = BarsSince( jjj ) ;
Sell_Cond_Time_Bars = Holdlength_Bars_long == Count_Bars_long ; //
TimeFrameRestore() ;
Holdlength_Bars_long = TimeFrameExpand(Holdlength_Bars_long, BarStop_TF_long, expandFirst) ;
Sell_Cond_Time_Bars = TimeFrameExpand(Sell_Cond_Time_Bars, BarStop_TF_long, expandFirst) ; //
jjj = TimeFrameExpand(jjj, BarStop_TF_long, expandFirst) ; //
//Sell_Cond_Time_Bars = Holdlength_Bars_long == Count_Bars_long ;
if(Incr_Close_long) Sell_Cond_Time_Bars = Sell_Cond_Time_Bars AND NOT Ref(Sell_Cond_Time_Bars, 1) ;
Sell = Sell_Cond_Time_Bars ;
Equity( 1, 0 ); // evaluate stops, all quotes
//SetOption("EveryBarNullCheck", True );
Short = Cover = 0 ;
AddColumn( Buy, "Buy", 1.0, TextColor = colorDefault, BkgndColor = IIf( Buy >= 1, colorGreen, colorDefault ) );
AddColumn( Buy_Cond, "Buy_Cond", 1.0) ;
AddColumn( ValueWhen( Buy, BarIndex() ), "VW - BarIndex" );
AddColumn( Sell, "Sell", 1.0, TextColor = colorDefault, BkgndColor = IIf( Sell >= 1, colorRed, colorDefault ) );
AddColumn( jjj , "jjj" );
AddColumn( Holdlength_Bars_long , "Holdlength_Bars_long" );
AddColumn( Count_Bars_long , "Count_Bars_long" );
AddColumn( Incr_Close_long , "Incr_Close_long" );
AddColumn( Sell_Cond_Time_Bars , "Sell_Cond_Time_Bars", 1.0, colorDefault, BkgndColor = IIf( Sell_Cond_Time_Bars >= 1, colorRed, colorDefault ) );
Well. This attempt works. But only, if the next Buy-Signal doesn't occur in the time window of the Bar-Stop.
Therefore " DayOfWeek() == 5 " works with a 2 daily-bar stop .
But if the Buy-Signal occurs every trading day, the stop can't work. " jjj " is overwriting itself !
I think there are 2 possible solutions:
a) fix this repeating Buy-Signals which haven't a corresponding Sell-Signal.
I've tried this. But I think I need to work with CBI for this purpose... Am I right?
b) the simpler solution would be to use Applystop.
It's doing exactly what I want: without overwriting, it waits for the Sell-Bar
I know, because of this reason it would be ways easier to use ApplyStop.
But as described above, I was struggling with ApplyStop (bars) in a higher TimeFrame.
I hope someone have a solution or can point me in the right direction.
Do I have to use the CBI?