Right intra-bar exit

Hello,
I'm facing a problem with the right intra-bar exit, if the stop and the target is triggered on the very same bar.
After some hours of trying and search, I found this problem several times on the internet.
But without a concrete solution.

( https://futures.io/amibroker/5429-backtesting-research-multicharts-amibroker-3.html )

In the last link, an user asks this question, which is similar to my problem:

"1.Does ab support intrabar execution?
How does it work/ what are the settings for it?"

The answer was to "look in the tutorial".
Well, I tried it again and understood that I must loop the lower timeframe (e.g. 5-minute) bar by bar to achieve an execution on the e.g. 1h-TF, which means it's more or less "realtime".
You see my code below, I tried to loop and their is no bug in the formula.
But if I run the backtest - even on only the last half month - my computer run and run and run.

I'm sure it's a stupid mistake from a newbie (approximately 5 weeks with AFL) or even probably multiple mistakes.
I hope you can help me. :slight_smile:

I tried to fix it but I'm not sure what the problem is about.

  • something in the for-loop that should not be in there?
  • a fail with this TF-stuff
  • I recently red that arrays in the for-loop are forbidden.

Please don't kill me instantly, I'm not familiar with loops. :wink:

// Hikake without an additional bar between the formation and the entry


// FÜR DIE UNLOGISCHEN Namen für st.GetValue MUSS MAN FOLGENDEN WEG GEHEN:
//  --> AmiBroker User's Guide
//  --> AmiBroker Formula Language (AFL) 
//  --> Advanced portfolio backtester interface  --> (fast) Ganz Unten!


// First we need to enable custom backtest procedure and 
// tell AmiBroker to use current formula 

SetCustomBacktestProc(""); 

/* Now custom-backtest procedure follows */ 

if( Status("action") == actionPortfolio ) 
 { 
     bo = GetBacktesterObject(); 

     bo.Backtest(); // run default backtest procedure  

     st = bo.GetPerformanceStats(0); // get stats for all trades  

// Number Of years
    // (1+ (AnnualReturn % / 100%))^(X) = NetProfit %
    // --> log:  X = (log (1+ (AnnualReturn % / 100%))) / (log (NetProfit %))
    
     NumberOfYears = (log (1 + (st.GetValue("NetProfitPercent")) / 100)) / (log (1 + ((st.GetValue("CAR")) / 100)));
     
     // "CAR" NICHT "AnnualReturnPercent" oder "CompoundedAnnualReturnPercent"
  
// Haltedauer pro Jahr in %     
     HandelstageJahr = 250 ;
     
     HaltedauerAbsolut = (st.GetValue("AllQty")) * (st.GetValue("AllAvgBarsHeld")) ;
     HaltedauerProJahr = HaltedauerAbsolut / NumberOfYears ;
     PercHaltedauerJahr = HaltedauerProJahr / HandelstageJahr * 100 ;
     
        
     // Here we add custom metric to backtest report 
     bo.AddCustomMetric( "NumberOfYears" , NumberOfYears ) ;
     bo.AddCustomMetric( "Haltedauer/Jahr %" , PercHaltedauerJahr) ;
 } 


// ---------------------------------------------------------------
// YOUR TRADING SYSTEM HERE  
// ---------------------------------------------------------------

TradeDelay = 0; // set it to 0 for no delays 
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay ); 

MarginDeposit = Param( "Margin / contract" , 20000, 1, 1000000, 1, sincr = 0 ) ;
TickSize = Param( "TickSize", 0.5, 0.001, 1000, 0.001, sincr = 0 ) ;
TickValue = Param( "TickValue", 12.50 , 0.01, 1000, 0.01, sincr = 0 ) ;
ComPerHalfturn = Param( "ComPerHalfturn" , 0.50, 0.001, 1000, 0.001, sincr = 0 ) ;


// sample entry rules

Insidebar = Ref(L,-2) > Ref(L,-3) AND Ref(H,-2) < Ref(H,-3) AND Ref(H,-2) - Ref(L,-2) > TickSize ;

a = Ref(L,-1) < (Ref(L,-2) - 0.1*Ref(ATR(20), -1)) ;  // InsidebarBreak
b = Ref(H,-1) < Ref(H,-2) ;
d = Ref(L,-2) - Ref(L,-1) < 0.7*(Ref(H,-2) - Ref(L,-2)) ;

HikakeLong = Insidebar AND a AND b AND d ;  // Signal OHNE Entry-Trigger


e = Ref(H,-1) > (Ref(H,-2) + 0.1*Ref(ATR(20), -1)) ;
f = Ref(L,-1) > Ref(L,-2) ;
g = Ref(H,-1) - Ref(H,-2) < 0.7*(Ref(H,-2) - Ref(L,-2)) ;

HikakeShort = Insidebar AND e AND f AND g ;   // Signal OHNE Entry-Trigger


Slippage = Param("Slippage*ATR(20)", 0.05, 0, 0.5, 0.01, sincr = 0 ) ;
SlippageGes = Slippage*(Ref(ATR(20), -2)) ;

BuyPrice = Max(Ref(H,-2) + 2*TickSize + SlippageGes , Open + 2*TickSize + SlippageGes ) ;
SellPrice = Open ;
ShortPrice = Min(Ref(L,-2) - 2*TickSize - SlippageGes , Open - 2*TickSize - SlippageGes ) ;
CoverPrice = Open ;

tn = TimeNum();
startTime = Param("StartTime", 91500, 000000, 235900, 000100) ; // start in HHMMSS format
endTime = Param("EndTime", 214500, 000000, 235900, 000100);  // end in HHMMSS format
timeOK = tn >= startTime AND tn <= endTime;

