-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1)add factor examples basing on dragon tiger data 2)add research exam…
…ple 3)add selecting stocks example with multiple condition
- Loading branch information
Showing
14 changed files
with
277 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# -*- coding: utf-8 -*- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
from zvt.api import get_top_performance_by_month | ||
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( | ||
start_timestamp=start_timestamp, list_days=250, data_provider=data_provider | ||
): | ||
for entity_id in df.index[:30]: | ||
query_timestamp = timestamp | ||
while True: | ||
kdata = Stock1dHfqKdata.query_data( | ||
provider=data_provider, | ||
entity_id=entity_id, | ||
start_timestamp=query_timestamp, | ||
order=Stock1dHfqKdata.timestamp.asc(), | ||
limit=1, | ||
return_type="domain", | ||
) | ||
if not kdata or kdata[0].turnover_rate == 0: | ||
if is_same_date(query_timestamp, month_end_date(query_timestamp)): | ||
break | ||
query_timestamp = next_date(query_timestamp) | ||
continue | ||
cap = kdata[0].turnover / kdata[0].turnover_rate | ||
break | ||
|
||
records.append( | ||
{"entity_id": entity_id, "timestamp": timestamp, "cap": cap, "score": df.loc[entity_id, "score"]} | ||
) | ||
|
||
return records | ||
|
||
|
||
if __name__ == "__main__": | ||
print(top_tags()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# -*- coding: utf-8 -*- | ||
from typing import List, Union | ||
|
||
import pandas as pd | ||
|
||
from zvt.contract import IntervalLevel | ||
from zvt.contract.factor import Factor, Transformer, Accumulator | ||
from zvt.domain import Stock, DragonAndTiger | ||
from zvt.factors import TargetSelector | ||
from zvt.trader import StockTrader | ||
|
||
|
||
class DragonTigerFactor(Factor): | ||
def __init__( | ||
self, | ||
provider: str = "em", | ||
entity_provider: str = "em", | ||
entity_ids: List[str] = None, | ||
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 = [DragonAndTiger.dep1 == "机构专用"], | ||
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, | ||
) -> None: | ||
super().__init__( | ||
DragonAndTiger, | ||
Stock, | ||
provider, | ||
entity_provider, | ||
entity_ids, | ||
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, | ||
) | ||
|
||
def compute_result(self): | ||
self.factor_df["filter_result"] = True | ||
super().compute_result() | ||
|
||
|
||
class MyTrader(StockTrader): | ||
def init_selectors( | ||
self, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp, adjust_type=None | ||
): | ||
myselector = TargetSelector( | ||
entity_ids=entity_ids, | ||
entity_schema=entity_schema, | ||
exchanges=exchanges, | ||
codes=codes, | ||
start_timestamp=start_timestamp, | ||
end_timestamp=end_timestamp, | ||
provider="em", | ||
) | ||
|
||
myselector.add_factor( | ||
DragonTigerFactor( | ||
entity_ids=entity_ids, | ||
exchanges=exchanges, | ||
codes=codes, | ||
start_timestamp=start_timestamp, | ||
end_timestamp=end_timestamp, | ||
) | ||
) | ||
|
||
self.selectors.append(myselector) | ||
|
||
|
||
if __name__ == "__main__": | ||
trader = MyTrader(start_timestamp="2020-01-01", end_timestamp="2022-05-01") | ||
trader.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.