Identifying the First Trading Day of a Year
-
Hello,
I am trying to track metrics such as Sharpe Ratio and Returns (lots of other stuff too, but irrelevant). I am backtesting a period of 25 years which I run all at once with Cerebro. So, for the whole 25 year period I can track my return by looking at my portfolio value at the beginning and then looking at the end. I also would like to track annualized returns though... I am trying to approach this in a similar way: record my portfolio value at the beginning of a year within a variable, record my portfolio value at the end of that year within a variable.
I tried just using "if month january if day 1" type of thing to then record my portfolio value, but the problem is that the first trading day of the year is variable. So, how would I identify the first trading day of a year to be able to do this? Or am I approaching this entirely wrong? I am a coding rookie and I'm sure the solution is simple, but I can't figure it out!
Thanks for the help guys!
-
@Z, check out the bt.analyzers.Return code. That already does what you want. Just give it years as timeframe. Or write your own analyzer with TimeFrameAnalyzerBase as base class and use the
_on_dt_over()
method, which is called on first trading day of a year if you use years as timeframe. -
@Reinke said in Identifying the First Trading Day of a Year:
@Z, check out the bt.analyzers.Return code. That already does what you want. Just give it years as timeframe. Or write your own analyzer with TimeFrameAnalyzerBase as base class and use the _on_dt_over() method, which is called on first trading day of a year if you use years as timeframe.
Thanks so much! Infinitely easier than what I was trying to do lol. Apparently I need to look through the documentation more before I start doing things :)