Exemplo n.º 1
0
 def __init__(self,
              is_backtest: bool,
              ts_type_name_for_current_price: str,
              engine: Engine = None,
              subscribe_codes: List[str] = None,
              mocked_current_prices: Dict[Timestamp, Dict[str,
                                                          Price]] = {}):
     self.ts_type_name_for_current_price = ts_type_name_for_current_price
     self.is_backtest = is_backtest
     self._current_price_map: Mapping[str, Price] = {}
     self.mocked_current_price = mocked_current_prices
     self.subscribe_codes = subscribe_codes
     if not is_backtest:
         if not subscribe_codes:
             raise RuntimeError("need subscribe codes")
         ts_repo: TimeSeriesRepo = BeanContainer.getBean(TimeSeriesRepo)
         self.realtime_ts: TimeSeries = ts_repo.find_one(
             ts_type_name_for_current_price)
         self.realtime_ts.subscribe(self, subscribe_codes)
         self.start_check_realtime_data_thread()
     else:
         if not engine:
             raise RuntimeError("need engine")
         engine.register_event(
             EventDefinition(
                 ed_type=EventDefinitionType.DATA,
                 ts_type_name=ts_type_name_for_current_price,
                 event_data_type=EventDataType.BAR,
                 bar_config=BarEventConfig(market_open_as_tick=True),
                 order=-100), self.set_current_price)
Exemplo n.º 2
0
    def is_unique_account(self, account_name):

        account_repo: AccountRepo = BeanContainer.getBean(AccountRepo)
        if not account_repo.find_one(account_name):
            return True
        else:
            return False
Exemplo n.º 3
0
    def start(self, scope: Scope):
        time_event_definitions = []
        for ed in self.event_definitions:
            if ed.ed_type == EventDefinitionType.TIME:
                # 启动线程来产生时间事件
                time_event_definitions.append(ed)

            else:
                ts = BeanContainer.getBean(TimeSeriesRepo).find_one(
                    ed.ts_type_name)
                ts.subscribe(self, scope.codes)

        TimeEventThread(self.subscriber, time_event_definitions,
                        scope.trading_calendar).start()
Exemplo n.º 4
0
import logging.config
import os
from configparser import ConfigParser

import yaml
from cassandra.cqlengine import connection

from se.domain2.account.account import AccountRepo
from se.domain2.domain import BeanContainer
from se.domain2.time_series.time_series import TSFunctionRegistry, TimeSeriesRepo
from se.infras.ib import IBMinBar, IBTick, IBAdjustedDailyBar, IBMarketData
from se.infras.repos import TimeSeriesRepoImpl, AccountRepoImpl

BeanContainer.register(TimeSeriesRepo, TimeSeriesRepoImpl())
BeanContainer.register(AccountRepo, AccountRepoImpl())

if not os.getenv("config.dir"):
    raise RuntimeError("没有配置config.dir")

# 初始化日志配置
log_config = "{}/log.yaml".format(os.getenv("config.dir"))
if os.getenv("config.log"):
    log_config = os.getenv("config.log")

if os.path.exists(log_config):
    logging.config.dictConfig(yaml.load(open(log_config), Loader=yaml.SafeLoader))
    logging.info("初始化日志配置成功")
else:
    logging.basicConfig(level=logging.INFO)
    logging.info("没有log的配置文件,将使用默认配置")
Exemplo n.º 5
0
# wrapper = EWrapper()
# cli = EClient(wrapper)
# cli.connect("192.168.0.221", 4002, 25)
# if cli.connState == EClient.CONNECTED:
#     threading.Thread(name="ib_msg_consumer", target=cli.run).start()
#
# cont = Contract()
# cont.symbol = 'SPCE'
# cont.currency = "USD"
# cont.exchange = "SMART"
# cont.secType = "STK"
# cli.reqContractDetails(1000, cont)
# cli.reqAccountUpdates(True, "DU268989")

import se.infras

ts_repo: TimeSeriesRepo = BeanContainer.getBean(TimeSeriesRepo)

ts: TimeSeries = ts_repo.find_one("ibTick")
command = HistoryDataQueryCommand(start=Timestamp("2020-01-20",
                                                  tz='Asia/Shanghai'),
                                  end=Timestamp.now(tz='Asia/Shanghai'),
                                  codes=['SPCE_STK_USD_SMART'],
                                  window=10)
datas = ts.history_data(command)

print("done")

