Backtesting (ApplyStop) - Buy Signal in Intraday but Stop ATR from Daily

Hi Seniors -

My intent is to generate the Buy Signal on First 5 Min data and Apply the Stop based on ATR (14) of Daily Close once the Buy is Triggered.

Even after reading all docs, I am stuck in my understating as to why I am I not able to achieve my objective.

Issues -

1 - Not sure what is going wrong with my ApplyStop() implementation, I want to apply stop when the price goes below the 14 Day Daily ATR (even though my base interval is 5 Min), but its Just not working.

2 - I am not able to Visualize the StopLoss correctly.

3 - How to ensure to not generate a new Buy Signal if the Existing one is already live (Means has not hit any stop loss yet) ?

SetBarsRequired(100,0);

bi = Barindex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );


open_htf = TimeFrameGetPrice("O", inDaily, 0, expandFirst);
high_htf = TimeFrameGetPrice("H", inDaily, 0, expandFirst); 
low_htf = TimeFrameGetPrice("L", inDaily, 0, expandFirst);
close_htf = TimeFrameGetPrice("C", inDaily, 0, expandFirst);
vol_htf = TimeFrameGetPrice("V", inDaily, 0, expandFirst);









up_bar = (open_htf <= close_htf);
down_bar = NOT up_bar;

dn = Day();
first_bar_cd = dn != Ref( dn, -1 );


_SECTION_BEGIN("Plot Price Chart");


condcol= IIf( up_bar, colorBrightGreen,  
		IIf( up_bar, colorBrightGreen,  
		IIf( down_bar,colorRed ,
		IIf(  down_bar, ColorRGB(234, 136, 123), //Light Red
		
		
		colorWhite
		) ) )

		)
		
		;


SetBarFillColor( condcol); // Color for Interiors of the CandleStick
Plot( C, "Price", condcol, styleCandle  | styleNoLabel  ); // Color of Outline of Candlestick

_SECTION_END();


true_range = (high_htf - low_htf);
body_range = abs( close_htf - open_htf );

body_fill =  body_range / true_range * 100;






lower_wick_range = (Min(close_htf, open_htf) - low_htf);

lower_wick_gap = lower_wick_range / true_range * 100;

upper_wick_range = (high_htf - max(close_htf, open_htf));

upper_wick_gap = upper_wick_range / true_range * 100;


close_htf_prev = ValueWhen(first_bar_cd, close_htf, 2); 
gap_open = ( open_htf - close_htf_prev) / close_htf_prev * 100;

starting_price_prev = Min(open_htf, close_htf);

starting_price_prev_prev = ValueWhen(first_bar_cd, starting_price_prev, 2); 
open_gapup = open_htf >= (starting_price_prev_prev * 0.995 );


close_htf_prev = ValueWhen(first_bar_cd, close_htf, 2); 

c_on_prev_close = ( close_htf - close_htf_prev) / close_htf_prev * 100;
c_on_curr_low = (close_htf - low_htf) / low_htf * 100;

true_range_prev = ValueWhen(first_bar_cd, true_range, 2); 
low_htf_prev = ValueWhen(first_bar_cd, low_htf, 2); 
c_on_prev_true_range = (close_htf - low_htf_prev) / true_range_prev * 100;




curr_bar_aroc = abs(close_htf - open_htf) / min(open_htf, close_htf) * 100; //ROC of an indivisual bar


v_comp = TimeFrameCompress(V, inDaily, compressVolume); 

v_ma_50_comp = MA(v_comp, 50);
v_ma_50_prev_comp = Ref(v_ma_50_comp, -1);
v_ma_50_prev = TimeFrameExpand(v_ma_50_prev_comp, inDaily, expandFirst);


v_ma_20_comp = MA(v_comp, 20);
v_ma_20_prev_comp = Ref(v_ma_20_comp, -1);
v_ma_20_prev = TimeFrameExpand(v_ma_20_prev_comp, inDaily, expandFirst);


rel_vol_20 = vol_htf / v_ma_20_prev * 100;
rel_vol_50 = vol_htf / v_ma_50_prev * 100;
rel_vol_20_prev = ValueWhen(first_bar_cd, rel_vol_20, 2); 






v_comp = TimeFrameCompress(V, inDaily, compressVolume); 

v_ma_50_comp = MA(v_comp, 50);
v_ma_50_prev_comp = Ref(v_ma_50_comp, -1);
v_ma_50_prev = TimeFrameExpand(v_ma_50_prev_comp, inDaily, expandFirst);


v_ma_20_comp = MA(v_comp, 20);
v_ma_20_prev_comp = Ref(v_ma_20_comp, -1);
v_ma_20_prev = TimeFrameExpand(v_ma_20_prev_comp, inDaily, expandFirst);


TimeFrameSet(inDaily); //Time Compressed
true_range_htf = (H - L);
body_range_htf = abs( C - O);