EntryPriceLong  = Ref(H,-2) ;
EntryPriceShort = Ref(L,-2) ;

LBhigh = Ref(H,-1) ;  // last bar high
LBlow  = Ref(L,-1) ;  // last bar low



TimeFrameSet( in5Minute ); // switch to 5 minute frame 

// new 5.90 function that counts leading Nulls
start = NullCount( Close );

// looping code
for ( i = start + 1; i < BarCount; i++ ) // Loop through all bars

	{
	
    ////////////////////////////////////////////////////////////////
    // LONG-SEITE
    ////////////////////////////////////////////////////////////////

	Buy = HikakeLong AND timeOK AND H > EntryPriceLong ;
	
	stopLevelLong = LBlow - 1*TickSize ; // use previous bar low  // define stop level 
	stopAmountLong = BuyPrice - stopLevelLong ;  // calculate stop amount 
	stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );  // make sure stop-amount is at least one tick
	
	Sell = TimeNum() >= endTime ;
	

    ////////////////////////////////////////////////////////////////
    // SHORT-SEITE
    ////////////////////////////////////////////////////////////////
    
    Short = HikakeShort AND timeOK AND L < EntryPriceShort ;
	
	stopLevelShort = LBhigh + 1*TickSize;   // define stop level
	stopAmountShort = stopLevelShort - ShortPrice ;  // calculate stop amount   
	stopAmountShort = Max( TickSize, stopLevelShort - ShortPrice );  // make sure stop-amount is at least one tick 
	
	Cover = TimeNum() >= endTime ;
	
	// assign stop amount conditionally by checking if there is a Buy signal on given bar 
	IsLong = Ref(Buy, -TradeDelay); 
	stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort ); 
	IsShort = Ref(Short, -TradeDelay) ;
	stopAmount = IIf( IsShort, stopAmountShort, stopAmountLong ); 
	
	ApplyStop( stopTypeLoss, stopModePoint, stopAmount, 1, True ); 
	ApplyStop( stopTypeProfit, stopModePoint, 1*stopAmount, 1, True ); 
	
	Risk = Param("Risk (absolut) %" , 0.5, 0.1, 10, 0.1, sincr = 0) ; // Risk in % 
	U = Risk/100 ; // umgewandelt in Q
	InitialEquity = Param("Initial Equity" , 100000, 1000, 10000000, 1000) ;
	Z = U * InitialEquity ; // relatives --> absolutes Risiko
	SetOption("InitialEquity", 100000 ); // set initial equity = 100K
	SetOption("CommissionMode" , 3 ) ;
	SetOption( "CommissionAmount" , ComPerHalfturn ) ;
	size = Z/(stopAmount*(TickValue/TickSize)) ;  // Z = size*stopAmount
	SetPositionSize( size, spsShares ); // number of shares/contracts per trade
	
	PointValue = TickValue / TickSize ;

    }

TimeFrameRestore() ;

@euroundusd:

I am not a user of the backtester (YET), so can't address any of the issues in that.

What I can say is that first, you can't expect to know what goes on within a bar (go to high before going to low) unless you have lower or shorter period data to check it.

For example, if you only have one hour data, you can't know if the Buy level would be triggered before the Sell level in a bar (when you touch both levels in the hour bar) without having data at a lower level (i.e. 1 minute, where both levels can't be reached in same bar).

If you have the data for the lower interval, then you should be able to run your back test on that interval (1 minute).

You may have to use the TimeFrame functions to generate reference values from the Hourly timeframe, but you can then refer to those reference values while in the minute based back test run.

Hope you get the idea.

Thanks for your answer!

I know that I must use lower TF. Thats not the problem.
I thought that's clear, at the latest if someone looks at the code.
I have enough data for example at the 5min-TF.
I wanted to backtest in my example in the 1h-TF and use my AVAILABLE lower data.

Thank you very much for your input!
I thought about this way, too. But I want to have my PA backtest signals in the e.g. 1h-TF to see if they work like they should.

So my question at all guys here:
Is it possible to backtest my 1h signals AT this 1h-TF and switch inside the code to the 5min-TF to have the required intra-bar execution?
In other words: I want to see my signals after I run the backtest. On the 5min-TF I can't see if they work like they should (at the 1h-TF).
Or is the only way to solve this intra-bar problem, like snoopy.pa30 suggested, to backtest my 1h-signal at the lower TF (e.g. 5min) ?

@euroundusd the lowest timeframe data that you have available in the back test is the "base" timeframe that you specify in the "Periodicity" field of the Analysis Settings. Thus, if you set your Periodicity to 1-hour bars, then you can't access anything shorter than 1-hour bars from within that back test.

What you probably want to do is set your Periodicity to 5 minutes, and then use the TimeFrame* functions in AmiBroker to compress those 5-minute bars into 1-hour bars for purposes of generating your signals, then expand the signal arrays back to 5-minute bars for execution of the back test.

If you haven't already done so, start by reading this: https://www.amibroker.com/guide/h_timeframe.html

1 Like

It works more or less.

But one problem is left:

With the code below, one can backtest the W1-TF with 1D-data.
If I look to the displayed trades on the chart, it shows me the Base-TF , that means in this case the Daily.
But I want to see if my Weekly-Signals had worked like they should.
That's why I must display the Trades on a weekly-Chart.
I tried it, but my problem is: only at the right border the Bars from the different TF's are matching. E.g. in the middle of the chart it doesn't work.

I also thinked about this way:

I tried to make this with one W1-TF-chart and one D1, but with the floating Charts it doesn't work if I want to display my Trades not only at the Base-Timeframe.

Could you help me, please? :slight_smile:

