Back

Current python code isn't buying positions until mid 2014.

When building off a learning sample I am running into an issue where the code is not executing trades until mid 2014. Before adding alternate equities, it was working fine for the time range, but now it does not. Am I missing something?

 

import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *

### <summary>
### In this example we look at the canonical 15/30 day moving average cross. This algorithm
### will go long when the 15 crosses above the 30 and will liquidate when the 15 crosses
### back below the 30.
### </summary>
### <meta name="tag" content="indicators" />
### <meta name="tag" content="indicator classes" />
### <meta name="tag" content="moving average cross" />
### <meta name="tag" content="strategy example" />
class MovingAverageCrossAlgorithm(QCAlgorithm):

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(2008, 1, 1) #Set Start Date
self.SetEndDate(2015, 1, 1) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddEquity("SPY")
self.AddEquity("GOOG")
self.AddEquity("AMZN")
self.AddEquity("MSFT")
self.AddEquity("AAPL")

# create a 15 day exponential moving average
self.fast = self.EMA("SPY", 15, Resolution.Daily)
self.fasta = self.EMA("GOOG", 15, Resolution.Daily)
self.fastb = self.EMA("AMZN", 15, Resolution.Daily)
self.fastc = self.EMA("MSFT", 15, Resolution.Daily)
self.fastd = self.EMA("AAPL", 15, Resolution.Daily)

# create a 30 day exponential moving average
self.slow = self.EMA("SPY", 30, Resolution.Daily)
self.slowa = self.EMA("GOOG", 30, Resolution.Daily)
self.slowb = self.EMA("AMZN", 30, Resolution.Daily)
self.slowc = self.EMA("MSFT", 30, Resolution.Daily)
self.slowd = self.EMA("AAPL", 30, Resolution.Daily)

self.previous = None


def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
# a couple things to notice in this method:
# 1. We never need to 'update' our indicators with the data, the engine takes care of this for us
# 2. We can use indicators directly in math expressions
# 3. We can easily plot many indicators at the same time

# wait for our slow and fast ema to fully initialize
if not self.slow.IsReady:
return
if not self.fast.IsReady:
return
if not self.slowa.IsReady:
return
if not self.fasta.IsReady:
return
if not self.slowb.IsReady:
return
if not self.fastb.IsReady:
return
if not self.slowc.IsReady:
return
if not self.fastc.IsReady:
return
if not self.slowd.IsReady:
return
if not self.fastd.IsReady:
return

# only once per day
if self.previous is not None and self.previous.date() == self.Time.date():
return

# define a small tolerance on our checks to avoid bouncing
tolerance = 0.00015

holdings = self.Portfolio["SPY"].Quantity
holdingsa = self.Portfolio["GOOG"].Quantity
holdingsb = self.Portfolio["AMZN"].Quantity
holdingsc = self.Portfolio["MSFT"].Quantity
holdingsd = self.Portfolio["AAPL"].Quantity

# we only want to go long if we're currently short or flat
if holdings <= 0:
# if the fast is greater than the slow, we'll go long
if self.fast.Current.Value > self.slow.Current.Value *(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["SPY"].Price))
self.SetHoldings("SPY", .2)
if holdingsa <= 0:
if self.fasta.Current.Value > self.slowa.Current.Value *(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["GOOG"].Price))
self.SetHoldings("GOOG", .2)
if holdingsb <= 0:
if self.fastb.Current.Value > self.slowb.Current.Value *(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["AMZN"].Price))
self.SetHoldings("AMZN", .2)
if holdingsc <= 0:
if self.fastc.Current.Value > self.slowc.Current.Value *(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["MSFT"].Price))
self.SetHoldings("MSFT", .2)
if holdingsd <= 0:
if self.fastd.Current.Value > self.slowd.Current.Value *(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["AAPL"].Price))
self.SetHoldings("AAPL", .2)

# we only want to liquidate if we're currently long
# if the fast is less than the slow we'll liquidate our long
if holdings > 0 and self.fast.Current.Value < self.slow.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["SPY"].Price))
self.Liquidate("SPY")
if holdingsa > 0 and self.fasta.Current.Value < self.slowb.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["GOOG"].Price))
self.Liquidate("GOOG")
if holdingsb > 0 and self.fastb.Current.Value < self.slowb.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["AMZN"].Price))
self.Liquidate("AMZN")
if holdingsc > 0 and self.fastc.Current.Value < self.slowc.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["MSFT"].Price))
self.Liquidate("MSFT")
if holdingsd > 0 and self.fastd.Current.Value < self.slowd.Current.Value:
self.Log("SELL >> {0}".format(self.Securities["AAPL"].Price))
self.Liquidate("AAPL")

self.previous = self.Time

 

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.


Hi Sterling, 

I've run your code. After replacing symbol "GOOG" with "GOOGL", your strategy works fine. Additionally, I found a typo in the strategy: on line 123 of your attached code snippet, "slowb" should be "slowa". I've attached a backtest. You can see that trades now start from 2008 successfully. 

The problem is that Google split its stock into "GOOGL" (Class A shares) and "GOOG" (Class C shares) in April 2014. Ticker symbol "GOOG" only pulls data since 2014. This article explains their difference. You may want to check it out to determine whether you want to trade "GOOG" or "GOOGL".

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.


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