Hi all
I am new to writing in python. I typically have been coducting my back tests using R and using data frames or basically working with historical data.
I started to learn on Quantopian, however, I am now here!
So what I am trying to do is make a rolling z-score of a price series:
# Create zscore
self.sma = self.SMA("SPY", 20, Resolution.Daily);
self.std = self.STD("SPY", 20, Resolution.Daily);
self.zscore = (SPY.Close.Current.Value - self.sma.Current.Value) / self.std.Current.Value
I am storing this in def Initialize(self):
My confusion lies in how the indicators are created... are they based on the Close price, or do i need to specify that?
Also as far as making indicators in this way, if i do the zscore calculation:
(close - sma) / std dev...
Will this take the current price or do i need to specify?
Getting hung up on the small stuff, anyone shed some light?
Thanks
Andrew
Alexandre Catarino
The SMA and STD methods create indicators that are updated by the engine.
However the zscore attribute you created won't be updated, because it is not an indicator, it is just a number:
# In Initialize: self.sma = self.SMA("SPY", 20, Resolution.Daily) self.std = self.STD("SPY", 20, Resolution.Daily) # In OnData(self, data): self.zscore = (data["SPY"].Close - self.sma.Current.Value) / self.std.Current.Value
By default, data point indicator (those that take only one value: close, open, high, low, etc) are based on closing price. If you want to choose another field:
# In Initialize: self.sma_open = self.SMA("SPY", 20, Resolution.Daily, Field.Open) self.sma_high = self.SMA("SPY", 20, Resolution.Daily, Field.High) self.sma_low = self.SMA("SPY", 20, Resolution.Daily, Field.Low) self.sma_close = self.SMA("SPY", 20, Resolution.Daily)
Andrew Bannerman
Ok that makes sense. So I can pre create anything in initialize, it would use historical data. Then as I am using current data, I place it in # In OnData(self, data)?
So if I place in OnData
self.zscore = (data["SPY"].Close - self.sma.Current.Value) / self.std.Current.Value
I may use this for trading logic... say for example:# we only want to go long if we're currently short or flat
if holdings <= 0:
# if the zscore < 0, we'll go long
if self.zscore.Current.Value < 0 * d.Decimal(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["UPRO"].Price))
self.SetHoldings("UPRO", 1.0)
# we only want to liquidate if we're currently long
# if zscore >0 we'll liquidate our long
if holdings > 0 and self.zscore.Current.Value > 0:
self.Log("SELL >> {0}".format(self.Securities["UPRO"].Price))
self.Liquidate("UPRO")
I have made this (essentially edited the moving average cross over on github). Everything is the same in the code essentially except a few edits to replace ma's with zscores and also using numerical values for enter / exit, however it comes back with errors.
Alexandre Catarino
Since zscore is just a number, not an indicator.
Current is a attribute of indicator, it is a data point (time + value), so we get the value with Current.Value.
You only need historical data if you want to warm up your indicators. In this case, it would take 20 days. Please use IsReady to check whether the indicator is ready to be used:
# In OnData:
if not self.sma.IsReady or not self.std.IsReady:
return
In Initialize, you can set up a warm up period and the algorithm will look for data before StartDate to warm up your indicators:
# In Initialize
# 30 days warm up for 20 point indicators because of weekends
self.SetWarmup(timedelta(30))
# In OnData
if self.IsWarmingUp:
return
Andrew Bannerman
Ok thanks - I hopfully find they have a more beginner tutorial for people like me!! One that explains the smaller details.
Andrew Bannerman
This is my code so far:
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 *
import decimal as d
class QCAlgorithm:
'''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.'''
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.'''
# 30 days warm up for 20 point indicators because of weekends
self.Warmup(timedelta(30))
self.SetStartDate(2009, 01, 01) #Set Start Date
self.SetEndDate(2017, 01, 01) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddEquity("SPY", Resolution.Daily)
self.AddEquity("UPRO", Resolution.Daily)
# Create sma / stdev
self.sma = self.SMA("SPY", 20, Resolution.Daily);
self.std = self.STD("SPY", 20, 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
# Create Zscore
self.zscore = (data["SPY"].Close - self.sma.Current.Value) / self.std.Current.Value
print(self.zscore)
if not self.sma.IsReady or not self.std.IsReady:
return
if self.IsWarmingUp:
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["UPRO"].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.zscore.Current.Value < 0 * d.Decimal(1 + tolerance):
self.Log("BUY >> {0}".format(self.Securities["UPRO"].Price))
self.SetHoldings("UPRO", 1.0)
# 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.zscore.Current.Value > 0:
self.Log("SELL >> {0}".format(self.Securities["UPRO"].Price))
self.Liquidate("UPRO")
self.previous = self.Time
It throws a warmup error for some reason.
Jared Broad
I think its self.SetWarmup(20) -- not Warmup
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.
Andrew Bannerman
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!