How to create trades from year end to year end?

The code below will create trades, 1 per year, with the P/L from the first trading day of January to the last trading day of December. How would I change it so the buy is the last trading day of December and the sell is the last trading day of December of the following year?

FirstDayofYear = IIf(DayOfYear() < Ref(DayOfYear(),-1),1,0); // if today's day is greater than tomorrow's day return true/1 else false/0
LastDayofYear = IIf(DayofYear() > Ref(DayOfYear(),1),1,0); // if today's day is greater than tomorrow's day return true/1 else false/0

Buy = FirstDayofYear;
Sell = LastDayofYear;

BuyPrice=Open;
SellPrice=Close;

Pretty please read "How to use this site" thread before posting. It's a pinned thread.

It is mandatory rule to use code tags for inserting code. See linked animation below.


As for your question...
You would need to use Year() function to check for year change.
NOTE: Below code is backtesting code only. It will not work in real-time but only works on historical data.

// This is Backtest code only. 
// Does not work in real-time
// because of looking one bar ahead.
// https://forum.amibroker.com/t/how-to-create-trades-from-year-end-to-year-end/14074/2
SetOption( "HoldMinDays", 1);
SetOption( "AllowSameBarExit", 1);

yr = Year();
is_new_year_TOMORROW = yr != Ref(yr,1);

Buy = is_new_year_TOMORROW;

// "current" year is greater than trade entry year
is_year_later = yr - ValueWhen(Buy, yr, 2) == 1; 

Sell = is_new_year_TOMORROW AND is_year_later;

8

Thanks for your help.

I'll try to remember to use the button next time I include code.