ImportError: No module named transforms

120 views
Skip to first unread message

Arshpreet Singh

unread,
Feb 20, 2017, 9:12:56 AM2/20/17
to Zipline Python Opensource Backtester
I have installed Zipline using Anaconda. I am not able to use batch_transform function. on the other hand I have looked into various parts of the code and transform class does not seem to bee present. Is bath_transform deprecated and now pipeline() is being used for such process? 


I am using following example in my jupyter notebook:

from zipline.transforms import batch_transform
from scipy import stats

@batch_transform
def regression_transform(data):
    """Computes regression coefficient (slope and intercept)
    via Ordinary Least Squares between two SIDs.
    """
    # data is now an array containg all past events up until now
    pep_price = data.price['PEP']
    ko_price = data.price['KO']
    slope, intercept, _, _, _ = stats.linregress(pep_price, ko_price)

    return intercept, slope


class Pairtrade(zp.TradingAlgorithm):

    def initialize(self, window_length=100):
        self.set_slippage(FixedSlippage())
        self.invested = False
        self.regression_transform = regression_transform(refresh_period=500,
                                                         window_length=window_length)

    def handle_data(self, data):
        # 1. Compute regression coefficients between PEP and KO
        params = self.regression_transform.handle_data(data)
        if params is None:
            self.record(buy_spread=False, sell_spread=False)
            return
        intercept, slope = params

        # 2. Compute spread
        spread = (slope * data['PEP'].price + intercept) - data['KO'].price
       
        buy_spread = False
        sell_spread = False
        # 3. Place orders
        if spread >= 5.0 and not self.invested:
            # Stocks drifted apart, long one, short the other
            self.order('KO', 100)
            self.order('PEP', -100)
            self.invested = True
            buy_spread = True
        elif spread <= 2. and self.invested:
            # The spread is closing again, exit positions.
            # ???
            # Profit!
            self.order('KO', -100)
            self.order('PEP', 100)
            self.invested = False
            sell_spread = True
       
        # Record spread for later analysis
        self.record(spread=spread,
                    buy_spread=buy_spread,
                    sell_spread=sell_spread)

fva...@quantopian.com

unread,
Mar 9, 2017, 11:20:27 AM3/9/17
to Zipline Python Opensource Backtester
Hi Arshpreet,

Yes. You're correct. The batch_transform() function is deprecated, and you should be using history() instead. You can take a look at this commit here that shows this.

Thanks,
Freddie
Reply all
Reply to author
Forward
0 new messages