如何更新不推荐使用的python zipline.transforms模块?
我使用quantopian zipline包编写了一个python程序。我最近更新了该软件包,发现zipline.transforms软件包已被弃用。我使用了zipline.transforms包中的两个函数,如何更新不推荐使用的python zipline.transforms模块?,python,zipline,Python,Zipline,我使用quantopian zipline包编写了一个python程序。我最近更新了该软件包,发现zipline.transforms软件包已被弃用。我使用了zipline.transforms包中的两个函数,batch\u transform()和MovingAverage 除了说用history()。然而,我不知道如何确切地替换它。我还没有找到一个帖子告诉我如何解决移动平均值的问题 这是我正在使用的代码 from zipline.algorithm import TradingAlgorit
batch\u transform()
和MovingAverage
除了说用history()。然而,我不知道如何确切地替换它。我还没有找到一个帖子告诉我如何解决移动平均值的问题
这是我正在使用的代码
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的流行程度,我假设有很多人在处理这些问题。似乎没有一种直接的方法可以使用历史
而不是批处理转换
在我看来,这些方法不仅改变了,而且它们的使用方式也完全改变了
文件中提到以下内容:
每个zipline算法都包含两个必须定义的函数:
初始化(上下文)
处理数据(上下文、数据)
以下是使用历史法创建一些基本移动平均线的文档示例:
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)
def初始化(上下文):
context.i=0
context.asset=symbol('AAPL')
def句柄_数据(上下文、数据):
#跳过前300天以获取完整窗口
context.i+=1
如果上下文i<300:
返回
#计算平均数
#必须使用相同的参数调用data.history()
#并返回一个数据帧。
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()
#交易逻辑
如果短变量>长变量:
#订购\目标订购所需数量的股票
#达到所需的股份数量。
订单_目标(context.asset,100)
elif短标<长标:
订单_目标(context.asset,0)
#保存值以供以后检查
记录(AAPL=data.current(context.asset,'price'),
short_mavg=short_mavg,
long_mavg=long_mavg)
related:下面是如何添加历史记录的:您能否提供一个使用batch_transform的代码示例,该示例可以为您提供与上述相同的结果,以便我可以比较两者?