from NodaTime import DateTimeZone
from QuantConnect.Python import PythonQuandl
from datetime import timedelta
import decimal as d
import numpy as np
class GoldMarketTimingAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2006, 1, 1)
self.SetEndDate(2012, 12, 31)
self.SetCash(100000)
maPeriod = 200
self._tolerance = d.Decimal(1 + 0.001)
self.IsUpTrend = False
self.IsDownTrend = False
self.SetWarmUp(200)
# Adds SPY
self.AddEquity("SPY", Resolution.Daily)
# Gold Prices (Daily) - Currency USD (All values are national currency units per troy ounce)
self.gold = "WGC/GOLD_DAILY_USD"
self.moving_average = self.EMA(self.gold, maPeriod, Resolution.Daily)
# Add custom quandl data
self.AddData(QuandlValue, self.gold, Resolution.Daily, DateTimeZone.Utc, True)
def OnData(self, data):
if self.moving_average.IsReady:
self.IsUpTrend = self.gold.Current.Value > self.moving_average.Current.Value * self._tolerance
self.IsDownTrend = self.gold.Current.Value < self.moving_average.Current.Value * self._tolerance
if (not self.Portfolio.Invested) and self.IsUpTrend:
self.SetHoldings(self.gold.Symbol, 1)
if self.Portfolio.Invested and self.IsDownTrend:
self.Liquidate()
def OnEndOfDay(self):
if self.IsUpTrend:
self.Plot("Indicator Signal", "EOD",1)
elif self.IsDownTrend:
self.Plot("Indicator Signal", "EOD",-1)
elif self._slow.IsReady and self._fast.IsReady:
self.Plot("Indicator Signal", "EOD",0)
def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))
class QuandlRate(PythonQuandl):
def __init__(self):
self.ValueColumnName = "rate"
class QuandlValue(PythonQuandl):
def __init__(self):
self.ValueColumnName = "Value"
This algo does not work. Is there a problem with the variable self.gold?
I get following message when i try to backtest:
During the algorithm initialization, the following exception has occurred: Exception : Please register to receive data for symbol ' ' using the AddSecurity() function.
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol, System.Nullable`1[T] tickType) [0x00098] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.ResolveConsolidator (QuantConnect.Symbol symbol, System.Nullable`1[T] resolution) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.RegisterIndicator (QuantConnect.Symbol symbol, QuantConnect.Indicators.IndicatorBase`1[T] indicator, System.Nullable`1[T] resolution, System.Func`2[T,TResult] selector) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.EMA (QuantConnect.Symbol symbol, System.Int32 period, System.Nullable`1[T] resolution, System.Func`2[T,TResult] selector) [0x00022] in <1d3439f9966d4d178af513edc463b9bd>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0
at Initialize in main.py:line 32
Exception : Please register to receive data for symbol ' ' using the AddSecurity() function.
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol, System.Nullable`1[T] tickType) [0x00098] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.ResolveConsolidator (QuantConnect.Symbol symbol, System.Nullable`1[T] resolution) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.RegisterIndicator (QuantConnect.Symbol symbol, QuantConnect.Indicators.IndicatorBase`1[T] indicator, System.Nullable`1[T] resolution, System.Func`2[T,TResult] selector) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.EMA (QuantConnect.Symbol symbol, System.Int32 period, System.Nullable`1[T] resolution, System.Func`2[T,TResult] selector) [0x00022] in <1d3439f9966d4d178af513edc463b9bd>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0