Пример #1
0
    def __init__(self, main_engine: MainEngine, event_engine: EventEngine):
        """"""
        super(CtaEngine, self).__init__(main_engine, event_engine, APP_NAME)

        self.strategy_setting = {}  # strategy_name: dict
        self.strategy_data = {}  # strategy_name: dict

        self.classes = {}  # class_name: stategy_class
        self.strategies = {}  # strategy_name: strategy

        self.symbol_strategy_map = defaultdict(
            list)  # vt_symbol: strategy list
        self.orderid_strategy_map = {}  # vt_orderid: strategy
        self.strategy_orderid_map = defaultdict(
            set)  # strategy_name: orderid list

        self.stop_order_count = 0  # for generating stop_orderid
        self.stop_orders = {}  # stop_orderid: stop_order

        self.init_executor = ThreadPoolExecutor(max_workers=1)

        self.rq_client = None
        self.rq_symbols = set()

        self.vt_tradeids = set()  # for filtering duplicate trade

        self.offset_converter = OffsetConverter(self.main_engine)

        self.database: BaseDatabase = get_database()
        self.datafeed: BaseDatafeed = get_datafeed()
Пример #2
0
    def importDataToVnpyDB(df: pd.DataFrame, stkid: str) -> None:
        bar_data = []
        TW_TZ = pytz.timezone("Asia/Taipei")

        if df is None:
            return print("資料不存在")

        for index, row in df.iterrows():
            bar = BarData(symbol=stkid,
                          exchange=Exchange.LOCAL,
                          datetime=TW_TZ.localize(row['ts'].to_pydatetime()) -
                          timedelta(minutes=1),
                          interval=Interval.MINUTE,
                          volume=row.Volume,
                          open_price=row.Open,
                          high_price=row.High,
                          low_price=row.Low,
                          close_price=row.Close,
                          gateway_name="Sinopac")

            bar_data.append(bar)

        database = get_database()
        database.save_bar_data(bar_data)
        print(
            f"股票代號:{stkid}|{bar_data[0].datetime}-{bar_data[-1].datetime} 歷史數據匯入成功,總共{len(bar_data)}筆資料"
        )
Пример #3
0
def load_tick_data(
    symbol: str,
    exchange: Exchange,
    start: datetime,
    end: datetime
):
    """"""
    database = get_database()

    return database.load_tick_data(
        symbol, exchange, start, end
    )
Пример #4
0
def load_bar_data(
    symbol: str,
    exchange: Exchange,
    interval: Interval,
    start: datetime,
    end: datetime
):
    """"""
    database = get_database()

    return database.load_bar_data(
        symbol, exchange, interval, start, end
    )
Пример #5
0
def load_bar_data(
    vt_symbol: str,
    interval: Interval,
    start: datetime,
    end: datetime
):
    """"""
    symbol, exchange = extract_vt_symbol(vt_symbol)

    database = get_database()

    return database.load_bar_data(
        symbol, exchange, interval, start, end
    )
Пример #6
0
def download_ib_data(symbol, exchange, start, end):

    req = HistoryRequest(symbol=symbol,
                         exchange=Exchange(exchange),
                         interval=Interval.MINUTE,
                         start=datetime.strptime(start, '%Y-%m-%d'),
                         end=datetime.strptime(end, '%Y-%m-%d'))

    bar_data = engine.main_engine.query_history(req, 'IB')

    if not bar_data:
        print(f'下載總數據量為:{len(bar_data)}')
        return

    database = get_database()
    database.save_bar_data(bar_data)

    print(f'{start}-{end}下載成功,總共{len(bar_data)}筆資料')
Пример #7
0
TW_TZ = pytz.timezone("Asia/Taipei")

if __name__ == '__main__':

    symbol = '2883'

    df = pd.read_csv(f'{symbol}.csv', parse_dates=['ts'])

    bar_data = []

    if df is not None:
        for index, row in df.iterrows():
            bar = BarData(symbol=symbol,
                          exchange=Exchange.LOCAL,
                          datetime=TW_TZ.localize(row['ts'].to_pydatetime()) -
                          datetime.timedelta(minutes=1),
                          interval=Interval.MINUTE,
                          volume=row['Volume'],
                          open_price=row['Open'],
                          high_price=row['High'],
                          low_price=row['Low'],
                          close_price=row['Close'],
                          gateway_name='Sinopac')

            bar_data.append(bar)

        database = get_database()
        database.save_bar_data(bar_data)
        print(
            f'股票代號:{symbol}|{bar_data[0].datetime}-{bar_data[-1].datetime} 歷史數據匯入成功,總共{len(bar_data)}筆資料'
        )