Is This Looking Into The Future?

Hi,

I have a following strategy using 15min Bars, to buy at market open 9:30AM, if Open is below VWAP, sell, if close is higher that VWAP, stop loss 0.35%.

Here's the code

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

//Define starting capital and position size
SetOption("initialequity", 100000);
SetPositionSize(300,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("MaxOpenPositions", 3);
SetOption("holdminbars",1);

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

// Historical Volatility 
lenPeriod = Param("Periods",100,10,1000000,1);

HV = 100 * StDev(log(C/Ref(C,-1)),lenPeriod) * sqrt(252);
Plot(HV, "HV("+lenPeriod+")", ParamColor( "Color", colorRed ), ParamStyle("Style")  );

TimeFrameSet(inDaily);
dailyMA 		= MA(C,20);
trendfilter		= C > dailyMA;
dailyRSI		= RSI(14);
yestL			= Ref(L,-1);
yestH4			= Ref(H,-1);
todayO			= O;
yestC 			= Ref(C,-1);
dailyRSI		= RSI(4);
dailyHV			= HV;
dailyATR		= ATR(14);
sessionclose 	= Ref(C,-1) AND TimeNum() == 160000;
TimeFrameRestore();
dailyMA			= TimeFrameExpand(dailyMA, inDaily, expandFirst);
dailyRSI		= TimeFrameExpand(dailyRSI, inDaily, expandFirst);
trendfilter		= TimeFrameExpand(trendfilter, inDaily, expandFirst);
yestL			= TimeFrameExpand(yestL, inDaily, expandFirst);
todayO			= TimeFrameExpand(todayO, inDaily, expandFirst);
yestH4			= TimeFrameExpand(yestH4, inDaily, expandFirst);
dailyRSI		= TimeFrameExpand(dailyRSI, inDaily, expandFirst);
yestC			= TimeFrameExpand(yestC , inDaily, expandFirst);
dailyHV			= TimeFrameExpand(dailyHV , inDaily, expandFirst);
dailyATR		= TimeFrameExpand(dailyATR, inDaily, expandFirst);
sessionclose	= TimeFrameExpand(sessionclose, inDaily, expandFirst);

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

Bars_so_far_today = 1 + BarsSince(Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V, Bars_so_far_today);
VWAP_OHLC = Sum((O + H + L + C) * V, Bars_so_far_today) / (4 * TodayVolume);
Plot(VWAP_OHLC, "VWAP_OHLC", colorBlueGrey, styleThick);

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

PositionScore = Random();

StartTime 	= 093000;
Endtime 	= 094459;

tn = TimeNum();
tn_range = tn >= StartTime AND tn <= EndTime;
timeOK = tn_range;

Buy = O < VWAP_OHLC AND HV < 4 AND TimeNum() == 093000;
BuyPrice = Open;
Sell = C > VWAP_OHLC;
SellPrice = Close;

Buy = ExRem(Buy,Sell);

amount = 0.35; // 0.35% loss
ApplyStop( stopTypeLoss, stopModePercent, amount, True );

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

Filter = C < VWAP_OHLC AND C < yestC AND HV < 4 AND TimeNum() == 091500;

AddColumn (C, "Last Close");
AddColumn (VWAP_OHLC, "VWAP Price");

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

and results

And then, I have the same strategy with same rules, only I buy if 9:15AM bar close is below VWAP

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

//Define starting capital and position size
SetOption("initialequity", 100000);
SetPositionSize(300,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("MaxOpenPositions", 3);
SetOption("holdminbars",1);

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

// Historical Volatility 
lenPeriod = Param("Periods",100,10,1000000,1);

HV = 100 * StDev(log(C/Ref(C,-1)),lenPeriod) * sqrt(252);
Plot(HV, "HV("+lenPeriod+")", ParamColor( "Color", colorRed ), ParamStyle("Style")  );

TimeFrameSet(inDaily);
dailyMA 		= MA(C,20);
trendfilter		= C > dailyMA;
dailyRSI		= RSI(14);
yestL			= Ref(L,-1);
yestH4			= Ref(H,-1);
todayO			= O;
yestC 			= Ref(C,-1);
dailyRSI		= RSI(4);
dailyHV			= HV;
dailyATR		= ATR(14);
sessionclose 	= Ref(C,-1) AND TimeNum() == 160000;
TimeFrameRestore();
dailyMA			= TimeFrameExpand(dailyMA, inDaily, expandFirst);
dailyRSI		= TimeFrameExpand(dailyRSI, inDaily, expandFirst);
trendfilter		= TimeFrameExpand(trendfilter, inDaily, expandFirst);
yestL			= TimeFrameExpand(yestL, inDaily, expandFirst);
todayO			= TimeFrameExpand(todayO, inDaily, expandFirst);
yestH4			= TimeFrameExpand(yestH4, inDaily, expandFirst);
dailyRSI		= TimeFrameExpand(dailyRSI, inDaily, expandFirst);
yestC			= TimeFrameExpand(yestC , inDaily, expandFirst);
dailyHV			= TimeFrameExpand(dailyHV , inDaily, expandFirst);
dailyATR		= TimeFrameExpand(dailyATR, inDaily, expandFirst);
sessionclose	= TimeFrameExpand(sessionclose, inDaily, expandFirst);

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

Bars_so_far_today = 1 + BarsSince(Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 093000, BarIndex());
TodayVolume = Sum(V, Bars_so_far_today);
VWAP_OHLC = Sum((O + H + L + C) * V, Bars_so_far_today) / (4 * TodayVolume);
Plot(VWAP_OHLC, "VWAP_OHLC", colorBlueGrey, styleThick);

StartTime 	= 091500;
Endtime 	= 092959;

tn = TimeNum();
tn_range = tn >= StartTime AND tn <= EndTime;
timeOK = tn_range;

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

PositionScore = Random();

Buy = C < VWAP_OHLC AND HV < 4 AND timeOK;
BuyPrice = Close;
Sell = C > VWAP_OHLC;
SellPrice = Close;

Buy = ExRem(Buy,Sell);

amount = 0.35; // 0.35% loss
ApplyStop( stopTypeLoss, stopModePercent, amount, True );

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

Filter = C < VWAP_OHLC AND C < yestC AND HV < 4 AND TimeNum() == 091500;

AddColumn (C, "Last Close");
AddColumn (VWAP_OHLC, "VWAP Price");

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

and results

If the first strategy works that much better, I am I looking into future (I did code check, and had zero future quotes)?

2nd question, since VWAP value is dynamic throughout the day, when backtest is performed, it does consider VWAP value based on last bar, correct?

Thanks

[ Is This Looking Into The Future? ]

Yes

Change to ->

Buy = O < Ref(VWAP_OHLC,-1) AND Ref(HV < 4, -1) AND TimeNum() == 093000;
BuyPrice = Open;

BTW, your TimeFrame... code lines got nothing to do with your Buy/Sell rules.
Simply remove them if they have no usage.

Also remove ExrRem in Backtester.

1 Like

In principle when you are using expandFirst in TimeFrameExpand() you are looking into the future, if you are using anything other than Open.

Alright,

Thank you for your insight.

1 Like

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