一、指标特点
二、指标计算公式


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader.data as web
# 获取股票数据
def get_stock_data(stock_symbol, start_date, end_date):
df = web.DataReader(stock_symbol, 'yahoo', start_date, end_date)
return df
# 计算ROC指标
def calculate_roc(df, period=12):
df['roc'] = df['Close'].pct_change(period) * 100
return df
# 交易策略
def trading_strategy(df, roc_threshold=10):
# 生成交易信号
df['signal'] = 0
df['signal'][roc_threshold:] = np.where(df['roc'][roc_threshold:] > roc_threshold, 1, 0)
df['position'] = df['signal'].diff()
# 绘制ROC和交易信号
plt.figure(figsize=(14, 7))
plt.subplot(2, 1, 1)
plt.title('Rate of Change (ROC)')
plt.plot(df['roc'], label='ROC')
plt.legend(loc='upper left')
plt.subplot(2, 1, 2)
plt.title('Trading Signals')
plt.plot(df['position'], label='Buy Signal', marker='^', markersize=10, color='g', lw=0)
plt.plot(df['position'], label='Sell Signal', marker='v', markersize=10, color='r', lw=0)
plt.legend(loc='upper left')
plt.show()
return df
发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/105408
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!