Hello!
I'm trying to build a timer to update the settings on an indicator. I've read various forums posts that talk about a Update() function but I haven't been able to figure it out. Here is some simple sample code of what I'm trying to build. Any help?
Thanks!
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
resolutionSetting = Resolution.Minute
macdFast = 5
macdSlow = 15
macdPeriod = 5
self.__macd = self.MACD('SPY', macdFast, macdSlow, macdPeriod, MovingAverageType.Exponential, resolutionSetting)
# Set a timer for every 10 mins
self.Schedule.On(self.DateRules.EveryDay(), \
self.TimeRules.Every(timedelta(minutes=10)), \
Action(self.UpdateIndicators))
def UpdateIndicators(self):
# Update the MACD fast, slow, period settings here somehow?
# self.__macd.Update(NewMacdFast, NewMacdSlow, NewMacdPeriod) maybe?
Quant Trader
Not sure if I help you by providing some C# code, but you need to take into account what the base class of the indicator is and then bring in new tradebars or indicator data, you cannot directly influence the values as the class does not expose them to you as the maker of the indicator has protected the code so you cannot.
//warm up https://www.quantconnect.com/forum/discussion/2557/dealing-with-futures-and-technical-indicators/p1 var history = History(contract.Symbol, (Math.Max(_annualizedExponentialSlope[contract.Symbol].Period, _movingAverageWindowSlow) + 2), Resolution.Daily); foreach (var bar in history) { //if (bar.EndTime < Time) //avoid looking forward bias //{ _annualizedExponentialSlope[contract.Symbol].Update(new IndicatorDataPoint(contract.Symbol, bar.EndTime, bar.Close)); _movingAverageSlow[contract.Symbol].Update(new IndicatorDataPoint(contract.Symbol, bar.EndTime, bar.Close)); _movingAverageFast[contract.Symbol].Update(new IndicatorDataPoint(contract.Symbol, bar.EndTime, bar.Close)); _averageTrueRange[contract.Symbol].Update(new TradeBar(bar.EndTime, contract.Symbol, bar.Open, bar.High, bar.Low, bar.Close, bar.Volume)); //} }
Drew Baker
Thanks @Quant_trader.
I'm not very good at C#, so if anyone can help translate this to something a little easier to understand that would be great,
Drew Baker
If anyone is curious, here is what I ended up doing. It's not really updating the MACD, it's just replacing it with a new one on a timer.
It seems to work. If anyone spots anything wrong, please let me know as I'm new to this.
class myAlgorithm(QCAlgorithm): def Initialize(self): # Define our MACD Signal resolutionSetting = Resolution.Minute macdFast = 5 macdSlow = 15 macdPeriod = 5 self.__macd = self.MACD('BTCUSD', macdFast, macdSlow, macdPeriod, MovingAverageType.Exponential, resolutionSetting) # Setup charts self.SetupCharts() # Set a timer for every 10 mins self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(timedelta(minutes=10)), Action(self.UpdateIndicators)) def SetupCharts(self): # Setup MACD lines chart macdChart = Chart('MACD') macdChart.AddSeries(Series('MACD Fast', SeriesType.Line, 0)) macdChart.AddSeries(Series('MACD Slow', SeriesType.Line, 0)) self.AddChart(macdChart) def UpdateIndicators(self): # New MACD settings resolutionSetting = Resolution.Minute macdFast = 20 macdSlow = 60 macdPeriod = 10 self.__macd = self.MACD('BTCUSD', macdFast, macdSlow, macdPeriod, MovingAverageType.Exponential, resolutionSetting) def OnData(self, data): # Wait for our MACD to fully initialize if not self.__macd.IsReady: return # Plot MACD manually to a chart (because I couldn't figure out how to get PlotIndicator update with new settings) self.plotCharts() # Do MACD stuff here, buy/sell, whatever... self.signalDeltaPercent = (self.__macd.Current.Value - self.__macd.Signal.Current.Value)/self.__macd.Fast.Current.Value def plotCharts(self): # Plot MACD lines self.Plot('MACD', 'MACD Fast', self.__macd.Fast.Current.Value) self.Plot('MACD', 'MACD Slow', self.__macd.Slow.Current.Value)
Andrea Ardemagni
Hi Drew, I'm 100% new to QuantConnect and trying to find some examples to learn from. Apologizes for my simple questions, I hope you could help me a little bit:
1. I see you don't have the start date and end date, why?
2. what's the goal of this code? You first have macdslow and macdfast at 15 and 5 but then every ten minutes you change these values to 20 and 60? I don't follow the logic.
Any suggestions it's incredibly appreciated. Thanks
Drew Baker
Hello Andrea Andrea! Here are my answers:
1. I see you don't have the start date and end date, why?
I was just trying to show the bare basics of how to get an updating indicator working. You obviously want to put start/stop and what stock/cryto you were using etc... This is a good example of a basic algo that helped me a lot to lern how QC works.
2. what's the goal of this code? You first have macdslow and macdfast at 15 and 5 but then every ten minutes you change these values to 20 and 60? I don't follow the logic.
This code had no goal, just to show a basic example of how to update an indicator. How you use htis is up to you. One idea might be to have different MACD trend line settings for a bear or bull market...
Brent Lewis
Drew Baker Did that update implimentation end up working? Seems like the engine runs a computation on the whole data set similar to TA-Lib if that's the case.. shouldn't be too hard to make an interface in python that you can use to just call them then. Would that be worth the time to make it though?
Drew Baker
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!