Back

How to update an indicator on a timer?

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?

Update Backtest








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));
//}
}
0

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,

0

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)
0

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

0

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...

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