ZiplinePythonic 交易算法庫(kù)
Zipline 是一個(gè) Pythonic 算法交易庫(kù)。 它是一個(gè)事件驅(qū)動(dòng)的系統(tǒng),支持回測(cè)檢驗(yàn)和實(shí)時(shí)交易。
Zipline 目前在生產(chǎn)中用作 Quantopian(托管平臺(tái)) 的測(cè)試和實(shí)時(shí)交易引擎。
特性
-
使用簡(jiǎn)單,以便你可以專(zhuān)注于算法開(kāi)發(fā)
-
帶有許多常見(jiàn)的統(tǒng)計(jì)數(shù)據(jù),包括常用統(tǒng)計(jì)方法如移動(dòng)平均和線性回歸
-
歷史數(shù)據(jù)的輸入和性能統(tǒng)計(jì)的輸出基于 Pandas DataFrames,與現(xiàn)有 python 生態(tài)圈能很好融合
-
一些常用統(tǒng)計(jì)和機(jī)器學(xué)習(xí)庫(kù),如 matplotlib、scipy、statsmodels 和 sklearn,支持交易系統(tǒng)的開(kāi)發(fā)、數(shù)據(jù)分析和可視化
快速開(kāi)始
下面的代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的雙重移動(dòng)平均算法。
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])評(píng)論
圖片
表情
