最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python實現(xiàn)構(gòu)建量化小程序

 更新時間:2025年06月25日 10:29:17   作者:DDAshley126  
這篇文章主要為大家介紹了如何基于Python構(gòu)建查詢歷史股票行情的應(yīng)用,本文采用了dash框架,快速搭建服務(wù),減少前端渲染頁面和后端交互的工作量,感興趣的小伙伴可以了解下

1 數(shù)據(jù)獲取

量化數(shù)據(jù)接口有很多,有tushare、alltick、xtquant等等。本文使用的是akshare庫,它是開源的財經(jīng)數(shù)據(jù)庫,不僅包含股票、期貨、期權(quán)、外匯、基金等常見的金融數(shù)據(jù),還包括能源、事件、輿情和藝人指數(shù)這類不常見但可能影響市場行為的其他數(shù)據(jù),可以說是目前市面上免費接口里最全的一個。

以獲取深市A股歷史日線數(shù)據(jù)為例,首先獲取深市A股的股票信息:

import akshare as ak
stock_info = ak.stock_info_sz_name_code()
stock_info.to_excel('stock_code.xlsx')

2 應(yīng)用架構(gòu)

本應(yīng)用基于dash框架、開源組件庫feffery_antd_components與akshare金融數(shù)據(jù)接口開發(fā)的輕量級應(yīng)用。app.py中進(jìn)行應(yīng)用對象的實例化,并構(gòu)建應(yīng)用的初始化頁面內(nèi)容;setting.css進(jìn)行前端界面樣式的調(diào)整;data儲存的是深交所A股信息。

- demo
  - data
    - stock_code.xlsx
  - assets
    - setting.css
  - app.py

3 應(yīng)用啟動

安裝好當(dāng)前項目依賴庫,然后直接在終端執(zhí)行python app.py即可啟動應(yīng)用,按照控制臺提示的信息,瀏覽器訪問本地http://127.0.0.1:8050地址即可訪問應(yīng)用。

4 代碼

# app.py
import pandas as pd
import dash
from pyecharts import options as opts
import akshare as ak
from dash import Dash, html, dcc, callback, Output, Input, State
import dash_bootstrap_components as dbc
from pyecharts.charts import Kline
import feffery_antd_components as fac


# 應(yīng)用實例化
app = Dash(
    __name__,
    title='量化小應(yīng)用',
    update_title='加載中...',
    assets_url_path='assets/setting.css'
)

app.layout = fac.AntdSpace([
    dcc.Store(storage_type='local', id='stock-code-store', data=pd.read_excel('data\stock_code.xlsx', dtype={'A股代碼': 'str'}).to_dict('records')),
    fac.AntdFlex([
        fac.AntdIcon(icon='antd-right', style={'color': '#0069d9'}),
        fac.AntdText('股票歷史日線數(shù)據(jù)查詢', className='header')
    ]),
    fac.AntdFlex([
        fac.AntdFlex([
            '股票代碼:',
            dcc.Dropdown(
                id='stock-dropdown',
                multi=False,
                searchable=True,
                value='000001',
                placeholder='股票代碼',
                style={'width': '100px'}
            ),
        ], align='center', gap='small'),
        fac.AntdFlex([
            '日期:',
            fac.AntdDateRangePicker(
                placeholder=['選擇開始日期', '選擇結(jié)束日期'],
                id='stock-dropdown datepicker',
                size='middle',
                prefix=fac.AntdIcon(icon='antd-calendar')
            ),
        ], gap='small'),
        fac.AntdButton(
            '搜索',
            type='primary',
            id='stock-dropdown btn',
            loadingChildren="查詢中",
        ),
    ], align='center', gap='large'),
    fac.AntdCard(
        title='收益概述',
        headStyle={'background': 'rgba(0, 0, 0, 0.3)', 'text-align': 'left'},
        id='card-content',
        className='card'
    ),
], className='container')


@app.callback(
    Output('stock-dropdown', 'options'),
    Input('stock-code-store', 'data')
)
def code_info(data):
    data = pd.DataFrame(data)
    data['A股代碼'] = data['A股代碼'].astype('str')
    options = [
        {'label': x, 'value': 'sz' + y}
        for x, y in zip(data['A股代碼'], data['A股代碼'])
    ]
    return options


