I'm wondering whether I misunderstand the intention of the Insights that my Alpha class generates. I expect Up would be a buy. Down would be a short or a sell. Flat would close a current position (short or long).
I've created an Alpha Framework algorithm that uses the components in the code snippet below. My intention at the moment is simply o understand what actions the alpha framework algo will take and when. I am trying to make it very simple to narrow down on the effects of insights emitted from my customized Alpha Model:
self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
self.alpha = PPOAlphaModel()
self.SetAlpha(self.alpha)
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(NullRiskManagementModel()
My Alpha Model emits insights as follows:
#magnitude is the PPO value and the trigger level is 0.
if magnitude > self.trigger: direction = InsightDirection.Up
if magnitude < self.trigger: direction = InsightDirection.Down
insights.append(Insight.Price(symbol, self.predictionInterval, direction))
algorithm.Log(str(symbol) + ": " + str(magnitude) + ": " + str(direction))
My objective is very simple - that a security will be bought when it's PPO is above 0. Sold when below 0. I actually don't want any shorts, just to be long or flat. I have tried this two ways - by using InsightDirection.Down or InsightDirection.Flat to exit a position.
I rebalance periodically - once a month insights are generated using the above code. Essentially I would expect to see trades generated once a month, such that I have a newly rebalanced, equally weighted portfolio containing of all my symbol universe with PPO values currently above 0, and none below.
This is not what happens!
When I get a negative PPO, my Alpha class properly generates a Down Insight. However in the backtest, I look at my trade logs, and my position is not exited. In one example, it takes three months to sell it all, by which time my trigger has become quite dated and my strategy is not doing what it is intended to.
Do I misunderstand what is supposed to happen based on these insights? If Up does not generate a Buy, Down or Flat a sell, then what do they do? How do I control when an Alpha Framework algo buys and sells?
Here are my logs to show an example of the delay in insight generation and closing out a position:
Here are some of the backtest logs I generate from my Alpha class, showing my PPO value and
the Insight Direction (-1 is Down, 0 is Flat):
It works great as long as my universe contains only one security:
PPO < 0:
Insight logging from my alpha class:
2014-10-07 09:31:00 : IWM: -0.06023164327084131: 0
2014-11-12 09:31:00 : IWM: 0.03493766334515402: 1
2014-12-09 09:31:00 : IWM: -0.12236496101256578: 0
2015-01-12 09:31:00 : IWM: -0.02013623384732165: 0
2015-02-10 09:31:00 : IWM: -0.09530165947069787: 0
Trade logs (note time is reversed here):
2014-12-09 09:31:00 IWM $ 108.965395812 USD Market -903 Short Filled
2014-11-12 09:31:00 IWM $ 110.624430462 USD Market 903 Long Filled
Above you can see it properly opened a position on the Up Insight and closed it on the first Flat.
The proper buy/sell/sizing continued for the duration of the backtest.
However, once I have more than one security in my universe, it does not work as I would expect.
I would expect with 3 securities on Up Insights, I'd hold 1/3 of my portfolio in each.
I'd expect if one goes to Flat and the others stays Up, I'd rebalance and hold half of portfolio in
each of the securities still on an Up Insight. This doesn't happen.
My positions aren't fully closed when they should be and other odd things start happening:
Insight Logs from my Alpha class:
2014-10-07 00:00:00 : Converted OrderID: 1 into a MarketOnOpen order.
2014-10-07 00:00:00 : Converted OrderID: 2 into a MarketOnOpen order.
2014-10-07 00:00:00 : EEM: 1.0627595534358796: 1
2014-10-07 00:00:00 : IWM: 0.020534941748699374: 1
2014-10-07 00:00:00 : QQQ: 3.2573919225656276: 1
2014-11-12 00:00:00 : Converted OrderID: 3 into a MarketOnOpen order.
2014-11-12 00:00:00 : Converted OrderID: 4 into a MarketOnOpen order.
2014-11-12 00:00:00 : EEM: -0.5276680844380808: 0
2014-11-12 00:00:00 : IWM: 0.04415293826105228: 1
2014-11-12 00:00:00 : QQQ: 2.6348830215483208: 1
2014-12-09 00:00:00 : Converted OrderID: 7 into a MarketOnOpen order.
2014-12-09 00:00:00 : EEM: -0.8606975336564506: 0
2014-12-09 00:00:00 : IWM: -0.10461210688436362: 0
2014-12-09 00:00:00 : QQQ: 3.3503688445070448: 1
2015-01-12 09:31:00 : EEM: -1.987278124003212: 0
2015-01-12 09:31:00 : IWM: -0.02013623384732165: 0
2015-01-12 09:31:00 : QQQ: 2.9066547548976502: 1
2015-02-10 00:00:00 : EEM: -1.405379222477798: 0
2015-02-10 00:00:00 : IWM: -0.10606839436527297: 0
2015-02-10 00:00:00 : QQQ: 2.238126385714986: 1
Here are the actual trades from the same months (note no trades took place in Feb):
2015-01-12 09:31:00 QQQ $ 99.201008544 USD Market 1 Long Filled
2014-12-09 00:00:00 QQQ $ 99.584078184 USD Market On Open -5 Short Filled
2014-12-09 00:00:00 IWM $ 108.965395812 USD Market On Open -303 Short Filled
2014-11-12 00:00:00 IWM $ 110.63391066 USD Market On Open 303 Long Filled
2014-11-12 00:00:00 QQQ $ 98.101054488 USD Market On Open -10 Short Filled
2014-11-12 00:00:00 EEM $ 38.003353443 USD Market On Open -866 Short Filled
2014-10-07 00:00:00 QQQ $ 93.883103976 USD Market On Open 353 Long Filled
2014-10-07 00:00:00 EEM $ 38.45413154 USD Market On Open 866 Long Filled
Note I had 3 Up Insights in October but only two securities were purchased. IWM is not
purchased until its November Up Insight. Then in December I would expect my whole portfolio to
be rebalanced to the remaining security with an Up Insight (QQQ). Instead, some QQQ is sold.
The odd behavior continues for the remainder of the backtest with one or two shares generally being
bought or sold but to real rebalancing taking place.
Jared Broad
Hey Heather, it's an interesting post -
In the EqualWeightingPortfolioConstructionModel (long only) portfolio model when one of the Insights goes flat/down; it should re-shuffle the capital to be fully allocated to the remaining active insights assets. If possible can you attach an algorithm or send one through to support if you don't think this is happening. We can look into it further that way, (possibly some of the orders are being rejected?)
PS:
direction = activeInsights[-1].Direction
targets.append(PortfolioTarget.Percent(algorithm, symbol, direction * percent))
Looking at the raw model code it looks like its incorrectly assigning 33% weighting to the flat case -- locking up that capital even though it shouldn't be allocated any resources.
Github Issue Tracker for the case.
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.
Heather Lamb
I attached my test algo and a backtest showing the problem.
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!