Skip to content

Commit

Permalink
1)add dragon and tiger data 2)add currency entity and kdata 3)improve…
Browse files Browse the repository at this point in the history
… compare intent
  • Loading branch information
foolcage committed Apr 25, 2022
1 parent 78df4b2 commit 7e069c5
Show file tree
Hide file tree
Showing 25 changed files with 660 additions and 105 deletions.
36 changes: 36 additions & 0 deletions examples/data_runner/trading_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
import logging

from apscheduler.schedulers.background import BackgroundScheduler

from examples.recorder_utils import run_data_recorder
from zvt import init_log
from zvt.domain import (
DragonAndTiger,
)

logger = logging.getLogger(__name__)

sched = BackgroundScheduler()


@sched.scheduled_job("cron", hour=18, minute=00, day_of_week="mon-fri")
def record_dragon_tiger(data_provider="em", entity_provider="em", sleeping_time=2):
# 龙虎榜数据
run_data_recorder(
domain=DragonAndTiger,
data_provider=data_provider,
entity_provider=entity_provider,
day_data=True,
sleeping_time=sleeping_time,
)


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

record_dragon_tiger()

sched.start()

sched._thread.join()
60 changes: 55 additions & 5 deletions examples/intent/intent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
# -*- coding: utf-8 -*-
def compare_kline():
from zvt.api.intent import compare
from zvt.domain import Index, Indexus, Index1dKdata, Indexus1dKdata
from zvt.api.intent import compare
from zvt.domain import Indexus1dKdata, Index, Indexus, Index1dKdata
from zvt.domain import TreasuryYield


def china_vs_us_stock():
# 上证,道琼斯指数
Index.record_data()
Indexus.record_data()
Index1dKdata.record_data(entity_id="index_sh_000001")
Indexus1dKdata.record_data(entity_id="indexus_us_SPX")
compare(entity_ids=["index_sh_000001", "indexus_us_SPX"], start_timestamp="2000-01-01", scale_value=100)


def us_yield_and_stock():
# 美债收益率,道琼斯指数
entity_ids = ["country_galaxy_US", "indexus_us_SPX"]
compare(
entity_ids=entity_ids,
start_timestamp="1990-01-01",
scale_value=None,
schema_map_columns={TreasuryYield: ["yield_2", "yield_5"], Indexus1dKdata: ["close"]},
)


def commodity_and_stock():
# 江西铜业,沪铜
entity_ids = ["stock_sh_600362", "future_shfe_CU"]
compare(
entity_ids=entity_ids,
start_timestamp="2005-01-01",
scale_value=100,
)


def compare_metal():
# 沪铜,沪铝,螺纹钢
entity_ids = ["future_shfe_CU", "future_shfe_AL", "future_shfe_RB"]
compare(
entity_ids=entity_ids,
start_timestamp="2009-04-01",
scale_value=100,
)


compare(entity_ids=["index_sh_000001", "indexus_us_SPX"])
def compare_udi_and_stock():
# 美股指数
# Indexus.record_data()
entity_ids = ["indexus_us_NDX", "indexus_us_SPX", "indexus_us_UDI"]
# Indexus1dKdata.record_data(entity_ids=entity_ids, sleeping_time=0)
compare(
entity_ids=entity_ids,
start_timestamp="2015-01-01",
scale_value=100,
schema_map_columns={Indexus1dKdata: ["close"]},
)


if __name__ == "__main__":
compare_kline()
# compare_kline()
# us_yield_and_stock()
# commodity_and_stock()
# compare_metal()
compare_udi_and_stock()
17 changes: 11 additions & 6 deletions src/zvt/api/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def compare(
columns=None,
schema_map_columns: dict = None,
chart_type: ChartType = ChartType.line,
start_timestamp=None,
scale_value: int = None,
):
"""
compare indicators(columns) of entities
Expand All @@ -26,34 +28,37 @@ def compare(
:param columns:
:param schema_map_columns: key represents schema, value represents columns
:param chart_type: "line", "area", "scatter", default "line"
:param start_timestamp: "
:param scale_value: compare with same value which scaled to scale_value
"""

# compare
dfs = []
# default compare kdata
if schema_map_columns is None and schema is None:
entity_type_map_ids = _group_entity_ids(entity_ids=entity_ids)
for entity_type in entity_type_map_ids:
schema = get_kdata_schema(entity_type=entity_type)
df = schema.query_data(entity_ids=entity_type_map_ids.get(entity_type))
df = schema.query_data(entity_ids=entity_type_map_ids.get(entity_type), start_timestamp=start_timestamp)
dfs.append(df)
all_df = pd.concat(dfs)
drawer = Drawer(main_df=all_df, sub_df_list=[all_df[["entity_id", "timestamp", "turnover"]].copy()])
drawer.draw_kline(show=True)
drawer.draw_kline(show=True, scale_value=scale_value)
else:
if schema_map_columns:
for schema in schema_map_columns:
columns = ["entity_id", "timestamp"] + schema_map_columns.get(schema)
df = schema.query_data(entity_ids=entity_ids, codes=codes, columns=columns)
df = schema.query_data(
entity_ids=entity_ids, codes=codes, columns=columns, start_timestamp=start_timestamp
)
dfs.append(df)
elif schema:
columns = ["entity_id", "timestamp"] + columns
df = schema.query_data(entity_ids=entity_ids, codes=codes, columns=columns)
df = schema.query_data(entity_ids=entity_ids, codes=codes, columns=columns, start_timestamp=start_timestamp)
dfs.append(df)

all_df = pd.concat(dfs)
drawer = Drawer(main_df=all_df)
drawer.draw(main_chart=chart_type, show=True)
drawer.draw(main_chart=chart_type, show=True, scale_value=scale_value)


def compare_df(df: pd.DataFrame, chart_type: ChartType = ChartType.line):
Expand Down
8 changes: 8 additions & 0 deletions src/zvt/contract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ class TradableType(Enum):
#: 基金(中国)
#: China fund
fund = "fund"
#: 货币汇率
#: currency exchange rate
currency = "currency"


class Exchange(Enum):
Expand Down Expand Up @@ -219,6 +222,10 @@ class Exchange(Enum):
#: 上海国际能源交易中心
ine = "ine"

#: 外汇交易所(虚拟)
#: currency exchange(virtual)
forex = "forex"


tradable_type_map_exchanges = {
TradableType.block: [Exchange.cn],
Expand All @@ -228,6 +235,7 @@ class Exchange(Enum):
TradableType.indexus: [Exchange.us],
TradableType.future: [Exchange.shfe, Exchange.dce, Exchange.czce, Exchange.cffex, Exchange.ine],
TradableType.coin: [Exchange.binance, Exchange.huobipro],
TradableType.currency: [Exchange.forex],
}


Expand Down
Loading

0 comments on commit 7e069c5

Please sign in to comment.