Buy Close, Sell Next Day Open


#1

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


#2

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;

#3

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.


#4

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


#6

Perfect. This is what I needed.

Thanks for your help!

Kind regards,
Hasn


#7

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


#8

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;

#9

Thanks again for you help!