【问题标题】:How to add labels to candlestick plots?如何为烛台图添加标签?
【发布时间】:2021-06-26 00:17:41
【问题描述】:

https://www.kaggle.com/tencars/interactive-bollinger-bands-for-technical-analysis

我使用上述方法用布林带绘制烛台图。

谁能告诉我如何添加额外的标签来标记地块的某些特征。例如,用一个向上的三角形标记局部最小值“close”,用​​一个向下的三角形标记局部最大值“close”。

【问题讨论】:

    标签: python plotly plotly-python


    【解决方案1】:

    由于无法下载提供的 kaggle 数据,我从 Yahoo Finance 获得了 AAPL 股票价格并将其用作数据。对于图表本身,我使用 kaggle 中的代码更改每条线的颜色来制作图表。对于文本注释,我使用了 add_annotation()。注释数据设置得当。

    import pandas as pd
    from datetime import datetime
    import plotly.graph_objects as go
    import yfinance as yf
    
    df = yf.download("AAPL", start="2021-06-21", interval='1m', end="2021-06-22")
    df.reset_index(inplace=True)
    df.columns =['time', 'open', 'high', 'low', 'close', 'adj close', 'volume']
    # Create a working copy of the last 2 hours of the data
    btc_data = df.iloc[-240:].copy()
    
    # Define the parameters for the Bollinger Band calculation
    ma_size = 20
    bol_size = 2
    
    # Convert the timestamp data to a human readable format
    btc_data.index = pd.to_datetime(btc_data.time, unit='ms')
    
    # Calculate the SMA
    btc_data.insert(0, 'moving_average', btc_data['close'].rolling(ma_size).mean())
    
    # Calculate the upper and lower Bollinger Bands
    btc_data.insert(0, 'bol_upper', btc_data['moving_average'] + btc_data['close'].rolling(ma_size).std() * bol_size)
    btc_data.insert(0, 'bol_lower', btc_data['moving_average'] - btc_data['close'].rolling(ma_size).std() * bol_size)
    
    # Remove the NaNs -> consequence of using a non-centered moving average
    btc_data.dropna(inplace=True)
    
    # Create an interactive candlestick plot with Plotly
    fig = go.Figure(data=[go.Candlestick(x = btc_data.index,
                                         open = btc_data['open'],
                                         high = btc_data['high'],
                                         low = btc_data['low'],
                                         showlegend = False,
                                         close = btc_data['close'])])
    
    # Plot the three lines of the Bollinger Bands indicator
    parameter = ['moving_average', 'bol_lower', 'bol_upper']
    colors = ['blue', 'orange', 'orange']
    for param,c in zip(parameter, colors):
        fig.add_trace(go.Scatter(
            x = btc_data.index,
            y = btc_data[param],
            showlegend = False,
            line_color = c,
            mode='lines',
            line={'dash': 'solid'},
            marker_line_width=2, 
            marker_size=10,
            opacity = 0.8))
        
    # Add title and format axes
    fig.update_layout(
        title='Bitcoin price in the last 2 hours with Bollinger Bands',
        yaxis_title='BTC/USD')
    
    x = '2021-06-21 10:56:00'
    y = df['close'].max()
    fig.add_annotation(x=x, y=y,
                       text='\U000025bc',
                       showarrow=False,
                       yshift=10)
    
    xx = '2021-06-21 09:50:00'
    yy = df['close'].min()
    fig.add_annotation(x=xx, y=yy,
                       text='\U000025b2',
                       showarrow=False,
                       yshift=10)
    
    fig.show()
    

    【讨论】:

    • 谢谢。我有很多这样的箭头要添加。我需要在 for 循环中调用 add_annotation 吗?或者有一种方法可以将所有箭头位置放在一个数组中。然后,为数组中的所有箭头调用一次 add_annotation?
    • 我认为可以通过this answer所示的循环处理来处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    相关资源
    最近更新 更多