Skip to content

Commit

Permalink
Realtime Stock1dKada from qmt tick
Browse files Browse the repository at this point in the history
  • Loading branch information
foolcage committed Aug 4, 2024
1 parent 2004a83 commit b032c4c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 45 deletions.
6 changes: 5 additions & 1 deletion src/zvt/broker/qmt/qmt_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from zvt.contract import IntervalLevel, AdjustType
from zvt.contract.api import decode_entity_id, df_to_db, get_db_session
from zvt.domain import StockQuote, Stock
from zvt.domain import StockQuote, Stock, Stock1dKdata
from zvt.domain.quotes.stock.stock_quote import Stock1mQuote, StockQuoteLog
from zvt.utils.pd_utils import pd_is_not_null
from zvt.utils.time_utils import (
Expand Down Expand Up @@ -198,6 +198,7 @@ def on_data(datas, stock_df=entity_df):
)

df = df.rename(columns={"lastPrice": "price", "amount": "turnover"})
df["close"] = df["price"]

df["timestamp"] = df["time"].apply(to_pd_timestamp)

Expand Down Expand Up @@ -230,8 +231,11 @@ def on_data(datas, stock_df=entity_df):
df["float_cap"] = df["float_volume"] * df["price"]
df["total_cap"] = df["total_volume"] * df["price"]

df["provider"] = "qmt"
# 实时行情统计,只保留最新
df_to_db(df, data_schema=StockQuote, provider="qmt", force_update=True, drop_duplicates=False)
df["level"] = "1d"
df_to_db(df, data_schema=Stock1dKdata, provider="qmt", force_update=True, drop_duplicates=False)