body_fill_htf =  body_range_htf / true_range_htf * 100;


body_fill_htf_ma_20_prev_comp = Ref( MA(body_fill_htf, 20), -1);
body_fill_htf_ma_20_prev = TimeFrameExpand(body_fill_htf_ma_20_prev_comp, inDaily, expandFirst);

body_fill_htf_ma_50_prev_comp = Ref( MA(body_fill_htf, 50), -1);
body_fill_htf_ma_50_prev = TimeFrameExpand(body_fill_htf_ma_50_prev_comp, inDaily, expandFirst);

TimeFrameRestore();




rel_body_20 = body_fill / body_fill_htf_ma_20_prev * 100;
rel_body_50 = body_fill / body_fill_htf_ma_50_prev * 100;

rel_body_20_prev =  ValueWhen(first_bar_cd, rel_body_20, 2); 





//Debug on Title

//if(Interval() == inDaily)
{
Title = EncodeColor(colorWhite) + 
"Strong Open" 
+ "\n" + "--- gap_open %: " + NumToStr(gap_open,1.1)
+ "\n" + "--- open_gapup : " + NumToStr(open_gapup,1) 
+ "\n" + "\n"

+ "Strength"
 + "\n" + "--- upper_wick_gap %: " + NumToStr(upper_wick_gap,1)
+ "\n" + "--- body_fill %: " + NumToStr(body_fill,1)
+ "\n" + "--- lower_wick_gap %: " + NumToStr(lower_wick_gap,1)
//+ "\n" + "--- ppv_up_bar %: " + NumToStr(ppv_up_bar,1) 
+ "\n" + "\n"

+ "Urgency" 
/*
+ "\n" + "--- rel_vol_1 %: " + NumToStr(rel_vol_1,1)
+ "\n" + "--- rel_body_1 %: " + NumToStr(rel_body_1,1)
+ "\n"
*/
+ "\n" + "--- rel_vol_20 %: " + NumToStr(rel_vol_20,1)
+ "\n" + "--- rel_body_20 %: " + NumToStr(rel_body_20,1)

/*
+ "\n" + "--- rel_vol_50 %: " + NumToStr(rel_vol_50,1)
+ "\n" + "--- rel_body_50 %: " + NumToStr(rel_body_50,1)
*/
+ "\n" + "\n"


+ "Contraction" 
+ "\n" + "--- rel_vol_20_prev %: " + NumToStr(rel_vol_20_prev,1)
+ "\n" + "--- rel_body_20_prev %: " + NumToStr(rel_body_20_prev,1)
+ "\n" + "\n"

+ "Risk" 
+ "\n" + "--- c_on_curr_low %: " + NumToStr(c_on_curr_low,1.1)
+ "\n" + "\n"


+ "Entry" 
+ "\n" + "--- c_on_prev_true_range %: " + NumToStr(c_on_prev_true_range,1)
+ "\n" + "\n"

+ "Misc" 
+ "\n" + "--- c_on_prev_close %: " + NumToStr(c_on_prev_close,1)
+ "\n" + "--- curr_bar_aroc %: " + NumToStr(curr_bar_aroc,1.2)
+ "\n" + "\n"

;

}
 













// Backtesting Starts


Buy = 
up_bar 
&& lower_wick_gap < 5 
&& body_fill > 50 
&& rel_vol_20 >= 7
&& rel_body_20 > 90
&& TimeNum() <= 091500
;




ATR_ma_length = Param("ATR MA", 14, 5, 50, 1 );
ATR_Multi_length = Param("ATR_Multi_length", 5, 5, 20, 1 );


c_comp = TimeFrameCompress(C, inDaily, compressLast);
c_comp_ma = MA(c_comp, ATR_ma_length);
c_expnd_ma = TimeFrameExpand(c_comp_ma, inDaily, expandFirst);

h_expand = TimeFrameGetPrice("H", inDaily, expandFirst);

BuyPrice = C;                        							
SellPrice = C;                        							

Sell = Short = Cover = 0;

PlotShapes(Buy*shapeUpTriangle, colorBrightGreen, 0, L, -80 );
PlotShapes(Sell*shapeDownTriangle,colorRed,0,High, 80);

// Plots Stops & Applies Stops

ATR_stop_point = ATR_Multi_length * c_expnd_ma;

SetTradeDelays(0,0,0,0);

ApplyStop( stopTypeTrailing, stopModePoint, ATR_stop_point, True );

Equity( 1, 0 ); // evaluate stops, all quotes

InTrade = Flip( Buy, Sell );

SetOption("EveryBarNullCheck", True );
ATR_stopline = IIf( InTrade, HighestSince( Buy, ATR_stop_point  ) , Null );



Plot( ATR_stopline, "ATR_stopline: ", colorGold , styleLine, 0, 0, 0, 0);


Sincerely Appreciate all Help.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.