@app.callback(
    Output('card-content', 'children'),
     Input('stock-dropdown btn', 'nClicks'),
    [State('stock-code-store', 'data'),
     State('stock-dropdown', 'value'),
     State('stock-dropdown datepicker', 'value')],
    running=[(Output('stock-dropdown btn', "loading"), True, False)],
)
def update(nClicks, data, value, date):
    stock_info = pd.DataFrame(data)

    stock_info = stock_info[stock_info['A股代碼'] == value[2:]].to_dict('records')
    title = stock_info[0]['A股簡稱'] + '(SZ:' + stock_info[0]['A股代碼'] + ')'

    result = ak.stock_zh_a_daily(symbol=value, start_date=date[0], end_date=date[1])
    result.rename(columns={'open': '開盤價', 'close': '收盤價', 'low': '最低', 'high': '最高'}, inplace=True)
    fig = (
        Kline(init_opts=opts.InitOpts(width='1200px', height='300px'))
        .add_xaxis(list(result['date']))
        .add_yaxis(series_name='k線', y_axis=result[['開盤價', '收盤價', '最低', '最高']].values.tolist(),
                   itemstyle_opts=opts.ItemStyleOpts(color='rgb(192, 51, 47)', color0='green'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title=f'{date[0]}-{date[1]}日K線'),
            yaxis_opts=opts.AxisOpts(name='單位凈值(元)', min_=result[['開盤價', '收盤價', '最低', '最高']].min().min(), max_=result[['開盤價', '收盤價', '最低', '最高']].max().max()),
            tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross', formatter=': {c}', border_color='#ccc', background_color='rgba(245, 245, 245, 0.8)'),
            datazoom_opts=opts.DataZoomOpts(is_show=True, range_start=result['date'].min(), range_end=result['date'].max()),
        )
        .set_series_opts(
            markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='max', name='最高價', value=result['最高'].max()), opts.MarkPointItem(type_='min', name='最低價', value=result['最低'].min())], symbol='pin'),
            splitline_opts=opts.SplitAreaOpts(is_show=True),
            linestyle_opts=opts.LineStyleOpts(type_='dashed', color='lightgrey'),
        )
    )
    fig.render('kline.html')

    card_layout = [
        fac.AntdRow(title, className='card-content title', wrap=True),
        fac.AntdRow([
            fac.AntdCol([
                fac.AntdText('最高:', className='card-content index'),
                fac.AntdText(result['最高'].max(), className='card-content value', style={'color': 'red'})
            ]),
            fac.AntdCol([
                fac.AntdText('最低:', className='card-content index'),
                fac.AntdText(result['最低'].max(), className='card-content value', style={'color': 'green'})
            ]),
            fac.AntdCol([
                fac.AntdText(f'成交量:{str(result['volume'].sum())}手', className='card-content index'),]),
            fac.AntdCol([
                fac.AntdText(f'成交額:{str(result['amount'].sum())}元', className='card-content index'),]),
        ], gutter=20),
        html.Iframe(
            srcDoc=open('kline.html', 'r').read(),
            style={
               'height': 300,
               'width': '100%',
               'align': 'center'
            }
        )
    ]
    return card_layout


if __name__ == '__main__':
    app.run(debug=False)
/* setting.css*/
html,body {
    background-color: #ffffff;
    color: #000000;
    text-align: center !important;
    margin: 1%;
    padding: 1%;
    font-family: 'Sans ser-if';
}

.container {
    display: grid;
    width: 1200px;
}

/* 標(biāo)題樣式 */
.header {
    font-size: 18px;
    font-weight: bold;
}

/* 卡片樣式 */
.card {
    width: 1400px;
}

.ant-card-body {
    display: flex;
    flex-direction: column;
}

.card-content {

}
.title {
    font-size: 24px;
    font-weight: bold;
}
.index {
    font-size: 12px;
    font-weight: bold;
}
.value {
    font-weight: bold;
}

5 優(yōu)化方向

