Skip to content

Commit

Permalink
improve report top stocks
Browse files Browse the repository at this point in the history
  • Loading branch information
foolcage committed Jun 16, 2022
1 parent ad91105 commit d931875
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 48 deletions.
42 changes: 25 additions & 17 deletions examples/report_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from zvt import zvt_config
from zvt.api import get_top_volume_entities, get_top_performance_entities, TopType
from zvt.api.kdata import get_latest_kdata_date, get_kdata_schema, default_adjust_type
from zvt.api.selector import get_stocks
from zvt.contract import IntervalLevel
from zvt.contract.api import get_entities, get_entity_schema, get_entity_ids
from zvt.contract.factor import Factor
Expand Down Expand Up @@ -158,6 +159,7 @@ def report_top_entities(
data_provider,
periods=None,
ignore_new_stock=True,
ignore_st=True,
entity_type="stock",
adjust_type=None,
top_count=30,
Expand All @@ -177,21 +179,19 @@ def report_top_entities(
target_date = get_latest_kdata_date(provider=data_provider, entity_type=entity_type, adjust_type=adjust_type)
email_action = EmailInformer()

# 至少上市一年
filter_entity_ids = []
if ignore_new_stock:
pre_year = next_date(target_date, -365)

entity_ids = get_entity_ids(
provider=entity_provider, entity_schema=entity_schema, filters=[entity_schema.timestamp <= pre_year]
)
filter_entity_ids = get_stocks(
provider=entity_provider,
ignore_st=ignore_st,
ignore_new_stock=ignore_new_stock,
entity_schema=entity_schema,
target_date=target_date,
)

if not entity_ids:
msg = f"{entity_type} no entity_ids listed one year"
logger.error(msg)
email_action.send_message(zvt_config["email_username"], "report_top_stats error", msg)
return
filter_entity_ids = entity_ids
if not filter_entity_ids:
msg = f"{entity_type} no entity_ids selected"
logger.error(msg)
email_action.send_message(zvt_config["email_username"], "report_top_stats error", msg)
return

filter_turnover_df = kdata_schema.query_data(
filters=[
Expand Down Expand Up @@ -262,11 +262,19 @@ def report_top_entities(

if __name__ == "__main__":
report_top_entities(
entity_type="stockhk",
entity_type="block",
entity_provider="em",
data_provider="em",
turnover_threshold=0,
turnover_rate_threshold=0,
top_count=10,
periods=[365, 750],
ignore_new_stock=False,
ignore_st=False,
adjust_type=None,
turnover_threshold=50000000,
turnover_rate_threshold=0.005,
em_group=None,
em_group_over_write=False,
return_type=TopType.negative,
)

# the __all__ is generated
Expand Down
24 changes: 15 additions & 9 deletions examples/reports/report_top_stocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def report_top_stocks():
data_provider="em",
periods=[3, 8, 15],
ignore_new_stock=True,
ignore_st=True,
adjust_type=None,
top_count=20,
turnover_threshold=0,
Expand All @@ -35,6 +36,7 @@ def report_top_stocks():
data_provider="em",
periods=[30, 60],
ignore_new_stock=True,
ignore_st=True,
adjust_type=None,
top_count=20,
turnover_threshold=0,
Expand All @@ -48,10 +50,11 @@ def report_top_stocks():
entity_type="stock",
entity_provider="em",
data_provider="em",
periods=[800],
periods=[365, 750],
ignore_new_stock=True,
ignore_st=True,
adjust_type=None,
top_count=40,
top_count=25,
turnover_threshold=100000000,
turnover_rate_threshold=0.01,
em_group="谁有我惨",
Expand All @@ -69,9 +72,10 @@ def report_top_stockhk():
top_count=10,
periods=[3, 8, 15],
ignore_new_stock=True,
ignore_st=False,
adjust_type=None,
turnover_threshold=100000000,
turnover_rate_threshold=0.002,
turnover_threshold=50000000,
turnover_rate_threshold=0.005,
em_group="短期最强",
em_group_over_write=False,
return_type=TopType.positive,
Expand All @@ -84,9 +88,10 @@ def report_top_stockhk():
top_count=10,
periods=[30, 60],
ignore_new_stock=True,
ignore_st=False,
adjust_type=None,
turnover_threshold=100000000,
turnover_rate_threshold=0.002,
turnover_threshold=50000000,
turnover_rate_threshold=0.005,
em_group="中期最强",
em_group_over_write=False,
return_type=TopType.positive,
Expand All @@ -97,11 +102,12 @@ def report_top_stockhk():
entity_provider="em",
data_provider="em",
top_count=10,
periods=[800],
periods=[365, 750],
ignore_new_stock=True,
ignore_st=False,
adjust_type=None,
turnover_threshold=100000000,
turnover_rate_threshold=0.002,
turnover_threshold=50000000,
turnover_rate_threshold=0.005,
em_group="谁有我惨",
em_group_over_write=False,
return_type=TopType.negative,
Expand Down
23 changes: 2 additions & 21 deletions examples/reports/report_vol_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,10 @@ def report_vol_up():
over_mode="or",
up_intervals=60,
turnover_threshold=300000000,
turnover_rate_threshold=0.02,
turnover_rate_threshold=0.01,
entity_ids=entity_ids,
)

report_targets(
factor_cls=VolumeUpMaFactor,
entity_provider="em",
data_provider="em",
em_group="bull股票",
title="放量突破(半)年线板块",
entity_type="block",
em_group_over_write=True,
filter_by_volume=False,
adjust_type=AdjustType.qfq,
start_timestamp="2019-01-01",
# factor args
windows=[120, 250],
over_mode="or",
up_intervals=40,
turnover_threshold=10000000000,
turnover_rate_threshold=0.02,
)

report_targets(
factor_cls=VolumeUpMaFactor,
entity_provider="em",
Expand All @@ -95,7 +76,7 @@ def report_vol_up():
over_mode="or",
up_intervals=20,
turnover_threshold=100000000,
turnover_rate_threshold=0.02,
turnover_rate_threshold=0.01,
)


Expand Down
20 changes: 19 additions & 1 deletion src/zvt/api/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from zvt.api.kdata import default_adjust_type, get_kdata_schema
from zvt.contract import IntervalLevel
from zvt.domain import DragonAndTiger, Stock1dHfqKdata
from zvt.contract.api import get_entity_ids
from zvt.domain import DragonAndTiger, Stock1dHfqKdata, Stock
from zvt.utils import to_pd_timestamp, next_date, current_date, pd_is_not_null

logger = logging.getLogger(__name__)
Expand All @@ -24,6 +25,23 @@
OUT_DEPS = ["dep_1", "dep_2", "dep_3", "dep_4", "dep_5"]


def get_stocks(provider="em", ignore_st=True, ignore_new_stock=True, target_date=None, entity_schema=Stock):
filters = []
if ignore_new_stock:
if not target_date:
target_date = current_date()
pre_year = next_date(target_date, -365)
filters += [entity_schema.timestamp <= pre_year]
if ignore_st:
filters += [
entity_schema.name.not_like("%退%"),
entity_schema.name.not_like("%ST%"),
entity_schema.name.not_like("%*ST%"),
]
entity_ids = get_entity_ids(provider=provider, entity_schema=entity_schema, filters=filters)
return entity_ids


def get_dragon_and_tigger_player(start_timestamp, end_timestamp=None, direction="in"):
assert direction in ("in", "out")

Expand Down

0 comments on commit d931875

Please sign in to comment.