# 1分钟分时
df["id"] = df[["entity_id", "timestamp"]].apply(
Expand Down
23 changes: 3 additions & 20 deletions src/zvt/domain/quotes/stock/stock_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@

from zvt.contract import Mixin
from zvt.contract.register import register_schema
from zvt.domain import StockKdataCommon

StockQuoteBase = declarative_base()


class StockQuote(StockQuoteBase, Mixin):
class StockQuote(StockQuoteBase, StockKdataCommon):
__tablename__ = "stock_quote"
code = Column(String(length=32))
name = Column(String(length=32))

#: UNIX时间戳
time = Column(Integer)
#: 最新价
price = Column(Float)
# 涨跌幅
change_pct = Column(Float)
# 成交金额
turnover = Column(Float)
# 换手率
turnover_rate = Column(Float)
#: 是否涨停
is_limit_up = Column(Boolean)
#: 封涨停金额
Expand All @@ -41,21 +33,12 @@ class StockQuote(StockQuoteBase, Mixin):
total_cap = Column(Float)


class StockQuoteLog(StockQuoteBase, Mixin):
class StockQuoteLog(StockQuoteBase, StockKdataCommon):
__tablename__ = "stock_quote_log"
code = Column(String(length=32))
name = Column(String(length=32))

#: UNIX时间戳
time = Column(Integer)
#: 最新价
price = Column(Float)
# 涨跌幅
change_pct = Column(Float)
# 成交金额
turnover = Column(Float)
# 换手率
turnover_rate = Column(Float)
#: 是否涨停
is_limit_up = Column(Boolean)
#: 封涨停金额
Expand Down
46 changes: 23 additions & 23 deletions src/zvt/tag/tag_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

class TagInfoModel(MixinModel):
tag: str
tag_reason: Optional[str] = None
tag_reason: Optional[str] = Field(default=None)


class CreateTagInfoModel(CustomModel):
tag: str
tag_reason: Optional[str] = None
tag_reason: Optional[str] = Field(default=None)


class StockTagsModel(MixinModel):
main_tag: str
main_tag_reason: Optional[str] = None
main_tag: Optional[str] = Field(default=None)
main_tag_reason: Optional[str] = Field(default=None)
main_tags: Dict[str, str]

sub_tag: Union[str, None]
sub_tag_reason: Optional[str] = None
sub_tag: Optional[str] = Field(default=None)
sub_tag_reason: Optional[str] = Field(default=None)
sub_tags: Union[Dict[str, str], None]

active_hidden_tags: Union[Dict[str, str], None]
Expand All @@ -36,11 +36,11 @@ class StockTagsModel(MixinModel):
class SimpleStockTagsModel(CustomModel):
entity_id: str
name: str
main_tag: str
main_tag_reason: Optional[str] = None
main_tag: Optional[str] = Field(default=None)
main_tag_reason: Optional[str] = Field(default=None)
main_tags: Dict[str, str]
sub_tag: Union[str, None]
sub_tag_reason: Optional[str] = None
sub_tag_reason: Optional[str] = Field(default=None)
sub_tags: Union[Dict[str, str], None]
active_hidden_tags: Union[Dict[str, str], None]

Expand All @@ -56,31 +56,31 @@ class QuerySimpleStockTagsModel(CustomModel):
class BatchSetStockTagsModel(CustomModel):
entity_ids: List[str]
tag: str
tag_reason: Optional[str] = None
tag_reason: Optional[str] = Field(default=None)
tag_type: TagType


class TagParameter(CustomModel):
main_tag: str
main_tag_reason: Optional[str] = None
sub_tag: Optional[str] = None
sub_tag_reason: Optional[str] = None
main_tag_reason: Optional[str] = Field(default=None)
sub_tag: Optional[str] = Field(default=None)
sub_tag_reason: Optional[str] = Field(default=None)


class StockTagOptions(CustomModel):
main_tag: Optional[str] = None
sub_tag: Optional[str] = None
main_tag: Optional[str] = Field(default=None)
sub_tag: Optional[str] = Field(default=None)
main_tag_options: List[CreateTagInfoModel]
sub_tag_options: List[CreateTagInfoModel]


class SetStockTagsModel(CustomModel):
entity_id: str
main_tag: str
main_tag_reason: Optional[str] = None
sub_tag: Optional[str] = None
sub_tag_reason: Optional[str] = None
active_hidden_tags: Optional[Dict[str, str]] = None
main_tag_reason: Optional[str] = Field(default=None)
sub_tag: Optional[str] = Field(default=None)
sub_tag_reason: Optional[str] = Field(default=None)
active_hidden_tags: Optional[Dict[str, str]] = Field(default=None)

# @field_validator("main_tag")
# @classmethod
Expand Down Expand Up @@ -148,8 +148,8 @@ class CreateStockPoolsModel(CustomModel):


class QueryStockTagStatsModel(CustomModel):
stock_pool_name: Optional[str] = None
entity_ids: Optional[List[str]] = None
stock_pool_name: Optional[str] = Field(default=None)
entity_ids: Optional[List[str]] = Field(default=None)
query_type: Optional[TagStatsQueryType] = Field(default=TagStatsQueryType.details)

@field_validator("stock_pool_name", "entity_ids")
Expand Down Expand Up @@ -180,8 +180,8 @@ def stock_pool_name_must_be_in(cls, v: str) -> str:

class StockTagDetailsModel(CustomModel):
entity_id: str
main_tag: str
sub_tag: Union[str, None]
main_tag: Optional[str] = Field(default=None)
sub_tag: Optional[str] = Field(default=None)
hidden_tags: Union[List[str], None]

#: 代码
Expand Down
2 changes: 1 addition & 1 deletion src/zvt/trading/trading_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class StockQuoteModel(MixinModel):
#: 总市值
total_cap: float

main_tag: str = Field(default=None)
main_tag: Optional[str] = Field(default=None)
sub_tag: Union[str, None] = Field(default=None)
hidden_tags: Union[List[str], None] = Field(default=None)

Expand Down

0 comments on commit b032c4c

Please sign in to comment.