我在交易系统中工作,我正在测试一个简单的移动平均线来买入和卖出点数。我从互联网上得到了一个代码来帮助我,但在回测试部分我遇到了错误: " TypeError:' float'对象不支持项目分配"
以下是代码:
initial_capital = float(1000)
positions = pd.DataFrame(index=signals.index).fillna=(0.0)
positions['AAPL'] = 100*signals['signal']
portfolio = positions.multiply(aapl['Adj Close'], axis=0)
pos_diff = positions.diff()
portfolio['holdings'] = (positions.multiply(aapl['Adj `Close'],axis=0)).sum(axis=1).cumsum()`
portfolio['total'] = portfolio['cash'] + portfolio['holdings']
portfolio['returns'] = portfolio['total'].pct_change()
运行第三行
后出现错误# Initialize the `signals` DataFrame with the `signal` column
signals = pd.DataFrame(index=aapl.index)
signals['signal'] = 0.0
# Create short simple moving average over the short window
signals['short_mavg'] = aapl['Close'].rolling(window=short_window, min_periods=1, center=False).mean()
# Create long simple moving average over the long window
signals['long_mavg'] = aapl['Close'].rolling(window=long_window, min_periods=1, center=False).mean()
# Create signals
signals['signal'][short_window:] = np.where(signals['short_mavg'][short_window:]
> signals['long_mavg'][short_window:], 1.0, 0.0)
# Generate trading orders
signals['positions'] = signals['signal'].diff()