I've been trying to add an awesome oscillator into my strategy with the dynamic universe. However, I'm having problems warming up the awesome oscillator manually. Part of my code is as below:
def OnData(self, data):
self.Debug(f'Today is {self.Time} in OnData')
if self._changes is None:
return
# close positions in removed securities
for x in self._changes.RemovedSecurities:
symbol = x.Symbol
self.Debug(f'AddedSecurities: {len(self._changes.AddedSecurities)}')
for x in self._changes.AddedSecurities:
symbol = x.Symbol
self.AddEquity(symbol, Resolution.Daily)
if symbol not in self.awesome_osc_indicator:
self.awesome_osc_indicator[symbol] = self.AO(
symbol,
5,
34,
MovingAverageType.Simple,
Resolution.Daily
)
warmUpData = self.History(symbol, 40, Resolution.Daily)
for bar in warmUpData.loc[symbol, :].itertuples():
self.awesome_osc_indicator[symbol].Update(pd.DataFrame(bar))
When I ran this code, I'll receive the below error message (the other indicators like MACD can pass the backtest without error):
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'pandas.core.frame.DataFrame'>) method. Please checkout the API documentation.
at OnData
self.awesome_osc_indicator[symbol].Update(pd.DataFrame(bar))
===
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py:line 161
TypeError : No method matches given arguments for Update: ()
I don't find any relevant documentation regarding this indicator in the indicator document, source code, or any relevant post in the forum. Can anyone enlight me on how to achieve this?
Michael Hsia
Did another round of research and testing.
To manually update the awesome oscillator, there is only one step missing from the above code.
In one of the error messages in my backtest results, I think I found a piece of clue telling me that I need to update the awesome oscillator with TradeBar data. Also, as stated in historic data document, the history API returns a DataFrame. So the missing part would be how to transform a DataFrame into a TradeBar object.
Thanks to the post here, you simply need to add one line of code before we update/warmup the awesome_osc_indicator object:
By adding this code, I would be able to run my algo without error. Still checking if there's anything I missed.
Hope this help those who have the same question as I do.
Varad Kabade
Hi Michael Hsia,
You are correct about the part of using TradeBar to update the given indicator. After looking at the code snippet we recommend following changes:
Moving the above code to OnSecuritiesChanges event handeler instead of OnData method. Also we need to remove
from OnData method as UniverseSelection automatically subscribes to the securities selected by the universe.
Best,
Varad Kabade
Vladimir
Varad kabade,
I am interested in your approach to the problem, but my attempts
to assemble your recommendations were unsuccessful.
Can you attach a backtest with your recommendations?
Michael Hsia
@Varad kabade
Thanks for the suggestion. Actually I'm thinking to move those codes into the ScheduleOn function to calculate the indicators whenever I need them. And you're right, I will remove the AddEquity() part.
Varad Kabade
Hi Vladimir and Michael,
Vladimir, I have implemented a simple algorithm where we subscribe to single security using universe selection and create an indicator in OnSecuritiesChanged event handler. Refer to the attached backtest.
Micheal, using a Schedule event seems like a good approach. Moving forward, as you are using dynamic universe algorithm is creating indicator subscriptions for every security added to the universe, but it never removes them. This can slow down you're backtesting considerably. To resolve the issue, we should:
Refer to this related thread for an example of this process.Best,Varad Kabade
Michael Hsia
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!