Hi everyone,
I want to backtest my own data using zipline
, as we can't use history
() and backtest for imported data.
I have got zipline
installed but I can't find many examples to learn how to use zipline
. Also the last two examples in zipline's example folder are not working. I want to find more detailed and comprehensive examples to learn writing algo using zipline.
Do you know where can I find more strategies examples of using zipline outside quantopian?
I have tried the following code to import my own data and calculate and plot indicators. However, as we can't use history() and backtest for imported data, so it won't work as I intended.
So, my request is that Could anyone help me to rewrite the code below (using zipline, not in quantopian) to achieve the following goals:
- to import the data using the public link I provided and name it 'IF' (how to use
fetcher.csv
)- to calculate and plot
RSI
andSMA
usinghistory
() on close prices of 'IF' data ( how to usehistory
, how to plot signals)- to calculate and print the logbook of any simple strategy you write ( how to calc and print logbook of strategy)
import talib
import numpy as np
import math
import pandas as pd
def initialize(context):
fetch_csv('https://copy.com/OWPM6TLGOZ8IlLj2',
date_column = 'Date',
date_format = '%Y/%m/%d',
pre_func = preview,
post_func = add_fields,
symbol='IF'
) # used as signal
context.stock = symbol('IF') ## I intend to use `history` and backtest to my own imported 'IF' data,
## but only symbol('IF') of quantopian can use history() and backtest
set_benchmark(symbol('IF'))
## set_commission()
## To set the cost of your trades, use the set_commission method and
## pass in PerShare or PerTrade.
## Like the slippage model, set_commission must be used in the initialize
## method and has no effect if used in handle_data.
## If you don't specify a commission,
## your backtest defaults to $0.03 per share.
def preview(df):
log.info(df['IF_close'].head())
return df
def add_fields(df): ## if you can calc indicator without history(), do it here
closes = df['IF_close']
fastMA = pd.rolling_mean(closes, 10)
df['fastMA'] = fastMA # calc and add more signals to IF dataframe
slowMA = pd.rolling_mean(closes, 100)
df['slowMA'] = slowMA
macd = fastMA - slowMA
df['MACD'] = macd
log.info('\n%s ' % df.tail())
return df
def handle_data(context, data):
# close_price = history(1, '1d', 'IF_close')
# log.info(close_price[context.stock])
record(IF_close = data['IF']['IF_close'])
## calc volumes' standard deviation
volumes = history(15, '1d', 'volume') ## select volume data of IF symbol
std = volumes.std()
std_data = std[context.stock]
## calc RSI for close_price
prices = history(15, '1d', 'close_price') ## select close data
## calc RSI for high_price
high_prices = history(15, '1d', 'high') ## select high data
rsi_data = prices.apply(talib.RSI, timeperiod=14).iloc[-1]
rsi_data_high = high_prices.apply(talib.RSI, timeperiod=14).iloc[-1]
IF_rsi = rsi_data[context.stock] # not [data['IF']['close']]
IF_rsi_high = rsi_data_high[context.stock] # not [data['IF']['close']]
## record(close = data['IF']['close']) # there can only be one extra plot area
## record(std = std_data)
# record(RSI = IF_rsi, RSI_high = IF_rsi_high)
record(fastMA = data['IF']['fastMA'])