Exemplo n.º 1
0
    def get_order_books(self, instrument: ExchangeInstrument) -> Signal:
        symbol = instrument.get_exchange_instrument_code()
        exchange_code_upper = instrument.get_exchange().get_exchange_code().upper()

        order_books = self.book_signal_by_symbol.get(symbol, None)
        if order_books is None:
            self._maybe_notify_subscription(instrument)

            order_books = MutableSignal()
            self.scheduler.network.attach(order_books)
            self.book_signal_by_symbol[symbol] = order_books

            class OrderBookGenerator(SignalGenerator):
                def __init__(self, mds):
                    self.mds = mds

                def generate(self, scheduler: NetworkScheduler):
                    tickstore = AzureBlobTickstore(self.mds.credential, f'{exchange_code_upper}_BOOKS',
                                                   timestamp_column='time')
                    books_df = tickstore.select(symbol, self.mds.start_time, self.mds.end_time)
                    for row in books_df.itertuples():
                        at_time = row[0].asm8.astype('int64') / 10**6
                        best_bid = BookLevel(row[2], row[1])
                        best_ask = BookLevel(row[4], row[3])
                        book = OrderBook(bids=[best_bid], asks=[best_ask])
                        scheduler.schedule_update_at(order_books, book, at_time)
                    tickstore.close()

            self.scheduler.add_generator(OrderBookGenerator(self))

            # workaround: force scheduling of all historic trade events
            self.get_trades(instrument)

        return order_books
Exemplo n.º 2
0
 def __get_feed_uri(self, instrument: ExchangeInstrument) -> str:
     if instrument not in self.notified_instruments:
         self.notified_instruments.add(instrument)
         self.scheduler.schedule_update(self.subscribed_instruments,
                                        instrument)
     symbol = instrument.get_exchange_instrument_code()
     return f'{instrument.get_exchange().get_exchange_code().lower()}:{self.instance_id}:{symbol}'
Exemplo n.º 3
0
 def capture_daily_position(self, account: str,
                            instrument: ExchangeInstrument, qty: float):
     self.capture(
         'DailyPositions', {
             'date': pd.to_datetime(self.scheduler.get_time(),
                                    unit='ms').date(),
             'account': account,
             'instrument': instrument.get_exchange_instrument_code(),
             'qty': qty
         })
Exemplo n.º 4
0
    def get_trades(self, instrument: ExchangeInstrument) -> Signal:
        trade_symbol = instrument.get_exchange_instrument_code()
        exchange_code_upper = instrument.get_exchange().get_exchange_code(
        ).upper()

        self._maybe_notify_subscription(instrument)

        trades = self.trade_signal_by_symbol.get(trade_symbol, None)
        if trades is None:
            trades = MutableSignal()
            self.scheduler.network.attach(trades)
            self.trade_signal_by_symbol[trade_symbol] = trades

            class TradeGenerator(SignalGenerator):
                def __init__(self, mds):
                    self.mds = mds

                def generate(self, scheduler: NetworkScheduler):
                    tickstore = AzureBlobTickstore(
                        self.mds.azure_connect_str,
                        f'{exchange_code_upper}_TRADES',
                        timestamp_column='time')
                    trades_df = tickstore.select(trade_symbol,
                                                 self.mds.start_time,
                                                 self.mds.end_time)
                    for row in trades_df.itertuples():
                        at_time = row[0].asm8.astype('int64') / 10**6
                        sequence = row[1]
                        trade_id = row[2]
                        side = Side.SELL if row[4] == 'sell' else Side.BUY
                        qty = row[5]
                        price = row[6]
                        trade = Trade(instrument, sequence, trade_id, side,
                                      qty, price)
                        scheduler.schedule_update_at(trades, trade, at_time)

                    tickstore.close()

            self.scheduler.add_generator(TradeGenerator(self))

            # workaround: force scheduling of all historic order book events
            self.get_order_books(instrument)

        return trades
 def _create_feed(self, instrument: ExchangeInstrument):
     symbol = instrument.get_exchange_instrument_code()
     return Feed(instrument, self.instrument_trades[symbol], self.instrument_order_book_events[symbol],
                 self.instrument_order_books[symbol])
 def __get_feed_uri(self, instrument: ExchangeInstrument) -> str:
     symbol = instrument.get_exchange_instrument_code()
     return f'{instrument.get_exchange().get_type_code().lower()}:{self.instance_id}:{symbol}'