You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DataFrame中基于RSI阈值提取对应收盘价并标记买卖信号

How to Plot RSI-Based Buy/Sell Signals with Corresponding Close Prices

Got it, let's work through this. You've already got your DataFrame set up with Close prices and RSI values—half the battle is done. The issue you're hitting is isolating the exact dates where your RSI triggers buy/sell conditions, then grabbing the matching Close prices to mark on your chart. Here's a straightforward solution using pandas and matplotlib:

Step 1: Define Your Signal Conditions

First, we'll add boolean columns to your DataFrame to flag when buy/sell signals fire. We can even tweak this to avoid redundant consecutive signals if you want:

import pandas as pd

# Assume your DataFrame is named `df` with columns 'Close' and 'RSI'
# Basic signal flags (triggers every day RSI meets the condition)
df['Buy_Signal'] = df['RSI'] < 25
df['Sell_Signal'] = df['RSI'] > 85

# Optional: Only trigger on the FIRST day the condition is met (avoids daily repeats)
# df['Buy_Signal'] = (df['RSI'] < 25) & (df['RSI'].shift(1) >= 25)
# df['Sell_Signal'] = (df['RSI'] > 85) & (df['RSI'].shift(1) <= 85)

Step 2: Extract Signal Data

Now filter your DataFrame to get only the rows where signals are active—this gives you the exact dates and corresponding Close prices:

# Grab buy signals: date (index) + Close price
buy_signals = df[df['Buy_Signal']][['Close']]
# Grab sell signals: date (index) + Close price
sell_signals = df[df['Sell_Signal']][['Close']]

Step 3: Plot Prices, RSI, and Signals

Use a dual-axis plot to show both Close prices and RSI (since their scales are very different), then add scatter markers for your buy/sell signals:

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots(figsize=(12, 6))

# Plot Close prices on the primary axis
ax1.plot(df.index, df['Close'], label='Close Price', color='navy')
# Mark buy signals with green upward arrows
ax1.scatter(buy_signals.index, buy_signals['Close'], 
            marker='^', color='limegreen', label='Buy Signal', s=120)
# Mark sell signals with red downward arrows
ax1.scatter(sell_signals.index, sell_signals['Close'], 
            marker='v', color='crimson', label='Sell Signal', s=120)

ax1.set_xlabel('Date')
ax1.set_ylabel('Close Price')
ax1.legend(loc='upper left')
ax1.grid(alpha=0.3)

# Add RSI on the secondary axis
ax2 = ax1.twinx()
ax2.plot(df.index, df['RSI'], label='RSI', color='orange')
# Add threshold lines for your RSI rules
ax2.axhline(y=25, color='limegreen', linestyle='--', alpha=0.7)
ax2.axhline(y=85, color='crimson', linestyle='--', alpha=0.7)

ax2.set_ylabel('RSI (14-period)')
ax2.legend(loc='upper right')

plt.title('Stock Price + RSI with Buy/Sell Signals')
plt.tight_layout()
plt.show()

Key Notes

  • The dual-axis setup lets you visualize how RSI movements correlate with price changes, making it clear why a signal fired.
  • Using df.index as the x-axis ensures your signals line up perfectly with the correct dates on the chart.
  • The optional signal filtering (commented out) prevents multiple consecutive markers if RSI stays below 25 or above 85 for several days—use this if you only want to mark the initial trigger.

This should resolve your issue of mapping RSI signals to their corresponding Close prices and plotting them cleanly.

内容的提问来源于stack exchange,提问作者Jimbo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:53:02