Ejemplo n.º 1
0
 def analyze_ticker(self, pair, data):
     '''analyze tick data'''
     if pair == RTAPI.TradePair.BTC_JPY.value:
         self.dsc_sfd.update_date_spot(n2d(data.ltp))
     elif pair == RTAPI.TradePair.FX_BTC_JPY.value:
         self.dsc_tick_fx.update_date(data)
         self.dsc_sfd.update_date_fx(n2d(data.ltp))
Ejemplo n.º 2
0
        def __init__(self, mid_price, depth_asks, depth_bids,
                     amount_filter_ask, amount_filter_bid):
            self.amount_filter_ask = amount_filter_ask
            self.amount_filter_bid = amount_filter_bid

            # for ask
            self.ask_idx = self.__get_filter_top_idx(depth_asks,
                                                     n2d(amount_filter_ask))
            self.ask_price = depth_asks[self.ask_idx][0]
            self.ask_amount = self.__get_amount_in_range(
                depth_asks, depth_asks[0][0], self.ask_price)
            self.ask_spread = self.ask_price - mid_price

            # for bid
            self.bid_idx = self.__get_filter_top_idx(depth_bids,
                                                     n2d(amount_filter_bid))
            self.bid_price = depth_bids[self.bid_idx][0]
            self.bid_amount = self.__get_amount_in_range(
                depth_bids, depth_bids[0][0], self.bid_price)
            self.bid_spread = mid_price - self.bid_price

            # spread
            self.spread = self.ask_price - self.bid_price
            self.percentage = (
                (self.ask_price / self.bid_price) - n2d(1.0)) * n2d(100.0)
            self.amount = self.ask_amount + self.bid_amount
Ejemplo n.º 3
0
    def __init__(self):

        self.SFD_DECISION_TABLE = (
            (n2d(0.05), n2d(0.0000)
             ),  # Level0: At least 0% but less than 5%	0.00% of the settlement
            (
                n2d(0.10), n2d(0.0025)
            ),  # Level1: At least 5% but less than 10%	0.25% of the settlement
            (
                n2d(0.15), n2d(0.0050)
            ),  # Level2: At least 10% but less than 15% 0.50% of the settlement
            (
                n2d(0.20), n2d(0.0100)
            ),  # Level3: At least 15% but less than 20% 1.00% of the settlement
            (None,
             n2d(0.0200)),  # Level4: At least 20% 2.00% of the settlement
        )
        self.price_disparity_rate = None
        self.sfd_rate = None
        self.sfd_level = None

        self.occur_price_buy = None
        self.occur_price_sell = None

        self.spot_lpt = None
        self.fx_lpt = None
Ejemplo n.º 4
0
    def __update_depth(self, raw_list, depth_list, diff, asc, mpf):
        # make new list by numpy
        new_list = depth_list
        for data in raw_list:
            # get each detail data
            new_price = n2d(data['price'])
            new_amount = n2d(data['size'])

            # delete old data (only diff mode)
            if diff:
                new_list = np.delete(new_list,
                                     np.where(depth_list[:, 0] == new_price),
                                     axis=0)
            # add new data
            if new_amount != 0:
                new_list = np.append(new_list,
                                     np.array([[new_price, new_amount]]),
                                     axis=0)

        # sort depth by price and adjust array length and mid price filter
        if asc:  # for ask
            new_list = new_list[np.argsort(new_list[:, 0])]

            if new_list.shape[0] > self.PRM_MAX_LEN:
                new_list = np.delete(
                    new_list,
                    np.where(
                        new_list[:, 0] >= new_list[self.PRM_MAX_LEN - 1][0]),
                    axis=0)

            if mpf:
                new_list = np.delete(
                    new_list,
                    np.where(new_list[:, 0] <= self.mid_price),
                    axis=0)

        else:  # for bid
            new_list = new_list[np.argsort(new_list[:, 0])[::-1]]

            if new_list.shape[0] > self.PRM_MAX_LEN:
                new_list = np.delete(
                    new_list,
                    np.where(
                        new_list[:, 0] <= new_list[self.PRM_MAX_LEN - 1][0]),
                    axis=0)

            if mpf:
                new_list = np.delete(
                    new_list,
                    np.where(new_list[:, 0] >= self.mid_price),
                    axis=0)

        # return new list
        return new_list
Ejemplo n.º 5
0
        def __init__(self, depth_asks, depth_bids, amount_filter_ask,
                     amount_filter_bid):
            self.amount_filter_ask = amount_filter_ask
            self.amount_filter_bid = amount_filter_bid

            self.idx_ask = self.__get_filter_top_idx(depth_asks,
                                                     n2d(amount_filter_ask))
            self.idx_bid = self.__get_filter_top_idx(depth_bids,
                                                     n2d(amount_filter_bid))
            self.price_ask = depth_asks[self.idx_ask][0]
            self.price_bid = depth_bids[self.idx_bid][0]
            self.spread = self.price_ask - self.price_bid
            self.spread_rate = (self.price_ask / self.price_bid) - n2d(1.0)
