我使用quantopian zipline包http://www.zipline.io/beginner-tutorial.html编写了一个python程序。我最近更新了这个包,发现zipline.transforms包被弃用了。我使用的是zipline.transforms包中的两个函数:batch_transform()
和MovingAverage
。
除了说用history()
函数替换batch_transform
之外,我还找不到一个好的帖子来演示如何修复这个问题。然而,我不知道如何准确地替换它。我还没有找到一篇关于如何修复MovingAverage弃用的文章。
这是我正在使用的代码。
from zipline.algorithm import TradingAlgorithm
from zipline.transforms import batch_transform
from zipline.transforms import MovingAverage
class TradingStrategy(TradingAlgorithm):
def initialize(self, window_length=6):
self.add_transform(
MovingAverage, 'kernel', ['price'], window_length=self.window_length)
@batch_transform
def get_data(data, context):
'''
Collector for some days of historical prices.
'''
daily_prices = data.price[STOCKS + [BENCHMARK]]
return daily_prices
strategy = TradingStrategy()
有人能提供一个如何更新上面的代码的例子吗?考虑到quantopian的受欢迎程度,我认为有很多人在处理这些问题。
发布于 2017-10-12 19:48:31
似乎没有一种直接的方法来代替batch_transform
来使用history
。
在我看来,不仅方法发生了变化,而且它们的使用方式也完全改变了。
文档中提到了以下内容:
每个zipline算法都包含两个必须定义的函数:
initialize(context)
handle_data(context, data)
以下是文档中使用历史方法创建一些基本移动平均值的示例:
def initialize(context):
context.i = 0
context.asset = symbol('AAPL')
def handle_data(context, data):
# Skip first 300 days to get full windows
context.i += 1
if context.i < 300:
return
# Compute averages
# data.history() has to be called with the same params
# from above and returns a pandas dataframe.
short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean()
long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean()
# Trading logic
if short_mavg > long_mavg:
# order_target orders as many shares as needed to
# achieve the desired number of shares.
order_target(context.asset, 100)
elif short_mavg < long_mavg:
order_target(context.asset, 0)
# Save values for later inspection
record(AAPL=data.current(context.asset, 'price'),
short_mavg=short_mavg,
long_mavg=long_mavg)
https://stackoverflow.com/questions/46347001
复制相似问题