Back

Python: indicator on multiple timeframe - default registration causes an issue

My intention is to look at the same indicator (Ichimoku) on different time frames.

I'm writing a strategy in Python and came across the following blocker:

Here is the code:

def Initialize(self):
instr = "AUDJPY"
self.SetStartDate(DateTime(2017, 01, 01))
self.SetEndDate(datetime.now() - timedelta(1))          
symbol = self.AddForex(instr).Symbol
fourHourConsolidator = QuoteBarConsolidator(timedelta(minutes=240))
fourHourConsolidator.DataConsolidated += self.FourHourBarHandler
self.SubscriptionManager.AddConsolidator(instr, fourHourConsolidator)
self.fourHourIchimoku = self.ICHIMOKU(instr, 9, 26, 26, 52, 26, 26)  # the indicator is automatically registered with the default resolution
self.RegisterIndicator(symbol, self.fourHourIchimoku, fourHourConsolidator, None) # this will cause Update to be called after 240 min and will fail.

And after the first 239 minutes, the backtest fails:

2017-01-03 01:00:00 Runtime Error: System.ArgumentException: This is a forward only indicator: ICHIMOKU(9,26)(AUDJPY_min) Input: 2017-01-03 00:00:00Z Previous: 2017-01-03 00:58:00Z

The reason being: Algorithm\QCAlgorithm.Indicators.cs, line 430 the indicator is automatically registered with the default resolution (1 min). Hence once the 240 minutes are reached, Update is called a second time and because the time of the consolidated 240 minutes bar equals the time of the very first minute, it fails on line 77 \Indicators\IndicatorBase.cs

Is there a work around? Instantiating an indicator without having it registered? In C#, I could invoke the constructor directly... 

Thanks,

Jerome

Update Backtest








Yes, you should invoke the constructor directly and then register it:

self.fourHourIchimoku = IchimokuKinkoHyo("", 9, 26, 26, 52, 26, 26)
self.RegisterIndicator(symbol, self.fourHourIchimoku, fourHourConsolidator, None)

 

0

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.


Hi Alexandre, 

Thanks for your answer. But that's my point: the indicator class hasn't been made available to python. So if I use the suggested syntax I get a 

Algorithm.Initialize() Error: Loader.TryCreatePythonAlgorithm(): Unable to import python module

 

 

0

Could you please try to re-run your algorithm?
We couldn't reproduce that issue. 
Please checkout the attached backtest.

0

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.


Hi Alexandre, thanks again for taking the time around this. I pasted your code in my project and it works! For information, I took my template directly from the github repo and it adds loads of imports and reference, without investigating further, I assume this causes the issue.

link to DataConsolidationAlgorithm.py

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/DataConsolidationAlgorithm.py

from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import *
from QuantConnect.Data.Consolidators import *

 

0

Update Backtest





0

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.


Loading...

This discussion is closed