Skip to content

Commit 6e40033

Browse files
committed
1)Add change_main_tag api 2)Add get_top_vol api 3)Add gfex exchange
1 parent f2848e0 commit 6e40033

File tree

7 files changed

+110
-30
lines changed

7 files changed

+110
-30
lines changed

api-tests/tag/change_main_tag.http

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
POST http://127.0.0.1:8090/api/work/change_main_tag
2+
accept: application/json
3+
Content-Type: application/json
4+
5+
{
6+
"current_main_tag": "医疗器械",
7+
"new_main_tag": "医药"
8+
}

src/zvt/api/selector.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import pandas as pd
55
from sqlalchemy import or_, and_
66

7-
from zvt.api.kdata import default_adjust_type, get_kdata_schema
8-
from zvt.contract import IntervalLevel
7+
from zvt.api.kdata import default_adjust_type, get_kdata_schema, get_latest_kdata_date
8+
from zvt.contract import IntervalLevel, AdjustType
99
from zvt.contract.api import get_entity_ids
1010
from zvt.domain import DragonAndTiger, Stock1dHfqKdata, Stock, LimitUpInfo, StockQuote, StockQuoteLog
1111
from zvt.utils.pd_utils import pd_is_not_null
@@ -335,6 +335,34 @@ def get_shoot_today(up_change_pct=0.03, down_change_pct=-0.03, interval=2):
335335
return up.index.tolist(), down.index.tolist()
336336

337337

338+
def get_top_vol(
339+
entity_ids,
340+
target_date=None,
341+
limit=500,
342+
provider="qmt",
343+
):
344+
if provider == "qmt":
345+
df = StockQuote.query_data(
346+
entity_ids=entity_ids,
347+
columns=[StockQuote.entity_id],
348+
order=StockQuote.turnover.desc(),
349+
limit=limit,
350+
)
351+
return df["entity_id"].to_list()
352+
else:
353+
if not target_date:
354+
target_date = get_latest_kdata_date(provider="em", entity_type="stock", adjust_type=AdjustType.hfq)
355+
df = Stock1dHfqKdata.query_data(
356+
provider="em",
357+
filters=[Stock1dHfqKdata.timestamp == to_pd_timestamp(target_date)],
358+
entity_ids=entity_ids,
359+
columns=[Stock1dHfqKdata.entity_id],
360+
order=Stock1dHfqKdata.turnover.desc(),
361+
limit=limit,
362+
)
363+
return df["entity_id"].to_list()
364+
365+
338366
def get_top_down_today(n=100):
339367
df = StockQuote.query_data(columns=[StockQuote.entity_id], order=StockQuote.change_pct.asc(), limit=n)
340368
if pd_is_not_null(df):
@@ -348,22 +376,8 @@ def get_limit_down_today():
348376

349377

350378
if __name__ == "__main__":
351-
# target_date = get_latest_kdata_date(provider="em", entity_type="stock", adjust_type=AdjustType.hfq)
352-
# big = get_big_cap_stock(timestamp=target_date)
353-
# print(len(big))
354-
# print(big)
355-
# middle = get_middle_cap_stock(timestamp=target_date)
356-
# print(len(middle))
357-
# print(middle)
358-
# small = get_small_cap_stock(timestamp=target_date)
359-
# print(len(small))
360-
# print(small)
361-
# mini = get_mini_cap_stock(timestamp=target_date)
362-
# print(len(mini))
363-
# print(mini)
364-
# df = get_player_performance(start_timestamp="2022-01-01")
365-
# print((get_entity_ids_by_filter(ignore_new_stock=False)))
366-
print(get_limit_up_stocks(timestamp="2023-12-2"))
379+
stocks = get_top_vol(entity_ids=None, provider="em")
380+
assert len(stocks) == 500
367381

368382

369383
# the __all__ is generated

src/zvt/contract/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ class Exchange(Enum):
232232
#: 上海国际能源交易中心
233233
ine = "ine"
234234

235+
#: 广州期货所
236+
gfex = "gfex"
237+
235238
#: 外汇交易所(虚拟)
236239
#: currency exchange(virtual)
237240
forex = "forex"

src/zvt/recorders/em/em_api.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,16 @@ def get_future_list():
569569
entity["exchange"] = "cffex"
570570
entity["code"] = to_zvt_code(entity["code"])
571571
else:
572-
entity["exchange"] = Exchange(entity["exchange"].lower()).value
573-
if entity["code"][-1].lower() == "m":
574-
entity["code"] = entity["code"][:-1]
575-
else:
576-
assert False
577-
entity["code"] = entity["code"].upper()
572+
try:
573+
entity["exchange"] = Exchange(entity["exchange"].lower()).value
574+
if entity["code"][-1].lower() == "m":
575+
entity["code"] = entity["code"][:-1]
576+
else:
577+
assert False
578+
entity["code"] = entity["code"].upper()
579+
except Exception as e:
580+
logger.error(f"wrong item: {item}", e)
581+
continue
578582