// Hikake without an additional bar between the formation and the entry


// FÜR DIE UNLOGISCHEN Namen für st.GetValue MUSS MAN FOLGENDEN WEG GEHEN:
//  --> AmiBroker User's Guide
//  --> AmiBroker Formula Language (AFL) 
//  --> Advanced portfolio backtester interface  --> (fast) Ganz Unten!


// First we need to enable custom backtest procedure and 
// tell AmiBroker to use current formula 

SetCustomBacktestProc(""); 

/* Now custom-backtest procedure follows */ 

if( Status("action") == actionPortfolio ) 
 { 
     bo = GetBacktesterObject(); 

     bo.Backtest(); // run default backtest procedure  

     st = bo.GetPerformanceStats(0); // get stats for all trades  

// Number Of years
    // (1+ (AnnualReturn % / 100%))^(X) = NetProfit %
    // --> log:  X = (log (1+ (AnnualReturn % / 100%))) / (log (NetProfit %))
    
     NumberOfYears = (log (1 + (st.GetValue("NetProfitPercent")) / 100)) / (log (1 + ((st.GetValue("CAR")) / 100)));
     
     // "CAR" NICHT "AnnualReturnPercent" oder "CompoundedAnnualReturnPercent"
  
// Haltedauer pro Jahr in %     
     HandelstageJahr = 250 ;
     
     HaltedauerAbsolut = (st.GetValue("AllQty")) * (st.GetValue("AllAvgBarsHeld")) ;
     HaltedauerProJahr = HaltedauerAbsolut / NumberOfYears ;
     PercHaltedauerJahr = HaltedauerProJahr / HandelstageJahr * 100 ;
     
        
     // Here we add custom metric to backtest report 
     bo.AddCustomMetric( "NumberOfYears" , NumberOfYears ) ;
     bo.AddCustomMetric( "Haltedauer/Jahr %" , PercHaltedauerJahr) ;
 } 


// ---------------------------------------------------------------
// YOUR TRADING SYSTEM HERE  
// ---------------------------------------------------------------
 
 
TradeDelay = 0; // set it to 0 for no delays 
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay); 

MarginDeposit = Param( "Margin / contract" , 20000, 1, 1000000, 1, sincr = 0 ) ;
TickSize = Param( "TickSize", 0.5, 0.001, 1000, 0.001, sincr = 0 ) ;
TickValue = Param( "TickValue", 12.50 , 0.01, 1000, 0.01, sincr = 0 ) ;
ComPerHalfturn = Param( "ComPerHalfturn" , 0.50, 0.001, 1000, 0.001, sincr = 0 ) ;


// sample entry rules

TimeFrameSet( inWeekly ); // switch to 1h frame 
Insidebar = Ref(L,-2) > Ref(L,-3) AND Ref(H,-2) < Ref(H,-3) AND Ref(H,-2) - Ref(L,-2) > TickSize ;

a = Ref(L,-1) < (Ref(L,-2) - 0.1*Ref(ATR(20), -1)) ;  // InsidebarBreak
b = Ref(H,-1) < Ref(H,-2) ;
d = Ref(L,-2) - Ref(L,-1) < 1*(Ref(H,-2) - Ref(L,-2)) ;

HikakeLong = Insidebar AND a AND b AND d ;  // Signal OHNE Entry-Trigger


e = Ref(H,-1) > (Ref(H,-2) + 0.1*Ref(ATR(20), -1)) ;
f = Ref(L,-1) > Ref(L,-2) ;
g = Ref(H,-1) - Ref(H,-2) < 1*(Ref(H,-2) - Ref(L,-2)) ;

HikakeShort = Insidebar AND e AND f AND g ;   // Signal OHNE Entry-Trigger


Slippage = Param("Slippage*ATR(20)", 0.05, 0, 0.5, 0.01, sincr = 0 ) ;
SlippageGes = Slippage*(Ref(ATR(20), -2)) ;

EPL = Ref(H,-2) ;  // Entry-Price Long
EPS = Ref(L,-2) ;  // Entry-Price Short

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
HikakeLongReal = TimeFrameExpand( HikakeLong, inWeekly) ; 
HikakeShortReal = TimeFrameExpand( HikakeShort, inWeekly) ; 
SlippageGesReal = TimeFrameExpand( SlippageGes, inWeekly) ;
EPLreal = TimeFrameExpand( EPL, inWeekly) ;  // EntryPriceLong
EPSreal = TimeFrameExpand( EPS, inWeekly) ;  // EntryPriceShort

BuyPrice = Max(EPLreal + 2*TickSize + SlippageGesReal , Open + 2*TickSize + SlippageGesReal ) ;
SellPrice = Open ;
ShortPrice = Min(EPSreal - 2*TickSize - SlippageGesReal , Open - 2*TickSize - SlippageGesReal ) ;
CoverPrice = Open ;

tn = TimeNum();
startTime = Param("StartTime", 000000, 000000, 235900, 000100) ; // start in HHMMSS format
endTime = Param("EndTime", 235900, 000000, 235900, 000100);  // end in HHMMSS format
timeOK = tn >= startTime AND tn <= endTime;


TimeFrameSet( inWeekly ); // switch to 1h frame 

LBhigh = Ref(H,-1) ;  // last bar high
LBlow  = Ref(L,-1) ;  // last bar low

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
LBhighReal = TimeFrameExpand( LBhigh, inWeekly );  
ABhighReal = HHV(H,5) ; // aktuelles Bar-High (Weekly) 
LBlowReal  = TimeFrameExpand( LBlow, inWeekly ); 
ABlowReal  = LLV(L,5) ; // aktuelles Bar-Low (Weekly) 

