def stock_process(): start = datetime(2010, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start, end=end) plt.plot(data[:sample_size]) plt.show()
def main(algofile, start, end, save, define, securities): """Runs a strategy in FILE and saves the data in the database""" frequency = 'daily' symbols = [a.upper() for a in securities.split(',')] start_data = start - pd.DateOffset(months=12) define_parts = [assign.split('=', 2) for assign in define] define_dict = {a[0]: a[1] for a in define_parts} define_dict['securities'] = symbols define_dict['frequency'] = frequency data = load_from_yahoo(stocks=symbols, indexes={}, start=start_data, end=end) data = data.dropna() algotext = algofile.read() algoname = getattr(algofile, 'name', '<algorithm>') algo = TradingAlgorithm( script=algotext, algo_filename=algoname, sim_params=create_simulation_parameters(start=start, end=end, data_frequency=frequency), **define_dict) perf = algo.run(data, overwrite_sim_params=False) if save: write_to_db(algoname, algotext, perf, define_dict, start, end)
def example(): start = datetime(1990, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(1991, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=['IBM'], indexes={}, start=start, end=end) dma = DualMovingAverage() results = dma.run(data) index = [br.date for br in trading.environment.benchmark_returns] rets = [br.returns for br in trading.environment.benchmark_returns] bm_returns = pd.Series(rets, index=index).ix[start:end] results['benchmark_returns'] = (1 + bm_returns).cumprod().values results['algorithm_returns'] = (1 + results.returns).cumprod() fig = plt.figure() ax1 = fig.add_subplot(211, ylabel='cumulative returns') results[['algorithm_returns', 'benchmark_returns']].plot(ax=ax1, sharex=True) ax2 = fig.add_subplot(212) data['IBM'].plot(ax=ax2, color='r') results[['short_mavg', 'long_mavg']].plot(ax=ax2) ax2.plot(results.ix[results.buy].index, results.short_mavg[results.buy], '^', markersize=10, color='m') ax2.plot(results.ix[results.sell].index, results.short_mavg[results.sell], 'v', markersize=10, color='k') plt.legend(loc=0)
def run_algorithm( security='AAPL', start_date='20100101', end_date='20150101', initial_cash=100000, rsi_window=15, low_RSI=30, high_RSI=70): logging.debug('run_algorithm begin') # dates start = dateutil.parser.parse(start_date) end = dateutil.parser.parse(end_date) # get data from yahoo data = load_from_yahoo(stocks=[security], indexes={}, start=start, end=end) logging.debug('done loading from yahoo. {} {} {}'.format( security, start_date, end_date)) # create and run algorithm algo = TradingAlgorithm( initialize=initialize, handle_data=handle_data, capital_base=initial_cash) algo.security = security initialize.low_RSI = low_RSI initialize.high_RSI = high_RSI initialize.rsi_window = rsi_window logging.debug('starting to run algo...') results = algo.run(data).dropna() logging.debug('done running algo') return results
def run_algorithm(security='AAPL', start_date='20100101', end_date='20150101', initial_cash=100000, rsi_window=15, low_RSI=30, high_RSI=70): logging.debug('run_algorithm begin') # dates start = dateutil.parser.parse(start_date) end = dateutil.parser.parse(end_date) # get data from yahoo data = load_from_yahoo(stocks=[security], indexes={}, start=start, end=end) logging.debug('done loading from yahoo. {} {} {}'.format( security, start_date, end_date)) # create and run algorithm algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=initial_cash) algo.security = security initialize.low_RSI = low_RSI initialize.high_RSI = high_RSI initialize.rsi_window = rsi_window logging.debug('starting to run algo...') results = algo.run(data).dropna() logging.debug('done running algo') return results
def setUp(self): setup_logger(self) start = pd.datetime(1993, 1, 1, 0, 0, 0, 0, pytz.utc) end = pd.datetime(1994, 1, 1, 0, 0, 0, 0, pytz.utc) self.data = factory.load_from_yahoo(stocks=['AAPL'], indexes={}, start=start, end=end)
def __init__(self, movimientos): self.movimientos = movimientos fechas = [] emisoras = set() for movimiento in movimientos: fechas.append(movimiento.fecha) emisoras.add(movimiento.emisora) emisoras = list(emisoras) fecha_inicio = min(fechas) self.data = load_from_yahoo(stocks=emisoras, indexes={}, start=fecha_inicio, adjusted=False)
def load_symbols(self, name, symbols=['AAPL','FB','CSCO','INTC'], start=None, end=None): start = start or date.today() - datetime.timedelta(days=1*365) end = end or date.today() name = "%s%s" % (self.path, name) print "Loading %s from %s to %s into %s" % (",".join(symbols), start, end, name) if os.path.isfile(name): self.data = pd.read_pickle(name) else: self.data = load_from_yahoo(stocks=symbols, start=start, end=end) self.data.to_pickle(name) return self.data
def querydata(symbols, start, end, useDatastore=True): def utc(dt): return datetime(dt.year, dt.month, dt.day, 0, 0, 0, 0, pytz.utc) if not isinstance(start, datetime): start = utc(datetime.strptime(start, '%Y-%m-%d')) end = utc(datetime.strptime(end, '%Y-%m-%d')) if useDatastore: return load_from_datastore(stocks=symbols, start=start, end=end) else: return load_from_yahoo(stocks=symbols, indexes={}, start=start, end=end)
def test_load_from_yahoo(self): stocks = ['AAPL', 'GE'] start = pd.datetime(1993, 1, 1, 0, 0, 0, 0, pytz.utc) end = pd.datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=stocks, start=start, end=end) assert data.index[0] == pd.Timestamp('1993-01-04 00:00:00+0000') assert data.index[-1] == pd.Timestamp('2001-12-31 00:00:00+0000') for stock in stocks: assert stock in data.columns np.testing.assert_raises( AssertionError, load_from_yahoo, stocks=stocks, start=end, end=start )
def run(pair, start, end, z_threshold, window): class Pairtrade(TradingAlgorithm): def initialize(context): context.in_short = False context.in_long = False context.p1 = context.symbol(pair[0]) context.p2 = context.symbol(pair[1]) context.window = window context.i = 0 def handle_data(context, data): context.i += 1 if context.i < context.window: return s1_history = data.history(context.p1, 'price', context.window, '1d') s2_history = data.history(context.p2, 'price', context.window, '1d') diff = (s1_history - s2_history) mean = diff.mean() s1_price = data.current(context.p1, 'price') s2_price = data.current(context.p2, 'price') zscore = ((s1_price - s2_price) - diff.mean()) / diff.std() context.record(zscore=zscore, s1=s1_price, s2=s2_price) if zscore > z_threshold and not context.in_short: context.order_target_percent(context.p1, -0.5) context.order_target_percent(context.p2, 0.5) context.in_short = True elif zscore < -z_threshold and not context.in_long: context.order_target_percent(context.p1, 0.5) context.order_target_percent(context.p2, -0.5) context.in_long = True elif abs(zscore) < .5: context.order_target_percent(context.p1, 0) context.order_target_percent(context.p2, 0) context.in_short = False context.in_long = False data = load_from_yahoo(stocks=pair, indexes={}, start=start, end=end) pairtrade = Pairtrade() return pairtrade.run(data)
def fetch_equities_daily(self, symbols, ohlc=False, r_type=False, returns=False, **kwargs): if len(symbols) == 0: return pd.DataFrame() if isinstance(symbols, str): symbols = symbols.split(",") if ohlc: data = load_bars_from_yahoo(stocks=symbols, **kwargs) # data.items = symbols else: data = load_from_yahoo(stocks=symbols, **kwargs) # data.columns = symbols # NOTE Would it work with a pandas panel ? if returns: data = ((data - data.shift(1)) / data).fillna(method="bfill") if r_type: data = convert_to_r_matrix(data) return data
def run(symbol, startDate, endDate, startingCapital=100000): class MovingAverage(TradingAlgorithm): def initialize(self): self.stock = self.symbol(symbol) def handle_data(self, data): current_date = data.current_dt.strftime('%Y-%m-%d') if math.isnan(emaLong[current_date]): return if emaShort[current_date] > emaLong[current_date]: self.order_target(self.stock, 100) elif emaShort[current_date] < emaLong[current_date]: self.order_target(self.stock, 0) self.record(price=data.current(self.stock, 'price'), emaShort=emaShort[current_date]) # Fetch Data data = load_from_yahoo(stocks=[symbol], indexes={}, start=startDate, end=endDate) stockReturns = data.tail(1)[symbol].values[0] - data.head( 1)[symbol].values[0] # Setup emaShort = pd.rolling_mean(data[symbol], window=100) emaLong = pd.rolling_mean(data[symbol], window=200) # Back test simpleTrade = MovingAverage() results = simpleTrade.run(data) returnAmount = (results[-1:].portfolio_value.values[0] / startingCapital) - 1 return (stockReturns, returnAmount, results)
def fetch_equities_daily(self, equities, ohlc=False, r_type=False, returns=False, **kwargs): if len(equities) == 0: return pd.DataFrame() if isinstance(equities, str): equities = equities.split(',') symbols = [self.datafeed.guess_name(equity) for equity in equities] if ohlc: data = load_bars_from_yahoo(stocks=symbols, **kwargs) data.items = equities else: data = load_from_yahoo(stocks=symbols, **kwargs) data.columns = equities #NOTE Would it work with a pandas panel ? if returns: data = ((data - data.shift(1)) / data).fillna(method='bfill') if r_type: data = convert_to_r_matrix(data) return data
import warnings warnings.filterwarnings('ignore') import matplotlib.pyplot as plt from datetime import datetime import pytz from zipline.algorithm import TradingAlgorithm from zipline.utils.factory import load_from_yahoo start = datetime(2016, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2016, 12, 1, 0, 0, 0, 0, pytz.utc) pair = ['AMZN', 'FB'] # 6% pair = ['RRC', 'HAL'] # 119% data = load_from_yahoo(stocks=pair, indexes={}, start=start, end=end) z_threshold = 1 class Pairtrade(TradingAlgorithm): def initialize(context): context.in_short = False context.in_long = False context.p1 = context.symbol(pair[0]) context.p2 = context.symbol(pair[1]) context.window = 20 context.i = 0 def handle_data(context, data): context.i += 1
self.sell_spread() self.invested = False def sell_spread(self): """ decrease exposure, regardless of position long/short. buy for a short position, sell for a long. """ ko_amount = self.portfolio.positions['KO'].amount self.order('KO', -1 * ko_amount) pep_amount = self.portfolio.positions['PEP'].amount self.order('PEP', -1 * pep_amount) if __name__ == '__main__': data = load_from_yahoo(stocks=['PEP', 'KO'], indexes={}) pairtrade = Pairtrade() results = pairtrade.run(data) data['spreads'] = np.nan data.spreads[pairtrade.window_length:] = pairtrade.spreads ax1 = plt.subplot(211) data[['PEP', 'KO']].plot(ax=ax1) plt.ylabel('price') plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(212, sharex=ax1) data.spreads.plot(ax=ax2, color='r') plt.ylabel('spread')
tw) > window_size else tw # (6) Create CI for n-day window as price target range # Return tuple with upper and lower bound def CI(window, sds): mean = np.mean(window) sd = np.std(window) return (mean - sds * sd, mean + sds * sd) if __name__ == '__main__': universe = ['FB'] data = load_from_yahoo(stocks=universe, indexes={}, start=datetime(2016, 4, 3), end=datetime.today()) olmar = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=10000) backtest = olmar.run(data) backtest.to_csv('backtest-50-2012.csv') print backtest['algorithm_period_return'][-1] import pyfolio as pf returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline( backtest) pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, gross_lev=gross_lev,
# sad1_amount = self.portfolio.positions[sid1].amount # self.order(sid1, -1 * sad1_amount) for i in range(0, len(securities)): self.order(securities[i], self.portfolio.positions[securities[i]].amount*-1) for i in range(0, len(factors)): self.order(factors[i], self.portfolio.positions[factors[i]].amount*-1) # sad2_amount = self.portfolio.positions[sid2].amount # self.order(sid2, -1 * sad2_amount) if __name__ == '__main__': ########################################################################################################### start = datetime(2006, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks= (securities + factors), indexes={}, start=start, end=end) ## heard to save in the csv file and reget it. because the time zone and the timestampe is different #data.to_csv("PEP_KO_2007-2008.csv",index = True ,cols=('PEP','KO')) # the cols = () is order sensitive. If we save them as PEP and KO in cols them they will change the order # If want to load another dataset then denote these lines. ############################################################################################################ #read_data = pd.DataFrame.from_csv('PEP_KO_2007-2008.csv',infer_datetime_format = True) #read_data.index = pd.DatetimeIndex(read_data["Date"]) pairtrade = Pairtrade() results = pairtrade.run(data) #pd.DataFrame.join(data, results.pnl, on = 'index') result = pd.concat([results.zscores,results.orders,results.positions,results.pnl],axis = 1)
self.order(self.stk.symbol, -1 * stk_amount) self.initBP = 0 # set buying power to 0 self.invested = False self.holdingTime = 0 if __name__ == '__main__': start = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2012, 2, 1, 0, 0, 0, 0, pytz.utc) stksymbol = 'CSCO' etfsymbol = 'SPY' data = load_from_yahoo(stocks=[stksymbol, etfsymbol], indexes={}, start=start, end=end) pairtrade = Pairtrade(stksymbol, etfsymbol) results = pairtrade.run(data) data['spreads'] = np.nan #print results.portfolio_value ax1 = plt.subplot(311) data[[stksymbol, etfsymbol]].plot(ax=ax1) plt.ylabel('price') plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(312, sharex=ax1) results.zscores.plot(ax=ax2, color='r')
def __init__(self, trace=False): BuyApple.trace = trace super(BuyApple, self).__init__() def initialize(context): if BuyApple.trace: print("---> initialize") if BuyApple.trace: print(context) if BuyApple.trace: print("<--- initialize") def handle_data(self, context): if BuyApple.trace: print("---> handle_data") if BuyApple.trace: print(context) self.order("AAPL", 1) if BuyApple.trace: print("<-- handle_data") # %% 02 通过zipline 下载网络数据 # zipline has its own method to load data from Yahoo! Finance data = zpf.load_from_yahoo(stocks=['AAPL'], indexes={}, start=datetime(1990, 1, 1), end=datetime(2014, 1, 1), adjusted=False) # %% 03 输出数据plot # data.plot(figsize=(12,8)); # %% 03 回测 result = BuyApple(trace=False).run(data[0:10]) # %% 04 输出回测结果 result.portfolio_value.plot(figsize=(12, 8))
self.amount = 100 self.data = [] print("--->BuEveryDay.initialize() end") def handle_data(self,data): print('----BuyEveryDay.handle_data().',data[0]['dt']) self.data.append(data[0]['dt']) #mid collect all data self.order(self.sid,self.amount) #mid open 1 long position. self.record(AAPL=data[self.sid].price) #mid add one column named 'AAPL' to returns of Algorithm.run() if __name__ == '__main__': import zipline.utils.factory as zpf from datetime import datetime import matplotlib.pyplot as plt data = zpf.load_from_yahoo(stocks=['AAPL'], indexes={}, start=datetime(1997, 1, 1), end=datetime(1998, 6, 1), adjusted=True) algo = BuyEveryDay(instant_fill=True, capital_base=50000, env=None, sim_params = None, # 设置有此参数时,start和end不能再设置,否则,显得多余也会运行assert错误 #start = algo['start'], #end = algo['end'], data_frequency = 'daily') def dumpDict(dictStr): """""" import json jsonDumpsIndentStr = json.dumps(dictStr, indent=4,skipkeys = False,default=str,sort_keys=True) print (jsonDumpsIndentStr) algo.dumpDict = dumpDict
plt.setp(ax1.get_xticklabels(), visible=False) plt.legend(loc=0) ax2 = fig.add_subplot(312, ylabel='Price') data[algo.symbol].plot(ax=ax2, color='green') plt.setp(ax2.get_xticklabels(), visible=False) plt.legend(loc=0) ax3 = fig.add_subplot(313, ylabel='Score') results.Score.plot(ax=ax3, color='red') results.Ideal.plot(ax=ax3, color='blue') plt.setp(ax3.get_xticklabels(), visible=False) plt.legend(loc=0) plt.gcf().set_size_inches(18, 8) plt.show() if __name__ == '__main__': start = datetime(2012, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2015, 3, 1, 0, 0, 0, 0, pytz.utc) algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=10000) data = load_from_yahoo(stocks=[algo.symbol], indexes={}, start=start, end=end).dropna() results = algo.run(data) show_results(algo, data, results)
'^', markersize=10, color='m') ax2.plot(sells.index, results.short_mavg.ix[sells.index], 'v', markersize=10, color='k') plt.legend(loc=0) else: msg = 'AAPL, short_mavg & long_mavg data not captured using record().' ax2.annotate(msg, xy=(0.1, 0.5)) log.info(msg) plt.show() if __name__ =='__main__': from datetime import datetime import pytz import matplotlib.pyplot as plt from zipline import TradingAlgorithm from zipline.utils.factory import load_from_yahoo start = datetime(1990, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) import pdb pdb.set_trace() data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start, end=end, adjusted=False) algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data) res = algo.run(data).dropna() analyze(res)
# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import matplotlib.pyplot as plt from zipline.algorithm import TradingAlgorithm from zipline.utils.factory import load_from_yahoo class BuyApple(TradingAlgorithm): # inherit from TradingAlgorithm """This is the simplest possible algorithm that does nothing but buy 1 apple share on each event. """ def handle_data(self, data): # overload handle_data() method self.order('SPY', 1) # order SID (=0) and amount (=1 shares) if __name__ == '__main__': data = load_from_yahoo(stocks=['SPY'], indexes={}, start="1990-01-01", end="2012-01-01") simple_algo = BuyApple() results = simple_algo.run(data) results.portfolio_value.plot() plt.show()
context.log.debug(str(notional) + ' - notional start ' + tradeday.strftime('%m/%d/%y')) context.d = tradeday #context.record(short_mavg = short[context.stocks[1]], #long_mavg = long[context.stocks[1]], #goog_price = context.price) if __name__ == '__main__': import sys sys.path.append('/home/mid/PythonProjects/xpower') import zipline.utils.factory as zpf import matplotlib.pyplot as plt data = zpf.load_from_yahoo(stocks=['AAPL', 'IBM', 'CSCO'], indexes={}, start=dt.datetime(2016, 1, 1), end=dt.datetime(2016, 3, 5), adjusted=True) algo = DualEmaTalib(instant_fill=True, capital_base=50000, env=None, sim_params = None, # 设置有此参数时,start和end不能再设置,否则,显得多余也会运行assert错误 #start = algo['start'], #end = algo['end'], data_frequency = 'daily') def dumpDict(dictStr): """""" import json jsonDumpsIndentStr = json.dumps(dictStr, indent=4,skipkeys = False,default=str,sort_keys=True) print (jsonDumpsIndentStr) def analyze(data, results):
def from_yahoo(): start = datetime(2016, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2016, 3, 8, 0, 0, 0, 0, pytz.utc) tickers = ['AAPL', 'GOOGL'] return load_from_yahoo(stocks=tickers, indexes={}, start=start, end=end)
stock.symbol, context.sector_mappings[context.fundamental_df[stock]['morningstar_sector_code']])) order_target_percent(stock, weight) if __name__ == '__main__': import sys sys.path.append('/home/mid/PythonProjects/xpower') import zipline.utils.factory as zpf from datetime import datetime import matplotlib.pyplot as plt data = zpf.load_from_yahoo(stocks=['XLY', 'XLF', 'XLK'], indexes={}, start=datetime(2015, 1, 1), end=datetime(2016, 3, 5), adjusted=True) algo = DualEmaTalib(instant_fill=False, capital_base=50000, env=None, sim_params = None, # 设置有此参数时,start和end不能再设置,否则,显得多余也会运行assert错误 #start = algo['start'], #end = algo['end'], data_frequency = 'daily') def dumpDict(dictStr): """""" import json jsonDumpsIndentStr = json.dumps(dictStr, indent=4,skipkeys = False,default=str,sort_keys=True) print (jsonDumpsIndentStr)
# -*- coding: utf-8 -*- """ Created on Thu Sep 24 15:48:18 2015 @author: mid """ import numpy as np import zipline as zp import zipline.utils.factory as zpf from datetime import datetime import matplotlib.pyplot as plt # load data for Coke and Pepsi and visualize data = zpf.load_from_yahoo(stocks=['PEP', 'KO'], indexes={}, start=datetime(1997, 1, 1), end=datetime(1998, 6, 1), adjusted=True) data.plot(figsize=(12, 8)) plt.savefig('5104OS_07_20.png', bbox_inches='tight', dpi=300) # calculate and plot the spread plt.subplot() data['Spread'] = data.PEP - data.KO data['1997':].Spread.plot(figsize=(12, 8)) plt.ylabel('Spread') plt.axhline(data.Spread.mean()) plt.savefig('5104OS_07_21.png', bbox_inches='tight', dpi=300) import statsmodels.api as sm
ax2 = plt.subplot(212, sharex=ax1) results.zscores.plot(ax=ax2, color='r') plt.ylabel('Z-scored spread') plt.gcf().set_size_inches(18, 8) plt.show() # Note: this if-block should be removed if running # this algorithm on quantopian.com if __name__ == '__main__': logbook.StderrHandler().push_application() # Set the simulation start and end dates. start = datetime(2000, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) # Load price data from yahoo. data = load_from_yahoo(stocks=['PEP', 'KO'], indexes={}, start=start, end=end) # Create and run the algorithm. pairtrade = Pairtrade() results = pairtrade.run(data) # Plot the portfolio data. analyze(results=results)
def get_data(self, sids, start, end): return factory.load_from_yahoo(stocks=sids, indexes={}, start=start, end=end)
# Note: this function can be removed if running # this algorithm on quantopian.com def analyze(context=None, results=None): import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) results.portfolio_value.plot(ax=ax) ax.set_ylabel('Portfolio value (USD)') plt.show() # Note: this if-block should be removed if running # this algorithm on quantopian.com if __name__ == '__main__': # Set the simulation start and end dates. start = datetime(2004, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc) # Load price data from yahoo. data = load_from_yahoo(stocks=STOCKS, indexes={}, start=start, end=end) data = data.dropna() # Create and run the algorithm. olmar = TradingAlgorithm(handle_data=handle_data, initialize=initialize, identifiers=STOCKS) results = olmar.run(data) # Plot the portfolio data. analyze(results=results)
(sym_price - etf_price)) else: action = 'BUY' self.set_log(day, sym, etf, zscore, action, (sym_price - etf_price)) else: action = '---' self.actions[sym]['ACTION'].append(action) return if __name__ == '__main__': feed = build_feed() data = load_from_yahoo(stocks=feed, indexes={}, start=start, end=end, adjusted=True) pairtrade = Pairtrade() results = pairtrade.run(data) ########################################################################### # Generate metrics print 'Generating Risk Report...........' print 'Using S&P500 as benchmark........' start = results.first_valid_index().replace(tzinfo=pytz.utc) end = results.last_valid_index().replace(tzinfo=pytz.utc) env = trading.SimulationParameters(start, end) returns_risk = create_returns_from_list(results.returns, env)
"""""" import json jsonDumpsIndentStr = json.dumps(dictStr, indent=4,skipkeys = False,default=str,sort_keys=True) print (jsonDumpsIndentStr) if __name__ == '__main__': import sys,os xpower = '/home/mid/PythonProjects/xpower' xpower = os.path.abspath(os.path.join(os.path.dirname(__file__),os.pardir)) sys.path.append(xpower) import zipline.utils.factory as zpf from datetime import datetime import matplotlib.pyplot as plt data = zpf.load_from_yahoo(stocks=['CERN', 'DLTR'], indexes={}, start=datetime(2015, 1, 1), end=datetime(2016, 3, 5), adjusted=True) algo = DualEmaTalib(instant_fill=False, capital_base=50000, env=None, sim_params = None, # 设置有此参数时,start和end不能再设置,否则,显得多余也会运行assert错误 #start = algo['start'], #end = algo['end'], data_frequency = 'daily') def analyze(data, results): fig = plt.figure() ax1 = fig.add_subplot(211, ylabel='Price in $') data.plot(ax=ax1, color='r', lw=2.) '''
if ranks is None: return if debug: print "Ranks: ",ranks if debug: print "Self.secs: ",self.secs ranked_secs = sorted(self.secs, key=lambda x: ranks[x], reverse=True) if debug: print "Ranked_secs: ", ranked_secs top_secs = ranked_secs[0:self.top_k] if debug: print "top_secs: ", top_secs wt = dict(((sec,self.weight if sec in top_secs else 0.0) for sec in self.secs)) if debug: print "wt: ",wt self.reweight(data,wt) if __name__ == '__main__': # SPY EFA AGG VNQ GLD data = load_from_yahoo(stocks=['SPY','XLY','XLP','XLE','XLF','XLB','XLK','XLV','XLI','XLU'], indexes={}, start= pd.datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc), end= pd.datetime(2013, 1, 1, 0, 0, 0, 0, pytz.utc)) dma = Faber_TacticalAllocation() results = dma.run(data) if debug: print results.transactions if debug: print results.returns if debug: print results.positions fig = plt.figure() ax1 = fig.add_subplot(211) results.portfolio_value.plot(ax=ax1) ax2 = fig.add_subplot(212) data['SPY'].plot(ax=ax2) # # results[['short_mavg', 'long_mavg']].plot(ax=ax2) # ax2.plot(results.ix[results.buy].index, results.short_mavg[results.buy],
if __name__ == '__main__': import matplotlib.pyplot as plt # run algorithm and get results results = run_algorithm(security='AAPL', start_date='20100101', end_date='20150101', initial_cash=100000, rsi_window=15, low_RSI=30, high_RSI=70) # get s&p500 and nasdaq indexes index_data = load_from_yahoo(stocks=['^gspc', '^ixic'], indexes={}, start=results.index[0], end=results.index[-1]) # portfolio value, stock holdings and S&P 500 index fig = plt.figure(figsize=(12, 6)) ax11 = fig.add_subplot(311) ax12, ax13 = ax11.twinx(), ax11.twinx() ax13.spines['right'].set_position(('axes', 1.07)) ax11.set_ylabel('portfolio value', color='blue') ax12.set_ylabel('holdings', color='green') ax13.set_ylabel('S&P 500', color='red') # portfolio value ax11.plot(results.index, results.portfolio_value, color='blue') # holdings (number of stocks owned)
def sell_spread(self): """ decrease exposure, regardless of position long/short. buy for a short position, sell for a long. """ ko_amount = self.portfolio.positions[self.KO].amount self.order(self.KO, -1 * ko_amount) pep_amount = self.portfolio.positions[self.PEP].amount self.order(self.PEP, -1 * pep_amount) if __name__ == '__main__': start = datetime(2000, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=['PEP', 'KO'], indexes={}, start=start, end=end) pairtrade = Pairtrade() results = pairtrade.run(data) data['spreads'] = np.nan ax1 = plt.subplot(211) # TODO Bugged - indices are out of bounds # data[[pairtrade.PEPsid, pairtrade.KOsid]].plot(ax=ax1) plt.ylabel('price') plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(212, sharex=ax1) results.zscores.plot(ax=ax2, color='r') plt.ylabel('zscored spread')
Original matlab implementation: John Duchi ([email protected]) Python-port: Copyright 2013 by Thomas Wiecki ([email protected]). """ v = np.asarray(v) p = len(v) # Sort v into u in descending order v = (v > 0) * v u = np.sort(v)[::-1] sv = np.cumsum(u) rho = np.where(u > (sv - b) / np.arange(1, p + 1))[0][-1] theta = np.max([0, (sv[rho] - b) / (rho + 1)]) w = (v - theta) w[w < 0] = 0 return w if __name__ == '__main__': import pylab as pl start = datetime(2004, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=STOCKS, indexes={}, start=start, end=end) data = data.dropna() olmar = TradingAlgorithm(handle_data=handle_data, initialize=initialize, identifiers=STOCKS) results = olmar.run(data) results.portfolio_value.plot() pl.show()
self.order(sym, -1 * sym_amount) if sym_amount > 0: action = 'SELL' self.set_log(day, sym, etf, zscore, action, (sym_price-etf_price)) else: action = 'BUY' self.set_log(day, sym, etf, zscore, action, (sym_price-etf_price)) else: action = '---' self.actions[sym]['ACTION'].append(action) return if __name__ == '__main__': feed = build_feed() data = load_from_yahoo(stocks=feed, indexes={}, start=start, end=end, adjusted=True) pairtrade = Pairtrade() results = pairtrade.run(data) ########################################################################### # Generate metrics print 'Generating Risk Report...........' print 'Using S&P500 as benchmark........' start = results.first_valid_index().replace(tzinfo=pytz.utc) end = results.last_valid_index().replace(tzinfo=pytz.utc) env = trading.SimulationParameters(start, end) returns_risk = create_returns_from_list(results.returns, env) algo_returns = RiskMetricsBase(start, end, returns_risk).algorithm_period_returns
elif abs(zscore) < .5 and self.invested: self.sell_spread() self.invested = False def sell_spread(self): """ decrease exposure, regardless of position long/short. buy for a short position, sell for a long. """ CVX_amount = self.portfolio.positions['CVX'].amount self.order('CVX', -1 * CVX_amount) XOM_amount = self.portfolio.positions['XOM'].amount self.order('XOM', -1 * XOM_amount) if __name__ == '__main__': data = load_from_yahoo(stocks=['XOM', 'CVX'], start=datetime(2008,1,1), end=datetime(2012,1,1), indexes={}) pairtrade = Pairtrade() results = pairtrade.run(data) data['spreads'] = np.nan data.spreads[pairtrade.window_length:] = pairtrade.spreads ax1 = plt.subplot(211) data[['XOM', 'CVX']].plot(ax=ax1) plt.ylabel('price') plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(212, sharex=ax1) data.spreads.plot(ax=ax2, color='r') plt.ylabel('spread')
# Record our variables to see the algo behavior. You can record up to # 5 custom variables. To see only a certain variable, deselect the # variable name in the custom graph in the backtest. context.record(short_mavg = short[context.stocks[1]], long_mavg = long[context.stocks[1]], goog_price = context.price) if __name__ == '__main__': import sys sys.path.append('/home/mid/PythonProjects/xpower') import zipline.utils.factory as zpf import matplotlib.pyplot as plt data = zpf.load_from_yahoo(stocks=['BA','AAPL'], indexes={}, start=datetime(2015, 1, 1), end=datetime(2016, 3, 5), adjusted=True) algo = DualEmaTalib(instant_fill=True, capital_base=50000, env=None, sim_params = None, # 设置有此参数时,start和end不能再设置,否则,显得多余也会运行assert错误 #start = algo['start'], #end = algo['end'], data_frequency = 'daily') def dumpDict(dictStr): """""" import json jsonDumpsIndentStr = json.dumps(dictStr, indent=4,skipkeys = False,default=str,sort_keys=True) print (jsonDumpsIndentStr) def analyze(data, results):
elif abs(zscore) < .5 and self.invested: self.sell_spread() self.invested = False def sell_spread(self): """ decrease exposure, regardless of position long/short. buy for a short position, sell for a long. """ ko_amount = self.portfolio.positions['KO'].amount self.order('KO', -1 * ko_amount) pep_amount = self.portfolio.positions['PEP'].amount self.order('KO', -1 * pep_amount) if __name__ == '__main__': data = load_from_yahoo(stocks=['PEP', 'KO'], indexes={}) pairtrade = Pairtrade() results = pairtrade.run(data) data['spreads'] = np.nan data.spreads[pairtrade.window_length:] = pairtrade.spreads ax1 = plt.subplot(211) data[['PEP', 'KO']].plot(ax=ax1) plt.ylabel('price') plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(212, sharex=ax1) data.spreads.plot(ax=ax2, color='r') plt.ylabel('spread')
#context.record(short_mavg = short[context.stocks[1]], #long_mavg = long[context.stocks[1]], #goog_price = context.price) if __name__ == '__main__': import sys sys.path.append('/home/mid/PythonProjects/xpower') import zipline.utils.factory as zpf import matplotlib.pyplot as plt data = zpf.load_from_yahoo(stocks=['AAPL', 'IBM', 'CSCO'], indexes={}, start=dt.datetime(2016, 1, 1), end=dt.datetime(2016, 3, 5), adjusted=True) algo = DualEmaTalib( instant_fill=True, capital_base=50000, env=None, sim_params=None, # 设置有此参数时,start和end不能再设置,否则,显得多余也会运行assert错误 #start = algo['start'], #end = algo['end'], data_frequency='daily') def dumpDict(dictStr): """""" import json jsonDumpsIndentStr = json.dumps(dictStr,
self.portfolio.positions[securities[i]].amount * -1) for i in range(0, len(factors)): self.order(factors[i], self.portfolio.positions[factors[i]].amount * -1) # sad2_amount = self.portfolio.positions[sid2].amount # self.order(sid2, -1 * sad2_amount) if __name__ == '__main__': ########################################################################################################### start = datetime(2006, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=(securities + factors), indexes={}, start=start, end=end) ## heard to save in the csv file and reget it. because the time zone and the timestampe is different #data.to_csv("PEP_KO_2007-2008.csv",index = True ,cols=('PEP','KO')) # the cols = () is order sensitive. If we save them as PEP and KO in cols them they will change the order # If want to load another dataset then denote these lines. ############################################################################################################ #read_data = pd.DataFrame.from_csv('PEP_KO_2007-2008.csv',infer_datetime_format = True) #read_data.index = pd.DatetimeIndex(read_data["Date"]) pairtrade = Pairtrade() results = pairtrade.run(data) #pd.DataFrame.join(data, results.pnl, on = 'index') result = pd.concat(
self.short_mavgs = [] self.long_mavgs = [] def handle_data(self, data): short_mavg = data['AAPL'].short_mavg['price'] long_mavg = data['AAPL'].long_mavg['price'] if short_mavg > long_mavg and not self.invested: self.order('AAPL', 100) self.invested = True elif short_mavg < long_mavg and self.invested: self.order('AAPL', -100) self.invested = False # Save mavgs for later analysis. self.short_mavgs.append(short_mavg) self.long_mavgs.append(long_mavg) if __name__ == '__main__': data = load_from_yahoo(stocks=['AAPL'], indexes={}) dma = DualMovingAverage() results = dma.run(data) results.portfolio_value.plot() data['short'] = dma.short_mavgs data['long'] = dma.long_mavgs data[['AAPL', 'short', 'long']].plot() plt.legend(loc=0) plt.show()
def sell_spread(self,sid1,sid2): """ decrease exposure, regardless of position long/short. buy for a short position, sell for a long. """ sad1_amount = self.portfolio.positions[sid1].amount self.order(sid1, -1 * sad1_amount) sad2_amount = self.portfolio.positions[sid2].amount self.order(sid2, -1 * sad2_amount) if __name__ == '__main__': ########################################################################################################### start = datetime(2006, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=['XLF', 'JPM'], indexes={}, start=start, end=end) ## heard to save in the csv file and reget it. because the time zone and the timestampe is different #data.to_csv("PEP_KO_2007-2008.csv",index = True ,cols=('PEP','KO')) # the cols = () is order sensitive. If we save them as PEP and KO in cols them they will change the order # If want to load another dataset then denote these lines. ############################################################################################################ #read_data = pd.DataFrame.from_csv('PEP_KO_2007-2008.csv',infer_datetime_format = True) #read_data.index = pd.DatetimeIndex(read_data["Date"]) pairtrade = Pairtrade() results = pairtrade.run(data) #pd.DataFrame.join(data, results.pnl, on = 'index') result = pd.concat([results.zscores,results.orders,results.positions,results.pnl],axis = 1)
self.buy = True elif self.short_mavg < self.long_mavg and self.invested: self.order('AAPL', -5000) self.invested = False self.sell = True self.record(short_mavg=self.short_mavg, long_mavg=self.long_mavg, buy=self.buy, sell=self.sell) if __name__ == '__main__': start = datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2003, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start, end=end) dma = DualMovingAverage() results = dma.run(data) br = trading.environment.benchmark_returns bm_returns = br[(br.index >= start) & (br.index <= end)] results['benchmark_returns'] = (1 + bm_returns).cumprod().values results['algorithm_returns'] = (1 + results.returns).cumprod() fig = plt.figure() ax1 = fig.add_subplot(211, ylabel='cumulative returns') results[['algorithm_returns', 'benchmark_returns']].plot(ax=ax1, sharex=True) ax2 = fig.add_subplot(212)
self.order('DDD', -100) self.invested = False sell = True # Record state variables. A column for each # variable will be added to the performance # DataFrame returned by .run() self.record(short_mavg=short_mavg, long_mavg=long_mavg, buy=buy, sell=sell) # Load data start = datetime(2008, 11, 11, 0, 0, 0, 0, pytz.utc) end = datetime(2013, 11, 11, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=['DDD'], indexes={}, start=start, end=end, adjusted=False) # Run algorithm dma = DualMovingAverage() perf = dma.run(data) # Plot results fig = plt.figure() ax1 = fig.add_subplot(211, ylabel='Price in $') data['DDD'].plot(ax=ax1, color='r', lw=2.) perf[['short_mavg', 'long_mavg']].plot(ax=ax1, lw=2.) ax1.plot(perf.ix[perf.buy].index, perf.short_mavg[perf.buy], '^', markersize=10, color='m') ax1.plot(perf.ix[perf.sell].index, perf.short_mavg[perf.sell], 'v', markersize=10, color='k')
self.order(self.stockB, -1 * stockB_amount) #log.info(str(self.get_datetime())+' '+str(stockB_amount)) stockA_amount = self.portfolio.positions[self.stockA].amount self.order(self.stockA, -1 * stockA_amount) if __name__ == '__main__': log = Logger('') start = datetime(2011, 3, 28, 0, 0, 0, 0, pytz.utc) end = datetime(2014, 3, 28, 0, 0, 0, 0, pytz.utc) #stockA='gaz' #stockB='uco' #stockA='mzz' #stockB='xop' stockA='IVV' stockB='SPY' data = load_from_yahoo(stocks=[stockA, stockB], indexes={}, start=start, end=end) pairtrade = Pairtrade(stockA,stockB) results = pairtrade.run(data) #data['spreads'] = np.nan ax1 = plt.subplot(411) data[[stockA,stockB]].plot(ax=ax1) plt.ylabel('price') plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(412, sharex=ax1) results.zscores.plot(ax=ax2, color='r') plt.ylabel('zscore') ax3 = plt.subplot(413, sharex=ax1) pd.TimeSeries(np.array(pairtrade.spreads),results.zscores.index).plot(ax=ax3, color='b') plt.ylabel('spread')
plt.subplots_adjust(left=0.1, right=0.98, bottom=0.0, top=0.96) ax1 = fig.add_subplot(311, ylabel='Cumulative Returns') results[['algorithm_returns', 'benchmark_returns']].plot(ax=ax1, sharex=True) plt.setp(ax1.get_xticklabels(), visible=False) plt.legend(loc=0) ax2 = fig.add_subplot(312, ylabel='Price') data[algo.symbol].plot(ax=ax2, color='green') plt.setp(ax2.get_xticklabels(), visible=False) plt.legend(loc=0) ax3 = fig.add_subplot(313, ylabel='Score') results.Score.plot(ax=ax3, color='red') results.Ideal.plot(ax=ax3, color='blue') plt.setp(ax3.get_xticklabels(), visible=False) plt.legend(loc=0) plt.gcf().set_size_inches(18, 8) plt.show() if __name__ == '__main__': start = datetime(2012, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2015, 3, 1, 0, 0, 0, 0, pytz.utc) algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=10000) data = load_from_yahoo(stocks=[algo.symbol], indexes={}, start=start, end=end).dropna() results = algo.run(data) show_results(algo, data, results)
cumulative['cp_sum'] = cumulative['cumsum_c'] + cumulative['cumsum_p'] mpp = cumulative.ix[cumulative['cp_sum'].idxmax()]['strike'] return mpp def add_to_window(context, window_size, datapoint, ticker): tw = context.target_window[ticker] tw.append(datapoint) context.target_window[ticker] = tw[-window_size:] if len(tw) > window_size else tw # (6) Create CI for n-day window as price target range # Return tuple with upper and lower bound def CI(window, sds): mean = np.mean(window) sd = np.std(window) return (mean - sds * sd, mean + sds * sd) if __name__ == '__main__': universe = ['FB'] data = load_from_yahoo(stocks=universe, indexes={}, start=datetime(2016, 4, 3), end=datetime.today()) olmar = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=10000) backtest = olmar.run(data) backtest.to_csv('backtest-50-2012.csv') print backtest['algorithm_period_return'][-1] import pyfolio as pf returns, positions, transactions, gross_lev = pf.utils.extract_rets_pos_txn_from_zipline(backtest) pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, gross_lev=gross_lev, live_start_date='2004-10-22')
results[["PEP", "KO"]].plot(ax=ax1) plt.ylabel("Price (USD)") plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(212, sharex=ax1) results.zscores.plot(ax=ax2, color="r") plt.ylabel("Z-scored spread") plt.gcf().set_size_inches(18, 8) plt.show() # Note: this if-block should be removed if running # this algorithm on quantopian.com if __name__ == "__main__": logbook.StderrHandler().push_application() # Set the simulation start and end dates. start = datetime(2000, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) # Load price data from yahoo. data = load_from_yahoo(stocks=["PEP", "KO"], indexes={}, start=start, end=end) # Create and run the algorithm. pairtrade = Pairtrade() results = pairtrade.run(data) # Plot the portfolio data. analyze(results=results)
results.portfolio_value.plot(ax=ax1) ax1.set_ylabel('Portfolio value (USD)') ax2 = plt.subplot(212, sharex=ax1) results.AAPL.plot(ax=ax2) ax2.set_ylabel('AAPL price (USD)') # Show the plot. plt.gcf().set_size_inches(18, 8) plt.show() # Set the simulation start and end dates start = datetime(2014, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2014, 11, 1, 0, 0, 0, 0, pytz.utc) # Load price data from yahoo. data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start,end=end) # %% 根据字符串数组构造Datetime strDate = ['2015-01-05', '2015-01-06', '2015-01-07', '2015-01-08', '2015-01-09', '2015-01-12', '2015-01-13', '2015-01-14', '2015-01-15', '2015-01-16'] date = pd.to_datetime(strDate) data = pd.DataFrame(np.random.randn(10),index = date) #%% 人工构建Datetime longer_ts = pd.Series(np.random.randn(100), index = pd.date_range('1/1/2000',periods = 100,tz = 'UTC',name = 'Date'),name='AAPL') data = pd.DataFrame(longer_ts) # Create and run the algorithm. algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data,identifiers=['AAPL'])
stk_amount = self.portfolio.positions[self.stk.symbol].amount self.order(self.stk.symbol, -1 * stk_amount) self.initBP = 0 # set buying power to 0 self.invested = False self.holdingTime = 0 if __name__ == '__main__': start = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2012, 2, 1, 0, 0, 0, 0, pytz.utc) stksymbol = 'CSCO' etfsymbol = 'SPY' data = load_from_yahoo(stocks=[stksymbol, etfsymbol], indexes={}, start=start, end=end) pairtrade = Pairtrade(stksymbol, etfsymbol) results = pairtrade.run(data) data['spreads'] = np.nan #print results.portfolio_value ax1 = plt.subplot(311) data[[stksymbol, etfsymbol]].plot(ax=ax1) plt.ylabel('price') plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(312, sharex=ax1) results.zscores.plot(ax=ax2, color='r') plt.ylabel('zscored spread')