PostgreSQL_数据使用与日数据分享

发布于:2025-03-23 ⋅ 阅读:(19) ⋅ 点赞:(0)

 目录

前置:

1 使用

1.1 获取前复权因子

1.2 查询股票的纵向数据

1.3 查询股票的横向数据

2 日数据分享(截止至:2025-03-21)

总结


前置:

本博文是一个系列。在本人“数据库专栏”-》“PostgreSQL_”开头的博文。

本文为该系列最后一篇。

1 使用

1.1 获取前复权因子

访问优矿官网

 ticker_list = []
field_list = ['secID','tradeDate','accumAdjFactor']
df = DataAPI.MktEqudGet(secID=u"",ticker=ticker_list,tradeDate=u"",beginDate=u"1990-01-01",endDate=u"2025-03-21",isOpen="",field=field_list,pandas="1")
df.to_csv('adj000.csv',encoding='utf-8')

 将要操作的股票的前复权因子获取,存入 t_ticker_adj_factor 表格,不存入也行,直接访问文件。

1.2 查询股票的纵向数据

select tradeDate,openPrice,highestPrice,lowestPrice,closePrice,turnoverVol,turnoverValue,dealAmount,turnoverRate,negMarketValue,marketValue,chgPct,PE,PE1,PB,isOpen,vwap from t_stock_daily where ticker='000001';

代码:

def query_vertical(ticker:str):
    sql_str = f"select tradeDate,openPrice,highestPrice,lowestPrice,closePrice,turnoverVol,turnoverValue,dealAmount,turnoverRate,negMarketValue,marketValue,chgPct,PE,PE1,PB,isOpen,vwap from t_stock_daily where ticker=\'{ticker}\';"
    conn = connect_db()
    cur = conn.cursor()
    cur.execute(sql_str)
    res = cur.fetchone()
    df = pd.DataFrame(data={
        'tradeDate': res[0],
        'openPrice': res[1],
        'highestPrice': res[2],
        'lowestPrice': res[3],
        'closePrice': res[4],
        'turnoverVol': res[5],
        'turnoverValue': res[6],
        'dealAmount': res[7],
        'turnoverRate': res[8],
        'negMarketValue': res[9],
        'marketValue': res[10],
        'chgPct': res[11],
        'PE': res[12],
        'PE1': res[13],
        'PB': res[14],
        'isOpen': res[15],
        'vwap': res[16]
    })
    df.to_excel(r'E:/temp002/'+ticker+'.xlsx',engine='openpyxl')
    cur.close()
    conn.close()
    pass

实现前复权数据方法:

将未复权数据的日期与复权因子的日期对其,然后收开高低都和复权因子相乘,所得就是前复权数据。

1.3 查询股票的横向数据

select ticker,openPrice,highestPrice,lowestPrice,closePrice,turnoverVol,turnoverValue,dealAmount,turnoverRate,negMarketValue,marketValue,chgPct,PE,PE1,PB,isOpen,vwap from t_daily where tradeDate='2025-03-19';

代码:

def query_horizontal(date_str:str):
    sql_str = f"select ticker,openPrice,highestPrice,lowestPrice,closePrice,turnoverVol,turnoverValue,dealAmount,turnoverRate,negMarketValue,marketValue,chgPct,PE,PE1,PB,isOpen,vwap from t_daily where tradeDate=\'{date_str}\';"
    conn = connect_db()
    cur = conn.cursor()
    cur.execute(sql_str)
    res = cur.fetchone()
    df = pd.DataFrame(data={
        'ticker': res[0],
        'openPrice': res[1],
        'highestPrice': res[2],
        'lowestPrice': res[3],
        'closePrice': res[4],
        'turnoverVol': res[5],
        'turnoverValue': res[6],
        'dealAmount': res[7],
        'turnoverRate': res[8],
        'negMarketValue': res[9],
        'marketValue': res[10],
        'chgPct': res[11],
        'PE': res[12],
        'PE1': res[13],
        'PB': res[14],
        'isOpen': res[15],
        'vwap': res[16]
    })
    df.to_excel(r'E:/temp002/' + date_str + '.xlsx', engine='openpyxl')
    cur.close()
    conn.close()
    pass

2 日数据分享(截止至:2025-03-21)

链接: https://pan.baidu.com/s/1Ng-0InEHtEhZcUZWLdjqcQ?pwd=krb3 提取码: krb3

文件名:db_stock.dump,1.5G左右

下载完后通过 pg_restore 命令导入。

总结

1 postgreSQL功能强大,如果有其他数据库经验的,上手很容易

2 postgreSQL的性能挺好的,通过python批量操作能传入的sql语句可以很大,而且执行效率很高

3 postgreSQL功能全面,提供了命令行,界面操作,一个软件包啥都包,安装十分方便

4 postgreSQL包含的数组、json等数据对象对处理复杂数据十分方便,总之就很棒^_^

注意:

1 数组的索引是从1开始

2 python操作上传sql语句时,涉及到参数是字符串的,要在字符串开头结尾增加\'