在使用自定义数据进行回测时出现问题
Dear Zipline Maintainers, Before I tell you about my issue, let me describe my environment: # Environment <details> Operating System: macOS High Sierra Python Version: Python 3.9 How did you install Zipline: conda </details> Now that you know a little about me, let me tell you about the issue I am having: # Description of Issue While following '机器学习 4 Trading' by Stefan Jansen, I am trying to perform backtesting using Zipline. I have successfully ingested custom data. In the pipeline, I assign the custom data - 'returns' and factor data - 'DEMA' (Double Exponential Moving Average) to the pipeline columns and then run LinearRegression. But, when the pipeline is run, I see that the pipeline columns have all 'nan' values but the original values for 'returns' as well as 'DEMA' are correctly populated and their dates also align. It seems because of the 'nan' values - I get the error - "ValueError: Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required by StandardScaler." Here is how you can reproduce this issue on your machine: ## Reproduction Steps Here is the snippet of the code I am using: START = pd.Timestamp('2017-01-01').normalize() END = pd.Timestamp('2020-12-31').normalize() EARLIEST_START = pd.Timestamp('2017-03-03').normalize() common_dates = returns.index.get_level_values('Date').intersection(dema21.index.get_level_values('Date')) returns_aligned = returns[returns.index.get_level_values('Date').isin(common_dates)] dema21_aligned = dema21[dema21.index.get_level_values('Date').isin(common_dates)] print('..............', returns_aligned) print('..............', dema21_aligned) Here is the output of the above print statements: ................ returns Date sid 2017-03-03 0 0.011678 1 0.014784 8 0.024059 14 -0.012773 15 0.005901 ... ... 2020-12-31 6715 0.001511 6718 -0.000102 6733 0.005090 6753 0.005517 6755 0.007488 [867468 rows x 1 columns] ................ dema21 Date sid 2017-03-03 0 49.301678 1 35.406822 8 45.047879 14 142.457072 15 32.793468 ... ... 2020-12-31 6715 387.504155 6718 87.496984 6733 43.274391 6753 27.739478 6755 161.174313 [867468 rows x 1 columns] I have defined MyDataSet as: class MyDataSet(DataSet): returns = Column(dtype=float) dema_21 = Column(dtype=float) domain= US_EQUITIES column_returns_frame = pd.DataFrame( data=returns_aligned, ) loaders = { MyDataSet.returns: DataFrameLoader(MyDataSet.returns, returns_aligned), MyDataSet.dema_21: DataFrameLoader(MyDataSet.dema_21, dema21_aligned), } def load_creator(column): return loaders[column] returns_loader = load_creator(MyDataSet.returns) dema21_loader
内容来源: quantopian/zipline