highReal = Max(LBhighReal, ABhighReal) ;
lowReal  = Min(LBlowReal, ABlowReal) ;


    ////////////////////////////////////////////////////////////////
    // LONG-SEITE
    ////////////////////////////////////////////////////////////////

	Buy = HikakeLongReal AND timeOK AND H > EPLreal ;
	
	stopLevelLong = lowReal - 1*TickSize ; // use previous bar low  // define stop level 
	stopAmountLong = BuyPrice - stopLevelLong ;  // calculate stop amount 
	stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );  // make sure stop-amount is at least one tick
	
	Sell = TimeNum() >= endTime ;
	

    ////////////////////////////////////////////////////////////////
    // SHORT-SEITE
    ////////////////////////////////////////////////////////////////
    
    Short = HikakeShortReal AND timeOK AND L < EPSreal ;
	
	stopLevelShort = highReal + 1*TickSize;   // define stop level
	stopAmountShort = stopLevelShort - ShortPrice ;  // calculate stop amount   
	stopAmountShort = Max( TickSize, stopLevelShort - ShortPrice );  // make sure stop-amount is at least one tick 
	
	Cover = TimeNum() >= endTime ;
	
	// assign stop amount conditionally by checking if there is a Buy signal on given bar 
	IsLong = Ref(Buy, -TradeDelay); 
	stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort ); 
	IsShort = Ref(Short, -TradeDelay) ;
	stopAmount = IIf( IsShort, stopAmountShort, stopAmountLong ); 

ApplyStop( stopTypeLoss, stopModePoint, stopAmount, False ); 
CRVOpti = Optimize("X", 1, 0.5, 5, 0.5);  // zum anschauen 
// kleinere CRVs erst mit Intraday-Data
ApplyStop( stopTypeProfit, stopModePoint, CRVOpti*stopAmount, False );  


	Risk = Param("Risk (absolut) %" , 0.5, 0.1, 10, 0.1, sincr = 0) ; // Risk in % 
	U = Risk/100 ; // umgewandelt in Q
	InitialEquity = Param("Initial Equity" , 100000, 1000, 10000000, 1000) ;
	Z = U * InitialEquity ; // relatives --> absolutes Risiko
	SetOption("InitialEquity", 100000 ); // set initial equity = 100K
	SetOption("CommissionMode" , 3 ) ;
	SetOption( "CommissionAmount" , ComPerHalfturn ) ;
	size = Z/(stopAmount*(TickValue/TickSize)) ;  // Z = size*stopAmount
	SetPositionSize( size, spsShares ); // number of shares/contracts per trade
	
	PointValue = TickValue / TickSize ;

//PlotShapes( IIf(Buy, shapeCircle, shapeNone),colorBrightGreen, 0, BuyPrice, 0 );
//PlotShapes( IIf(Short, shapeCircle, shapeNone),colorRed, 0, ShortPrice, 0 );


// plot Signal-TF (W1) 
wo = TimeFrameGetPrice( "O", inWeekly, 0, expandPoint ); 
wh = TimeFrameGetPrice( "H", inWeekly, 0, expandPoint ); 
wl = TimeFrameGetPrice( "L", inWeekly, 0, expandPoint ); 
wc = TimeFrameGetPrice( "C", inWeekly, 0, expandPoint ); 

TimeFrameSet(inWeekly); 
wo = Open ;
wh = High ;
wl = Low ;
wc = Close ; 
TimeFrameRestore(); 

TimeFrameExpand(wo, inWeekly) ;
TimeFrameExpand(wh, inWeekly) ;
TimeFrameExpand(wl, inWeekly) ;
TimeFrameExpand(wc, inWeekly) ;

PlotOHLC( wo, wh, wl, wc, "Weekly Close", colorRed, styleCandle ); 


// (instead of this above)  nearly, must connect the littles lines ( O, H, L, C) to big W1-Bars or Candles.

//wo = TimeFrameGetPrice( "O", inWeekly, 0, expandPoint ); 
//wh = TimeFrameGetPrice( "H", inHourly, 0, expandPoint ); 
//wl = TimeFrameGetPrice( "L", inHourly, 0, expandPoint ); 
//wc = TimeFrameGetPrice( "C", inHourly, 0, expandPoint ); 

//PlotOHLC( wo, wh, wl, wc, "Weekly Close", colorRed, styleBar ); 
//PlotOHLC( O, H, L, C, "D1 Close", colorGreen, styleCandle );

Ok. Sry. I solved it.
I didn't noticed that I only set the WeeklyOpen to "In Weekly" the rest was staying "in Hourly" ....

// Hikake without an additional bar between the formation and the entry


// FÜR DIE UNLOGISCHEN Namen für st.GetValue MUSS MAN FOLGENDEN WEG GEHEN:
//  --> AmiBroker User's Guide
//  --> AmiBroker Formula Language (AFL) 
//  --> Advanced portfolio backtester interface  --> (fast) Ganz Unten!


// First we need to enable custom backtest procedure and 
// tell AmiBroker to use current formula 

SetCustomBacktestProc(""); 

/* Now custom-backtest procedure follows */ 

