Buy Close, Sell Next Day Open

Hi,

I try to program a simple rule: Buy Close, Sell Next Day Open.

My code:

Settradedelays(0,1,0,0);

Buy = Sell = 1;
Short = Cover = 0;

BuyPrice = C;
SellPrice = O;
ShortPrice = Coverprice = C;

Still this AFL is buying and selling at the same day.

Any idea how to code this?

Kind regards,
Hans

https://www.amibroker.com/guide/afl/setoption.html

SetTradeDelays( 0, 1, 0, 0 );
SetOption( "AllowSameBarExit", 0 );
Buy = Sell = 1;
Short = Cover = 0;

BuyPrice = C;
SellPrice = O;

But this code is skipping the entry on the day of the exit. That’s not what I want. There should be a trade every day.

Apologies for that. I think this might be what you are looking for.

SetTradeDelays( 0, 0, 0, 0 );
Buy = 1;
Sell = Day() > Ref( Day(), -1 );
Short = Cover = 0;

BuyPrice = C;
SellPrice = O;

Capture

Perfect. This is what I needed.

Thanks for your help!

Kind regards,
Hasn

Really, you NEED to read the manual. ALL common scenarios are described in the manual, see
http://www.amibroker.com/guide/h_portfolio.html
(scroll down to “resolving same bar conflicts”)

1 Like

My last code is still not right. It will miss a day at a month change. After taking Tomasz advice the issue is addressed in the linked article.

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "AllowSameBarExit", True );
SetOption( "HoldMinBars", 1 );
Buy = 1;
Sell = 1;
Short = Cover = 0;

BuyPrice = C;
SellPrice = O;
4 Likes

Thanks again for you help!

Hans/rb250660/others:
Just wondering if you had any success live trading the night time edge with "Buy Close, Sell Next Day Open?" Any tips on designing a strategy?
I show really promising backtests trading 3x ETFs such as TQQQ, FAS, BIB, SPXL in the $0 commission world in the US as of Oct 2019+, further improved by filtering out extremes such as only taking trades above MA200.
-Mark

I'm not trading this strategy. Still, I'm interested in your (backtest) results. Can you share them?

Kind regards,
Hans

Here are some backtest stats:

> //SPY Buy and Hold  			 1/02/2004-12/20/2019: 									 							  6.80% CAR,  6.80% RAR, -56.47% MaxDD, 100.0% exposure,   1 trade/total
> //SPY Open to Close 			 1/02/2004-12/20/2019 OE27-OpenToClose.afl: 	1.02 pf, 54% win rate, 0.01% avg pl,  1.35% CAR,  1.36% RAR, -44.44% MaxDD, 99.92% exposure, 251 trades/year
> //SPY Close to Open 			 1/02/2004-12/20/2019 OE27-NOMinorFilters.afl: 	1.12 pf, 54% win rate, 0.02% avg pl,  5.38% CAR,  5.39% RAR, -27.70% MaxDD, 99.92% exposure, 251 trades/year
> 
> //TQQQ Buy and Hold  			 2/11/2010-12/20/2019: 									 							 48.46% CAR, 48.46% RAR, -58.08% MaxDD, 100.0% exposure,   1 trade/total
> //TQQQ Open to Close 			 2/11/2010-12/20/2019 OE27-OpenToClose.afl: 	1.02 pf, 54% win rate, 0.06% avg pl,  5.78% CAR,  5.78% RAR, -60.87% MaxDD, 99.99% exposure, 251 trades/year
> //Buy = 1; // TQQQ Close to Open 2/11/2010-12/20/2019 OE27-NOMinorFilters.afl:  1.24 pf, 57% win rate, 0.16% avg pl, 40.91% CAR, 41.21% RAR, -30.88% MaxDD, 99.28% exposure, 248 trades/year
> //TQQQ Close to Open filtered    2/11/2010-12/20/2019 OE27-TQQQ.afl:			1.78 pf, 62% win rate, 0.28% avg pl, 35.52% CAR, 79.84% RAR, -13.75% MaxDD, 44.49% exposure, 111 trades/year
> 
> //LABU Buy and Hold  			 5/28/2015-12/20/2019: 									 							-18.83% CAR,-18.83% RAR, -91.05% MaxDD, 100.0% exposure,   1 trade/total
> //LABU Open to Close 			 5/28/2015-12/20/2019 OE27-OpenToClose.afl: 	0.81 pf, 50% win rate,-0.13% avg pl,-51.51% CAR,-51.56% RAR, -97.85% MaxDD, 99.73% exposure, 247 trades/year
> //Buy = 1; // LABU Close to Open 5/28/2015-12/20/2019 OE27-NOMinorFilters.afl:  1.34 pf, 55% win rate, 0.24% avg pl, 66.90% CAR, 67.56% RAR, -42.20% MaxDD, 99.02% exposure, 247 trades/year
> //LABU Close to Open filtered    5/28/2015-12/20/2019 OE27-LABU.afl:			1.45 pf, 55% win rate, 0.28% avg pl, 71.45% CAR, 85.32% RAR, -30.15% MaxDD, 83.74% exposure, 209 trades/year
1 Like