mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-05 05:03:26 +08:00
129 lines
4.2 KiB
Python
Executable File
129 lines
4.2 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
# Copyright 2014 Quantopian, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# 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.
|
|
|
|
"""Dual Moving Average Crossover algorithm.
|
|
|
|
This algorithm buys apple once its short moving average crosses
|
|
its long moving average (indicating upwards momentum) and sells
|
|
its shares once the averages cross again (indicating downwards
|
|
momentum).
|
|
"""
|
|
|
|
from zipline.api import order_target, record, symbol, history, add_history
|
|
|
|
|
|
def initialize(context):
|
|
# Register 2 histories that track daily prices,
|
|
# one with a 100 window and one with a 300 day window
|
|
add_history(100, '1d', 'price')
|
|
add_history(300, '1d', 'price')
|
|
|
|
context.sym = symbol('AAPL')
|
|
|
|
context.i = 0
|
|
|
|
|
|
def handle_data(context, data):
|
|
# Skip first 300 days to get full windows
|
|
context.i += 1
|
|
if context.i < 300:
|
|
return
|
|
|
|
# Compute averages
|
|
# history() has to be called with the same params
|
|
# from above and returns a pandas dataframe.
|
|
short_mavg = history(100, '1d', 'price').mean()
|
|
long_mavg = history(300, '1d', 'price').mean()
|
|
|
|
# Trading logic
|
|
if short_mavg[context.sym] > long_mavg[context.sym]:
|
|
# order_target orders as many shares as needed to
|
|
# achieve the desired number of shares.
|
|
order_target(context.sym, 100)
|
|
elif short_mavg[context.sym] < long_mavg[context.sym]:
|
|
order_target(context.sym, 0)
|
|
|
|
# Save values for later inspection
|
|
record(AAPL=data[context.sym].price,
|
|
short_mavg=short_mavg[context.sym],
|
|
long_mavg=long_mavg[context.sym])
|
|
|
|
|
|
# Note: this function can be removed if running
|
|
# this algorithm on quantopian.com
|
|
def analyze(context=None, results=None):
|
|
import matplotlib.pyplot as plt
|
|
import logbook
|
|
logbook.StderrHandler().push_application()
|
|
log = logbook.Logger('Algorithm')
|
|
|
|
fig = plt.figure()
|
|
ax1 = fig.add_subplot(211)
|
|
results.portfolio_value.plot(ax=ax1)
|
|
ax1.set_ylabel('Portfolio value (USD)')
|
|
|
|
ax2 = fig.add_subplot(212)
|
|
ax2.set_ylabel('Price (USD)')
|
|
|
|
# If data has been record()ed, then plot it.
|
|
# Otherwise, log the fact that no data has been recorded.
|
|
if ('AAPL' in results and 'short_mavg' in results and
|
|
'long_mavg' in results):
|
|
results['AAPL'].plot(ax=ax2)
|
|
results[['short_mavg', 'long_mavg']].plot(ax=ax2)
|
|
|
|
trans = results.ix[[t != [] for t in results.transactions]]
|
|
buys = trans.ix[[t[0]['amount'] > 0 for t in
|
|
trans.transactions]]
|
|
sells = trans.ix[
|
|
[t[0]['amount'] < 0 for t in trans.transactions]]
|
|
ax2.plot(buys.index, results.short_mavg.ix[buys.index],
|
|
'^', 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()
|
|
|
|
|
|
# Note: this if-block should be removed if running
|
|
# this algorithm on quantopian.com
|
|
if __name__ == '__main__':
|
|
from datetime import datetime
|
|
import pytz
|
|
from zipline.algorithm import TradingAlgorithm
|
|
from zipline.utils.factory import load_from_yahoo
|
|
|
|
# Set the simulation start and end dates.
|
|
start = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc)
|
|
end = datetime(2013, 1, 1, 0, 0, 0, 0, pytz.utc)
|
|
|
|
# Load price data from yahoo.
|
|
data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start,
|
|
end=end)
|
|
|
|
# Create and run the algorithm.
|
|
algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data,
|
|
identifiers=['AAPL'])
|
|
results = algo.run(data)
|
|
|
|
# Plot the portfolio and asset data.
|
|
analyze(results=results)
|