Skip to content

Commit

Permalink
1)improve target selector 2)add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
foolcage committed May 24, 2022
1 parent a830952 commit c31b898
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 97 deletions.
82 changes: 23 additions & 59 deletions examples/report_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import time
from typing import Type

from tabulate import tabulate

from examples.utils import add_to_eastmoney
from zvt import zvt_config
from zvt.api import get_top_volume_entities, get_top_performance_entities
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.contract import IntervalLevel
from zvt.contract.api import get_entities, get_entity_schema, get_entity_ids
Expand Down Expand Up @@ -155,19 +153,21 @@ def report_targets(
)


def report_top_stats(
def report_top_entites(
entity_provider,
data_provider,
periods=[7, 30, 365],
periods=None,
ignore_new_stock=True,
entity_type="stock",
adjust_type=None,
top_count=30,
turnover_threshold=100000000,
turnover_rate_threshold=0.02,
em_group_over_write=True,
report_stats=False,
return_type=TopType.positive,
):
if periods is None:
periods = [7, 30, 365]
if not adjust_type:
adjust_type = default_adjust_type(entity_type=entity_type)
kdata_schema = get_kdata_schema(entity_type=entity_type, adjust_type=adjust_type)
Expand Down Expand Up @@ -215,88 +215,52 @@ def report_top_stats(

logger.info(f"{entity_type} filter_entity_ids size: {len(filter_entity_ids)}")
filters = [kdata_schema.entity_id.in_(filter_entity_ids)]

stats = []
ups = []
downs = []

for period in periods:
for i, period in enumerate(periods):
start = next_date(target_date, -period)
df, _ = get_top_performance_entities(
positive_df, negative_df = get_top_performance_entities(
entity_type=entity_type,
start_timestamp=start,
filters=filters,
pct=1,
show_name=True,
entity_provider=entity_provider,
data_provider=data_provider,
return_type=return_type,
)
df.rename(columns={"score": f"score_{period}"}, inplace=True)

if report_stats:
ups.append(tabulate(df.iloc[:top_count], headers="keys"))
downs.append(tabulate(df.iloc[-top_count:], headers="keys"))

stats.append(tabulate(df.describe(), headers="keys"))
if return_type == TopType.positive:
tag = "最靓仔"
df = positive_df
else:
tag = "谁有我惨"
df = negative_df

# 最近一周最靓仔的
if period == 7:
if i == 0:
inform(
email_action,
entity_ids=df.index[:top_count].tolist(),
target_date=target_date,
title=f"{entity_type} {period}日内 最靓仔",
title=f"{entity_type} {period}日内 {tag}",
entity_provider=entity_provider,
entity_type=entity_type,
em_group="最靓仔",
em_group=tag,
em_group_over_write=em_group_over_write,
)
# 最近一月最靓仔的
elif period == 30:
else:
inform(
email_action,
entity_ids=df.index[:top_count].tolist(),
target_date=target_date,
title=f"{entity_type} {period}日内 最靓仔",
title=f"{entity_type} {period}日内 {tag}",
entity_provider=entity_provider,
entity_type=entity_type,
em_group="最靓仔",
em_group=tag,
em_group_over_write=False,
)

# 一年内跌幅最大的
elif period == 365:
inform(
email_action,
entity_ids=df.index[-top_count:].tolist(),
target_date=target_date,
title=f"{entity_type} {period}日内 谁有我惨",
entity_provider=entity_provider,
entity_type=entity_type,
em_group="谁有我惨",
em_group_over_write=em_group_over_write,
)

if report_stats:
msg = "\n"
for s in stats:
msg = msg + s + "\n"
email_action.send_message(zvt_config["email_username"], f"{target_date} {entity_type}统计报告", msg)

msg = "\n"
for up in ups:
msg = msg + up + "\n"
email_action.send_message(zvt_config["email_username"], f"{target_date} {entity_type}涨幅统计报告", msg)

msg = "\n"
for down in downs:
msg = msg + down + "\n"

email_action.send_message(zvt_config["email_username"], f"{target_date} {entity_type}跌幅统计报告", msg)


if __name__ == "__main__":
report_top_stats(
report_top_entites(
entity_type="stockhk",
entity_provider="em",
data_provider="em",
Expand All @@ -305,4 +269,4 @@ def report_top_stats(
)

# the __all__ is generated
__all__ = ["report_targets", "report_top_stats"]
__all__ = ["report_targets", "report_top_entites"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,17 @@

from apscheduler.schedulers.background import BackgroundScheduler

from examples.report_utils import report_top_stats
from examples.report_utils import report_top_entites
from zvt import init_log

logger = logging.getLogger(__name__)

sched = BackgroundScheduler()


@sched.scheduled_job("cron", hour=17, minute=30, day_of_week="mon-fri")
def report_stats():
report_top_stats(
entity_type="stock",
entity_provider="em",
data_provider="em",
periods=[7, 30, 365],
ignore_new_stock=True,
adjust_type=None,
top_count=30,
turnover_threshold=100000000,
turnover_rate_threshold=0.02,
em_group_over_write=True,
)
report_top_stats(
@sched.scheduled_job("cron", hour=18, minute=30, day_of_week="mon-fri")
def report_top_stockhks():
report_top_entites(
entity_type="stockhk",
entity_provider="em",
data_provider="em",
Expand All @@ -40,9 +28,9 @@ def report_stats():


if __name__ == "__main__":
init_log("report_stats.log")
init_log("report_hk_stats.log")

report_stats()
report_top_stockhks()

sched.start()

Expand Down
52 changes: 52 additions & 0 deletions examples/reports/report_top_stocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import logging

from apscheduler.schedulers.background import BackgroundScheduler

from examples.report_utils import report_top_entites
from zvt import init_log
from zvt.api import TopType

logger = logging.getLogger(__name__)

sched = BackgroundScheduler()


@sched.scheduled_job("cron", hour=17, minute=0, day_of_week="mon-fri")
def report_top_stocks():
report_top_entites(
entity_type="stock",
entity_provider="em",
data_provider="em",
periods=[3, 8, 30],
ignore_new_stock=True,
adjust_type=None,
top_count=20,
turnover_threshold=400000000,
turnover_rate_threshold=0.02,
em_group_over_write=True,
return_type=TopType.positive,
)
report_top_entites(
entity_type="stock",
entity_provider="em",
data_provider="em",
periods=[365],
ignore_new_stock=True,
adjust_type=None,
top_count=30,
turnover_threshold=200000000,
turnover_rate_threshold=0.02,
em_group_over_write=True,
return_type=TopType.negative,
)


if __name__ == "__main__":
init_log("report_top_stocks.log")

report_top_stocks()

sched.start()

sched._thread.join()
2 changes: 1 addition & 1 deletion examples/reports/report_vol_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def report_vol_up():
windows=[120, 250],
over_mode="or",
up_intervals=60,
turnover_threshold=200000000,
turnover_threshold=300000000,
turnover_rate_threshold=0.02,
entity_ids=entity_ids,
)
Expand Down
122 changes: 122 additions & 0 deletions examples/research/top_dragon_tiger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# -*- coding: utf-8 -*-
from typing import Optional, Type, List, Union

import pandas as pd

from zvt.api import get_top_performance_by_month
from zvt.api.selector import get_players
from zvt.contract import TradableEntity, IntervalLevel, AdjustType
from zvt.contract.factor import Transformer, Accumulator
from zvt.domain import Stock
from zvt.factors import TechnicalFactor
from zvt.utils import pd_is_not_null, pre_month_start_date, next_date


def top_dragon_and_tiger(data_provider="em", start_timestamp="2021-01-01", end_timestamp="2022-01-01"):
dfs = []
for start_date, end_date, df in get_top_performance_by_month(
start_timestamp=start_timestamp, end_timestamp=end_timestamp, list_days=250, data_provider=data_provider
):
pre_month_start = pre_month_start_date(start_date)
for entity_id in df.index[:30]:
players = get_players(
entity_id=entity_id,
start_timestamp=next_date(start_date, 15),
end_timestamp=end_timestamp,
provider=data_provider,
direction="in",
)
print(players)
dfs.append(players)

player_df = pd.concat(dfs, sort=True)
return player_df.sort_index(level=[0, 1])


class DragonTigerFactor(TechnicalFactor):
def __init__(
self,
entity_id: str,
entity_schema: Type[TradableEntity] = Stock,
provider: str = "em",
entity_provider: str = "em",
exchanges: List[str] = None,
codes: List[str] = None,
start_timestamp: Union[str, pd.Timestamp] = None,
end_timestamp: Union[str, pd.Timestamp] = None,
columns: List = None,
filters: List = None,
order: object = None,
limit: int = None,
level: Union[str, IntervalLevel] = IntervalLevel.LEVEL_1DAY,
category_field: str = "entity_id",
time_field: str = "timestamp",
computing_window: int = None,
keep_all_timestamp: bool = False,
fill_method: str = "ffill",
effective_number: int = None,
transformer: Transformer = None,
accumulator: Accumulator = None,
need_persist: bool = False,
only_compute_factor: bool = False,
factor_name: str = None,
clear_state: bool = False,
only_load_factor: bool = False,
adjust_type: Union[AdjustType, str] = None,
) -> None:
super().__init__(
entity_schema,
provider,
entity_provider,
[entity_id],
exchanges,
codes,
start_timestamp,
end_timestamp,
columns,
filters,
order,
limit,
level,
category_field,
time_field,
computing_window,
keep_all_timestamp,
fill_method,
effective_number,
transformer,
accumulator,
need_persist,
only_compute_factor,
factor_name,
clear_state,
only_load_factor,
adjust_type,
)
self.player_df = get_players(
entity_id=entity_id,
start_timestamp=start_timestamp,
end_timestamp=end_timestamp,
provider="em",
direction="in",
)

def drawer_annotation_df(self) -> Optional[pd.DataFrame]:
def order_type_flag(df):
return "<br>".join(df.tolist())

if pd_is_not_null(self.player_df):
annotation_df = self.player_df.copy()
annotation_df["value"] = self.factor_df.loc[annotation_df.index]["close"]
annotation_df["flag"] = annotation_df[["dep1", "dep2", "dep3", "dep4", "dep5"]].apply(
lambda x: order_type_flag(x), axis=1
)
annotation_df["color"] = "#ff7f0e"
return annotation_df


if __name__ == "__main__":
top_dragon_and_tiger()
# Stock1dHfqKdata.record_data(entity_id="stock_sz_002561", provider="em")
# f = DragonTigerFactor(entity_id="stock_sz_002561", provider="em")
# f.draw(show=True)
3 changes: 2 additions & 1 deletion examples/research/top_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from zvt.domain import Stock1dHfqKdata
from zvt.utils import next_date, month_end_date, is_same_date


# 每月涨幅前30,市值90%分布在100亿以下
# 重复上榜的有1/4左右
# 连续两个月上榜的1/10左右
def top_tags(data_provider="em", start_timestamp="2010-01-01"):
records = []
for timestamp, df in get_top_performance_by_month(
for _, timestamp, df in get_top_performance_by_month(
start_timestamp=start_timestamp, list_days=250, data_provider=data_provider
):
for entity_id in df.index[:30]:
Expand Down
Loading

0 comments on commit c31b898

Please sign in to comment.