Cover not consistently following conditions - Premarket Range Break

I have here an AFL code for short position only.

Basically I identified the Highest and Lowest prices the Premarket to box the premarket H/L.
My goal for it to:

Cover = take profit - when the price reaches 1R (When point drop from Premarket Low == to the Range of Premarket H/L).

Cover = Stop Loss - when the price goes back to premarket High.

Here's the code for that.

Plot( Close, "Close", colorDefault, styleCandle );  

tm = TimeNum()/10000; 

//SESSION BREAKS  

PreMarket = (tm >= 4.00 AND tm < 9.29);  
AfterHours = (tm > 16.00 AND tm <= 20.00);
Plot( PreMarket, "", ColorRGB(255, 242, 230), styleArea | styleOwnScale, 0, 1,0,-1 );  
Plot( AfterHours, "", ColorRGB(230, 255, 255), styleArea | styleOwnScale, 0, 1,0,-1 );  


//PREMARKET HIGH AND LOW
StartTime = 4.00;  
Endtime = 9.25;
EndBar = tm == Endtime; 
printf("EndBar = %s\n", NumToStr(EndBar));

bspmo = BarsSince(tm==StartTime); 
 

// on the end bar we read the value of highest high or lowest low since the start bar

//W2W
pmHigh = ValueWhen( EndBar, HHV(H, bspmo), 1); // High of Premarket  
pmLow = ValueWhen( EndBar, LLV(L, bspmo), 1); // Low of Premarket

//B2B
isGreen = C > O;
isRed = C < O;

pmHb2b = IIf(isGreen,C,O);
pmLb2b = IIf(isRed,C,O);

pmHighb2b = ValueWhen( EndBar, HHV(pmHb2b, bspmo), 0); // High of Premarket (body to  body) 
pmLowb2b = ValueWhen( EndBar, LLV(pmLb2b, bspmo), 0); // Low of Premarket (body to body)
pmDB50 = pmHighb2b - ((pmHighb2b - pmLowb2b)/2);

printf("B2B - High = %s\n", NumToStr(pmHighb2b));  
printf("B2B - Low = %s\n", NumToStr(pmLowb2b));
printf("DB50 = %s\n", NumToStr(PMdb50));


//1R of PM
Rpercent = ((pmHighb2b - pmLowb2b) / pmHighb2b) *100;
Rpoints = pmHighb2b - pmLowb2b;
 
printf("R Percent = %s\n", NumToStr(Rpercent));
printf("R Points = %s\n", NumToStr(Rpoints));
printf("Time = %s\n", NumToStr(tm));

isPCL = C < Ref(L, -1) AND isRed;  
isPCH = C > Ref(H, -1) AND isGreen;


//After Entry

tStart = Param("trade start", 9.3, 9.3, 16, 0.05);  
tEnd   = 11.00;  
  
MarketOpen = tm == tStart;  
  
wnTime = tm >= tStart AND  
		 tm <= tEnd;


R1 = abs(pmLowb2b - C) >= Rpoints AND C < pmLowb2b AND wnTime;
StopHit = Cross(C,pmHighb2b)  AND wnTime;
printf("1R = %s\n", NumToStr(R1));
printf("Stop Hit = %s\n", NumToStr(StopHit));
printf("Within Time = %s\n", NumToStr(wntime));

Minprice = Param("Minimum Stock Price", 100, 50, 199, 10);
MaxPrice =  Param("Maximum Stock Price", 200,200, 299, 10);

priceRange = (C >= Minprice) AND (C <= MaxPrice); 


Short = (tm == 9.20) AND isPCL AND priceRange AND C < pmDB50;	  		  
Cover = R1 OR StopHit;

Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);

image

There are times when Cover is triggered even if the price has not yet reached the Premarket High.

Another problem is the trade should only happen between 9:20 -11:00 but there are times when cover is trigerred the next day.

Hope you can help me find my way.

Thanks in advance.

Wondering if the problem occurs if premarket doesn't start at exactly 4:00. But I can't find a way to rule it out.

Depending on data source, and timestamping settings in the preferences, the bar time might be 4:00:00 but it can also be 3:59:59.999.

It is better to use ranges not single values, such as:

EndBar = TimeNum() >= 92500 AND TimeNum() <= 92559;
1 Like

Thanks @Tomasz. I applied that to add a bit of accuracy on the time. currently my problem is not resolved.

I'll try my best to explain in details in this post.

So the afl I created determines the High and Low of the premarket. Which includes the time on which the High and the Low should be established.
in this case 4:00 to 9:25. as shown in the code below

Plot( Close, "Close", colorDefault, styleCandle );  

tm = TimeNum()/10000; 

//SESSION BREAKS  

PreMarket = (tm >= 4.00 AND tm < 9.29);  
AfterHours = (tm > 16.00 AND tm <= 20.00);
Plot( PreMarket, "", ColorRGB(255, 242, 230), styleArea | styleOwnScale, 0, 1,0,-1 );  
Plot( AfterHours, "", ColorRGB(230, 255, 255), styleArea | styleOwnScale, 0, 1,0,-1 );  

//PREMARKET HIGH AND LOW
StartTime = 4.00;  	//Start of Premarket
Endtime = 9.25;		// Time of Entry
EndBar = tm >= 9.25 AND tm<=9.2559; 
printf("EndBar = %s\n", NumToStr(EndBar));

And then here's the code that Determines the High, Low and Mid of the premarket during those times

//Premarket Measurement - Body to Body
bspmo = BarsSince(tm==StartTime); // Number of Bars since Premarket Open
isGreen = C > O;  //Green Candle
isRed = C < O;	// Red Candle

pmHb2b = IIf(isGreen,C,O); //Highest candle body on premarket
pmLb2b = IIf(isRed,C,O);	// Lowest candle body on premarket

pmHighb2b = ValueWhen( EndBar, HHV(pmHb2b, bspmo), 0); // High of Premarket (body to body) 
pmLowb2b = ValueWhen( EndBar, LLV(pmLb2b, bspmo), 0); // Low of Premarket (body to body)
pmDB50 = pmHighb2b - ((pmHighb2b - pmLowb2b)/2); //Mid of Premarket

Then for the entry - it will be triggered when

  1. Price breaks previous candle's low &&
  2. Candle is red &&
  3. time is 9:20 &&
  4. Price closes below Mid of Premarket
//Entry Trigger
isPCL = C < Ref(L, -1) AND //Previous Candle Low
		isRed AND //Red
		tm == 9.20 AND //Time == 9:20
		C < pmDB50;  //Price is Below mid of Premarket

After entry Take Profits or Stop Loss should only be triggered within the time given (that is from 9:30 to 11:00 only)

tStart = 9.30; //Set Market open Time
tEnd   = 11.00;  
  
MarketOpen = tm == tStart;  
  
wnTime = tm >= tStart AND  
		 tm <= tEnd;

Take profit (Cover) Trigger is when the price drop from premarket low == to the Height of Premarket Range - High to Low

//1R of PM --> Premarket Height in Points and in Percentage
Rpercent = ((pmHighb2b - pmLowb2b) / pmHighb2b) *100;
Rpoints = pmHighb2b - pmLowb2b;

R1 = C <  pmLowb2b AND (abs(pmLowb2b - C) >= Rpoints) AND wnTime;

And then stop hit is triggered when the high of premarket is hit

StopHit = Cross(C,pmHighb2b) AND wnTime;

Here's a sample of the backtest for MSFT on JANUARY 13, 2020

image
image

Now the problem is that Neither the Stop loss nor Take profit area Is hit but the Cover trade was already triggered.

As for the trades for backtesting here's the current settings
image

Hope you can help me out. Thanks always.
And my apologies for the long post. :smiley:

Is there a way to know which of my triggers were initialized?
I really still can't figure out what's going on.

My only exit/cover triggers are when

  1. Time stop is Hit - That is 11:00
  2. Price reveses back to premarket High
  3. Profit Target is Hit - That's 1R

None of the above triggers were hit but it still it gets the rade already exits.
image

Hope you can help me out. :smiling_face_with_tear:
Thanks in advance.

Just figured it out. The problem is when I was determining the HHV of premarket I counted the bars since premarket open at 4:00 not realizing that does not necessarily open exactly at 4:00.
:joy:

1 Like

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