Ejemplo n.º 6
0
        def _query_data(target_list, prm_range):

            if len(target_list) > 0:
                sum_amount = n2d(0.0)
                query_list = [
                    td for td in target_list
                    if td[self.TRADE_ARRAY.TIME] >= prm_range
                ]
                for exec_data in query_list:
                    sum_amount += exec_data[self.TRADE_ARRAY.AMOUNT]
                return sum_amount

            return n2d(0.0)
Ejemplo n.º 7
0
 def __init__(self, depth_data):
     if len(depth_data[:, 1]) > 0:
         self.am_min = np.min(depth_data[:, 1], axis=0)
         self.am_max = np.max(depth_data[:, 1], axis=0)
         self.am_sum = np.sum(depth_data[:, 1], axis=0)
         self.am_mean = np.mean(depth_data[:, 1], axis=0)
         self.am_median = np.median(depth_data[:, 1], axis=0)
     else:
         self.am_min = n2d(0.0)
         self.am_max = n2d(0.0)
         self.am_sum = n2d(0.0)
         self.am_mean = n2d(0.0)
         self.am_median = n2d(0.0)
Ejemplo n.º 8
0
    def __update_occur_price(self):
        if self.spot_lpt is None:
            self.occur_price_buy = None
            self.occur_price_sell = None
            return

        self.occur_price_buy = [dfloor(self.spot_lpt, 0)]
        self.occur_price_sell = [dceiling(self.spot_lpt, 0)]
        for row in self.SFD_DECISION_TABLE:
            if row[0] is not None:
                self.occur_price_buy.append(
                    dfloor(self.spot_lpt * (n2d(1) - row[0]), 0))
                self.occur_price_sell.append(
                    dceiling(self.spot_lpt * (n2d(1) + row[0]), 0))
Ejemplo n.º 9
0
    def __update_sfd_date(self):
        if (self.spot_lpt is None or self.fx_lpt is None):
            self.price_disparity_rate = None
            self.sfd_rate = None
            return

        self.price_disparity_rate = (self.fx_lpt / self.spot_lpt) - n2d(1.0)
        self.sfd_rate = n2d(0.0)
        self.sfd_level = 0
        for row in self.SFD_DECISION_TABLE:
            if row[0] is None or self.price_disparity_rate < row[0]:
                self.sfd_rate = row[1]
                break
            self.sfd_level += 1
Ejemplo n.º 10
0
    def __add_data(self, exec_data):

        wk_utc_dt = datetime.strptime(exec_data.exec_date[0:22],
                                      self.BROKER_TIMESTAMP_FORMAT)
        val_dt = wk_utc_dt + timedelta(hours=9)
        val_price = n2d(exec_data.price)
        val_amount = n2d(exec_data.size)
        val_buy_id = exec_data.buy_child_order_acceptance_id
        val_sell_id = exec_data.sell_child_order_acceptance_id

        if exec_data.side == "BUY":
            self.buys.append(
                [val_dt, val_price, val_amount, val_buy_id, val_sell_id])
        elif exec_data.side == "SELL":
            self.sells.append(
                [val_dt, val_price, val_amount, val_buy_id, val_sell_id])

        self.event_values.append([val_dt, val_price, val_amount])
Ejemplo n.º 11
0
    def check_exec_buy(self, oid) -> (Decimal, Decimal):
        '''check the execution of buy order'''
        price_list = []
        amount_list = []
        total_price = n2d(0.0)
        total_amount = n2d(0.0)

        for datas in self.buys:  # taker
            if datas[self.TRADE_ARRAY.BUY_ID] == oid:
                price_list.append(datas[self.TRADE_ARRAY.PRICE])
                amount_list.append(datas[self.TRADE_ARRAY.AMOUNT])

        for datas in self.sells:  # maker
            if datas[self.TRADE_ARRAY.BUY_ID] == oid:
                price_list.append(datas[self.TRADE_ARRAY.PRICE])
                amount_list.append(datas[self.TRADE_ARRAY.AMOUNT])

        total_amount = sum(amount_list)
        for part_price, part_amount in zip(price_list, amount_list):
            total_price += (part_price * (part_amount / total_amount))

        return dfloor(total_price, 0), total_amount
Ejemplo n.º 12
0
    def update_date(self, raw_executions_list):
        '''update data'''
        # init value
        self.last_amount = n2d(0.0)

        # check start time
        if self.__range_start_dt is None:
            self.__range_start_dt = self.__adjtime.get_now()

        # add new data
        self.event_values = []
        for exec_data in raw_executions_list:
            self.__add_data(exec_data)
            self.last_amount += n2d(exec_data.size)

        # remove out of range data
        self.__remove_rangeout_data()

        # get the last tread info
        wk_utc = datetime.strptime(raw_executions_list[-1].exec_date[0:23],
                                   self.BROKER_TIMESTAMP_FORMAT)
        self.last_dt = wk_utc + timedelta(hours=9)
        self.last_price = n2d(raw_executions_list[-1].price)
