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
Davidii111
Any expert out there who feels like giving me a hint? I would appreciate it very much :)
Flame
self.gold is just a string. Therefore when you call self.gold.Current.Value you are just trying to pull the methods from the string. You also probably need to add the custom data before you assign your moving average variable.
Would it not be easier to use a gold ETF like GLD? or is there a specific reason you would like to use Quandl data?
Flame
Perhaps this is what you were after?
Jack Simonson
Hi David,
I took a look at your code and the reason the algorithm is returning the error
"During the algorithm initialization, the following exception has occurred: Exception: Please register to receive data for symbol ' " using the AddSecurity() function"
is because the Quandl data is being added after the indicator is initialized. Also, the best way to register an indicator for custom data would be as follows
self.moving_average = ExponentialMovingAverage(self.gold, 200, Resolution.Daily) self.RegisterIndicator("WGC/GOLD_DAILY_USD", self.moving_average, Resolution.Daily)
You can find helpful documentation on indicators and using custom data here.
In the OnData section, the algorithm is calling self.gold.Current.Value which won't return anything. In OnData, the 'Value' column of the Quandl data can be accessed by simply subsetting the slice object with the symbol, since self.gold is just a string, it doesn't have a Symbol attribute and so only self.gold needs to be passed as the symbol argument in the set.Holdings() call.
def OnData(self,data): if self.moving_average.IsReady: self.IsUpTrend = data[self.gold] > self.moving_average.Current.Value * self._tolerance self.IsDownTrend = data[self.gold] < self.moving_average.Current.Value * self._tolerance else: return if (not self.Portfolio.Invested) and self.IsUpTrend: self.SetHoldings(self.gold, 1) if self.Portfolio.Invested and self.IsDownTrend: self.Liquidate()
Davidii111
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!