由于本文涉及的數(shù)據(jù)交互與計算較少,歷史數(shù)據(jù)獲取的方式是直接調(diào)用akshare庫,而該庫有的數(shù)據(jù)不能頻繁獲取。若數(shù)據(jù)量增多,為提高系統(tǒng)效率與性能,需要采取適當(dāng)?shù)姆绞?,將?shù)據(jù)持久化保存到本地。

構(gòu)建成多頁面應(yīng)用,一個頁面存放一個指標(biāo)

到此這篇關(guān)于使用Python實現(xiàn)構(gòu)建量化小程序的文章就介紹到這了,更多相關(guān)Python構(gòu)建量化程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用tensorflow進(jìn)行音樂類型的分類

    使用tensorflow進(jìn)行音樂類型的分類

    這篇文章主要介紹了使用tensorflow進(jìn)行音樂類型的分類,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • python抽取指定url頁面的title方法

    python抽取指定url頁面的title方法

    今天小編就為大家分享一篇python抽取指定url頁面的title方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • pandas常用表連接merge/concat/join/append詳解

    pandas常用表連接merge/concat/join/append詳解

    使用python的pandas庫可以很容易幫你搞定,而且性能也是很出色的;百萬級的表關(guān)聯(lián),可以秒出,本文給大家分享pandas常用表連接merge/concat/join/append詳解,感興趣的朋友跟隨小編一起看看吧
    2023-02-02
  • Python變量、數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換相關(guān)函數(shù)用法實例詳解

    Python變量、數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換相關(guān)函數(shù)用法實例詳解

    這篇文章主要介紹了Python變量、數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換相關(guān)函數(shù)用法,結(jié)合實例形式詳細(xì)分析了Python變量類型、基本用法、變量類型轉(zhuǎn)換相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下
    2020-01-01
  • Python爬取動態(tài)網(wǎng)頁中圖片的完整實例

    Python爬取動態(tài)網(wǎng)頁中圖片的完整實例

    這篇文章主要給大家介紹了關(guān)于Python爬取動態(tài)網(wǎng)頁中圖片的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python實現(xiàn)Markdown格式消除工具

    Python實現(xiàn)Markdown格式消除工具

    這篇文章主要為大家詳細(xì)介紹了如何使用?Python?和?PyQt5?庫來創(chuàng)建一個簡單易用的?Markdown?格式消除工具,并且支持實時預(yù)覽和文件保存功能,需要的可以了解下
    2025-02-02
  • Python編程scoketServer實現(xiàn)多線程同步實例代碼

    Python編程scoketServer實現(xiàn)多線程同步實例代碼

    這篇文章主要介紹了Python編程scoketServer實現(xiàn)多線程同步實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python通過跳板機(jī)訪問數(shù)據(jù)庫的方法

    Python通過跳板機(jī)訪問數(shù)據(jù)庫的方法

    跳板機(jī)是一類可作為跳板批量操作的遠(yuǎn)程設(shè)備的網(wǎng)絡(luò)設(shè)備,是系統(tǒng)管理員和運(yùn)維人員常用的操作平臺之一。本文給大家介紹Python通過跳板機(jī)訪問數(shù)據(jù)庫的方法,感興趣的朋友跟隨小編一起看看吧
    2021-10-10
  • Python逐行讀取文件中內(nèi)容的簡單方法

    Python逐行讀取文件中內(nèi)容的簡單方法

    今天小編就為大家分享一篇關(guān)于Python逐行讀取文件中內(nèi)容的簡單方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Python opencv缺陷檢測的實現(xiàn)及問題解決

    Python opencv缺陷檢測的實現(xiàn)及問題解決

    這篇文章主要介紹了Python opencv缺陷檢測的實現(xiàn)及問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評論

常熟市| 通许县| 永靖县| 锦州市| 闵行区| 古丈县| 太原市| 周宁县| 高州市| 木里| 西林县| 调兵山市| 和政县| 安顺市| 通化市| 绥棱县| 新晃| 永康市| 奉化市| 遂昌县| 宁河县| 昌江| 潮安县| 晋城| 营口市| 黄梅县| 屏南县| 泽州县| 溆浦县| 左权县| 平塘县| 南靖县| 调兵山市| 扶风县| 淄博市| 普安县| 繁昌县| 花垣县| 离岛区| 瑞金市| 永清县|