如何更新不推荐的python zipline.Transms模块?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (115)

我使用quantopian zipline软件包http://www.zipline.io/beginner-tutorial.html编写了一个python程序。我用的是zipline.transforms包装两个功能,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()

有人可以提供一个如何更新上面的代码的例子吗?

提问于
用户回答回答于

似乎没有直接的方法用history 代替 batch_transform

以下是使用历史记录方法创建的文档示例:

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)

扫码关注云+社区

领取腾讯云代金券