Sell intraday when current bar H makes a new intraday high

Hello,

AFL beginner here.

I am trying to test a following strategy.

note. on exit comment there's a typo, I meant between 9:30AM and 4PM.

enter, when stock pulls back from intraday high to VWAP between 9:30AM and 4:00PM, if L touches VWAP, enter at bar close.

exit, when current high is higher that previous high(within same session) or makes a new session high next day between 9:30AM and 4:00PM EST.

Problem is I don't know how to define my high, and then sequentially I don't know how to define my exits.

The following code returned zero results.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SetOption("initialequity", 250000);
SetPositionSize(200,spsShares);
SetTradeDelays(0,0,0,0);
SetChartBkColor(colorwhite); // color of outer border
SetChartBkGradientFill(colorlightorange,colorPaleGreen,colorBlack); // color of inner panel
Plot( C, "Portfolio Equity", ColorBlend( colorBrightGreen, colorBlack ), styleGradient | styleLine, Null, Null, 0, -1 );
SetOption("AllowSameBarExit",true);
SetOption("holdminbars",0);

Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * V, Bars_so_far_today ) / TodayVolume,0);
Plot (VWAP,"VWAP",colorOrange, styleThick);

StartTime = 93000;
Endtime = 160000;

tn = TimeNum();
tn_range = tn >= StartTime AND tn <= EndTime;
timeOK = tn_range;
new_range = tn_range != Ref(tn_range, -1 );
Highestsinceopen = ValueWhen(tn_range, HighestSince(new_range, High));

condition2 = Highestsinceopen > BuyPrice;

condition = C < Ref(C,-1) AND Ref(C,-1) < Ref(C,-2);
vwapfilter = Ref(C,-1) > VWAP AND Ref(C,-2) > VWAP;

Buy = Cross(VWAP,L) AND condition AND timeOK AND condition2 AND vwapfilter;
BuyPrice = Close;
sell = H > Highestsinceopen;
SellPrice = Close;

buy = exRem(buy,sell);
sell = exRem(sell,buy);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Any suggestions?

SetOption("initialequity", 250000);
SetPositionSize(200,spsShares);
SetTradeDelays(0,0,0,0);
SetChartBkColor(colorwhite); // color of outer border
SetChartBkGradientFill(colorlightorange,colorPaleGreen,colorBlack); // color of inner panel
Plot( C, "Portfolio Equity", ColorBlend( colorBrightGreen, colorBlack ), styleGradient | styleLine, Null, Null, 0, -1 );
SetOption("AllowSameBarExit",true);
SetOption("holdminbars",0);

Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * V, Bars_so_far_today ) / TodayVolume,0);
Plot (VWAP,"VWAP",colorOrange, styleThick);

StartTime = 093000;
Endtime = 160000;

tn = TimeNum();
tn_range = tn >= StartTime AND tn <= EndTime;
timeOK = tn_range;
new_range = tn_range != Ref(tn_range, -1 );
highvalue = ValueWhen(tn_range,H);
sessionhigh = HighestSince(new_range, High);

condition2 = highvalue OR sessionhigh;
condition3 = VWAP < condition2;

condition = C < Ref(C,-1) AND Ref(C,-1) < Ref(C,-2);
vwapfilter = Ref(C,-1) > VWAP AND Ref(C,-2) > VWAP;

Buy = L < VWAP AND condition AND condition3 AND vwapfilter AND timeOK;
BuyPrice = Close;
sell = condition2;
SellPrice = Close;

buy = exRem(buy,sell);
sell = exRem(sell,buy);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Still no results

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

SetOption("initialequity", 250000);
SetPositionSize(200, spsShares);
SetChartBkColor(colorWhite);
SetChartBkGradientFill(colorLightOrange, colorPaleGreen, colorBlack);
Plot(C, "Portfolio Equity", ColorBlend(colorBrightGreen, colorBlack), styleGradient | styleLine, Null, Null, 0, -1);
SetOption("AllowSameBarExit", true);
SetOption("holdminbars", 0);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// VWAP

Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * V, Bars_so_far_today  ) / TodayVolume,0);
Plot (VWAP,"VWAP",colorOrange, styleThick);

vwapcross = L < VWAP;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

startTime = 093000;  // Start time in HHMMSS format
endTime = 160000;   // End time in HHMMSS format
timeOK = TimeNum() >= startTime AND TimeNum() <= endTime;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

condition = C < Ref(C,-1) AND Ref(C,-1) < Ref(C,-2); // close is less than close 1 bar ago for 3 consecutive bars
vwapfilter = Ref(C,-1) > VWAP AND Ref(C,-2) > VWAP; // close one bar ago was higher than VWAP, close 2 bars ago was higher than close one bar ago
vwapcross = L < VWAP; // current bar low crosses VWAP

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Bar index

