Skip to content

Commit 2badc51

Browse files
committed
1)Close WAL mode for most of db 2)add em api get_top_tradable_list 3)add today shoot/top runner 4)Upgrade some requirements to be compatible with Python 3.9 latter
1 parent bfeff46 commit 2badc51

File tree

12 files changed

+260
-33
lines changed

12 files changed

+260
-33
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ci:
22
autoupdate_schedule: monthly
33
repos:
44
- repo: https://github.com/psf/black
5-
rev: 21.11b1
5+
rev: 24.8.0
66
hooks:
77
- id: black
88
# - repo: https://github.com/PyCQA/flake8

README-cn.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
[炒股的三大原理](https://mp.weixin.qq.com/s/FoFR63wFSQIE_AyFubkZ6Q)
1414

15+
**声明**
16+
17+
本项目目前不保证任何向后兼容性,请谨慎升级。
18+
随着作者思想的变化,一些以前觉得重要的东西可能也变得不重要,从而可能不会进行维护。
19+
而一些新的东西的加入对你是否有用,需要自己去评估。
20+
21+
1522
**Read this in other languages: [English](README-cn.md).**
1623

1724
**详细文档:[https://zvt.readthedocs.io/en/latest/](https://zvt.readthedocs.io/en/latest/)**

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
[The Three Major Principles of Stock Trading](https://mp.weixin.qq.com/s/FoFR63wFSQIE_AyFubkZ6Q)
1414

15+
**Declaration**
16+
17+
This project does not currently guarantee any backward compatibility, so please upgrade with caution.
18+
As the author's thoughts evolve, some things that were once considered important may become less so, and thus may not be maintained.
19+
Whether the addition of some new elements will be useful to you needs to be assessed by yourself.
20+
1521
**Read this in other languages: [中文](README-cn.md).**
1622

1723
**Read the docs:[https://zvt.readthedocs.io/en/latest/](https://zvt.readthedocs.io/en/latest/)**

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
requests==2.31.0
2-
SQLAlchemy==2.0.28
2+
SQLAlchemy==2.0.36
33
pandas==2.0.3
44
pydantic==2.6.4
55
arrow==1.2.3
66
openpyxl==3.1.1
77
demjson3==3.0.6
88
plotly==5.13.0
9-
dash==2.8.1
9+
dash==2.18.2
1010
jqdatapy==0.1.8
1111
dash-bootstrap-components==1.3.1
1212
dash_daq==0.5.0
13-
scikit-learn==1.2.1
13+
scikit-learn==1.5.2
1414
fastapi==0.110.0
1515
fastapi-pagination==0.12.23
1616
apscheduler==3.10.4

src/zvt/broker/qmt/qmt_quote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def download_capital_data():
282282
def clear_history_quote():
283283
session = get_db_session("qmt", data_schema=StockQuote)
284284
session.query(StockQuote).filter(StockQuote.timestamp < current_date()).delete()
285-
start_date = date_time_by_interval(current_date(), -20)
285+
start_date = date_time_by_interval(current_date(), -10)
286286
session.query(Stock1mQuote).filter(Stock1mQuote.timestamp < start_date).delete()
287287
session.query(StockQuoteLog).filter(StockQuoteLog.timestamp < start_date).delete()
288288
session.commit()

src/zvt/contract/register.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ def register_schema(
108108
added_columns = [c for c in table.columns if c.name not in existing_columns]
109109
index_list = []
110110
with engine.connect() as con:
111-
con.execute(text("PRAGMA journal_mode=WAL;"))
111+
# FIXME: close WAL mode for saving space, most of time no need to write in multiple process
112+
if db_name in ("zvt_info","stock_news","stock_tags"):
113+
con.execute(text("PRAGMA journal_mode=WAL;"))
114+
112115
rs = con.execute(text("PRAGMA INDEX_LIST('{}')".format(table_name)))
113116
for row in rs:
114117
index_list.append(row[1])

src/zvt/recorders/em/em_api.py

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import requests
99
import sqlalchemy
1010
from requests import Session
11-
from sqlalchemy import func
1211

1312
from zvt.api.kdata import generate_kdata_id
1413
from zvt.api.utils import value_to_pct, china_stock_code_to_id
@@ -589,6 +588,61 @@ def get_future_list():
589588
return df
590589

591590

591+
def get_top_tradable_list(entity_type, fields, limit, entity_flag, exchange=None, return_quote=False):
592+
url = f"https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=2&invt=2&fields={fields}&pn=1&pz={limit}&fid=f3&po=1&{entity_flag}&ut=f057cbcbce2a86e2866ab8877db1d059&forcect=1&cb=cbCallbackMore&&callback=jQuery34109676853980006124_{now_timestamp() - 1}&_={now_timestamp()}"
593+
resp = requests.get(url, headers=DEFAULT_HEADER)
594+
595+
resp.raise_for_status()
596+
597+
result = json_callback_param(resp.text)
598+
resp.close()
599+
data = result["data"]["diff"]
600+
df = pd.DataFrame.from_records(data=data)
601+
602+
if return_quote:
603+
df = df[
604+
[
605+
"f12",
606+
"f14",
607+
"f2",
608+
"f3",
609+
]
610+
]
611+
df.columns = ["code", "name", "price", "change_pct"]
612+
else:
613+
if entity_type == TradableType.stock:
614+
df = df[["f12", "f13", "f14", "f20", "f21", "f9", "f23"]]
615+
df.columns = ["code", "exchange", "name", "cap", "cap1", "pe", "pb"]
616+
df[["cap", "cap1", "pe", "pb"]] = df[["cap", "cap1", "pe", "pb"]].apply(pd.to_numeric, errors="coerce")
617+
else:
618+
df = df[["f12", "f13", "f14"]]
619+
df.columns = ["code", "exchange", "name"]
620+
if exchange:
621+
df["exchange"] = exchange.value
622+
df["entity_type"] = entity_type.value
623+
df["id"] = df[["entity_type", "exchange", "code"]].apply(lambda x: "_".join(x.astype(str)), axis=1)
624+
df["entity_id"] = df["id"]
625+
626+
return df
627+
628+
629+
def get_top_stocks(limit=100):
630+
# 沪深和北交所
631+
entity_flag = "fs=m:0+t:6+f:!2,m:0+t:13+f:!2,m:0+t:80+f:!2,m:1+t:2+f:!2,m:1+t:23+f:!2,m:0+t:81+s:2048"
632+
fields = "f2,f3,f12,f14"
633+
return get_top_tradable_list(
634+
entity_type=TradableType.stock, fields=fields, limit=limit, entity_flag=entity_flag, return_quote=True
635+
)
636+
637+
638+
def get_top_stockhks(limit=20):
639+
entity_flag = "fs=b:DLMK0144,b:DLMK0146"
640+
fields = "f2,f3,f12,f14"
641+
return get_top_tradable_list(
642+
entity_type=TradableType.stockhk, fields=fields, limit=limit, entity_flag=entity_flag, return_quote=True
643+
)
644+
645+
592646
def get_tradable_list(
593647
entity_type: Union[TradableType, str] = "stock",
594648
exchange: Union[Exchange, str] = None,
@@ -665,31 +719,15 @@ def get_tradable_list(
665719
else:
666720
assert False
667721

722+
# f2, f3, f4, f12, f13, f14, f19, f111, f148
668723
fields = "f1,f2,f3,f4,f12,f13,f14"
669724
if entity_type == TradableType.stock:
670725
# 市值,流通市值,pe,pb
671726
fields = fields + ",f20,f21,f9,f23"
672-
url = f"https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=2&invt=2&fields={fields}&pn=1&pz={limit}&fid=f3&po=1&{entity_flag}&ut=f057cbcbce2a86e2866ab8877db1d059&forcect=1&cb=cbCallbackMore&&callback=jQuery34109676853980006124_{now_timestamp() - 1}&_={now_timestamp()}"
673-
resp = requests.get(url, headers=DEFAULT_HEADER)
674-
675-
resp.raise_for_status()
676-
677-
result = json_callback_param(resp.text)
678-
resp.close()
679-
data = result["data"]["diff"]
680-
df = pd.DataFrame.from_records(data=data)
681-
if entity_type == TradableType.stock:
682-
df = df[["f12", "f13", "f14", "f20", "f21", "f9", "f23"]]
683-
df.columns = ["code", "exchange", "name", "cap", "cap1", "pe", "pb"]
684-
df[["cap", "cap1", "pe", "pb"]] = df[["cap", "cap1", "pe", "pb"]].apply(pd.to_numeric, errors="coerce")
685-
else:
686-
df = df[["f12", "f13", "f14"]]
687-
df.columns = ["code", "exchange", "name"]
688727

689-
df["exchange"] = exchange.value
690-
df["entity_type"] = entity_type.value
691-
df["id"] = df[["entity_type", "exchange", "code"]].apply(lambda x: "_".join(x.astype(str)), axis=1)
692-
df["entity_id"] = df["id"]
728+
df = get_top_tradable_list(
729+
entity_type=entity_type, fields=fields, limit=limit, entity_flag=entity_flag, exchange=exchange
730+
)
693731
if entity_type == TradableType.block:
694732
df["category"] = block_category.value
695733

@@ -996,11 +1034,14 @@ def to_zvt_code(code):
9961034
# print(df)
9971035
# print(len(df))
9981036
# df = get_tradable_list(entity_type="block")
1037+
# print(df)
9991038
# df = get_tradable_list(entity_type="indexus")
1039+
# print(df)
10001040
# df = get_tradable_list(entity_type="currency")
1041+
# print(df)
10011042
# df = get_tradable_list(entity_type="index")
1002-
# df = get_kdata(entity_id="index_us_SPX", level="1d")
10031043
# print(df)
1044+
# df = get_kdata(entity_id="index_us_SPX", level="1d")
10041045
# df = get_treasury_yield(pn=1, ps=50, fetch_all=False)
10051046
# print(df)
10061047
# df = get_future_list()
@@ -1022,10 +1063,12 @@ def to_zvt_code(code):
10221063
# print(events)
10231064
# print(get_hot_topic())
10241065
# record_hot_topic()
1025-
df = StockHotTopic.query_data(
1026-
filters=[func.json_extract(StockHotTopic.entity_ids, "$").contains("stock_sh_600809")],
1027-
)
1028-
print(df)
1066+
# df = StockHotTopic.query_data(
1067+
# filters=[func.json_extract(StockHotTopic.entity_ids, "$").contains("stock_sh_600809")],
1068+
# )
1069+
# print(df)
1070+
print(get_top_stocks())
1071+
print(get_top_stockhks())
10291072

10301073

10311074
# the __all__ is generated

src/zvt/tag/tag_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ def activate_sub_tags(activate_sub_tags_model: ActivateSubTagsModel):
651651
# entity_ids = df["entity_id"].tolist()
652652
entity_ids = None
653653

654+
# stock_tag with sub_tag but not set to related main_tag yet
654655
stock_tags = StockTags.query_data(
655656
session=session,
656657
entity_ids=entity_ids,

src/zvt/tasks/qmt_tick_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# -*- coding: utf-8 -*-
2+
from zvt import init_log
23
from zvt.broker.qmt.qmt_quote import record_tick
34

45
if __name__ == "__main__":
6+
init_log("qmt_tick_runner.log")
57
from apscheduler.schedulers.background import BackgroundScheduler
68

79
sched = BackgroundScheduler()

src/zvt/tasks/stock_pool_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from apscheduler.schedulers.background import BackgroundScheduler
55

6-
from zvt import zvt_config
6+
from zvt import zvt_config, init_log
77
from zvt.api.selector import get_entity_ids_by_filter
88
from zvt.domain import (
99
Stock,
@@ -125,6 +125,7 @@ def record_data_and_build_stock_pools():
125125

126126

127127
if __name__ == "__main__":
128+
init_log("sotck_pool_runner.log")
128129
record_data_and_build_stock_pools()
129130
sched.add_job(func=record_data_and_build_stock_pools, trigger="cron", hour=16, minute=00, day_of_week="mon-fri")
130131
sched.start()

0 commit comments

Comments
 (0)