Skip to content

Commit

Permalink
stock tick
Browse files Browse the repository at this point in the history
  • Loading branch information
foolcage committed May 3, 2024
1 parent d54a8f0 commit e3a0712
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 154 deletions.
18 changes: 13 additions & 5 deletions examples/tag_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
from collections import Counter

from zvt.api import china_stock_code_to_id, get_china_exchange
from zvt.api.selector import get_entity_ids_by_filter
from zvt.api.tag import tag_stock, get_stock_tags, get_limit_up_reasons
from zvt.contract.api import get_entities
from zvt.domain import BlockStock, Block, Stock
from zvt.utils import current_date, date_time_by_interval, pd_is_not_null

from zvt.domain import BlockStock, Block, Stock, LimitUpInfo


def get_limit_up_reasons(entity_id):
info = LimitUpInfo.query_data(
entity_id=entity_id, order=LimitUpInfo.timestamp.desc(), limit=1, return_type="domain"
)

topics = []
if info and info[0].reason:
topics = topics + info[0].reason.split("+")
return topics


def get_concept(code):
Expand Down
8 changes: 1 addition & 7 deletions examples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
import json
import logging
import os
import pprint
import time

import eastmoneypy
import pandas as pd

from zvt.api.selector import get_entity_ids_by_filter
from zvt.api.stats import get_top_performance_entities_by_periods
from zvt.api.tag import get_limit_up_reasons
from zvt.contract.api import get_entities, decode_entity_id
from zvt.domain import StockNews, Stock, LimitUpInfo
from zvt.utils import date_time_by_interval, today

Expand Down Expand Up @@ -191,5 +186,4 @@ def get_hot_topics(start_timestamp=None, days_ago=20, limit=15):
# entities = get_entities(provider="em", entity_type="stock", entity_ids=ids, return_type="domain")
#
# print(msg_group_stocks_by_topic(entities=entities, threshold=1))
# get_hot_topics(days_ago=10)
print(get_limit_up_reasons(entity_id="stock_sz_300857"))
get_hot_topics(days_ago=10)
73 changes: 0 additions & 73 deletions src/zvt/api/tag.py

This file was deleted.

219 changes: 215 additions & 4 deletions src/zvt/broker/qmt/qmt_api.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
# -*- coding: utf-8 -*-
import logging
from datetime import datetime

import pandas as pd
from xtquant import xtdata

from zvt.contract import IntervalLevel, AdjustType
from zvt.contract.api import decode_entity_id
from zvt.utils import to_time_str
from zvt.contract.api import decode_entity_id, df_to_db
from zvt.domain import StockQuotes, Stock
from zvt.utils import to_time_str, current_date, to_pd_timestamp


# https://dict.thinktrader.net/nativeApi/start_now.html?id=e2M5nZ

# http://docs.thinktrader.net/vip/QMT-Simple/
logger = logging.getLogger(__name__)


def _to_qmt_code(entity_id):
_, exchange, code = decode_entity_id(entity_id=entity_id)
return f"{code}.{exchange.upper()}"


def _to_zvt_entity_id(qmt_code):
code, exchange = qmt_code.split(".")
exchange = exchange.lower()
return f"stock_{exchange}_{code}"


def _to_qmt_dividend_type(adjust_type: AdjustType):
if adjust_type == AdjustType.qfq:
return "front"
Expand All @@ -24,6 +36,48 @@ def _to_qmt_dividend_type(adjust_type: AdjustType):
return "none"


def _qmt_instrument_detail_to_stock(stock_detail):
exchange = stock_detail["ExchangeID"].lower()
code = stock_detail["InstrumentID"]
name = stock_detail["InstrumentName"]
list_date = to_pd_timestamp(stock_detail["OpenDate"])
end_date = to_pd_timestamp(stock_detail["ExpireDate"])
pre_close = stock_detail["PreClose"]
limit_up_price = stock_detail["UpStopPrice"]
limit_down_price = stock_detail["DownStopPrice"]
float_volume = stock_detail["FloatVolume"]
total_volume = stock_detail["TotalVolume"]

