Ejemplo n.º 1
0
    def get_ticker(self, symbol):
        '''Return a ticker object. Generated from instrument.'''

        instrument = self.get_instrument(symbol)

        # If this is an index, we have to get the data from the last trade.
        if instrument['symbol'][0] == '.':
            ticker = {}
            ticker['mid'] = ticker['buy'] = ticker['sell'] = ticker[
                'last'] = instrument['markPrice']
        # Normal instrument
        else:
            bid = instrument['bidPrice'] or instrument['lastPrice']
            ask = instrument['askPrice'] or instrument['lastPrice']
            ticker = {
                "last": instrument['lastPrice'],
                "buy": bid,
                "sell": ask,
                "mid": (bid + ask) / 2
            }

        # The instrument has a tickSize. Use it to round values.
        return {
            k: to_nearest(float(v or 0), instrument['tickSize'])
            for k, v in iteritems(ticker)
        }
Ejemplo n.º 2
0
    def test_update_max_price(self):
        self.trailing_order.initial_price = 1000
        self.trailing_order.max_price = 1001

        # assert order was moved
        expected_price = to_nearest(1001 * 0.9, 0.5)
        self.assertEqual(expected_price, self.order.stop_px)
Ejemplo n.º 3
0
    def enter_fb_method(self, qty: int, price_type: str, timeout: int, max_retry: int, deviation: int = None) -> None:
        """
        Fb method is placing n orders after timeout seconds, then entry market.

        :param qty: entry position size
        :param timeout: timeout before replacing the order
        :param price_type: may be last, first_ob, third_ob and deviant
        :param deviation: deviation from last price in percents
        :param max_retry: max order placing count
        """

        if price_type == 'first_ob':
            init_price = self.exchange.get_first_orderbook_price_ws(bid=qty > 0)
        elif price_type == 'third_ob':
            init_price = self.exchange.get_third_orderbook_price_ws(bid=qty > 0)
        elif price_type == 'deviant':
            if qty > 0:
                init_price = self.exchange.get_last_price_ws() * (100 + deviation) / 100
            else:
                init_price = self.exchange.get_last_price_ws() * (100 - deviation) / 100
        else:
            init_price = self.exchange.get_last_price_ws()

        init_price = to_nearest(init_price, self.exchange.conn.get_tick_size())

        entry_order = Order(order_type='Limit', qty=qty, side='Buy' if qty > 0 else 'Sell',
                            price=init_price, passive=False)
        for _ in range(max_retry):
            self.exchange.place_order(entry_order)
            for _ in range(timeout):
                if self.exchange.get_order_status_ws(entry_order) == 'Filled':
                    return
                sleep(1)
            self.exchange.cancel_order(entry_order)
        self.enter_by_market_order(qty=qty)
Ejemplo n.º 4
0
    def calculate_new_price(self, extremum) -> float:
        if self.order.side == 'Sell':
            needed_price = extremum * (1 - self.offset / 100)
        else:
            needed_price = extremum * (1 + self.offset / 100)
        needed_price = to_nearest(needed_price, tickSize=self.tick_size)

        return needed_price
Ejemplo n.º 5
0
    def test_update_min_price(self):
        self.order.side = 'Buy'

        self.trailing_order.initial_price = 1000
        self.trailing_order.min_price = 999

        # assert order was moved
        expected_price = to_nearest(999 * 1.1, 0.5)
        self.assertEqual(expected_price, self.order.stop_px)