Trading Idea for sharing and discussion

Just wanted to share a trading idea and I hope to generate some discussion for it. You might like to use it as a basis to form your own trading system.

It's a simple system that looks at a fast and slow moving average. The system is supposed to buy if the open price is below yesterday's fast moving average. If it opens above yesterday's fast moving average, it can buy at a limit if the price goes below that fast moving average. Sells are the opposite, it will sell if the open is above yesterday's fast moving average or at a limit if the price goes above yesterday's fast moving average. The slow moving average is your market filter. It's only suppose to buy when the close is above the slow moving average.

I find it somewhat difficult to set the trade delays and REFs so that my trades work correctly and that I'm not looking at future data. Any help or suggestions for modifications to my code would certainly be appreciated.

My code:

//Sells at the open the next day if open price is above the Fast MA or later that day at limit if price goes above the Fast MA

SetTradeDelays( 1, 1, 1, 1 );
SetOption("AllowSameBarExit", True );
SetOption("HoldMinBars", 1); 
FastMAPeriod = 5; //optimize("FastMAPeriod",5,2,50,1);
FastMA = EMA( Close, FastMAPeriod );//should it REF previous day to avoid future data leak for limit order?

//Market Filter
SlowMA = MA( Close, 100 );//should it REF previous day to avoid future data leak for limit order?
Bullish = C > SlowMA;

Short=Cover=0;
Buy = IIf( FastMA > Low AND Bullish, 1, 0 );
Sell = IIf( FastMA < High, 1, 0 );

//remove excess Buy,Sell,Short,Cover signals
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

BuyPrice =  Min( Open, FastMA );
SellPrice = Max( Open, FastMA );

//Charting
SetChartOptions( 1, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) \n{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

Plot( FastMA, "FastMA" , ColorBlue, styleLine | styleThick );
Plot( SlowMA, "LongTermMA", IIf( Bullish, colorGreen, colorRed ), styleLine | styleThick );
// Plot Buy, Sell, Short, Cover Arrows
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );~~~

One problem that I see with my code is that on my chart or exploration, I'll see two buy trades in a row without a sell trade in between. For instance if you run the code against the SPY for daily bars in 2018, you'll see a buy signal on Jan 30, then another on Jan 31 but no sell until Feb 1.

And here is a copy of my backtest results:
image

And this is a copy of my exploration:
image

This is my code with the added exploration:

//Sells at the open the next day if open price is above the Fast MA or later that day at limit if price goes above the Fast MA

SetTradeDelays( 1, 1, 1, 1 );
SetOption("AllowSameBarExit", True );
SetOption("HoldMinBars", 1); 
FastMAPeriod = 5; //optimize("FastMAPeriod",5,2,50,1);
FastMA = ref(EMA( Close, FastMAPeriod ),-1);//should it REF previous day to avoid future data leak for limit order?

//Market Filter
SlowMA = MA( Close, 100 );//should it REF previous day to avoid future data leak for limit order?
Bullish = C > SlowMA;

Short=Cover=0;
Buy = IIf( FastMA > Low AND Bullish, 1, 0 );
Sell = IIf( FastMA < High, 1, 0 );

//remove excess Buy,Sell,Short,Cover signals
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

BuyPrice =  Min( Open, FastMA );
SellPrice = Max( Open, FastMA );

//Charting
SetChartOptions( 1, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) \n{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

Plot( FastMA, "FastMA" , ColorBlue, styleLine | styleThick );
Plot( SlowMA, "LongTermMA", IIf( Bullish, colorGreen, colorRed ), styleLine | styleThick );
// Plot Buy, Sell, Short, Cover Arrows
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );

Filter=1; 

AddColumn(Bullish,"Bullish",1.0,colorDefault,colorDefault,50); 
AddColumn(Buy,"Buy",1.0,colorDefault,colorDefault,50);
AddColumn(BuyPrice,"BuyPrice");
AddColumn(Sell,"Sell",1.0,colorDefault,colorDefault,50);
AddColumn(SellPrice,"SellPrice");
AddColumn(O,"Open"); 
AddColumn(H,"High"); 
AddColumn(L,"Low"); 
AddColumn(C,"Close");
AddColumn(FastMA,"FastMA",1.0);
AddColumn(Ref(C,-1),"YdayClose");```

Exploration is not the same one as backtester! Same applies for chart.

SetOption( "backtester_field" ) and SetTradeDelays() are Backtester features.

You would have to add Equity function instead of ExRem.
BUT (a big "but") not all SetOption( "backtester_field" ) do work with Equity function.

Why?
Because Equity() is an equivalent of the old (single-security) backtester (AB version 4.40 and earlier), while some SetOption fields have been introduced after that. And AFAIK, Equity function has not been updated to work with all later SetOption fields (introduced after AB 4.40). E.g. "HoldMinBars" has been introduced in AB 4.80.

Other feature Equity function will not work with is Currency settings of Tools -> Preferences -> Currencies

It is also mentioned in the manual

IMPORTANT NOTE: Equity() function uses so called "old" single-security backtester that offers only subset of features of new backtester.

So in short... You simply can not replicate (entire) Backtester via Exploration or Chart. Well, you would not need Backtester then if Exploration (or chart) would be the same ones as backtester and would have same features. Right?

3 Likes

Thank you very much @fxshrat for taking the time to explain the difference I see between the backtest, the exploration and the charting.

I've tried a different variation of my code and now the backtest results look like what I was expecting. It's set to trade on the open with zero delay which I know you shouldn't do. However, the bullish trend and the fast moving average are calculated from the previous day.

I also changed "AllowSameBarExit" to false to stop new trades being entered before the existing trade was closed.

My modified code:

//Sells at the open the next day if open price is above the Fast MA or later that day at limit if price goes above the Fast MA

SetTradeDelays( 0, 0, 0, 0 );//Buys on open with 0 delay but fast and slow MA refer to yesterday
SetOption("AllowSameBarExit", False );
SetOption("HoldMinBars", 1); 
FastMAPeriod = 5; //optimize("FastMAPeriod",5,2,50,1);
FastMA = Ref(EMA( Close, FastMAPeriod ),-1);//should it REF previous day to avoid future data leak for limit order?

//Market Filter
SlowMA = Ref(MA( Close, 100 ),-1);//should it REF previous day to avoid future data leak for limit order?
Bullish = Ref(C,-1) > SlowMA;

Short=Cover=0;
Buy = IIf( FastMA > Low AND Bullish, 1, 0 );
Sell = IIf( FastMA < High, 1, 0 );

//remove excess Buy,Sell,Short,Cover signals
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

BuyPrice =  Min( Open, FastMA );
SellPrice = Max( Open, FastMA );

//Charting
SetChartOptions( 1, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) \n{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

Plot(FastMA, "FastMA" , ColorBlue, styleLine | styleThick );
Plot( SlowMA, "LongTermMA", IIf( Bullish, colorGreen, colorRed ), styleLine | styleThick );
// Plot Buy, Sell, Short, Cover Arrows
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );

Filter=1; 

AddColumn(Bullish,"Bullish",1.0,colorDefault,colorDefault,50); 
AddColumn(Buy,"Buy",1.0,colorDefault,colorDefault,50);
AddColumn(BuyPrice,"BuyPrice");
AddColumn(Sell,"Sell",1.0,colorDefault,colorDefault,50);
AddColumn(SellPrice,"SellPrice");
AddColumn(O,"Open"); 
AddColumn(H,"High"); 
AddColumn(L,"Low"); 
AddColumn(C,"Close");
AddColumn(FastMA,"FastMA",1.0);
AddColumn(Ref(C,-1),"YdayClose");```
1 Like