码迷,mamicode.com
首页 > 其他好文 > 详细

dddd

时间:2019-12-26 19:34:56      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:flow   time   连接   调用   mysql连接   dex   das   函数   group   

# -*- coding: utf-8 -*-
import pandas as pd
import tushare as ts
import lxml
from redis import Redis
import hashlib
import pymysql
import time
import datetime
# =============================================================非一字涨停板家数、炸板家数每日涨跌停价格stk_limit、日线行情daily====================================
def zrztCount():
    ‘‘‘
    daily,提供未复权数据,每分钟内最多调取200次,每次4000条数据
    stk_limit单次最多提取4800条记录,可循环调取,总量不限制
    ‘‘‘
    
    # 定义计数器
    success, fail, count = 0, 0, 0
    
    # 定义计数器
    zrzt, zb, zrdt = 0, 0, 0

    # 创建MySQL操作游标
    cursor = conn_mysql.cursor()
    
    # 获取当日市场股票数据
    for offset in range(0, 12000, 3000):
        daily = pro.daily(trade_date=cur_date, fields=ts_code,trade_date,open,high,low,close, offset=offset,limit=3000)
        limit = pro.stk_limit(trade_date=cur_date,fields=trade_date,ts_code,up_limit,down_limit, offset=offset,limit=3000)
        df = pd.merge(daily,limit,on=[trade_date,ts_code])
        trade_date = df[trade_date]
        cp_num = df.groupby(trade_date).count()    # 当天股票总数
        if df[open]!=df[up_limit] and df[close]==df[up_limit]:
            zrzt +=1    # 自然涨停家数
        elif df[high]==df[up_limit] and df[close]!=df[up_limit]:
            zb +=1    # 炸板家数
        elif df[open]!=df[down_limit] and df[close]==df[down_limit]:
            zrdt +=1    # 自然跌停家数
        
        zrzt_bl = zrdt/cp_num
        zb_bl = zb/cp_num
        zrdt_bl = zrdt/cp_num
    
# =============================================================SHIBOR利率数据:shibor=============================================================
def shiborRate():
    ‘‘‘
    单次最大2000,总量不限制
    ‘‘‘
    # 定义计数器
    success, fail, count = 0, 0, 0

    # 创建MySQL操作游标
    cursor = conn_mysql.cursor()
    
    # 获取当天shibor数据
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    shibor = pro.shibor(date=cur_date,fields=date,on,1w,3m)
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    # 获取历史shibor数据
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    shibor = pro.shibor(start_date=20190526,end_date=20191225,fields=date,on,1w,3m)
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    for index, row in shibor.iterrows()
        trade_date = row[date]    # 日期
        gy_shibor = row[on]    # 隔夜hibor
        yz_shibor = row[1w]    # 1周shibor
        sy_shibor = row[3m]    # 3个月shibor
        
        try:
            sql = ‘‘‘insert into shibor__data(trade_date,gy_shibor,yz_shibor,sy_shibor) values(‘%s‘,‘%s‘,‘%s‘,‘%s‘)‘‘‘(trade_date,gy_shibor,yz_shibor,sy_shibor)
            cursor.execute(sql)
            conn_mysql.commit()
            # 统计入库成功数量
            success += 1
        except:
            print(trade_date,total_amount)
            conn_mysql.rollback()
            # 统计入库失败数量
            fail += 1

    # 统计请求数据量
    count += 1

    # 关闭MySQL操作游标
    cursor.close()
    print(Shibor数据获取完毕,新入库数据:%d条 % success)
    print(Shibor数据获取完毕,入库失败数据:%d条 % fail)
    print(Shibor数据获取完毕,本次共请求数据:%d条 % count)    

# =============================================================两市成交总额:调用daily=============================================================
def totalTrade(conn_mysql,conn_redis,cur_date):
    ‘‘‘
    交易日每天15点~16点之间更新,每分钟内最多调取200次,每次4000条数据
    ‘‘‘

    # 定义计数器
    success, fail, count = 0, 0, 0

    # 创建MySQL操作游标
    cursor = conn_mysql.cursor()
    
    # 创建空的dataframe
    df = pd.DataFrame(columns=(trade_date,amount))
    
    # 计算当天沪深两市成交额
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    for offset in range(0, 12000, 3000):
        daily = pro.daily(trade_date=cur_date, fields=trade_date,amount, offset=offset,limit=3000)
        df = pd.DataFrame([df,daily],axis=0)
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    # 计算历史沪深两市成交额
    for offset in range(0, 900000, 3000):
        daily = pro.daily(start_date=20190526,end_date=20191225, fields=trade_date,amount, offset=offset,limit=3000)
        df = pd.DataFrame([df,daily],axis=0)
    # ---------------------------------------------------------------------------------------------------------------------------------------------    
    
    trade_date = df[trade_date]    # 交易日期
    total_amount = (df.groupby(trade_date).sum())/10  # 成交额,原始单位为:千元

    try:
        sql = ‘‘‘insert into total_amount_data(trade_date,total_amount) values(‘%s‘,‘%s‘)‘‘‘ % (trade_date,total_amount)
        cursor.execute(sql)
        conn_mysql.commit()
        # 统计入库成功数量
        success += 1
    except:
        print(trade_date,total_amount)
        conn_mysql.rollback()
        # 统计入库失败数量
        fail += 1

    # 统计请求数据量
    count += 1

    # 关闭MySQL操作游标
    cursor.close()
    print(沪深总成交额数据计算完毕,新入库数据:%d条 % success)
    print(沪深总成交额数据计算完毕,入库失败数据:%d条 % fail)
    print(沪深总成交额数据计算完毕,本次共请求数据:%d条 % count)
    
# =============================================================龙虎榜成交总额:调用top_list龙虎榜每日明细===========================================================
def totalTopinst(conn_mysql,conn_redis,cur_date):
    ‘‘‘
    交易日每天15点~16点之间更新,每分钟内最多调取200次,每次4000条数据
    ‘‘‘

    # 定义计数器
    success, fail, count = 0, 0, 0

    # 创建MySQL操作游标
    cursor = conn_mysql.cursor()
    
    # 计算当天沪深两市龙虎榜成交额
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    total_inst_amount = pro.top_list(trade_date=cur_date,fields=trade_date,l_sell,l_buy,net_amount)
    total = total_inst_amount.groupby(trade_date)
    
    trade_date = total_inst_amount[trade_date]    # 交易日期
    total_l_sell = total_inst_amount.groupby(l_sell).sum()    # 龙虎榜总卖出额
    total_l_buy = total_inst_amount.groupby(l_buy).sum()    # 龙虎榜总买入额
    total_net_amount = total_inst_amount.groupby(net_amount).sum()    # 龙虎榜总净买入额
    try:
        sql = ‘‘‘insert into total_inst_amount_data(trade_date,total_l_sell,total_l_buy,total_net_amount) values(‘%s‘,‘%s‘,‘%s‘,‘%s‘)‘‘‘ %(trade_date,total_l_sell,total_l_buy,total_net_amount)
        cursor.execute(sql)
        conn_mysql.commit()
        # 统计入库成功数量
        success += 1
    except:
        print(trade_date,total_amount)
        conn_mysql.rollback()
        # 统计入库失败数量
        fail += 1

    # 统计请求数据量
    count += 1    
    
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    
    # 计算历史沪深两市龙虎榜成交额
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    start_date = datetime.datetime.strptime(20190526,%Y%m%d)
    end_date = datetime.datetime.strptime(20191225,%Y%m%d)
    if start_date <= end_date:
        trade_date = (start_date + datetime.timedelta(days=1)).strftime(%Y%m%d)
        total_inst_amount = pro.top_list(trade_date=cur_date,fields=trade_date,l_sell,l_buy,net_amount)
        total = total_inst_amount.groupby(trade_date)
    
        trade_date = total_inst_amount[trade_date]    # 交易日期
        total_l_sell = total_inst_amount.groupby(l_sell).sum()    # 龙虎榜总卖出额
        total_l_buy = total_inst_amount.groupby(l_buy).sum()    # 龙虎榜总买入额
        total_net_amount = total_inst_amount.groupby(net_amount).sum()    # 龙虎榜总净买入额
    
        try:
            sql = ‘‘‘insert into total_inst_amount_data(trade_date,total_l_sell,total_l_buy,total_net_amount) values(‘%s‘,‘%s‘,‘%s‘,‘%s‘)‘‘‘ %(trade_date,total_l_sell,total_l_buy,total_net_amount)
            cursor.execute(sql)
            conn_mysql.commit()
            # 统计入库成功数量
            success += 1
        except:
            print(trade_date,total_amount)
            conn_mysql.rollback()
            # 统计入库失败数量
            fail += 1

        # 统计请求数据量
        count += 1
    # ---------------------------------------------------------------------------------------------------------------------------------------------

    # 关闭MySQL操作游标
    cursor.close()
    print(沪深龙虎榜总成交额数据计算完毕,新入库数据:%d条 % success)
    print(沪深龙虎榜总成交额数据计算完毕,入库失败数据:%d条 % fail)
    print(沪深龙虎榜总成交额数据计算完毕,本次共请求数据:%d条 % count)
    

