stefan-jansen / machine-learning-for-trading

Code for Machine Learning for Algorithmic Trading, 2nd edition.
https://ml4trading.io
12.57k stars 4.03k forks source link

Chapter 8 02_backtesting_with_zipline custom_loader not working #307

Closed weilhuiz closed 10 months ago

weilhuiz commented 10 months ago

Hi, following the 02_backtesting_with_zipline.ipynb

Have the custom loader set it up as:

class SignalData(DataSet):
    predictions = Column(dtype=float)
    domain = US_EQUITIES

signal_loader = {SignalData.predictions: DataFrameLoader(SignalData.predictions, 
                                                         predictions)}

class MLSignal(CustomFactor):
    """Converting signals to Factor
        so we can rank and filter in Pipeline"""
    inputs = [SignalData.predictions]
    window_length = 1

    def compute(self, today, assets, out, preds):
        # print(preds) -> [nan, nan....nan, nan],
        out[:] = preds

def compute_signals():
    signals = MLSignal()
    return Pipeline(columns={
        'longs' : signals.top(N_LONGS, mask=signals > 0),
        'shorts': signals.bottom(N_SHORTS, mask=signals < 0)},
            screen=StaticAssets(assets)
    )

# Other functions
results = run_algorithm(start=start_date,
                       end=end_date,
                       initialize=initialize,
                       before_trading_start=before_trading_start,
                       capital_base=1e6,
                       data_frequency='daily',
                       bundle='quandl',
                       custom_loader=signal_loader)

I'm using the zipline-reloaded The preds passed to MLSignal.compute function are all [nan, nan....nan, nan], therefore none of the longs and shorts were executed. Also, the predictions generated from load_predictions looks fine. Seems that the custom_loader is not recognized somehow?

weilhuiz commented 10 months ago

The root cause what the dates format didn't match, running predictions.index = predictions.index.tz_localize(None) solved for me