I have been thinking thinking lately how to make any algorithm slightly more accurate, but first I need to understand how algorithms are executed.
I am assuming for my moderate programming skills that if you import 1 minute data that an algorithm is then triggered/executed every time there is a new minute data imported. For example, an algorithm is triggered at 10:30am, 10:31, 10:32, etc.
For a simple example let's say we have two EMAs. One at 5 minutes and the other at 30 minutes. Buy when the 5min crosses above the 30min and vice versa.
With the algorithm being updated every minute, we will know when the two EMAs cross with a minute resolution (Program updates say at 10:32, you know if the 5 minute crossed over/under the 30 minute EMA in the last 60 seconds).
Is it possible to have the program run every tick or second so that you know the exact moment the two EMAs cross? Therefore, if the EMAs crossed at 10:31:24, you would pretty much instantly know instead of 36 seconds later if the algo updated every minute (which would be at 10:32). Also the EMAs would then be calculated using a significant more data points rather than only 5 and 30 points (The 5 minute would use data from 10:26:24 to 10:31:24).
I figured this could make algos slightly more accurate. But not sure how it would look for programming it, my first thought would be to import tick/second data (assuming the algo is updated every time data is brought in) then consolidate that into 5min and 30min to get the two EMAs (but I feel that waits to have the total amount of data before outputting a result) I would think you need a moving window style consolidator.
Any feedback on this would be great, I am trying to grow a better understanding on how this program works, while working towards a successful Algorithm.
Derek Melchin
Hi Tate,
To check what the value of an indicator would be if we updated it with a new value, we can use the `ComputeNextValue` method. For example, if we create two EMAs and update them each minute.
self.symbol = self.AddEquity("SPY", Resolution.Second).Symbol self.fast_ema = ExponentialMovingAverage(5) self.slow_ema = ExponentialMovingAverage(30) self.Consolidate("SPY", timedelta(minutes=1), self.consolidation_handler)
Then when a new second resolution bar is passed to OnData, we can check what the current values of the EMAs would be at the current time.
# Check what the value of the EMAs would be currently, before the next minute-bar update close = data[self.symbol].Close slow = self.slow_ema.ComputeNextValue(IndicatorDataPoint(data.Time, close)) fast = self.fast_ema.ComputeNextValue(IndicatorDataPoint(data.Time, close))
See the attached backtest and logs for reference.
Best,
Derek Melchin
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.
R G Quant
Hi Derek,
isn't that a solution only for the `ExponentialMovingAverage` and few other indicators for which `ComputeNextValue` does not change the state of the indicator?
ExponentialMovingAverage
from my understanding looking at the implementation of e.g `SimpleMovingAverage` or `HullMovingAverage`, calling `ComputeNextValue` would actually update the indicator, not only return what it “would” be:
SimpleMovingAverage
HullMovingAverage
By using the solution you suggested with `SimpleMovingAverage` or `HullMovingAverage` we would end up filling the indicator with minutely values instead of Daily values.
Is there a way to actually compute what the value would be of any indicator without risking to update its state?
In order to achieve what Tate was thinking of I am currently on every iteration (Minute in my case) resetting my indicators and refilling them again with data from a rolling window (e.g of Close values of latest X days) plus the current minute close.
But of course this slows everything down.
Any idea on how to achieve this more efficiently?
It would be great if the Indicator class would expose a public method with no side effects for this purpose. What do you think?
Tate Linzel
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!