Hi
- User
Hi
- Assistant
Hello! How can I assist you today?
- User
def calculate_kdj(df, n=9, m1=3, m2=3): lowest_low = df['low'].rolling(n).min() highest_high = df['high'].rolling(n).max() df['rsv'] = (df['close'] - lowest_low) / (highest_high - lowest_low) * 100 df['k'] = df['rsv'].ewm(com=(m1 - 1)).mean() df['d'] = df['k'].ewm(com=(m2 - 1)).mean() df['j'] = 3 * df['k'] - 2 * df['d'] return df
def calculate_macd(df2, n1=12, n2=26, n3=9): ema12 = df2['close'].ewm(span=n1, min_periods=n1-1).mean() ema26 = df2['close'].ewm(span=n2, min_periods=n2-1).mean() df2['dif'] = ema12 - ema26 df2['dea'] = df2['dif'].ewm(span=n3, min_periods=n3-1).mean() df2['macd'] = (df2['dif'] - df2['dea']) * 2 return df2
读取SPX500的数据,假设数据存储在一个DataFrame中,列名为'close', 'high', 'low' df = pd.read_excel(f'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') df2 = pd.read_excel(f'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') pd.set_option('display.max_rows', None) #将时间列设为索引 df.set_index('time', inplace=True) df2.set_index('time', inplace=True)
运行错误,KeyError: "None of ['time'] are in the columns",KeyError: "None of ['low'] are in the columns",KeyError: "None of ['close'] are in the columns",KeyError: "None of ['high'] are in the columns",请将Time列,high列,close列和Low列用新的列名称1,2,3,4
- Assistant
Sure! Here's an updated version of the code with the column names changed to '1', '2', '3', '4':
import pandas as pd def calculate_kdj(df, n=9, m1=3, m2=3): lowest_low = df['4'].rolling(n).min() highest_high = df['2'].rolling(n).max() df['rsv'] = (df['1'] - lowest_low) / (highest_high - lowest_low) * 100 df['k'] = df['rsv'].ewm(com=(m1 - 1)).mean() df['d'] = df['k'].ewm(com=(m2 - 1)).mean() df['j'] = 3 * df['k'] - 2 * df['d'] return df def calculate_macd(df2, n1=12, n2=26, n3=9): ema12 = df2['1'].ewm(span=n1, min_periods=n1-1).mean() ema26 = df2['1'].ewm(span=n2, min_periods=n2-1).mean() df2['dif'] = ema12 - ema26 df2['dea'] = df2['dif'].ewm(span=n3, min_periods=n3-1).mean() df2['macd'] = (df2['dif'] - df2['dea']) * 2 return df2 # Read SPX500 data from the DataFrame df = pd.read_excel(r'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') df2 = pd.read_excel(r'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') # Rename columns df = df.rename(columns={'time': '1', 'high': '2', 'close': '3', 'low': '4'}) df2 = df2.rename(columns={'time': '1', 'high': '2', 'close': '3'}) # Set time columns as index df.set_index('1', inplace=True) df2.set_index('1', inplace=True) pd.set_option('display.max_rows', None) # Calculate KDJ and MACD df_kdj = calculate_kdj(df) df_macd = calculate_macd(df2) - User
def calculate_kdj(df, n=9, m1=3, m2=3): lowest_low = df['low'].rolling(n).min() highest_high = df['high'].rolling(n).max() df['rsv'] = (df['close'] - lowest_low) / (highest_high - lowest_low) * 100 df['k'] = df['rsv'].ewm(com=(m1 - 1)).mean() df['d'] = df['k'].ewm(com=(m2 - 1)).mean() df['j'] = 3 * df['k'] - 2 * df['d'] return df
- Assistant
The revised code should be as follows, with the column names changed to '1', '2', '3', '4':
def calculate_kdj(df, n=9, m1=3, m2=3): lowest_low = df['4'].rolling(n).min() highest_high = df['2'].rolling(n).max() df['rsv'] = (df['3'] - lowest_low) / (highest_high - lowest_low) * 100 df['k'] = df['rsv'].ewm(com=(m1 - 1)).mean() df['d'] = df['k'].ewm(com=(m2 - 1)).mean() df['j'] = 3 * df['k'] - 2 * df['d'] return dfPlease note that you also need to ensure that the column names '1', '2', '3', '4' correspond to the correct columns in your DataFrame.
- User
def calculate_kdj(df, n=9, m1=3, m2=3): lowest_low = df['low'].rolling(n).min() highest_high = df['high'].rolling(n).max() df['rsv'] = (df['close'] - lowest_low) / (highest_high - lowest_low) * 100 df['k'] = df['rsv'].ewm(com=(m1 - 1)).mean() df['d'] = df['k'].ewm(com=(m2 - 1)).mean() df['j'] = 3 * df['k'] - 2 * df['d'] return df,用第三列替换low的表达方式
- Assistant
Certainly! To replace the 'low' column in the code with the third column, you can modify the code as follows:
def calculate_kdj(df, n=9, m1=3, m2=3): lowest_low = df.iloc[:, 2].rolling(n).min() highest_high = df['high'].rolling(n).max() df['rsv'] = (df['close'] - lowest_low) / (highest_high - lowest_low) * 100 df['k'] = df['rsv'].ewm(com=(m1 - 1)).mean() df['d'] = df['k'].ewm(com=(m2 - 1)).mean() df['j'] = 3 * df['k'] - 2 * df['d'] return dfHere,
df.iloc[:, 2]refers to the third column in the DataFrame. Make sure to adjust other parts of the code accordingly if you have any other modifications. - User
从Excel读取SPX500的数据,假设数据存储在一个DataFrame中,将第5列的列名称改为close,将第5列的列名称改为high
df = pd.read_excel(f'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') df2 = pd.read_excel(f'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl')
- Assistant
To change the column names in the DataFrame for the 5th column to 'close' and the 6th column to 'high', you can use the
rename()function. Here's an example of how you can modify the code:import pandas as pd df = pd.read_excel(r'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') df2 = pd.read_excel(r'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') # Rename column names df = df.rename(columns={df.columns[4]: 'close', df.columns[5]: 'high'}) # Verify the new column names print(df.columns) # Continue with further processing # ...The above code uses the
rename()function to change the column names. Thedf.columns[4]refers to the 5th column, anddf.columns[5]refers to the 6th column. Adjust the code accordingly if you have any other modifications.Note: Please ensure that the file path to the Excel file
D:\DATA50020230629_083238.xlsxis correct and modify it if necessary. - User
workbook.save(f'D:\DATA500{timestamp}.xlsx'),K,D,J公式,打印K,D,J的值
- Assistant
To calculate the values for K, D, and J using the given formulas, and save the results to an Excel file, you can modify the code as follows:
import pandas as pd # Read the data from the Excel file df = pd.read_excel(r'D:\DATA50020230629_083238.xlsx', sheet_name='values', engine='openpyxl') # Calculate K, D, and J n = 9 m1 = 3 m2 = 3 lowest_low = df['low'].rolling(n).min() highest_high = df['high'].rolling(n).max() df['rsv'] = (df['close'] - lowest_low) / (highest_high - lowest_low) * 100 df['k'] = df['rsv'].ewm(com=(m1 - 1)).mean() df['d'] = df['k'].ewm(com=(m2 - 1)).mean() df['j'] = 3 * df['k'] - 2 * df['d'] # Print K, D, and J values print("K values:") print(df['k']) print("D values:") print(df['d']) print("J values:") print(df['j']) # Save the updated DataFrame to a new Excel file timestamp = pd.Timestamp.now().strftime("%Y%m%d_%H%M%S") df.to_excel(f'D:\DATA500{timestamp}.xlsx', index=False)After calculating the K, D, and J values, the code will print the values to the console using the
print()function. Theto_excel()function saves the updated DataFrame to a new Excel file with a timestamp in the filename.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.