if( Status("action") == actionPortfolio ) 
 { 
     bo = GetBacktesterObject(); 

     bo.Backtest(); // run default backtest procedure  

     st = bo.GetPerformanceStats(0); // get stats for all trades  

// Number Of years
    // (1+ (AnnualReturn % / 100%))^(X) = NetProfit %
    // --> log:  X = (log (1+ (AnnualReturn % / 100%))) / (log (NetProfit %))
    
     NumberOfYears = (log (1 + (st.GetValue("NetProfitPercent")) / 100)) / (log (1 + ((st.GetValue("CAR")) / 100)));
     
     // "CAR" NICHT "AnnualReturnPercent" oder "CompoundedAnnualReturnPercent"
  
// Haltedauer pro Jahr in %     
     HandelstageJahr = 250 ;
     
     HaltedauerAbsolut = (st.GetValue("AllQty")) * (st.GetValue("AllAvgBarsHeld")) ;
     HaltedauerProJahr = HaltedauerAbsolut / NumberOfYears ;
     PercHaltedauerJahr = HaltedauerProJahr / HandelstageJahr * 100 ;
     
        
     // Here we add custom metric to backtest report 
     bo.AddCustomMetric( "NumberOfYears" , NumberOfYears ) ;
     bo.AddCustomMetric( "Haltedauer/Jahr %" , PercHaltedauerJahr) ;
 } 


// ---------------------------------------------------------------
// YOUR TRADING SYSTEM HERE  
// ---------------------------------------------------------------
 
 
TradeDelay = 0; // set it to 0 for no delays 
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay); 

MarginDeposit = Param( "Margin / contract" , 20000, 1, 1000000, 1, sincr = 0 ) ;
TickSize = Param( "TickSize", 0.5, 0.001, 1000, 0.001, sincr = 0 ) ;
TickValue = Param( "TickValue", 12.50 , 0.01, 1000, 0.01, sincr = 0 ) ;
ComPerHalfturn = Param( "ComPerHalfturn" , 0.50, 0.001, 1000, 0.001, sincr = 0 ) ;


// sample entry rules

TimeFrameSet( inWeekly ); // switch to 1h frame 
Insidebar = Ref(L,-2) > Ref(L,-3) AND Ref(H,-2) < Ref(H,-3) AND Ref(H,-2) - Ref(L,-2) > TickSize ;

a = Ref(L,-1) < (Ref(L,-2) - 0.1*Ref(ATR(20), -1)) ;  // InsidebarBreak
b = Ref(H,-1) < Ref(H,-2) ;
d = Ref(L,-2) - Ref(L,-1) < 1*(Ref(H,-2) - Ref(L,-2)) ;

HikakeLong = Insidebar AND a AND b AND d ;  // Signal OHNE Entry-Trigger


e = Ref(H,-1) > (Ref(H,-2) + 0.1*Ref(ATR(20), -1)) ;
f = Ref(L,-1) > Ref(L,-2) ;
g = Ref(H,-1) - Ref(H,-2) < 1*(Ref(H,-2) - Ref(L,-2)) ;

HikakeShort = Insidebar AND e AND f AND g ;   // Signal OHNE Entry-Trigger


Slippage = Param("Slippage*ATR(20)", 0.05, 0, 0.5, 0.01, sincr = 0 ) ;
SlippageGes = Slippage*(Ref(ATR(20), -2)) ;

EPL = Ref(H,-2) ;  // Entry-Price Long
EPS = Ref(L,-2) ;  // Entry-Price Short

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
HikakeLongReal = TimeFrameExpand( HikakeLong, inWeekly) ; 
HikakeShortReal = TimeFrameExpand( HikakeShort, inWeekly) ; 
SlippageGesReal = TimeFrameExpand( SlippageGes, inWeekly) ;
EPLreal = TimeFrameExpand( EPL, inWeekly) ;  // EntryPriceLong
EPSreal = TimeFrameExpand( EPS, inWeekly) ;  // EntryPriceShort

BuyPrice = Max(EPLreal + 2*TickSize + SlippageGesReal , Open + 2*TickSize + SlippageGesReal ) ;
SellPrice = Open ;
ShortPrice = Min(EPSreal - 2*TickSize - SlippageGesReal , Open - 2*TickSize - SlippageGesReal ) ;
CoverPrice = Open ;

tn = TimeNum();
startTime = Param("StartTime", 000000, 000000, 235900, 000100) ; // start in HHMMSS format
endTime = Param("EndTime", 235900, 000000, 235900, 000100);  // end in HHMMSS format
timeOK = tn >= startTime AND tn <= endTime;


TimeFrameSet( inWeekly ); // switch to 1h frame 

LBhigh = Ref(H,-1) ;  // last bar high
LBlow  = Ref(L,-1) ;  // last bar low

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
LBhighReal = TimeFrameExpand( LBhigh, inWeekly );  
ABhighReal = HHV(H,5) ; // aktuelles Bar-High (Weekly) 
LBlowReal  = TimeFrameExpand( LBlow, inWeekly ); 
ABlowReal  = LLV(L,5) ; // aktuelles Bar-Low (Weekly) 

highReal = Max(LBhighReal, ABhighReal) ;
lowReal  = Min(LBlowReal, ABlowReal) ;


    ////////////////////////////////////////////////////////////////
    // LONG-SEITE
    ////////////////////////////////////////////////////////////////

	Buy = HikakeLongReal AND timeOK AND H > EPLreal ;
	
	stopLevelLong = lowReal - 1*TickSize ; // use previous bar low  // define stop level 
	stopAmountLong = BuyPrice - stopLevelLong ;  // calculate stop amount 
	stopAmountLong = Max( TickSize, BuyPrice - stopLevelLong );  // make sure stop-amount is at least one tick
	
	Sell = TimeNum() >= endTime ;
	

    ////////////////////////////////////////////////////////////////
    // SHORT-SEITE
    ////////////////////////////////////////////////////////////////
    
    Short = HikakeShortReal AND timeOK AND L < EPSreal ;
	
	stopLevelShort = highReal + 1*TickSize;   // define stop level
	stopAmountShort = stopLevelShort - ShortPrice ;  // calculate stop amount   
	stopAmountShort = Max( TickSize, stopLevelShort - ShortPrice );  // make sure stop-amount is at least one tick 
	
	Cover = TimeNum() >= endTime ;
	
	// assign stop amount conditionally by checking if there is a Buy signal on given bar 
	IsLong = Ref(Buy, -TradeDelay); 
	stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort ); 
	IsShort = Ref(Short, -TradeDelay) ;
	stopAmount = IIf( IsShort, stopAmountShort, stopAmountLong ); 

