栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

HOW TO BUILD AN INTERACTIVE APP USING PYTHON IN SEEING THE IBM‘S STOCK PRICE IN REAL TIME?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HOW TO BUILD AN INTERACTIVE APP USING PYTHON IN SEEING THE IBM‘S STOCK PRICE IN REAL TIME?

import pandas as pd
import plotly
import plotly.express as px

import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate

# "pip install alpha_vantage" (if you haven't done so)
from alpha_vantage.timeseries import TimeSeries

#-------------------------------------------------------------------------------
# Set up initial key and financial category

key = '7FEDJMHY3CM2KPEC' # Your API Key
# https://github.com/RomelTorres/alpha_vantage
# Chose your output format or default to JSON (python dict)
ts = TimeSeries(key, output_format='pandas') # 'pandas' or 'json' or 'csv'

#-------------------------------------------------------------------------------
# (only for example purposes) Pull data from API and prepare it for plotting on line chart

# Get the data, returns a tuple
# IBM_data is a pandas dataframe, IBM_meta_data is a dict
# https://github.com/RomelTorres/alpha_vantage/blob/develop/alpha_vantage/timeseries.py
IBM_data, IBM_meta_data = ts.get_daily(symbol='IBM', outputsize='compact')
print(IBM_meta_data)

df = IBM_data.copy()
print(df.head())

df=df.transpose()
print(df.head())

df.rename(index={"1. open":"open", "2. high":"high", "3. low":"low",
                 "4. close":"close","5. volume":"volume"},inplace=True)
df=df.reset_index().rename(columns={'index': 'indicator'})
print(df.head())

df = pd.melt(df,id_vars=['indicator'],var_name='date',value_name='rate')
df = df[df['indicator']!='volume']
print(df[:15])

#-------------------------------------------------------------------------------
# Building our Web app and update financial data automatically

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Interval(
                id='my_interval',
                n_intervals=0,       # number of times the interval was activated
                interval=120*1000,   # update every 2 minutes
    ),
    dcc.Graph(id="world_finance"),   # empty graph to be populated by line chart
])

#-------------------------------------------------------------------------------
@app.callback(
    Output(component_id='world_finance', component_property='figure'),
    [Input(component_id='my_interval', component_property='n_intervals')]
)
def update_graph(n):
    """Pull financial data from Alpha Vantage and update graph every 2 minutes"""

    IBM_data, IBM_meta_data = ts.get_intraday(symbol='IBM',outputsize='compact')
    df = IBM_data.copy()
    df=df.transpose()
    df.rename(index={"1. open":"open", "2. high":"high", "3. low":"low",
                     "4. close":"close","5. volume":"volume"},inplace=True)
    df=df.reset_index().rename(columns={'index': 'indicator'})
    df = pd.melt(df,id_vars=['indicator'],var_name='date',value_name='rate')
    df = df[df['indicator']!='volume']
    print(df[:15])

    line_chart = px.line(
                    data_frame=df,
                    x='date',
                    y='rate',
                    color='indicator',
                    title="Stock: {}".format(IBM_meta_data['2. Symbol'])
                 )
    return (line_chart)

#-------------------------------------------------------------------------------
if __name__ == '__main__':
    app.run_server(debug=True)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号