示例#1
0
def collect_stock_daily_trading():
    """
    获取并保存每日股票交易数据
    """
    url = eastmoney_stock_api
    data = request_and_handle_data(url)

    stock_data = data['rank']
    for i in stock_data:
        stock = i.split(',')
        stock_number = stock[1]
        stock_name = stock[2]
        sdt = SDT(stock_number=stock_number, stock_name=stock_name)
        sdt.yesterday_closed_price = float(stock[3])
        sdt.today_opening_price = float(stock[4])
        sdt.today_closing_price = float(stock[5])
        sdt.today_highest_price = float(stock[6])
        sdt.today_lowest_price = float(stock[7])
        sdt.turnover_amount = int(stock[8])
        sdt.turnover_volume = int(stock[9])
        sdt.increase_amount = float(stock[10])
        sdt.increase_rate = stock[11]
        sdt.today_average_price = float(stock[12])
        sdt.quantity_relative_ratio = float(stock[22])
        sdt.turnover_rate = stock[23]

        if float(sdt.turnover_rate.replace('%', '')) == 0.0:
            # 去掉停牌的交易数据
            continue

        if not check_duplicate(sdt):
            sdt.save()
def collect_stock_daily_trading():
    """
    获取并保存每日股票交易数据
    """
    url = eastmoney_stock_api
    data = request_and_handle_data(url)

    stock_data = data["rank"]
    for i in stock_data:
        stock = i.split(",")
        stock_number = stock[1]
        stock_name = stock[2]
        sdt = SDT(stock_number=stock_number, stock_name=stock_name)
        sdt.yesterday_closed_price = float(stock[3])
        sdt.today_opening_price = float(stock[4])
        sdt.today_closing_price = float(stock[5])
        sdt.today_highest_price = float(stock[6])
        sdt.today_lowest_price = float(stock[7])
        sdt.turnover_amount = int(stock[8])
        sdt.turnover_volume = int(stock[9])
        sdt.increase_amount = float(stock[10])
        sdt.increase_rate = stock[11]
        sdt.today_average_price = float(stock[12])
        sdt.quantity_relative_ratio = float(stock[22])
        sdt.turnover_rate = stock[23]

        if not check_duplicate(sdt):
            sdt.save()
def collect_stock_daily_trading():
    """
    获取并保存每日股票交易数据
    """
    url = eastmoney_stock_api
    data = {}
    global retry
    while retry > 0:
        try:
            data = request_and_handle_data(url)
            retry = 0
        except Exception:
            retry -= 1

    stock_data = data.get('rank', [])
    today_trading = {}
    for i in stock_data:
        stock = i.split(',')
        if stock[4] == '-':
            continue
        stock_number = stock[1]
        stock_name = stock[2]
        sdt = SDT(stock_number=stock_number, stock_name=stock_name)
        sdt.yesterday_closed_price = float(stock[3])
        sdt.today_opening_price = float(stock[4])
        sdt.today_closing_price = float(stock[5])
        sdt.today_highest_price = float(stock[6])
        sdt.today_lowest_price = float(stock[7])
        sdt.turnover_amount = int(stock[8])
        sdt.turnover_volume = int(stock[9])
        sdt.increase_amount = float(stock[10])
        sdt.increase_rate = stock[11]
        sdt.today_average_price = float(stock[12])
        quantity_relative_ratio = stock[22]
        sdt.quantity_relative_ratio = 0 if quantity_relative_ratio == '-' else float(
            quantity_relative_ratio)
        sdt.turnover_rate = stock[23]
        sdt.date = datetime.datetime.combine(datetime.date.today(),
                                             datetime.time(0, 0))

        if sdt.turnover_amount == 0:
            # 去掉停牌的交易数据
            continue
        today_trading[stock_number] = sdt
    return today_trading
def collect_stock_daily_trading(date):
    trading_data = get_pro_client().daily(trade_date=date.strftime('%Y%m%d'))

    for i in range(0, len(trading_data)):
        stock = trading_data.iloc[i]
        stock_number = stock.ts_code.split('.')[0]

        existSdt = SDT.objects(Q(stock_number=stock_number) & Q(date=date))
        if existSdt and len(existSdt) > 0:
            sdt = existSdt[0]
        else:
            sdt = SDT(stock_number=stock_number)
            sdt.date = date

        sdt.yesterday_closed_price = stock.pre_close
        sdt.today_opening_price = stock.open
        sdt.today_closing_price = stock.close
        sdt.today_highest_price = stock.high
        sdt.today_lowest_price = stock.low
        sdt.turnover_amount = stock.amount
        sdt.turnover_volume = stock.vol
        sdt.increase_amount = stock.change
        sdt.increase_rate = str(stock.pct_chg) + '%'
        sdt.save()
示例#5
0
def collect_his_trading(stock_number, stock_name, start_date, end_date):
    ts_code = tushare_util.gen_ts_code(stock_number)

    his_data = tushare_util.get_pro_client().query(
        'daily',
        ts_code=ts_code,
        start_date=start_date.strftime('%Y%m%d'),
        end_date=end_date.strftime('%Y%m%d'))
    for i in range(0, len(his_data)):
        trade_data = his_data.iloc[i]
        try:
            date = datetime.datetime.strptime(trade_data.trade_date, '%Y%m%d')
        except Exception as e:
            continue

        existSdt = SDT.objects(Q(stock_number=stock_number) & Q(date=date))
        if existSdt and len(existSdt) > 0:
            sdt = existSdt[0]
        else:
            sdt = SDT(stock_number=stock_number)
            sdt.date = date

        sdt.stock_name = stock_name
        sdt.yesterday_closed_price = trade_data.pre_close
        sdt.today_opening_price = trade_data.open
        sdt.today_closing_price = trade_data.close
        sdt.today_highest_price = trade_data.high
        sdt.today_lowest_price = trade_data.low
        sdt.turnover_amount = trade_data.amount
        sdt.turnover_volume = trade_data.vol
        sdt.increase_amount = trade_data.change
        sdt.increase_rate = str(trade_data.pct_chg) + '%'
        try:
            sdt.save()
        except Exception as e:
            continue