I want to call multiple Chinese stocks from Quandl using my strategy in the following algo. Can someone give me a hand to allow me to download multiple Chinese stocks using external data set like Quandl and test their performance in Quantopian? Thanks!
The stocks I want to fetch into the algorithm are listed below:
000023.SZ
000020.SZ
000019.SZ
000018.SZ
000014.SZ
000011.SZ
000010.SZ
000008.SZ
Here is my strategy:
from pandas import TimeSeries
from numpy import NaN
RESISTANCE = 80
def initialize(context):
"""Initialize context object. It's passed to the handle_data function."""
# algo securities and (prev_ma5, prev_ma30, prev_long_trend) placeholders
context.secs = {sid(8554): (None, None, None),
sid(33652): (None, None, None)}
@batch_transform(refresh_period=1, window_length=25)
def get_df(datapanel, field):
# We are looking for the min and the max price to return. Just because it's interesting
# we also are logging the current price.
return datapanel[field]
def SMMA(tseries, period):
"""
Calculate smoothed moving average as described here:
https://mahifx.com/indicators/smoothed-moving-average-smma
:param tseries: time series
:param period: smoothing period
"""
result = TimeSeries([NaN for i in tseries], tseries.index)
# first value: SUM (CLOSE, N)/N
result[period] = tseries[:period].sum()/period
for i in xrange(period+1, len(tseries)):
# second and subsequent values:
# SMMA (i) = (SUM1 – SMMA1 + CLOSE (i))/ N
result[i] = (tseries[i-period:i].sum() - result[period] + tseries.iget(i))/period
return result
def handle_data(context, data):
"""
The main proccessing function.
Called whenever a market event occurs for any of algorithm's securities.
:param context: context object
:param data: Object contains all the market data for algorithm securities
keyed by security id. It represents a snapshot of algorithm's
universe as of when this method is called.
:returns: None
"""
# calculate parameters, based on historical data
high = get_df(data, 'high')
if not high:
# less than 25 days of data
return
low = get_df(data, 'low')
close = get_df(data, 'close_price')
ratio = (close - low)/(high - low)
positions = context.portfolio.positions
for security, (prev_ma5, prev_ma30, prev_long_trend) in context.secs.iteritems():
sec_data = data[security]
up = SMMA(ratio[security.sid] * 30, 4)[3:]
mid = SMMA(up, 3)[2:]
ground = SMMA(mid, 2)[1:]
long_trend = 9 * up.iget(-1) - 3 * mid.iget(-1) - 2 * ground.iget(-1)
if get_open_orders(security):
# skip the security if it has active orders
continue
if security in positions:
# check exit conditions for open positions
if prev_long_trend != None and prev_long_trend >= RESISTANCE \
and long_trend < RESISTANCE:
position = positions[security]
log.debug('%s: prev_long_trend=%f, long_trend=%f' % (security.symbol,
prev_long_trend,
long_trend))
log.info("selling %d shares of %s" % (position.amount, position.sid.symbol))
order(position.sid, -position.amount)
else:
# check enter conditions if no open positions for this security
if prev_ma5 != None and prev_ma30 != None and prev_ma5 <= prev_ma30 \
and sec_data.mavg(5) > sec_data.mavg(30):
log.debug('%s: prev_ma5=%f, prev_ma30=%f, ma5=%f, ma30=%f' % \
(security.symbol, prev_ma5, prev_ma30,
sec_data.mavg(5), sec_data.mavg(30)))
# calculate amount of cash for the order
cash = context.portfolio.cash/(len(context.secs) - len(positions))
# and amount of shares to buy
shares = int(cash/sec_data.price)
log.info("buying %d shares of %s" % (shares, security.symbol))
order(security, shares)
context.secs[security] = (sec_data.mavg(5), sec_data.mavg(30), long_trend)
Here is the fetcher written by David Edwards
https://www.quantopian.com/posts/use-the-fetcher-for-any-quandl-dataset
I want to combine his fetcher with my strategy to use external data via Quantopian.