ZiplinePythonic 交易算法庫
Zipline 是一個 Pythonic 算法交易庫。 它是一個事件驅(qū)動的系統(tǒng),支持回測檢驗和實時交易。
Zipline 目前在生產(chǎn)中用作 Quantopian(托管平臺) 的測試和實時交易引擎。
特性
-
使用簡單,以便你可以專注于算法開發(fā)
-
帶有許多常見的統(tǒng)計數(shù)據(jù),包括常用統(tǒng)計方法如移動平均和線性回歸
-
歷史數(shù)據(jù)的輸入和性能統(tǒng)計的輸出基于 Pandas DataFrames,與現(xiàn)有 python 生態(tài)圈能很好融合
-
一些常用統(tǒng)計和機器學(xué)習庫,如 matplotlib、scipy、statsmodels 和 sklearn,支持交易系統(tǒng)的開發(fā)、數(shù)據(jù)分析和可視化
快速開始
下面的代碼實現(xiàn)了一個簡單的雙重移動平均算法。
from zipline.api import (
history,
order_target,
record,
symbol,
)
def initialize(context):
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()
sym = symbol('AAPL')
# Trading logic
if short_mavg[sym] > long_mavg[sym]:
# order_target orders as many shares as needed to
# achieve the desired number of shares.
order_target(sym, 100)
elif short_mavg[sym] < long_mavg[sym]:
order_target(sym, 0)
# Save values for later inspection
record(AAPL=data[sym].price,
short_mavg=short_mavg[sym],
long_mavg=long_mavg[sym])評論
圖片
表情