Ejemplo n.º 13
0
    def update_date(self, data: RTAPI.TickerData):
        '''update data'''
        self.__available = False

        wk_utc_dt = datetime.strptime(data.timestamp[0:23], self.BROKER_TIMESTAMP_FORMAT)
        self.ts_dt = wk_utc_dt + timedelta(hours=9)
        self.bid_price = n2d(data.best_bid)
        self.ask_price = n2d(data.best_ask)
        self.bid_amount = n2d(data.best_bid_size)
        self.ask_amount = n2d(data.best_ask_size)
        self.total_bid_amount = n2d(data.total_bid_depth)
        self.total_ask_amount = n2d(data.total_ask_depth)
        self.trade_price = n2d(data.ltp)
        self.trade_volume_24h = n2d(data.volume_by_product)
        self.spread = self.ask_price - self.bid_price
        self.spread_rate = (self.ask_price / self.bid_price) - n2d(1.0)

        self.__update_tick_data_list(self.ts_dt, self.trade_price)

        self.__available = True
Ejemplo n.º 14
0
    def get_range_depth(self, price_range):
        '''get range depth data'''
        # check data available
        if not self.is_available():
            return None, None

        range_ask_from = self.mid_price
        range_ask_to = self.mid_price + n2d(price_range)
        query_asks = self.asks[np.where(((self.asks[:, 0] >= range_ask_from)
                                         & (self.asks[:, 0] < range_ask_to)))]

        range_bid_from = self.mid_price
        range_bid_to = self.mid_price - price_range
        query_bids = self.bids[np.where(((self.bids[:, 0] <= range_bid_from)
                                         & (self.bids[:, 0] > range_bid_to)))]

        return query_asks, query_bids
Ejemplo n.º 15
0
    def update_data(self, raw_mid_price, raw_ask_list, raw_bid_list, mpf=True):
        '''update data (for differential data)'''
        # initial check
        if self.asks is None or self.bids is None:
            return

        # set mid price
        self.mid_price = n2d(raw_mid_price)

        # set ask depth
        self.asks = self.__update_depth(raw_ask_list,
                                        self.asks,
                                        diff=True,
                                        asc=True,
                                        mpf=mpf)

        # set bid depth
        self.bids = self.__update_depth(raw_bid_list,
                                        self.bids,
                                        diff=True,
                                        asc=False,
                                        mpf=mpf)
Ejemplo n.º 16
0
    def init_data(self, raw_mid_price, raw_ask_list, raw_bid_list, mpf=True):
        '''initialize data (for snapshot data)'''
        # set mid price
        self.mid_price = n2d(raw_mid_price)

        # set ask depth
        if len(raw_ask_list) > 0:
            self.asks = self.__mk_empty_depth_data()
            self.asks = self.__update_depth(raw_ask_list,
                                            self.asks,
                                            diff=False,
                                            asc=True,
                                            mpf=mpf)

        # set bid depth
        if len(raw_bid_list) > 0:
            self.bids = self.__mk_empty_depth_data()
            self.bids = self.__update_depth(raw_bid_list,
                                            self.bids,
                                            diff=False,
                                            asc=False,
                                            mpf=mpf)
Ejemplo n.º 17
0
 def price_disparity_per(self):
     '''price disparity (unit percent) '''
     if self.price_disparity_rate is None:
         return None
     return self.price_disparity_rate * n2d(100.0)
Ejemplo n.º 18
0
 def sfd_per(self):
     '''sfd (unit percent) '''
     if self.sfd_rate is None:
         return None
     return self.sfd_rate * n2d(100.0)
Ejemplo n.º 19
0
        def __analize_data(self, tick_list: list):
            if tick_list is None or len(tick_list) <= 0:
                return

            tick_list.sort(key=itemgetter(DatasetTick.TRADE_PRICE_ARRAY.TIME))
            __price_list = [
                row[DatasetTick.TRADE_PRICE_ARRAY.PRICE] for row in tick_list
            ]

            self.p_open = n2d(__price_list[0])
            self.p_close = n2d(__price_list[-1])
            self.p_high = n2d(max(__price_list))
            self.p_low = n2d(min(__price_list))
            self.r_hight = n2d(self.p_high - self.p_low)

            if self.p_open < self.p_close:
                self.c_white = True
                self.c_black = False
                self.r_body = n2d(self.p_close - self.p_open)
                self.r_upper_shadow = n2d(self.p_high - self.p_close)
                self.r_lower_shadow = n2d(self.p_open - self.p_low)
                self.p_body_high = self.p_close
                self.p_body_low = self.p_open

            elif self.p_open > self.p_close:
                self.c_white = False
                self.c_black = True
                self.r_body = n2d(self.p_open - self.p_close)
                self.r_upper_shadow = n2d(self.p_high - self.p_open)
                self.r_lower_shadow = n2d(self.p_close - self.p_low)
                self.p_body_high = self.p_open
                self.p_body_low = self.p_close

            else:
                self.c_white = False
                self.c_black = False
                self.r_body = n2d(self.p_open - self.p_close)
                self.r_upper_shadow = n2d(self.p_high - self.p_open)
                self.r_lower_shadow = n2d(self.p_close - self.p_low)
                self.p_body_high = self.p_open
                self.p_body_low = self.p_close