Example #1
0
def _trim_orderbook_list(arr: list,
                         ascending: bool,
                         limit_len: int = 50) -> list:
    """trims prices up to 4 digits precision"""
    first_price = arr[0][0]
    if first_price < 0.1:
        unit = 1e-5
    elif first_price < 1:
        unit = 1e-4
    elif first_price < 10:
        unit = 1e-3
    elif first_price < 100:
        unit = 1e-2
    elif first_price < 1000:
        unit = 1e-1
    elif first_price < 10000:
        unit = 1
    else:
        unit = 10

    trimmed_price = jh.orderbook_trim_price(first_price, ascending, unit)
    temp_qty = 0
    trimmed_arr = []
    for a in arr:
        if len(trimmed_arr) == limit_len:
            break

        if (ascending and a[0] > trimmed_price) or (not ascending
                                                    and a[0] < trimmed_price):
            # add previous record
            trimmed_arr.append([trimmed_price, temp_qty])
            # update temp values
            temp_qty = a[1]
            trimmed_price = jh.orderbook_trim_price(a[0], ascending, unit)
        else:
            temp_qty += a[1]

    return trimmed_arr
Example #2
0
def test_orderbook_trim_price():
    # bids
    assert jh.orderbook_trim_price(101.12, False, .1) == 101.1
    assert jh.orderbook_trim_price(101.1, False, .1) == 101.1

    assert jh.orderbook_trim_price(10.12, False, .01) == 10.12
    assert jh.orderbook_trim_price(10.1, False, .01) == 10.1
    assert jh.orderbook_trim_price(10.122, False, .01) == 10.12
    assert jh.orderbook_trim_price(1.1223, False, .001) == 1.122

    # asks
    assert jh.orderbook_trim_price(101.12, True, .1) == 101.2
    assert jh.orderbook_trim_price(101.1, True, .1) == 101.1
    assert jh.orderbook_trim_price(10.12, True, .01) == 10.12
    assert jh.orderbook_trim_price(10.122, True, .01) == 10.13
    assert jh.orderbook_trim_price(1.1223, True, .001) == 1.123