2 Different Codes, Same Code Logic, But Different Results. Any ideas?

Hello,

I am backtesting an inside day strategy, where previous bar (D) low is higher than day before, and high is less than high day before, enter next morning open, if open is less than close day before, sell if close is higher than 2 days ago.

I used 2 different codes, but with same logic, one uses defined variables and one uses just O,H,L,C data. Somehow, I am getting two different results.

Data source: IQFeed
Testing period: Daily
Amibroker version: 64-Bit / 6.40 version

1st code:

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

//Define starting capital and position size
SetOption("initialequity", 25000);
SetPositionSize(50,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);

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

Buy = Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2) AND O < Ref(C,-1);
BuyPrice = Open;
Sell = C > Ref(H,-2);
SellPrice = Close;

results:

image

2nd code

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

//Define starting capital and position size
SetOption("initialequity", 25000);
SetPositionSize(50,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);

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

daybeforeH = Ref(H,-2);
daybeforeL = Ref(L,-2);

yesterdayH = Ref(H,-1);
yesterdayL = Ref(L,-1);
yesterdayC = Ref(C,-1);

InsideH = yesterdayH < daybeforeH;
InsideL = yesterdayL > daybeforeL;

insideday = insideH AND insideL;

Buy = (Ref(insideday,-1)) AND O < Ref(C,-1);
BuyPrice = Open;
Sell = C > Ref(H,-2);
SellPrice = Close;

results:

image

2nd code produced more trades, but it seems to me that I using the same inputs, can't figure what causes discrepancies in results?

Any ideas?

Thanks

in your first code there is a Ref missing, this should give the same:

Buy = Ref( Ref(H,-1) < Ref(H,-2) AND Ref(L,-1) > Ref(L,-2), -1 ) AND O < Ref(C,-1);
3 Likes

Thank you,

I am trying now to modify this code for intraday 5min bars.

but I am getting no results. Any ideas what I am doing wrong. I am still learning use of multiple timeframe functions.

test instrument: $SPY
timeframe: 5min
data source: IQFeed

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

//Define starting capital and position size
SetOption("initialequity", 25000);
SetPositionSize(50,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",1);

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

yestH = TimeFrameGetPrice ("H", inDaily, -1);
yestL = TimeFrameGetPrice ("L", inDaily, -1);
daybeforeH = TimeFrameGetPrice ("H", inDaily, -2);
daybeforeL = TimeFrameGetPrice ("H", inDaily, -2);

TodayOpen = TimeFrameGetPrice ("O", inDaily, 0);
yestC = TimeFrameGetPrice ("C", inDaily, -1);

TimeFrameSet(inDaily);
insideL = daybeforeL < yestL;
insideH = daybeforeH > yestH;
insideday = InsideL AND insideH;
gap = TodayOpen < yestC;
daybeforeHigh = Ref(H,-2);
yesterdayHigh = Ref(H,-1);
TimeFrameRestore();

entryrules = Ref(insideday,-1) AND gap;

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

StartTime = 063000; // my PC is PST timezone
Endtime = 130000;

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

N = 1;

dn = DateNum();
newDay = dn != Ref( dn,-1);

Buy = entryrules AND C > yesterdayHigh AND timeOK;
BuyPrice = Close;
Sell = C > daybeforeHigh AND timeOK;
SellPrice = Close;

Buy = Buy AND Sum( Buy, BarsSince( newDay) +1 ) <= N;

Buy = ExRem(Buy,sell); 

See Multiple Timeframe Support.

@nkrastins, a (hopefully) helpful hint...

If you don't already know about "Explorations" then it will be worth you time to learn about them. I find using explorations to be one of the best tools to debug my code, as you can have each calculation value displayed over a set timeframe.

Additionally, breaking down the code into smaller segments makes it easier to debug as well, especially when working with (new to you) unfamiliar functions. Tie that in with Explorations, and you should be "Off to the Races!".

4 Likes

New Strategy.

I have an intraday strategy here.

Buy at the next bar open when last bar close is higher than close bar ago, open position at 10:30 AM EST. Sell when close is higher than VWAP.

I backtested my strategy on following ETF's using 30min bars for entries and exits.

GLD, QQQ, SPY, TLT, VNQ, XLE, XLF, XLK, XLP, XLU, XLV, XLY

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

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

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

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

ibs = (C-L)/(H-L);

StartTime = 103000;
Endtime = 103000;

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

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

N = 1;

dn = DateNum();
newDay = dn != Ref( dn,-1);

range = (H-L);

condition = Ref(range,-2) > 2*Ref(range,-1);

Buy = Ref(C,-2) < Ref(C,-1) AND Ref(C,-1) < vwap AND  O < vwap AND Ref(ibs,-2) < .20 AND timeOK;
BuyPrice = Open;
Sell = C > vwap;
SellPrice = Close;

Buy = Buy AND Sum( Buy, BarsSince( newDay) +1 ) <= N;

Buy = ExRem(Buy,Sell);

Here are the results

and now here is a variation

instead of waiting for the next bar open, I buy at the bar close, results go down considerably.

to me it seems its the same logic, but I can't figure what makes such difference?

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

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

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

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

ibs = (C-L)/(H-L);

StartTime = 103000;
Endtime = 103000;

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

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

N = 1;

dn = DateNum();
newDay = dn != Ref( dn,-1);

range = (H-L);

condition = Ref(range,-2) > 2*Ref(range,-1);

Buy = Ref(C,-1) < C AND C < vwap AND  O < vwap AND Ref(ibs,-2) < .20 AND TimeOK;
BuyPrice = Close;
Sell = C > vwap;
SellPrice = Close;

Buy = Buy AND Sum( Buy, BarsSince( newDay) +1 ) <= N;

Buy = ExRem(Buy,Sell);

I think problem is with time rule, since position should have been opened 10:29:59, I looked back in backtest results, trades seemed to be off.

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