def capm(symbol, index, start_date=None, end_date=None): stock = data.get_hist(symbol, start_date, end_date) bench = data.get_hist(index, start_date, end_date) y = stock.close y = ind.returnize(y) x = stock.ix[bench.index]['close'] x = ind.returnize(x) regr = OLS(y, x) return regr
def get_non_trading_days(start, end): sh = data.get_hist('sh') # reverse to get non trading_days day = pd.tseries.offsets.CDay(holidays=sh.index) return pd.date_range(start=start, end=end, freq=day)
def get_trade_days(start_date, end_date): sh = data.get_hist('sh') start_i, end_i = trade_index(sh, start_date, end_date) return sh.index[start_i: end_i]
from zipline.utils.factory import create_simulation_parameters sim_params = create_simulation_parameters( start = pd.to_datetime("2014-01-01 09:30:00").tz_localize("Asia/Shanghai").tz_convert("UTC"), #Bug in code doesn't set tz if these are not specified (finance/trading.py:SimulationParameters.calculate_first_open[close]) end = pd.to_datetime("2014-12-31 15:00:00").tz_localize("Asia/Shanghai").tz_convert("UTC"), data_frequency = "daily", emission_rate = "daily", sids = ["600000"]) prefix = '000666' codes = filter(lambda s: s.startswith(prefix), data.get_basics().index) start = '2014-01-01' end = '2015-04-30' benchmark = data.get_hist('sh') d = Market.get_stocks(codes, start, end) # d[code].prices.index = d[code].prices.index.to_datetime().tz_localize('UTC') # d[code].prices['price'] = d[code].prices['close'] d = pd.Panel(d) with open('/home/leo/Workspace/stock/algorithms/aberration.py', 'r') as f: algo_text = f.read() # d = zipline.data.load_bars_from_yahoo(stocks=['AAPL'], start=start, end=end) algo = zipline.TradingAlgorithm(script=algo_text, namespace={}, capital_base=100000, sim_params=sim_params) # results = algo.run(d, benchmark_return_source=d[code]['close'].pct_change()) results = algo.run(d)
import pandas as pd import pytz from datetime import datetime, timedelta from dateutil import rrule from functools import partial import data sh = data.get_hist('sh') # start = sh.index[0] # end_base = pd.Timestamp('today', tz='UTC') # # Give an aggressive buffer for logic that needs to use the next trading # # day or minute. # end = end_base + timedelta(days=365) def get_non_trading_days(start, end): sh = data.get_hist('sh') # reverse to get non trading_days day = pd.tseries.offsets.CDay(holidays=sh.index) return pd.date_range(start=start, end=end, freq=day) non_trading_days = get_non_trading_days(sh.index[0], sh.index[-1]) trading_day = pd.tseries.offsets.CDay(holidays=non_trading_days) def get_trading_days(start, end, trading_day=trading_day): return pd.date_range(start=start.date(), end=end.date(), freq=trading_day).tz_localize('UTC')