579583
entity["entity_type"] = "future"
580584
entity["name"] = item["name"]
@@ -764,10 +768,13 @@ def get_hot_topic(session: Session = None):
764768
if data_list:
765769
hot_topics = []
766770
for position, data in enumerate(data_list):
767-
entity_ids = [
768-
market_code_to_entity_id(market=stock["qMarket"], code=stock["qCode"])
769-
for stock in data["stockList"]
770-
]
771+
if data["stockList"]:
772+
entity_ids = [
773+
market_code_to_entity_id(market=stock["qMarket"], code=stock["qCode"])
774+
for stock in data["stockList"]
775+
]
776+
else:
777+
entity_ids = []
771778
topic_id = data["topicid"]
772779
entity_id = f"hot_topic_{topic_id}"
773780
hot_topics.append(

src/zvt/rest/work.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
MainTagIndustryRelation,
2929
MainTagSubTagRelation,
3030
IndustryInfoModel,
31+
ChangeMainTagModel,
3132
)
3233
from zvt.tag.tag_schemas import (
3334
StockTags,
@@ -250,3 +251,8 @@ def build_main_tag_sub_tag_relation(relation: MainTagSubTagRelation):
250251
tag_service.build_main_tag_sub_tag_relation(main_tag_sub_tag_relation=relation)
251252
tag_service.activate_sub_tags(activate_sub_tags_model=ActivateSubTagsModel(sub_tags=relation.sub_tag_list))
252253
return "success"
254+
255+
256+
@work_router.post("/change_main_tag", response_model=List[StockTagsModel])
257+
def change_main_tag(change_main_tag_model: ChangeMainTagModel):
258+
return tag_service.change_main_tag(change_main_tag_model=change_main_tag_model)

src/zvt/tag/tag_models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class MainTagSubTagRelation(CustomModel):
3737
sub_tag_list: List[str]
3838

3939

40+
class ChangeMainTagModel(CustomModel):
41+
current_main_tag: str
42+
new_main_tag: str
43+
44+
4045
class StockTagsModel(MixinModel):
4146
main_tag: Optional[str] = Field(default=None)
4247
main_tag_reason: Optional[str] = Field(default=None)

src/zvt/tag/tag_service.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
StockTagOptions,
2323
MainTagIndustryRelation,
2424
MainTagSubTagRelation,
25+
ChangeMainTagModel,
2526
)
2627
from zvt.tag.tag_schemas import (
2728
StockTags,
@@ -748,9 +749,45 @@ def build_main_tag_sub_tag_relation(main_tag_sub_tag_relation: MainTagSubTagRela
748749
session.commit()
749750

750751

752+
def change_main_tag(change_main_tag_model: ChangeMainTagModel):
753+
new_main_tag = change_main_tag_model.new_main_tag
754+
_create_main_tag_if_not_existed(main_tag=new_main_tag, main_tag_reason=new_main_tag)
755+
with contract_api.DBSession(provider="zvt", data_schema=StockTags)() as session:
756+
stock_tags: List[StockTags] = StockTags.query_data(
757+
filters=[StockTags.main_tag == change_main_tag_model.current_main_tag],
758+
session=session,
759+
return_type="domain",
760+
)
761+
762+
for stock_tag in stock_tags:
763+
tag_parameter: TagParameter = build_tag_parameter(
764+
tag_type=TagType.main_tag,
765+
tag=new_main_tag,
766+
tag_reason=new_main_tag,
767+
stock_tag=stock_tag,
768+
)
769+
set_stock_tags_model = SetStockTagsModel(
770+
entity_id=stock_tag.entity_id,
771+
main_tag=tag_parameter.main_tag,
772+
main_tag_reason=tag_parameter.main_tag_reason,
773+
sub_tag=tag_parameter.sub_tag,
774+
sub_tag_reason=tag_parameter.sub_tag_reason,
775+
active_hidden_tags=stock_tag.active_hidden_tags,
776+
)
777+
778+
build_stock_tags(
779+
set_stock_tags_model=set_stock_tags_model,
780+
timestamp=now_pd_timestamp(),
781+
set_by_user=True,
782+
keep_current=False,
783+
)
784+
session.refresh(stock_tag)
785+
return stock_tags
786+
787+
751788
if __name__ == "__main__":
752-
# activate_default_main_tag(industry="半导体")
753-
activate_sub_tags(ActivateSubTagsModel(sub_tags=["航天概念", "天基互联", "北斗导航", "通用航空"]))
789+
activate_industry_list(industry_list=["半导体"])
790+
# activate_sub_tags(ActivateSubTagsModel(sub_tags=["航天概念", "天基互联", "北斗导航", "通用航空"]))
754791

755792

756793
# the __all__ is generated

0 commit comments

Comments
 (0)