0

I'm working in a trading system and I was backtesting a simple moving average to make buy and sell points. I got a code from the internet to help me but in the backtesting part I got stuck in a error: "TypeError: 'float' object does not support item assignment"

Here is the code:

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()

The error appears after I run the third line

# 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()
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 2
    Please include a full stack trace. – roganjosh Dec 31 '17 at 19:53
  • 2
    I'd like to say that `positions` is a float but I'm not sure how that would be, and I'm not sure you'd get that error from `signals['signal']`. Really, we need a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – roganjosh Dec 31 '17 at 19:55
  • Yes, I know, I will do that – Lucas Saladini Dec 31 '17 at 20:10
  • Yes, thank you, I'm new here. That's okay now? – Lucas Saladini Dec 31 '17 at 20:12
  • 1
    I'm not sure how the edit fits with the initial code exactly, and it's not an MCVE, but from your code I'm assuming you have a decent understanding of the language and debugging. Therefore, I'll take a stab at a different angle since this is tagged with `spyder`: https://stackoverflow.com/questions/47738537/builtin-function-not-working-with-spyder/47738834#47738834 – roganjosh Dec 31 '17 at 20:20

0 Answers0