如何在交互式绘图中添加斐波那契回撤(绘图)

How to add fibonacci retracement in interactive plots (plotly)

如何在 go.figure add_hline() 函数中添加不同颜色的线条 之前的 pyplot 函数 hlines 有用于为 pyplot 添加不同颜色的颜色部分,我怎么能这样做呢?

# highest_swing and lowest_swings generate the area for which we have to check ratios
highest_swing = -1
lowest_swing = -1
for i in range(1,df.shape[0]-1):
  if df['High'][i] > df['High'][i-1] and df['High'][i] > df['High'][i+1] and (highest_swing == -1 or df['High'][i] > df['High'][highest_swing]):
    highest_swing = i
  if df['Low'][i] < df['Low'][i-1] and df['Low'][i] < df['Low'][i+1] and (lowest_swing == -1 or df['Low'][i] < df['Low'][lowest_swing]):
    lowest_swing = i

ratios = [0,0.236, 0.382, 0.5 , 0.618, 0.786,1]
colors = ["black","r","g","b","cyan","magenta","yellow"]
levels = []
max_level = df['High'][highest_swing]
min_level = df['Low'][lowest_swing]
for ratio in ratios:
  if highest_swing > lowest_swing: # Uptrend
    levels.append(max_level - (max_level-min_level)*ratio)
  else: # Downtrend
    levels.append(min_level + (max_level-min_level)*ratio)

# candlestick plot
fig = go.Figure()
fig.add_traces(go.Candlestick(x=df['Date'],
                              open=df['Open'],
                              high=df['High'],
                              low=df['Low'],
                              close=df['Close']))

start_date = df['Date'][df.index[min(highest_swing,lowest_swing)]]
end_date = df['Date'][df.index[max(highest_swing,lowest_swing)]]
y=np.array([start_date,end_date])
print(y)
for i in range(len(levels)):
  # previous pyplot plot
  # plt.hlines(levels[i],start_date, end_date,label="{:.1f}%".format(ratios[i]*100),colors=colors[i], linestyles="dashed")
  fig.add_hline(go.Scatter(x=levels,y=y, line_shape='linear'))
fig.show()

水平线段可以尝试以下方法:

fig.add_shape(type='line', 
    x0=start_date, y0=levels[i], x1=end_date, y1=levels[i],
    line=dict(
        color=colors[i],
        dash="dash"
    ))

您还需要将列表颜色更改为 colors = ["black","red","green","blue","cyan","magenta","yellow"],因为 Plotly 不会像 matplotlib.pyplot 那样理解 "r", "g", "b" 的缩写。

使用一些示例数据:

import plotly.graph_objects as go

import numpy as np
import pandas as pd
from datetime import datetime

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

df.rename({'AAPL.Open':'Open','AAPL.High':'High', 'AAPL.Low':'Low', 'AAPL.Close':'Close'}, axis=1, inplace=True)

# highest_swing and lowest_swings generate the area for which we have to check ratios
highest_swing = -1
lowest_swing = -1
for i in range(1,df.shape[0]-1):
  if df['High'][i] > df['High'][i-1] and df['High'][i] > df['High'][i+1] and (highest_swing == -1 or df['High'][i] > df['High'][highest_swing]):
    highest_swing = i
  if df['Low'][i] < df['Low'][i-1] and df['Low'][i] < df['Low'][i+1] and (lowest_swing == -1 or df['Low'][i] < df['Low'][lowest_swing]):
    lowest_swing = i

ratios = [0,0.236, 0.382, 0.5 , 0.618, 0.786,1]
colors = ["black","red","green","blue","cyan","magenta","yellow"]
levels = []
max_level = df['High'][highest_swing]
min_level = df['Low'][lowest_swing]
for ratio in ratios:
  if highest_swing > lowest_swing: # Uptrend
    levels.append(max_level - (max_level-min_level)*ratio)
  else: # Downtrend
    levels.append(min_level + (max_level-min_level)*ratio)

# candlestick plot
fig = go.Figure()
fig.add_traces(go.Candlestick(x=df['Date'],
                              open=df['Open'],
                              high=df['High'],
                              low=df['Low'],
                              close=df['Close']))

start_date = df['Date'][df.index[min(highest_swing,lowest_swing)]]
end_date = df['Date'][df.index[max(highest_swing,lowest_swing)]]
y=np.array([start_date,end_date])
print(y)
for i in range(len(levels)):
  # previous pyplot plot
  # plt.hlines(levels[i],start_date, end_date,label="{:.1f}%".format(ratios[i]*100),colors=colors[i], linestyles="dashed")
  fig.add_shape(type='line', 
    x0=start_date, y0=levels[i], x1=end_date, y1=levels[i],
    line=dict(
        color=colors[i],
        dash="dash"
    ))

fig.show()