【问题标题】:How can I plot a moving average from Pandas to a chart?如何将 Pandas 的移动平均线绘制到图表中?
【发布时间】:2021-08-25 13:27:06
【问题描述】:

给定一个如下所示的表格:

ts,open,high,low,close,adj_close,volume
2014-08-20T12:00:00.123456Z,198.119995,199.160004,198.080002,198.919998,180.141846,72763000
2014-08-21T12:00:00.123456Z,199.089996,199.759995,198.929993,199.500000,180.667160,67791000
2014-08-22T12:00:00.123456Z,199.339996,199.690002,198.740005,199.190002,180.386368,76107000
2014-08-25T12:00:00.123456Z,200.139999,200.589996,199.149994,200.199997,181.301010,63855000
2014-08-26T12:00:00.123456Z,200.330002,200.820007,200.279999,200.330002,181.418716,47298000
2014-08-27T12:00:00.123456Z,200.429993,200.570007,199.940002,200.250000,181.346298,47874000
2014-08-28T12:00:00.123456Z,199.589996,200.270004,199.389999,200.139999,181.246689,58330000
2014-08-29T12:00:00.123456Z,200.449997,200.729996,199.820007,200.710007,181.762909,65907000
2014-09-02T12:00:00.123456Z,200.970001,201.000000,199.860001,200.610001,181.672318,72426000

如何绘制结果的移动平均值?我正在连接到数据库并将结果加载到具有以下内容的数据框中:

import psycopg2
import pandas as pd

df_trades = pd.DataFrame()

try:
    connection = psycopg2.connect(user="admin",
                                  password="quest",
                                  host="127.0.0.1",
                                  port="8812",
                                  database="qdb")
    cursor = connection.cursor()
    df_trades = pd.read_sql_query("select * from my_table",connection)

except (Exception, psycopg2.Error) as error:
    print("Error while connecting to QuestDB", error)
finally:
    if (connection):
        cursor.close()
        connection.close()
        print("QuestDB connection closed")

print(df_trades.head())
# moving average
df_trades['10point_ma'] = df_trades['close'].rolling(window=10).mean()

【问题讨论】:

    标签: questdb


    【解决方案1】:

    在这种情况下你可以做的是使用 plotly:

    import psycopg2
    import pandas as pd
    import plotly.graph_objects as go
    
    df_trades = pd.DataFrame()
    
    try:
        connection = psycopg2.connect(user="admin",
                                      password="quest",
                                      host="127.0.0.1",
                                      port="8812",
                                      database="qdb")
        cursor = connection.cursor()
        df_trades = pd.read_sql_query("select * from my_table",connection)
    
    except (Exception, psycopg2.Error) as error:
        print("Error while connecting to QuestDB", error)
    finally:
        if (connection):
            cursor.close()
            connection.close()
            print("QuestDB connection closed")
    
    print(df_trades.head())
    
    fig = go.Figure()
    
    fig.update_layout(title_text="Table candlestick")
    
    df_trades['10point_ma'] = df_trades['close'].rolling(window=10).mean()
    
    fig.add_trace(go.Scatter(x=df_trades['ts'], y=df_trades['10point_ma'], 
                                name='10 point moving average',
                                mode='lines',
                                opacity=1,
                                marker=dict(color='MediumPurple',
                                size=1)))
    
    # original table as candlestick chart
    fig.add_trace(go.Candlestick(x=df_trades['ts'],
                                open=df_trades['open'],
                                high=df_trades['high'],
                                low=df_trades['low'],
                                close=df_trades['close'],
                                name='My Awesome Chart'))
    
    fig.update(layout_xaxis_rangeslider_visible=False)
    fig.show()
    

    这将生成以下图表:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      相关资源
      最近更新 更多