ApplyStop( stopTypeLoss, stopModePoint, stopAmount, False ); 
CRVOpti = Optimize("X", 1, 0.5, 5, 0.5);  // zum anschauen 
// kleinere CRVs erst mit Intraday-Data
ApplyStop( stopTypeProfit, stopModePoint, CRVOpti*stopAmount, False );  


	Risk = Param("Risk (absolut) %" , 0.5, 0.1, 10, 0.1, sincr = 0) ; // Risk in % 
	U = Risk/100 ; // umgewandelt in Q
	InitialEquity = Param("Initial Equity" , 100000, 1000, 10000000, 1000) ;
	Z = U * InitialEquity ; // relatives --> absolutes Risiko
	SetOption("InitialEquity", 100000 ); // set initial equity = 100K
	SetOption("CommissionMode" , 3 ) ;
	SetOption( "CommissionAmount" , ComPerHalfturn ) ;
	size = Z/(stopAmount*(TickValue/TickSize)) ;  // Z = size*stopAmount
	SetPositionSize( size, spsShares ); // number of shares/contracts per trade
	
	PointValue = TickValue / TickSize ;


wo = TimeFrameGetPrice( "O", inWeekly, 0, expandPoint ); 
wh = TimeFrameGetPrice( "H", inWeekly, 0, expandPoint ); 
wl = TimeFrameGetPrice( "L", inWeekly, 0, expandPoint ); 
wc = TimeFrameGetPrice( "C", inWeekly, 0, expandPoint ); 

PlotOHLC( wo, wh, wl, wc, "Weekly Close", colorRed, styleBar, Null, Null, 0, 0, -40 /* line width as percent of bar */); 
// PlotOHLC( O, H, L, C, "D1 Close", colorGreen, styleCandle );

Well, I've made big progress (for my situation and skill-level) in thinks like showing Trades Buy- and SellPrices, stop- and targetlines, ... .
But my IntraBar-Execution won't work like it should.
I tried to implement the hints from mradtke, but some things don't work correct.

Maybe someone can tell me what's wrong with my formula.

--> Backtest it on D1 and be aware with the Parameters on specific markets
--> Drag and drop into a blank chart
(Sorry this combi-chart doesn't scale the stop- and targetlines perfectly at any time.)

What I found is:
--> It buys often (but NOT always) at Friday's Open (also if the real trigger was e.g. on wednesday)
--> It sells often at the false price ( and sometimes also at the false bar - so the target or stop wasn't triggered at this bar (on the chart) )

// Hikake without an additional bar between the formation and the entry


// ---------------------------------------------------------------
// YOUR TRADING SYSTEM HERE  
// ---------------------------------------------------------------
 
TradeDelay = 0; // set it to 0 for no delays 
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay); 

MarginDeposit = Param( "Margin / contract" , 10000, 1, 1000000, 1, sincr = 0 ) ;
TickSize = Param( "TickSize", 0.5, 0.001, 1000, 0.001, sincr = 0 ) ;
TickValue = Param( "TickValue", 0.5 , 0.01, 1000, 0.01, sincr = 0 ) ;
ComPerHalfturn = Param( "ComPerHalfturn" , 0.50, 0.001, 1000, 0.001, sincr = 0 ) ;

Base_TimeFrame = Interval() ;
higher_TimeFrame = inWeekly ;

CRV = 1 ;

// sample entry rules

TimeFrameSet( higher_TimeFrame ); // switch to 1h frame 
Insidebar = Ref(L,-2) > Ref(L,-3) AND Ref(H,-2) < Ref(H,-3) AND Ref(H,-2) - Ref(L,-2) > TickSize ;

a = Ref(L,-1) < (Ref(L,-2) - 0.1*Ref(ATR(20), -1)) ;  // InsidebarBreak
b = Ref(H,-1) < Ref(H,-2) ;
d = Ref(L,-2) - Ref(L,-1) < 1*(Ref(H,-2) - Ref(L,-2)) ;

HikakeLong = Insidebar AND a AND b AND d ;  // Signal OHNE Entry-Trigger


e = Ref(H,-1) > (Ref(H,-2) + 0.1*Ref(ATR(20), -1)) ;
f = Ref(L,-1) > Ref(L,-2) ;
g = Ref(H,-1) - Ref(H,-2) < 1*(Ref(H,-2) - Ref(L,-2)) ;

HikakeShort = Insidebar AND e AND f AND g ;   // Signal OHNE Entry-Trigger


Slippage = Param("Slippage*ATR(20)", 0.00, 0, 0.5, 0.01, sincr = 0 ) ;
SlippageGes = Slippage*(Ref(ATR(20), -2)) ;

EPL = Ref(H,-2) ;  // Entry-Price Long
EPS = Ref(L,-2) ;  // Entry-Price Short

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
HikakeLongReal = TimeFrameExpand( HikakeLong, higher_TimeFrame) ; 
HikakeShortReal = TimeFrameExpand( HikakeShort, higher_TimeFrame) ; 
SlippageGesReal = TimeFrameExpand( SlippageGes, higher_TimeFrame) ;
EPLreal = TimeFrameExpand( EPL, higher_TimeFrame) ;  // EntryPriceLong
EPSreal = TimeFrameExpand( EPS, higher_TimeFrame) ;  // EntryPriceShort

tn = TimeNum();
startTime = Param("StartTime", 000000, 000000, 235900, 000100) ; // start in HHMMSS format
endTime = Param("EndTime", 235900, 000000, 235900, 000100);  // end in HHMMSS format
timeOK = tn >= startTime AND tn <= endTime;


TimeFrameSet( inWeekly ); // switch to 1h frame 

LBhigh = Ref(H,-1) ;  // last bar high
LBlow  = Ref(L,-1) ;  // last bar low

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
LBhighReal = TimeFrameExpand( LBhigh, higher_TimeFrame );  
ABhighReal = HHV(H,5) ; // aktuelles Bar-High (Weekly) 
LBlowReal  = TimeFrameExpand( LBlow, higher_TimeFrame ); 
ABlowReal  = LLV(L,5) ; // aktuelles Bar-Low (Weekly) 

highReal = Max(LBhighReal, ABhighReal) ;
lowReal  = Min(LBlowReal, ABlowReal) ;


    ////////////////////////////////////////////////////////////////
    // LONG-SEITE
    ////////////////////////////////////////////////////////////////

/* Buy */
Buy = HikakeLongReal AND timeOK AND H > EPLreal ;

BuyPrice = Max(EPLreal + 2*TickSize + SlippageGesReal , Open + 2*TickSize + SlippageGesReal ) ;
	
StopLevel_long = lowReal - 1*TickSize ; // use previous bar low  // define stop level 
StopAmount_long = BuyPrice - StopLevel_long ;  // calculate stop amount 
StopAmount_long = Max( TickSize, BuyPrice - StopLevel_long );  // make sure stop-amount is at least one tick
	
/* Sell */
Sell = TimeNum() >= endTime ;
ApplyStop( stopTypeLoss, stopModePoint, StopAmount_long, True );
ApplyStop( stopTypeProfit, stopModePoint, 1*StopAmount_long, True ); 

Equity( 1, 0 ); // evaluate stops, all quotes
SetOption("EveryBarNullCheck", True );	
InTrade_long = Flip( Buy, Sell );

TimeFrameSet( inWeekly ); // switch to 1h frame 

stopline_Condition_Long   = Ref(InTrade_long, -2) OR Ref(InTrade_long, -1) ;

targetline_Condition_Long = Ref(InTrade_long, -2) OR Ref(InTrade_long, -1) ;

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
stopline_Condition_Long = TimeFrameExpand( stopline_Condition_Long, higher_TimeFrame );  
targetline_Condition_Long = TimeFrameExpand( targetline_Condition_Long, higher_TimeFrame ); 

stopline_Long = ValueWhen(Buy, TimeFrameGetPrice("L", higher_TimeFrame, -1, expandLast) - 2*TickSize - SlippageGesReal) ;
targetline_Long = ValueWhen(Buy, BuyPrice + 1*StopAmount_long) ;

SellPrice = IIF(L <= stopline_Long, Min(Open, stopline_Long), IIf( H >= targetline_Long, Max(Open, targetline_Long),0)) ;

    ////////////////////////////////////////////////////////////////
    // SHORT-SEITE
    ////////////////////////////////////////////////////////////////

/* Short */    
Short = HikakeShortReal AND timeOK AND L < EPSreal ;

ShortPrice = Min(EPSreal - 2*TickSize - SlippageGesReal , Open - 2*TickSize - SlippageGesReal ) ;
	
StopLevel_short = highReal + 1*TickSize;   // define stop level
StopAmount_short = StopLevel_short - ShortPrice ;  // calculate stop amount   
StopAmount_short = Max( TickSize, StopLevel_short - ShortPrice );  // make sure stop-amount is at least one tick 
		
/* Cover */
Cover = TimeNum() >= endTime ;

ApplyStop( stopTypeLoss, stopModePoint, StopAmount_short, True );
ApplyStop( stopTypeProfit, stopModePoint, CRV*StopAmount_short, True ); 	

Equity( 1, 0 ); // evaluate stops, all quotes
SetOption("EveryBarNullCheck", True );	
InTrade_Short = Flip( Short, Cover );


TimeFrameSet( higher_TimeFrame ); // switch to 1h frame 

stopline_Condition_Short   = Ref(InTrade_short, -2) OR Ref(InTrade_short, -1) ;

targetline_Condition_Short = Ref(InTrade_short, -2) OR Ref(InTrade_short, -1) ;

TimeFrameRestore() ;

/* expand calculated stuff to hourly so we can use it with m5-signals */
stopline_Condition_Short = TimeFrameExpand( stopline_Condition_Short, higher_TimeFrame );  
targetline_Condition_Short = TimeFrameExpand( targetline_Condition_Short, higher_TimeFrame ); 

stopline_Short = ValueWhen(Short, TimeFrameGetPrice("H", higher_TimeFrame, -1, expandLast) + 1*TickSize + SlippageGesReal) ;
targetline_Short = ValueWhen(Short, ShortPrice + CRV*StopAmount_short) ;

CoverPrice = IIF(H >= Stopline_Short, Max(Open, Stopline_Short), IIf( L <= Targetline_Short, Min(Open, Targetline_Short),0)) ;


/* drawing the stop- & target-line */

// ==============================================================================================================
TimeFrameSet( higher_TimeFrame ); // switch to the higher timeframe 

Drawing_Condition_long = Ref(InTrade_long, -2) OR Ref(InTrade_long, -1) ;
Drawing_Condition_short = Ref(InTrade_short, -2) OR Ref(InTrade_short, -1) ;
TimeFrameRestore() ;

/* EXPAND CALCULATED STUFF to the lower TF, so we can use it with the lower data */
Drawing_Condition_long = TimeFrameExpand( Drawing_Condition_long, higher_TimeFrame ) ; 
Drawing_Condition_short = TimeFrameExpand( Drawing_Condition_short, higher_TimeFrame ) ; 
// ==============================================================================================================

stopline_drawing_Long = IIf( Drawing_Condition_long OR InTrade_long, ValueWhen(Buy, TimeFrameGetPrice("L", higher_TimeFrame, -1, expandLast)), Null) ;
targetline_drawing_Long = IIf( Drawing_Condition_long OR InTrade_long , ValueWhen(Buy, BuyPrice + CRV*StopAmount_long), Null) ;
stopline_drawing_Short = IIf( Drawing_Condition_short OR InTrade_short, ValueWhen(Short, TimeFrameGetPrice("H", higher_TimeFrame, -1, expandLast)), Null) ;
targetline_drawing_Short = IIf( Drawing_Condition_short OR InTrade_short, ValueWhen(Short, ShortPrice - CRV*StopAmount_short), Null) ;

Shift_X_Real = higher_TimeFrame / Base_TimeFrame ;

Plot( stopline_drawing_Long, "trailing stop line Long", colorRed, style = styleDashed, minvalue = 0, maxvalue = 0, XShift = Shift_X_Real, Zorder = 0, width = 4 ) ; 
Plot( targetline_drawing_Long, "profit target line Long", colorGreen, style = styleDashed, minvalue = 0, maxvalue = 0, XShift = Shift_X_Real, Zorder = 0, width = 4 ) ;
Plot( stopline_drawing_Short, "trailing stop line Short", colorRed, style = styleDashed, minvalue = 0, maxvalue = 0, XShift = Shift_X_Real, Zorder = 0, width = 4 ) ; 
Plot( targetline_drawing_Short, "profit target line Short", colorGreen, style = styleDashed, minvalue = 0, maxvalue = 0, XShift = Shift_X_Real, Zorder = 0, width = 4 ) ;


// assign stop amount conditionally by checking if there is a Buy signal on given bar 
IsLong = Ref(Buy, -TradeDelay); 
stopAmount = IIf( IsLong, StopAmount_long, StopAmount_short ); 
IsShort = Ref(Short, -TradeDelay) ;
stopAmount = IIf( IsShort, StopAmount_short, StopAmount_long ); 


PlotShapes( IIf(Buy, shapeCircle, shapeNone),colorBrightGreen, 0, BuyPrice, 0 );
PlotShapes( IIf(Sell, shapeCircle, shapeNone),colorRed, 0, SellPrice, 0 );
PlotShapes( IIf(Short, shapeCircle, shapeNone),colorRed, 0, ShortPrice, 0 );
PlotShapes( IIf(Cover, shapeCircle, shapeNone),colorBrightGreen, 0, CoverPrice, 0 );


PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
PlotShapes(Short*shapeDownArrow,colorRed,0,High);
PlotShapes(Cover*shapeUpArrow,colorGreen,0,Low);

Plot( Close,"Price",colorBlack,styleBar);


Risk = Param("Risk (absolut) %" , 0.5, 0.1, 10, 0.1, sincr = 0) ; // Risk in % 
U = Risk/100 ; // umgewandelt in Q
InitialEquity = Param("Initial Equity" , 100000, 1000, 10000000, 1000) ;
Z = U * InitialEquity ; // relatives --> absolutes Risiko
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetOption("CommissionMode" , 3 ) ;
SetOption( "CommissionAmount" , ComPerHalfturn ) ;
size = Z/(stopAmount*(TickValue/TickSize)) ;  // Z = size*stopAmount
SetPositionSize( size, spsShares ); // number of shares/contracts per trade
	
PointValue = TickValue / TickSize ;


wo = TimeFrameGetPrice( "O", inWeekly, 0, expandPoint ); 
wh = TimeFrameGetPrice( "H", inWeekly, 0, expandPoint ); 
wl = TimeFrameGetPrice( "L", inWeekly, 0, expandPoint ); 
wc = TimeFrameGetPrice( "C", inWeekly, 0, expandPoint ); 

PlotOHLC( wo, wh, wl, wc, "Weekly Close", IIf(wc > wo, colorGreen, colorRed), styleBar, Null, Null, 0, 0, -90 /* line width as percent of bar */); 
// PlotOHLC( O, H, L, C, "D1 Close", colorGreen, styleCandle );


In my (simple) Backtest the entry is everytime 1 Bar after the "trap" .

https://www.google.de/imgres?imgurl=http%3A%2F%2Fi1.wp.com%2Ftradingcoach.co.in%2Fwp-content%2Fuploads%2F2016%2F10%2FHikake-Pattern-e1477039395707.png&imgrefurl=http%3A%2F%2Ftradingcoach.co.in%2F3-price-action-trading-strategies-trapped-traders%2F&docid=5PY5_0tMbOD7pM&tbnid=7SaWTCisBLtWfM%3A&vet=10ahUKEwinrd3k44fbAhWnPZoKHdy5CxMQMwg5KAEwAQ..i&w=647&h=405&bih=589&biw=1263&q=hikake&ved=0ahUKEwinrd3k44fbAhWnPZoKHdy5CxMQMwg5KAEwAQ&iact=mrc&uact=8

I see a lot of code there, but no Filter variable which tells me you haven't done an Exploration. I suggest that you try that technique to better understand why your signals are not occurring when you expect them to.

You should also review this post from @Tomasz if you have not already done so: How do I debug my formula?

2 Likes