AFL intraday code help needed VWAP pullback

Hello,

I need to a help with my code.

rules are as follows:

stock selection

First three bars (at the session open, 930AM 945AM & 10AM EST) are sequentially higher than the
Previous bar.

H is higher than H one bar ago, between 930AM and 10AM.|
L is higher than L one bar ago, between 930AM and 10AM.|
C > O is higher for each bar
C > VWAP

Session initial high is established between 10-11AM EST
Buy on pullback once current bar low crosses VWAP below, buy on bar close.

Sell, when current bar C is higher than initial high or when C is less than 930AM bar's Open price (Stop-loss)

I managed to get my stock filters right on 9:30-10AM, but when it comes to entry

I had entry and exit at the same time.

any suggestions?

Here's the code

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////START

//Define starting capital and position size

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

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

// Define the time range
StartTime = 93000; // 9:30 AM
EndTime = 100000; // 10:00 AM

// Get the time of each bar
BarTime = TimeNum();

// Get the high and low values of each bar
H = High;
L = Low;

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

// Check if the second bar's high and low values are higher than the first bar's within the time range
SecondBarHigh = Ref(H, -1) > Ref(H, -2) AND Ref(BarTime, -1) >= StartTime AND Ref(BarTime, -1) <= EndTime;
SecondBarLow = Ref(L, -1) > Ref(L, -2) AND Ref(BarTime, -1) >= StartTime AND Ref(BarTime, -1) <= EndTime;

// Check if the third bar's high and low values are higher than the second bar's within the time range
ThirdBarHigh = H > Ref(H, -1) AND BarTime >= StartTime AND BarTime <= EndTime;
ThirdBarLow = L > Ref(L, -1) AND BarTime >= StartTime AND BarTime <= EndTime;

// Check if each bar's C > O within the time range

Firstbar = Ref(C,-2) > Ref(O,-2) AND BarTime >= StartTime AND BarTime <= EndTime;
SecondBar = Ref(C,-1) > Ref(O,-1) AND BarTime >= StartTime AND BarTime <= EndTime;
ThirdBar = C > O AND BarTime >= StartTime AND BarTime <= EndTime;

// Check session bar open price

Firstbaropen = Ref(O,-2) AND BarTime >= StartTime AND BarTime <= EndTime;

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

// VWAP

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

// Check if each bar's C is above VWAP

Firstbarvwap = Ref(C,-2) > VWAP AND BarTime >= StartTime AND BarTime <= EndTime;
Secondbarvwap = Ref(C,-1) > VWAP AND BarTime >= StartTime AND BarTime <= EndTime;
Thirdbarvwap = C > VWAP AND BarTime >= StartTime AND BarTime <= EndTime;

// Combine the conditions using the AND operator
Condition = SecondBarHigh AND SecondBarLow AND ThirdBarHigh AND ThirdBarLow AND Firstbar AND SecondBar AND ThirdBar AND Firstbarvwap AND Secondbarvwap AND Thirdbarvwap;

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

// Entry section

// Stock establishes initial session high between 10-11AM
sessionHigh = HighestSince(TimeNum() >= 100000 AND TimeNum() <= 110000, H);

// Entry conditions
entryFilter = TimeNum() >= 100000 AND TimeNum() <= 110000 AND Cross(VWAP, L) AND L > sessionHigh;

// Buy condition
Condition2 = sessionHigh + entryFilter;

Buy = Condition AND Condition2;
BuyPrice = Close;


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

// Exit section

// Sell if current bar's close is higher than the initial session high between 10-11AM
sessionHigh = HighestSince(TimeNum() >= 100000 AND TimeNum() <= 110000, H);
Profit = C > sessionHigh;

// Stoploss rules, sell if close is less that 9:30AM Open price
Firstbaropen = Ref(O,-2) AND BarTime >= StartTime AND BarTime <= EndTime;
Stoploss = C < Firstbaropen;

Sell = Profit OR Stoploss;
SellPrice = Close;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Buy,Sell);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////END
1 Like

This line is in your code twice but is only needed once. It's also the culprit, because it only returns the value of 1:
Firstbaropen = Ref(O,-2) AND BarTime >= StartTime AND BarTime <= EndTime;

It should be:

Firstbaropen = ValueWhen( TimeNum() == StartTime, O, 1 );

Also do yourself a favor and plot your variables so you can troubleshoot, because this is how it jumped out at me:

Plot(Firstbaropen,"Firstbaropen",colorBlue, styleThick);

Also for VWAP this is a better way:

VWAP = SafeDivide( Sum (C * V, Bars_so_far_today ), TodayVolume );

I didn't look at the rest of your code but that should get you started.

Cheers!

@nkrastins, just another small tip.

There is no need to do that assignment for the price array identifiers:

H is the abbreviation of High and L is the same for Low, like C stands for Close, O for Open, V for Volume and OI for OpenInt.

This is clearly documented here in section "Identifiers".

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