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);
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.