entity_id = f"stock_{exchange}_{code}"

return {
"id": entity_id,
"entity_id": entity_id,
"timestamp": list_date,
"entity_type": "stock",
"exchange": exchange,
"code": code,
"name": name,
"list_date": list_date,
"end_date": end_date,
"pre_close": pre_close,
"limit_up_price": limit_up_price,
"limit_down_price": limit_down_price,
"float_volume": float_volume,
"total_volume": total_volume,
}


def get_entity_list():
stocks = xtdata.get_stock_list_in_sector("沪深A股")
entity_list = []
for stock in stocks:
stock_detail = xtdata.get_instrument_detail(stock, False)
entity_list.append(_qmt_instrument_detail_to_stock(stock_detail))

return pd.DataFrame.from_records(data=entity_list)


def get_kdata(
entity_id,
start_timestamp,
Expand Down Expand Up @@ -52,5 +106,162 @@ def get_kdata(
return pd.concat(dfs, axis=1)


def _tick_to_quotes():
pass


def record_tick():
stocks = xtdata.get_stock_list_in_sector("沪深A股")
logger.info(f"today stocks[{len(stocks)}]:{stocks}")

df = Stock.query_data(provider="em", return_type="df", index="entity_id")

def on_data(datas, stock_df=df):
dt = datetime.now()
var = {
"time": 1714460254000,
"lastPrice": 36.08,
"open": 35.4,
"high": 36.62,
"low": 35.13,
"lastClose": 34.1,
"amount": 67690300.0,
"volume": 18793,
"pvolume": 1879308,
"stockStatus": 0,
"openInt": 18,
"transactionNum": 0,
"lastSettlementPrice": 0.0,
"settlementPrice": 0.0,
"pe": 0.0,
"askPrice": [36.07, 0.0, 0.0, 0.0, 0.0],
"bidPrice": [36.07, 0.0, 0.0, 0.0, 0.0],
"askVol": [14, 0, 0, 0, 0],
"bidVol": [14, 12, 0, 0, 0],
"volRatio": 0.0,
"speed1Min": 0.0,
"speed5Min": 0.0,
}
# #: 是否涨停
# is_limit_up = Column(Boolean)
# #: 封涨停金额
# limit_up_amount = Column(Float)
# #: 是否跌停
# is_limit_down = Column(Boolean)
# #: 封跌停金额
# limit_down_amount = Column(Float)
# #: 5挡卖单金额
# ask_amount = Column(Float)
# #: 5挡买单金额
# bid_amount = Column(Float)
# #: 流通市值
# float_cap = Column(Float)
# #: 总市值
# total_cap = Column(Float)
tick_df = pd.DataFrame.from_dict(datas, orient="index")
tick_df.index = tick_df.index.map(_to_zvt_entity_id)

meta_df = stock_df.loc[tick_df.index]
df = pd.concat([tick_df, meta_df], axis=1)

df = df.rename(columns={"lastPrice": "price", "amount": "turnover"})
df["timestamp"] = pd.to_datetime(df["time"], unit="ms")
df["turnover_rate"] = df["pvolume"]
df["change_pct"] = (df["price"] - df["lastClose"]) / df["lastClose"]
df["ask_amount"] = (df["askPrice"] * df["askVol"]).sum(axis=1)
df["bid_amount"] = (df["bidPrice"] * df["bidVol"]).sum(axis=1)
print(df)
df_to_db(df=df, data_schema=StockQuotes, provider="qmt")

xtdata.subscribe_whole_quote(["SH", "SZ"], callback=on_data)
xtdata.run()


def record_kdata(entity_ids):

stock_list = [_to_qmt_code(entity_id=entity_id) for entity_id in entity_ids]
now = datetime.now()
start_time = datetime(year=now.year, month=now.month, day=now.day, hour=9, minute=20, second=0)
start_time = to_time_str(start_time, fmt="YYYYMMDDHHmmss")

end_time = datetime(year=now.year, month=now.month, day=now.day, hour=15, minute=0, second=0)
end_time = to_time_str(end_time, fmt="YYYYMMDDHHmmss")
while True:
records = xtdata.get_local_data(
stock_list=stock_list,
period="tick",
start_time=start_time,
end_time=end_time,
fill_data=False,
)
datas = records.get(stock_list[0])
start_time = to_time_str(to_pd_timestamp(int(datas[-1][0])), fmt="YYYYMMDDHHmmss")
logger.info(f"to {start_time}")

if start_time == end_time:
logger.info("finished")
break


if __name__ == "__main__":
print(get_kdata(entity_id="stock_sz_000338", start_timestamp="20230101", end_timestamp="20230329"))
print(get_kdata(entity_id="stock_sz_000001", start_timestamp="20230101", end_timestamp="20230329"))
# 'time' #时间戳
# 'lastPrice' #最新价
# 'open' #开盘价
# 'high' #最高价
# 'low' #最低价
# 'lastClose' #前收盘价
# 'amount' #成交总额
# 'volume' #成交总量
# 'pvolume' #原始成交总量
# 'stockStatus' #证券状态
# 'openInt' #持仓量
# 'lastSettlementPrice' #前结算
# 'askPrice' #委卖价
# 'bidPrice' #委买价
# 'askVol' #委卖量
# 'bidVol' #委买量
# 'transactionNum' #成交笔数
{
"000001.SZ": {
"timetag": "20240425 14:57:36",
"lastPrice": 10.61,
"open": 10.5,
"high": 10.62,
"low": 10.48,
"lastClose": 10.53,
"amount": 1161898300,
"volume": 1100234,
"pvolume": 110023424,
"stockStatus": 0,
"openInt": 18,
"settlementPrice": 0,
"lastSettlementPrice": 0,
"askPrice": [10.6, 0, 0, 0, 0],
"bidPrice": [10.6, 0, 0, 0, 0],
"askVol": [3349, 0, 0, 0, 0],
"bidVol": [3349, 2600, 0, 0, 0],
},
"000002.SZ": {
"timetag": "20240425 14:57:36",
"lastPrice": 6.55,
"open": 6.5,
"high": 6.68,
"low": 6.48,
"lastClose": 6.54,
"amount": 1242969100,
"volume": 1889977,
"pvolume": 188997683,
"stockStatus": 0,
"openInt": 18,
"settlementPrice": 0,
"lastSettlementPrice": 0,
"askPrice": [6.55, 0, 0, 0, 0],
"bidPrice": [6.55, 0, 0, 0, 0],
"askVol": [4030, 1032, 0, 0, 0],
"bidVol": [4030, 0, 0, 0, 0],
},
}
# [(1713834900000, 0. , 0., 0. , 0. , 44.96, 0.00000000e+00, 0, 0, 0, 12, 0., list([44.95, 0.0, 0.0, 0.0, 0.0]), list([44.95, 0.0, 0.0, 0.0, 0.0]), list([8, 3, 0, 0, 0]), list([8, 0, 0, 0, 0]), 0., 0, 0.),
# (1713834909000, 0. , 0., 0. , 0. , 44.96, 0.00000000e+00, 0, 0, 0, 12, 0., list([44.95, 0.0, 0.0, 0.0, 0.0]), list([44.95, 0.0, 0.0, 0.0, 0.0]), list([13, 1, 0, 0, 0]), list([13, 0, 0, 0, 0]), 0., 0, 0.),
#
1 change: 0 additions & 1 deletion src/zvt/domain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def get_future_name(code):

__all__ += _macro_all


# import all from submodule actor
from .actor import *
from .actor import __all__ as _actor_all
Expand Down
Loading

0 comments on commit e3a0712

Please sign in to comment.