I'm attempting to implement a buy-and-hold strategy, but my algorithm never appears to hold. It appears to continually rebalance.
Here's how I've set up my algorithm:
def Initialize(self):
self.SetCash(100000)
self.SetStartDate(2005, 1, 1)
# if not specified, the Backtesting EndDate would be today
# self.SetEndDate(2015, 7, 1)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverseSelection(FineFundamentalUniverseSelectionModel(self.select_coarse, self.select_fine))
self.AddAlpha(LongShortAllValueAlphaModel(self._alpha_selection_max, self._hold_months.clone()))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel(rebalance=None))
self.SetExecution(VolumeWeightedAveragePriceExecutionModel())
And this is what I observe in my order stream, for just one symbol. There are two periods where my algorithm bought ABC, held on for a few months, and closed the position. But there were a very large number of small intervening orders that I really don't want happening.
2005-01-01T05:00:00Z,ABC,11.67187743,427,Market On Open,Filled,4983.89166261,
2005-01-08T05:00:00Z,ABC,11.516729775,-3,Market On Open,Filled,-34.550189325,
2005-01-11T05:00:00Z,ABC,11.6261287625,1,Market On Open,Filled,11.6261287625,
2005-01-13T05:00:00Z,ABC,11.596292675,3,Market On Open,Filled,34.788878025,
2005-01-14T05:00:00Z,ABC,11.4073307875,5,Market On Open,Filled,57.0366539375,
2005-01-20T05:00:00Z,ABC,11.437166875,4,Market On Open,Filled,45.7486675,
2005-01-21T05:00:00Z,ABC,11.5226969925,-7,Market On Open,Filled,-80.6588789475,
2005-01-22T05:00:00Z,ABC,11.341691395,4,Market On Open,Filled,45.36676558,
2005-01-25T05:00:00Z,ABC,11.41727615,-3,Market On Open,Filled,-34.25182845,
2005-01-26T05:00:00Z,ABC,11.317822525,6,Market On Open,Filled,67.90693515,
2005-01-27T05:00:00Z,ABC,11.55253308,-5,Market On Open,Filled,-57.7626654,
2005-01-28T05:00:00Z,ABC,11.5664565875,2,Market On Open,Filled,23.132913175,
2005-01-29T05:00:00Z,ABC,11.636074125,-10,Market On Open,Filled,-116.36074125,
2005-02-03T05:00:00Z,ABC,11.7414949675,10,Market On Open,Filled,117.414949675,
2005-02-04T05:00:00Z,ABC,11.6858009375,-6,Market On Open,Filled,-70.114805625,
2005-02-05T05:00:00Z,ABC,11.8548721,4,Market On Open,Filled,47.4194884,
2005-02-09T05:00:00Z,ABC,11.914544275,-3,Market On Open,Filled,-35.743632825,
2005-02-10T05:00:00Z,ABC,11.8091234325,7,Market On Open,Filled,82.6638640275,
2005-02-15T05:00:00Z,ABC,12.0080306825,1,Market On Open,Filled,12.0080306825,
2005-02-16T05:00:00Z,ABC,12.2775488025,-7,Market On Open,Filled,-85.9428416175,
2005-02-17T05:00:00Z,ABC,12.1501965945,4,Market On Open,Filled,48.600786378,
2005-02-18T05:00:00Z,ABC,11.899471935,6,Market On Open,Filled,71.39683161,
2005-02-23T05:00:00Z,ABC,11.86962376125,5,Market On Open,Filled,59.34811880625,
2005-02-25T05:00:00Z,ABC,11.9591682825,-1,Market On Open,Filled,-11.9591682825,
2005-03-01T05:00:00Z,ABC,11.94523913475,3,Market On Open,Filled,35.83571740425,
2005-03-02T05:00:00Z,ABC,12.20790306375,-12,Market On Open,Filled,-146.494836765,
2005-03-04T05:00:00Z,ABC,12.4128605235,-1,Market On Open,Filled,-12.4128605235,
2005-03-10T05:00:00Z,ABC,12.06861158625,10,Market On Open,Filled,120.6861158625,
2005-03-17T05:00:00Z,ABC,11.9392695,1,Market On Open,Filled,11.9392695,
2005-03-18T05:00:00Z,ABC,12.09845976,-5,Market On Open,Filled,-60.4922988,
2005-03-24T05:00:00Z,ABC,12.25963989825,-16,Market On Open,Filled,-196.154238372,
2005-03-25T05:00:00Z,ABC,10.944330375,1,Market On Open,Filled,10.944330375,
2005-03-29T05:00:00Z,ABC,10.72345388925,56,Market On Open,Filled,600.513417798,
2005-03-30T05:00:00Z,ABC,10.81896804525,-5,Market On Open,Filled,-54.09484022625,
2005-03-31T05:00:00Z,ABC,11.27266028625,-9,Market On Open,Filled,-101.45394257625,
2005-04-01T05:00:00Z,ABC,11.37215419875,-10,Market On Open,Filled,-113.7215419875,
2005-04-02T05:00:00Z,ABC,11.2826096775,5,Market On Open,Filled,56.4130483875,
2005-04-05T04:00:00Z,ABC,11.47164811125,-7,Market On Open,Filled,-80.30153677875,
2005-04-06T04:00:00Z,ABC,11.75023106625,-9,Market On Open,Filled,-105.75207959625,
2005-04-07T04:00:00Z,ABC,11.7124233795,3,Market On Open,Filled,35.1372701385,
2005-04-08T04:00:00Z,ABC,11.660686545,-2,Market On Open,Filled,-23.32137309,
2005-04-09T04:00:00Z,ABC,11.50746591975,4,Market On Open,Filled,46.029863679,
2005-04-12T04:00:00Z,ABC,11.636808006,-3,Market On Open,Filled,-34.910424018,
2005-04-14T04:00:00Z,ABC,11.73033228375,-7,Market On Open,Filled,-82.11232598625,
2005-04-15T04:00:00Z,ABC,11.85967437,-12,Market On Open,Filled,-142.31609244,
2005-04-20T04:00:00Z,ABC,11.8795731525,-1,Market On Open,Filled,-11.8795731525,
2005-04-21T04:00:00Z,ABC,11.9392695,-3,Market On Open,Filled,-35.8178085,
2005-04-22T04:00:00Z,ABC,11.90942132625,2,Market On Open,Filled,23.8188426525,
2005-04-26T04:00:00Z,ABC,11.92136059575,2,Market On Open,Filled,23.8427211915,
2005-04-28T04:00:00Z,ABC,12.01886463,-7,Market On Open,Filled,-84.13205241,
2005-04-30T04:00:00Z,ABC,12.17208525525,-2,Market On Open,Filled,-24.3441705105,
2005-05-03T04:00:00Z,ABC,12.3332653935,-3,Market On Open,Filled,-36.9997961805,
2005-05-04T04:00:00Z,ABC,12.2616297765,2,Market On Open,Filled,24.523259553,
2005-05-10T04:00:00Z,ABC,12.64567627875,-1,Market On Open,Filled,-12.64567627875,
2005-05-12T04:00:00Z,ABC,12.65562567,-1,Market On Open,Filled,-12.65562567,
2005-05-14T04:00:00Z,ABC,12.72527140875,-17,Market On Open,Filled,-216.32961394875,
2005-05-18T04:00:00Z,ABC,13.1212571805,-1,Market On Open,Filled,-13.1212571805,
2005-05-28T04:00:00Z,ABC,12.982879077,14,Market On Open,Filled,181.760307078,
2005-06-01T04:00:00Z,ABC,12.879366395,1,Market On Open,Filled,12.879366395,
2005-06-03T04:00:00Z,ABC,13.0127385045,-2,Market On Open,Filled,-26.025477009,
2005-06-07T04:00:00Z,ABC,13.034635418,-1,Market On Open,Filled,-13.034635418,
2005-06-08T04:00:00Z,ABC,12.9649634205,2,Market On Open,Filled,25.929926841,
2005-06-09T04:00:00Z,ABC,12.8455257105,1,Market On Open,Filled,12.8455257105,
2005-06-10T04:00:00Z,ABC,12.839553825,4,Market On Open,Filled,51.3582153,
2005-06-14T04:00:00Z,ABC,13.078429245,-7,Market On Open,Filled,-91.549004715,
2005-06-15T04:00:00Z,ABC,13.373042263,-5,Market On Open,Filled,-66.865211315,
2005-06-17T04:00:00Z,ABC,13.595992655,-1,Market On Open,Filled,-13.595992655,
2005-06-21T04:00:00Z,ABC,13.6577021385,-3,Market On Open,Filled,-40.9731064155,
2005-06-22T04:00:00Z,ABC,13.5103956295,3,Market On Open,Filled,40.5311868885,
2005-06-23T04:00:00Z,ABC,13.4068829475,3,Market On Open,Filled,40.2206488425,
2005-06-25T04:00:00Z,ABC,13.1958763265,-5,Market On Open,Filled,-65.9793816325,
2005-06-30T04:00:00Z,ABC,13.6895521945,-403,Market On Open,Filled,-5516.8895343835,
2009-07-01T04:00:00Z,ABC,14.84467302,372,Market On Open,Filled,5522.21836344,
2009-07-02T04:00:00Z,ABC,15.121908,-7,Market On Open,Filled,-105.853356,
2009-07-07T04:00:00Z,ABC,14.92028256,-8,Market On Open,Filled,-119.36226048,
2009-07-08T04:00:00Z,ABC,15.43274722,-12,Market On Open,Filled,-185.19296664,
2009-07-09T04:00:00Z,ABC,15.02109528,2,Market On Open,Filled,30.04219056,
2009-07-11T04:00:00Z,ABC,14.86147514,4,Market On Open,Filled,59.44590056,
2009-07-15T04:00:00Z,ABC,15.39074192,-1,Market On Open,Filled,-15.39074192,
2009-07-18T04:00:00Z,ABC,16.01242036,-2,Market On Open,Filled,-32.02484072,
2009-07-21T04:00:00Z,ABC,15.8780034,8,Market On Open,Filled,127.0240272,
2009-07-22T04:00:00Z,ABC,16.10483202,-2,Market On Open,Filled,-32.20966404,
2009-07-23T04:00:00Z,ABC,15.8359981,7,Market On Open,Filled,110.8519867,
2009-07-30T04:00:00Z,ABC,17.44900162,-16,Market On Open,Filled,-279.18402592,
2009-07-31T04:00:00Z,ABC,16.65090092,9,Market On Open,Filled,149.85810828,
2009-08-08T04:00:00Z,ABC,17.02894862,5,Market On Open,Filled,85.1447431,
2009-08-12T04:00:00Z,ABC,17.0121465,-2,Market On Open,Filled,-34.024293,
2009-08-15T04:00:00Z,ABC,16.98694332,1,Market On Open,Filled,16.98694332,
2009-08-19T04:00:00Z,ABC,17.24737618,-2,Market On Open,Filled,-34.49475236,
2009-08-20T04:00:00Z,ABC,17.617362939,-3,Market On Open,Filled,-52.852088817,
2009-08-21T04:00:00Z,ABC,17.870122809,-3,Market On Open,Filled,-53.610368427,
2009-08-22T04:00:00Z,ABC,17.827996164,3,Market On Open,Filled,53.483988492,
2009-08-25T04:00:00Z,ABC,17.566810965,5,Market On Open,Filled,87.834054825,
2009-08-26T04:00:00Z,ABC,17.583661623,-1,Market On Open,Filled,-17.583661623,
2009-08-28T04:00:00Z,ABC,17.827996164,-3,Market On Open,Filled,-53.483988492,
2009-08-29T04:00:00Z,ABC,17.802720177,-1,Market On Open,Filled,-17.802720177,
2009-09-01T04:00:00Z,ABC,17.903824125,-3,Market On Open,Filled,-53.711472375,
2009-09-09T04:00:00Z,ABC,17.836421493,4,Market On Open,Filled,71.345685972,
2009-09-16T04:00:00Z,ABC,18.139733337,6,Market On Open,Filled,108.838400022,
2009-09-18T04:00:00Z,ABC,18.190285311,4,Market On Open,Filled,72.761141244,
2009-09-19T04:00:00Z,ABC,18.232411956,-1,Market On Open,Filled,-18.232411956,
2009-09-22T04:00:00Z,ABC,18.502022484,-5,Market On Open,Filled,-92.51011242,
2009-09-23T04:00:00Z,ABC,18.5357238,4,Market On Open,Filled,74.1428952,
2009-09-25T04:00:00Z,ABC,18.156583995,-8,Market On Open,Filled,-145.25267196,
2009-09-30T04:00:00Z,ABC,19.159198146,-354,Market On Open,Filled,-6782.356143684
I've done a little searching in the forums and in the implementation. I found two threads that appear to be relevant:
Need Help Understanding EqualWeightingPortfolioConstructionModel by Daniel Wygant - QuantConnect.com
Unknown Order coming from Algorithm Framework by Malcolm Tok - QuantConnect.com
The latter suggests I change to minute resolution. I don't know how I feel about that recommendation. Though I will try it out. The former points to an interesting bit of implementation that at first glance looks suspect. Targets are instantiated based on insights here: Lean/PortfolioConstructionModel.cs at master · QuantConnect/Lean · GitHub
Note that at any given time, I don't understand the implementation enough to know if insights are being passed into CreateTargets. However, since my rebalance is None, I would expect IsRebalanceDue to always return false. I don't see an override for this method anywhere, so the only implementation is here: Lean/PortfolioConstructionModel.cs at master · QuantConnect/Lean · GitHub. The implementation of this function is unexpected. On line 273, there is a specific check for null rebalancing function, and true is immediately returned. This doesn't make sense to me.
Anyway, I hope someone can help me understand what's going on here, and how to better manage the number of orders that get placed. The algorithm framework is a great idea, but doesn't feel like it is fully fleshed out yet. Surprisingly there are no execution strategies that make use of limit orders. I would have expected StandardDeviationExecutionModel to be implemented through a limit order.
Sunil
Â
Sunil Mishra
I tried changing the resolution to minute, and also setting a different rebalance schedule, but it doesn't produce any improvement in the behavior of the algorithm.
def Initialize(self): self.SetCash(100000) self.SetStartDate(2020, 1, 1) # if not specified, the Backtesting EndDate would be today # self.SetEndDate(2015, 7, 1) self.UniverseSettings.Resolution = Resolution.Minute self.AddUniverseSelection(FineFundamentalUniverseSelectionModel(self.select_coarse, self.select_fine)) self.AddAlpha(LongShortAllValueAlphaModel(self._alpha_selection_max, self._hold_months.clone())) self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel(rebalance=timedelta(252))) self.SetExecution(VolumeWeightedAveragePriceExecutionModel())
I also verified I wasn't producing excessive insights. These are the ones the backtest had for ABC:
{"id":"2a578fecb13d4519850ea9c03c654fd7","group-id":null,"source-model":"3b32a1a9-2adf-441b-a6f2-06a096c14b5a","generated-time":1104555600,"created-time":1104555600,"close-time":1112331600,"symbol":"AAS R735QTJ8XC9X","ticker":"ABC","type":"price","reference":11.675855575,"reference-final":11.3860833465,"direction":"up","period":5356800,"magnitude":null,"confidence":null,"weight":null,"score-final":true,"score-magnitude":"0","score-direction":"0","estimated-value":"-2580.1319"}, {"id":"b737bec8c95b4bbdbd7c3408a645470d","group-id":null,"source-model":"3b32a1a9-2adf-441b-a6f2-06a096c14b5a","generated-time":1112331600,"created-time":1112331600,"close-time":1120017600,"symbol":"AAS R735QTJ8XC9X","ticker":"ABC","type":"price","reference":11.3860833465,"reference-final":13.526320657499999,"direction":"up","period":5356800,"magnitude":null,"confidence":null,"weight":null,"score-final":true,"score-magnitude":"0","score-direction":"1","estimated-value":"49345.3114"}, {"id":"d2212a944df84e05a7dc1d9c08c84096","group-id":null,"source-model":"3b32a1a9-2adf-441b-a6f2-06a096c14b5a","generated-time":1246420800,"created-time":1246420800,"close-time":1254196800,"symbol":"AAS R735QTJ8XC9X","ticker":"ABC","type":"price","reference":14.903480439999999,"reference-final":18.341941233,"direction":"up","period":5356800,"magnitude":null,"confidence":null,"weight":null,"score-final":true,"score-magnitude":"0","score-direction":"1","estimated-value":"114707.8773"},
Sunil
Â
Sunil Mishra
Attaching backtest.
Derek Melchin
Hi Sunil,
Please provide the missing files so we can assist with debugging.
from holdmonths import HoldMonthsTracker from pricehist import PriceHistoryManager from qcconsts import * from qcselectors import *
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.
Sunil Mishra
OK, here's a much simpler backtest that shows the same behavior. I have EqualWeightingPortfolioConstructionModel(rebalance=None) which I expect means no trading happens once a position has been established, until insights change. Can you help me understand why this doesn't work?
Thanks,
Sunil
Â
Sunil Mishra
Derek Melchin I have attached a simpler standalone demonstration of my issue, as you had requested. Can you please take a look again?
Regards,
Sunil
Â
Derek Melchin
Hi Sunil,
The web IDE doesn't currently support named arguments for python. So, to resolve the issue in the above algorithm, we need to replace
EqualWeightingPortfolioConstructionModel(rebalance=None)
with
EqualWeightingPortfolioConstructionModel(lambda time: None)
See the attached backtest for reference.
We have a GitHub Issue for adding support for named arguments in python. Subscribe to our progress here.
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.
Sunil Mishra
Thanks Derek Melchin . I assume this problem only applies to functions that call with named arguments from python to C#? I have used such arguments with pure python libraries without problems, so just want to make sure I understand.
Sunil
Â
Sunil Mishra
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!