nine30bar = ValueWhen(TimeNum() == 093000, BarIndex());
nine45bar = ValueWhen(TimeNum() == 093000, BarIndex());
ten0bar = ValueWhen(TimeNum() == 100000, BarIndex());
ten15bar = ValueWhen(TimeNum() == 101500, BarIndex());
ten30bar = ValueWhen(TimeNum() == 103000, BarIndex());
ten45bar = ValueWhen(TimeNum() == 104500, BarIndex());
eleven0bar = ValueWhen(TimeNum() == 110000, BarIndex());
trendday = ((nine30bar < nine45bar) AND (nine45bar < ten0bar) AND (ten0bar < ten15bar) OR (ten0bar < ten30bar) OR (ten0bar < ten45bar) OR (ten0bar < eleven0bar));

TimeFrameSet(inDaily);
ten_till_eleven_high = HighestSince(TimeNum() >= 100000 AND TimeNum() <= 110000, H);
nine_30_till_10_high = HighestSince(TimeNum() >= 093000 AND TimeNum() <= 100000, H);
sessionhigh = ten_till_eleven_high >= nine_30_till_10_high;
TimeFrameRestore();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Define the entry rule
entryCondition = (TimeNum() >= 100000 AND TimeNum() <= 110000 AND (L <= VWAP) AND (Ref(TimeNum(), -1) >= 100000) AND (Ref(TimeNum(), -1) <= 110000));

// Define the exit rule
exitCondition = H >= sessionhigh;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

buy = trendday AND entryCondition;
buyPrice = Close;
sell = exitCondition;
sellPrice = Close;

buy = ExRem(buy, sell);
sell = ExRem(sell, buy);

A few adjustments entry selection.

Stock/ETF has to have morning rally. i.e. 945am bar is higher than 930am bar, 10am bar is higher than 945am bar.

Initial intraday high occurs between 10-11AM, then sequentially VWAP touch. Exit when H > current session high.

but still I don't know how to define my exit variable.

code above returned zero results

actually hold,

got some results.

but the logic seems to be off...

SetOption("initialequity", 250000);
SetPositionSize(200, spsShares);
SetChartBkColor(colorWhite);
SetChartBkGradientFill(colorLightOrange, colorPaleGreen, colorBlack);
Plot(C, "Portfolio Equity", ColorBlend(colorBrightGreen, colorBlack), styleGradient | styleLine, Null, Null, 0, -1);
SetOption("AllowSameBarExit", true);
SetOption("holdminbars", 1);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// VWAP

Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * V, Bars_so_far_today  ) / TodayVolume,0);
Plot (VWAP,"VWAP",colorOrange, styleThick);

vwapcross = L < VWAP;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

startTime = 093000;  // Start time in HHMMSS format
endTime = 160000;   // End time in HHMMSS format
timeOK = TimeNum() >= startTime AND TimeNum() <= endTime;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

condition = C < Ref(C,-1) AND Ref(C,-1) < Ref(C,-2); // close is less than close 1 bar ago for 3 consecutive bars
vwapfilter = Ref(C,-1) > VWAP AND Ref(C,-2) > VWAP; // close one bar ago was higher than VWAP, close 2 bars ago was higher than close one bar ago
vwapcross = L < VWAP; // current bar low crosses VWAP

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Bar index

nine30bar = ValueWhen(TimeNum() == 093000, BarIndex());
nine45bar = ValueWhen(TimeNum() == 093000, BarIndex());
ten0bar = ValueWhen(TimeNum() == 100000, BarIndex());
ten15bar = ValueWhen(TimeNum() == 101500, BarIndex());
ten30bar = ValueWhen(TimeNum() == 103000, BarIndex());
ten45bar = ValueWhen(TimeNum() == 104500, BarIndex());
eleven0bar = ValueWhen(TimeNum() == 110000, BarIndex());
trendday = ((nine30bar < nine45bar) AND (nine45bar < ten0bar) AND (ten0bar < ten15bar) OR (ten0bar < ten30bar) OR (ten0bar < ten45bar) OR (ten0bar < eleven0bar));

TimeFrameSet(in15Minute);
ten_till_eleven_high = HighestSince(TimeNum() >= 100000 AND TimeNum() <= 110000, H);
nine_30_till_10_high = HighestSince(TimeNum() >= 093000 AND TimeNum() <= 100000, H);
sessionhigh = ten_till_eleven_high >= nine_30_till_10_high;
TimeFrameRestore();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Define the entry rule
entryCondition = (TimeNum() >= 100000 AND TimeNum() <= 110000 AND (L <= VWAP) AND (Ref(TimeNum(), -1) >= 100000) AND (Ref(TimeNum(), -1) <= 110000));

// Define the exit rule
exitCondition = H >= sessionhigh;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

buy = trendday AND entryCondition;
buyPrice = Close;
sell = exitCondition;
sellPrice = Close;

buy = ExRem(buy, sell);
sell = ExRem(sell, buy);

$APPL should have been held till it makes a new intraday high.

I will close this one too many changing parts. will make a new one.

If you mean create a new thread then don't do that - it's against forum rules (for obvious reasons)!

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