mgbam commited on
Commit
a215027
·
verified ·
1 Parent(s): 2a7db01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -14,13 +14,13 @@ This app demonstrates an extended MACD-RSI based trading strategy on SPY with th
14
  - **Dynamic Trailing Stop:** Each open position is updated with a trailing stop.
15
  - **Configurable Parameters:** Adjust strategy parameters via the sidebar.
16
  - **Buy Rule:**
17
- Buy 15% of available cash when:
18
  - The MACD line crosses above its signal line.
19
  - RSI is below 50.
20
- - No buy has been executed in the last 2 days.
21
  - **Sell Rule:**
22
  For each position:
23
- - **Partial Sell:** Sell 40% of the position when the price reaches 1.08× the entry price and RSI is above 50.
24
  - **Trailing Stop Exit:** If the price falls below the position’s dynamic trailing stop, sell the entire position.
25
  """)
26
 
@@ -43,8 +43,9 @@ data_load_state.text("Loading SPY data...done!")
43
 
44
  # Calculate technical indicators: MACD and RSI
45
  macd_indicator = MACD(close=data['Close'])
46
- data['MACD'] = macd_indicator.macd()
47
- data['MACD_signal'] = macd_indicator.macd_signal()
 
48
 
49
  rsi_indicator = RSIIndicator(close=data['Close'], window=14)
50
  data['RSI'] = rsi_indicator.rsi()
 
14
  - **Dynamic Trailing Stop:** Each open position is updated with a trailing stop.
15
  - **Configurable Parameters:** Adjust strategy parameters via the sidebar.
16
  - **Buy Rule:**
17
+ Buy a fraction of available cash when:
18
  - The MACD line crosses above its signal line.
19
  - RSI is below 50.
20
+ - No buy has been executed in the last few days.
21
  - **Sell Rule:**
22
  For each position:
23
+ - **Partial Sell:** Sell a fraction of the position when the price reaches a target multiple of the entry price and RSI is above 50.
24
  - **Trailing Stop Exit:** If the price falls below the position’s dynamic trailing stop, sell the entire position.
25
  """)
26
 
 
43
 
44
  # Calculate technical indicators: MACD and RSI
45
  macd_indicator = MACD(close=data['Close'])
46
+ # Squeeze the returned values to ensure a 1-dimensional array
47
+ data['MACD'] = pd.Series(macd_indicator.macd().squeeze(), index=data.index)
48
+ data['MACD_signal'] = pd.Series(macd_indicator.macd_signal().squeeze(), index=data.index)
49
 
50
  rsi_indicator = RSIIndicator(close=data['Close'], window=14)
51
  data['RSI'] = rsi_indicator.rsi()