# =============================================================沪深港通资金流向:moneyflow_hsgt=============================================================
def moneyflowHsgt(conn_mysql,conn_redis,cur_date):
    ‘‘‘
    每次最多返回300条记录,总量不限制
    ‘‘‘
    # 定义计数器
    success, fail, count = 0, 0, 0

    # 创建MySQL操作游标
    cursor = conn_mysql.cursor()
    
    # 获取当天数据
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    for offset in range(0, 12000, 300):
        hsgt = pro.moneyflow_hsgt(trade_date=cur_date, fields=trade_date,hgt,sgt,north_money, offset=offset,limit=300)
        
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    
    # 获取历史数据
    # ---------------------------------------------------------------------------------------------------------------------------------------------
    for offset in range(0, 12000, 300):
        hsgt = pro.moneyflow_hsgt(start_date=20190526,end_date=20191225,fields=trade_date,hgt,sgt,north_money, offset=offset,limit=300)
        
    # ---------------------------------------------------------------------------------------------------------------------------------------------
        for index, row in hsgt.iterrows():
            trade_date = row[trade_date]  # 交易日期
            hgt = row[hgt] * 100  # 沪股通(百万元)
            sgt = row[sgt] * 100  # 深股通(百万元)
            north_money = row[north_money] * 100  # 北向资金(百万元)

            try:
                sql = ‘‘‘insert into hsgt_data(trade_date,hgt,sgt,north_money) values(‘%s‘,‘%s‘,‘%s‘,‘%s‘)‘‘‘ % (trade_date, hgt, sgt, north_money)
                cursor.execute(sql)
                conn_mysql.commit()
                # 统计入库成功数量
                success += 1
            except:
                print(trade_date, hgt, sgt, north_money)
                conn_mysql.rollback()
                # 统计入库失败数量
                fail += 1

            # 统计请求数据量
            count += 1

    # 关闭MySQL操作游标
    cursor.close()
    print(沪深港通资金数据获取完毕,新入库数据:%d条 % success)
    print(沪深港通资金数据获取完毕,入库失败数据:%d条 % fail)
    print(沪深港通资金数据获取完毕,本次共请求数据:%d条 % count)

# ================================================================主函数================================================================
if __name__ == __main__:
    # 初始化tushare.pro接口
    pro = ts.pro_api(ac16b470869c5d82db5033ae9288f77b282d2b5519507d6d2c72fdd7)

    # 获取当天日期
    cur_date = time.strftime("%Y%m%d", time.localtime())

    # 创建MySQL连接对象
    conn_mysql = pymysql.connect(user=root, password=123456, database=stock, charset=utf8)

    # 创建Redis链接对象
    conn_redis = Redis(host=127.0.0.1, port=6379)

    moneyflowHsgt(conn_mysql,conn_redis,cur_date)

    tradeTotalamount(conn_mysql, conn_redis, cur_date)

    conn_mysql.close()
    

dddd

标签:flow   time   连接   调用   mysql连接   dex   das   函数   group   

原文地址:https://www.cnblogs.com/Iceredtea/p/12103664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!