Hello, I am interested in testing Joel Greenblatt's magic formula with monthly rebalancing for 15 years on QuantConnect. I found the code in https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/Alphas/GreenblattMagicFormulaAlpha.py which looks like daily rebalancing. Could it run with monthly rebalancing and how to modify it to trade monthly? Thanks.
Derek Melchin
Hi L Y,
To switch the algorithm to monthly rebalancing we should first reduce the data resolution. This is an optional step, but it will speed up the backtest.
self.UniverseSettings.Resolution = Resolution.Daily
Next, we need to remove the daily rebalancing from the PCM.
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel(lambda time: None))
The alpha model needs to also be updated so that it emits insights once per month
class RateOfChangeAlphaModel(AlphaModel): def __init__(self, *args, **kwargs): ... self.lastMonth = -1 # Add this property def Update(self, algorithm, data): if data.Time.month == self.lastMonth: # we add a monthly guard here return [] self.lastMonth = data.Time.month ...
Lastly, we need to change the insight duration to
Expiry.EndOfMonth(data.Time) - timedelta(seconds=1)
See the attached backtest for reference.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Sean Xian Shuo Chen
Hi Derek,
I cloned the algorithm and tried to backtest it, but I was presented with an error message on line 142 saying the super()_.init_ function only takes 2-3 arguments, but 4 were given.
Any help would be greatly appreciated
Thanks
Varad Kabade
Hi Sean Xian Shuo Chen,
This issue is due to this PR. Refer to this thread.
Solving the issue requires removing the SecurityInitializer argument being passed, attaching backtest.
Best,
Varad kabade
Gianluca Albino
ciao, i tried to do smt, but i think withj luck of success because is full of bug the code, what about u? did u resolve something?
L Y
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!