update this code and add rsi table with signals table
- User
update this code and add rsi table with signals table
Assistant
Here's the updated code with RSI and signals tables:
import pandas as pd import numpy as np def calculate_rsi(data, window=14): delta = data['close'].diff() gain = delta.where(delta > 0, 0) loss = -delta.where(delta < 0, 0) avg_gain = gain.rolling(window=window, min_periods=window).mean() avg_loss = loss.rolling(window=window, min_periods=window).mean() rs = avg_gain / avg_loss rsi = 100 - (100 / (1 + rs)) return rsi def generate_signals(rsi, overbought=70, oversold=30): signals = pd.DataFrame(index=rsi.index) signals['rsi'] = rsi signals['signal'] = 0 signals['signal'][rsi < oversold] = 1 # Buy signal signals['signal'][rsi > overbought] = -1 # Sell signal return signals # Load data (replace 'your_data.csv' with your actual data file) data = pd.read_csv('your_data.csv', parse_dates=['date']) data.set_index('date', inplace=True) # Calculate RSI data['rsi'] = calculate_rsi(data) # Generate signals signals = generate_signals(data['rsi']) # Create RSI table rsi_table = data[['close', 'rsi']].dropna() # Create signals table signals_table = signals[signals['signal'] != 0].dropna() signals_table['action'] = signals_table['signal'].apply(lambda x: 'Buy' if x == 1 else 'Sell') signals_table = signals_table[['rsi', 'action']] print("RSI Table:") print(rsi_table.tail()) # Showing last few rows for brevity print("\nSignals Table:") print(signals_table)This code calculates the RSI based on closing prices, generates buy and sell signals when the RSI crosses below 30 or above 70, respectively, and then creates two tables: one for RSI values and another for the signals with corresponding actions. Make sure to replace 'your_data.csv' with your actual data file and adjust column names if necessary.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.