簡體   English   中英

如何在燭台圖表上添加折線圖 plotly python

[英]How to add line chart on candlesticks chart in plotly python

我有plotly python某股票的K線圖,如下圖: 燭台圖

這工作正常。 但是當我嘗試添加折線圖時。 一切都散了。 圖片如下: 與折線圖。

有什么辦法讓它正確嗎? 下面是我的代碼:

response = response.json()['values']
ts = pd.DataFrame.from_records(response)
fig = go.Figure(data=[go.Candlestick(x=ts['datetime'],
                                         open=ts['open'],
                                         high=ts['high'],
                                         low=ts['low'],
                                         close=ts['close'],
                                         line=dict(width=1))])

 response_ti = response_ti.json()['values']
 df_ti = pd.DataFrame.from_records(response_ti)
 fig.add_trace(go.Line(x=df_ti['datetime'],y=df_ti[technical_indicator],))
  • 使用公共 API 重新創建
    1. 更改列名,這樣您的繪圖代碼就不會更改
    2. 假設一個技術指標
    3. 使用go.Line()忽略已棄用的警告
  • 創建的圖表沒有您注意到的問題。 我只能假設
    • 您的兩個數據框之間的日期時間列不統一
    • 版本問題,這是用plotly 5.2.1 創建的
import yfinance as yf
import plotly.graph_objects as go
from stockstats import StockDataFrame

appl = yf.Ticker("AAPL")

# get stock info
ts = (
    appl.history("1y")
    .reset_index()
    .pipe(lambda d: d.rename(columns={c: c.lower() if c != "Date" else "datetime" for c in d.columns}))
)

fig = go.Figure(data=[go.Candlestick(x=ts['datetime'],
                                         open=ts['open'],
                                         high=ts['high'],
                                         low=ts['low'],
                                         close=ts['close'],
                                         line=dict(width=1))])


df_ti = StockDataFrame.retype(ts)
technical_indicator = "close_10_sma"
fig.add_trace(go.Line(x=df_ti['datetime'],y=df_ti[technical_indicator],))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM