I keep running into an error when trying to backtest this Awesome Oscillator code. If anyone could help me out, thank you!
QUANTCONNECT COMMUNITY
I keep running into an error when trying to backtest this Awesome Oscillator code. If anyone could help me out, thank you!
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.
Samuele scarfone
import numpy as np
from collections import deque
from datetime import datetimeÂ
class AwesomeOscillator:
  '''
  //@version=3
  study(title="Awesome Oscillator", shorttitle="AO")
  ao = sma(hl2,5) - sma(hl2,34)
  plot(ao, color = change(ao) <= 0 ? red : green, style=histogram)
  '''
  def __init__(self, period_fast=5, period_slow=34):
    Â
    self.Name = "Awesome Osc - {}, {}".format(period_fast, period_slow)
    self.Time = datetime.min
    self.Value = 0
    self.IsReady = False
    Â
    self.fast_sma_queue = deque(maxlen=period_fast)
    self.slow_sma_queue = deque(maxlen=period_slow)
    Â
  def __repr__(self):
    return "{0} -> IsReady: {1}. Time: {2}. Value: {3}".format(self.Name, self.IsReady, self.Time, self.Value)
  # Update method is mandatory
  def Update(self, input):
    Â
    # Fill the queues
    hl2 = (input.High + input.Low) / 2
    self.fast_sma_queue.appendleft(hl2)
    self.slow_sma_queue.appendleft(hl2)
    Â
    # Calc the SMA's
    fast_count = len(self.fast_sma_queue)
    fast_sma = sum(self.fast_sma_queue) / fast_count
  Â
    slow_count = len(self.slow_sma_queue)
    slow_sma = sum(self.slow_sma_queue) / slow_count
    Â
    self.Value = fast_sma - slow_sma
    Â
    self.Time = input.EndTime
    self.IsReady = slow_count == self.slow_sma_queue.maxlen
    Â
    Â
class BasicTemplateAlgorithm(QCAlgorithm):
  '''Basic template algorithm simply initializes the date range and cash'''
  def Initialize(self):
    '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
    self.SetStartDate(2018,1,1)   #Set Start Date
    self.SetEndDate(2019,1,1)    #Set End Date
    self.SetCash(100000)      #Set Strategy Cash
    # Find more symbols here: http://quantconnect.com/data
    self.AddEquity("SPY", Resolution.Daily)
    Â
    self.AO = AwesomeOscillator()
    self.RegisterIndicator("SPY", self.AO, Resolution.Daily)
    Â
    # Create a chart for the indicator
    AOChart = Chart("Awesome", ChartType.Stacked)
    AOChart.AddSeries(Series('AO', SeriesType.Line))
    Â
Â
  def OnData(self, data):
    '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
    Arguments:
      data: Slice object keyed by symbol containing the stock data
    '''
    Â
    self.Debug(self.AO)
    Â
    if not self.AO.IsReady: return
    Â
    self.Plot('Awesome', 'AO', self.AO.Value)
Vladimir
Samuele scarfone,
Here is my attempt at creating Awesome Oscillator as custom indicator.
Samuele scarfone
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!