备份gp

import pandas as pd
import baostock as bs
import requests
from datetime import datetime, timedelta
import talib as ta

def get_history(gp_type,code,end_date=False,start_date=False,days=14):
    if not end_date:
        end_date=datetime.now().strftime("%Y-%m-%d")
    if not start_date:
        start_date=(datetime.strptime(end_date, "%Y-%m-%d") - timedelta(days=14)).strftime("%Y-%m-%d")
    bs.login()
    rs = bs.query_history_k_data_plus(
        code=gp_type+"."+str(code),         
        fields="code,date,open,high,low,close,volume,adjustflag",  # 要获取的字段(手动指定,Akshare 自动返回全字段)
        start_date=start_date,   # 日期格式:必须带横杠(Akshare 可无)
        end_date=end_date,
        frequency="d",             # 周期:d=日线(和 Akshare 的 period="daily" 对应)
        adjustflag="2"             # 复权:2=前复权(对应 Akshare 的 adjust="qfq")
    )
    # 3. 转为DataFrame(核心步骤)
    df = rs.get_data()
    df = df.replace("", 0)  # 把所有空字符串变成 0
    # 4. 数据类型转换(避免数值以字符串显示)
    df = df.astype({
        "code":str,
        "open": float, 
        "high": float, 
        "low": float,
        "close": float, 
        "volume": float,
        "adjustflag": int  # 复权标识转整数
    })
    bs.logout()
    return df

def gp_code(code):
    str_code=str(code)
    if str_code[0] in ['0','1','2','3']:
        gp_type='sz'
    elif str_code[0] in ['5','6','9']:
        gp_type='sh'
    elif str_code[0] in ['4','8']:
        gp_type='bj'
    else:
        return False
    return [gp_type,str_code]

def get_today(gp_type,code):
    str_code=str(code)
    url="https://qt.gtimg.cn/q="
    url=url+gp_type+str_code
    headers = {
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"
    }
    res=requests.get(url,headers=headers)
    if res.status_code==200:
        if len(res.content)>30:
            reb = res.content.decode("gbk")
            reb = reb.replace("~~~", "~").replace("~~", "~")
            vsx = reb.split('~')
            return [
                        {
                            "code":gp_type+"."+str_code,
                            "date":datetime.now().strftime("%Y-%m-%d"),
                            "open": vsx[5], 
                            "high": vsx[32], 
                            "low": vsx[33],
                            "close": vsx[3], 
                            "volume": float(vsx[6])*100,
                            "adjustflag": 2  # 复权标识转整数
                        },
                        {
                            "name":vsx[1],
                            'money':vsx[42]
                        }
                    ]
    return False

ea=gp_code(600302)

print(ea)

ma=get_today(ea[0],ea[1])

print(ma)

# c=get_history('sh','600302')
# print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
# print(c.head(100))

# df = pd.concat([c, pd.DataFrame([ma[0]])], ignore_index=True)


# df['MA5'] = ta.MA(df['close'], timeperiod=5)
# df['MA10'] = ta.MA(df['close'], timeperiod=10)
# df['MA20'] = ta.MA(df['close'], timeperiod=20)
# # df['MACD'], df['MACD_SIGNAL'], df['MACD_HIST'] = ta.MACD(df['close'])
# df['RSI'] = ta.RSI(df['close'], timeperiod=14)

# # 成交量均线
# df['VOL_MA5'] = ta.MA(df['volume'], timeperiod=5)
# df['VOL_MA10'] = ta.MA(df['volume'], timeperiod=10)
# df['VOL_MA20'] = ta.MA(df['volume'], timeperiod=20)
# df['ATR'] = ta.ATR(df['high'], df['low'], df['close'], timeperiod=6)

# print(df.head(10))
此条目发表在None分类目录。将固定链接加入收藏夹。

发表回复