print("done")
Exemplo n.º 6
0
 def history_data(self, ts_type_name, command: HistoryDataQueryCommand):
     ts = BeanContainer.getBean(TimeSeriesRepo).find_one(ts_type_name)
     return ts.history_data(command)
Exemplo n.º 7
0
    def history_events(self, scope: Scope, start: Timestamp,
                       end: Timestamp) -> List[Event]:
        total_events = []

        # 组装时间事件
        if len(self.time_event_definitions) > 0:

            delta = Timedelta(minutes=1)
            p = start
            while p <= end:
                for ed in self.time_event_definitions:
                    if ed.time_rule.second_offset != 0:
                        raise RuntimeError("回测过程中的时间事件不允许秒级偏移")
                    if ed.time_rule.is_match(scope.trading_calendar, p):
                        total_events.append(Event(ed, p, {}))

                p += delta

        # 组装数据事件
        if len(self.data_event_definitions) > 0:
            market_opens = DatetimeIndex(scope.trading_calendar.opens.values, tz="UTC") - \
                           Timedelta(minutes=1)
            market_opens = market_opens[(market_opens >= start)
                                        & (market_opens <= end)]
            market_closes = DatetimeIndex(scope.trading_calendar.closes.values,
                                          tz="UTC")
            market_closes = market_closes[(market_closes >= start)
                                          & (market_closes <= end)]

            for ed in self.data_event_definitions:
                ts = BeanContainer.getBean(TimeSeriesRepo).find_one(
                    ed.ts_type_name)
                command = HistoryDataQueryCommand(start, end, scope.codes)
                command.with_calendar(scope.trading_calendar)
                df = ts.history_data(command, from_local=True)
                for (visible_time, code), values in df.iterrows():
                    data: Dict = values.to_dict()
                    if ed.event_data_type == EventDataType.BAR:
                        # 添加bar事件
                        if "start_time" in data:
                            start_time = data['start_time']
                        else:
                            start_time = data['date']

                        bar = Bar(ed.ts_type_name, visible_time, code,
                                  start_time, data['open'], data['high'],
                                  data['low'], data['close'], data['volume'])
                        total_events.append(Event(ed, visible_time, bar))
                        if ed.bar_config.market_open_as_tick and not ed.bar_config.bar_open_as_tick:
                            if bar.start_time in market_opens:
                                total_events.append(
                                    Event(
                                        ed, bar.start_time + ed.bar_config.
                                        market_open_as_tick_delta,
                                        Tick(ed.ts_type_name, visible_time,
                                             code, bar.open_price, -1)))

                        if ed.bar_config.bar_open_as_tick:
                            tick_visible_time = bar.start_time + ed.bar_config.bar_open_as_tick_delta
                            total_events.append(
                                Event(
                                    ed, tick_visible_time,
                                    Tick(ed.ts_type_name, tick_visible_time,
                                         code, bar.open_price, -1)))

                        if ed.bar_config.market_close_as_tick:
                            if bar.visible_time in market_closes:
                                total_events.append(
                                    Event(
                                        ed, visible_time + ed.bar_config.
                                        market_close_as_tick_delta,
                                        Tick(ed.ts_type_name, visible_time,
                                             code, bar.close_price, -1)))
                    elif ed.event_data_type == EventDataType.TICK:
                        tick = Tick(ed.ts_type_name, visible_time, code,
                                    data['price'], data['size'])
                        total_events.append(Event(ed, tick.visible_time, tick))
                    else:
                        total_events.append(Event(ed, visible_time, data))

        return total_events
Exemplo n.º 8
0
 def save(self):
     time_series_repo: TimeSeriesRepo = BeanContainer.getBean(
         TimeSeriesRepo)
     time_series_repo.save(self)
Exemplo n.º 9
0
 def query(cls, ts_type_name: str,
           command: HistoryDataQueryCommand) -> List[TSData]:
     time_series_repo: TimeSeriesRepo = BeanContainer.getBean(
         TimeSeriesRepo)
     return time_series_repo.query_ts_data(ts_type_name, command)
Exemplo n.º 10
0
 def save(cls, data_list: List[TSData]):
     time_series_repo = BeanContainer.getBean(TimeSeriesRepo)
     time_series_repo.save_ts(data_list)
Exemplo n.º 11
0
 def save(self):
     account_repo: AccountRepo = BeanContainer.getBean(AccountRepo)
     account_repo.save(self)