示例#1
0
    def find_by(
        self,
        market_name: str,
        pair: Pair,
        interval: DateTimeInterval = DateTimeInterval(None, None),
        candle_size: CandleSize = CandleSize(CANDLE_SIZE_UNIT_MINUTE, 1)
    ) -> List[Candle]:
        parameters = {
            CANDLE_STORAGE_FIELD_MARKET: "= '{}'".format(market_name),
            CANDLE_STORAGE_FIELD_PAIR: "= '{}'".format(serialize_pair(pair)),
        }
        if interval.since is not None:
            parameters['"time" >'] = "'{}'".format(interval.since.isoformat())

        if interval.till is not None:
            parameters['"time" <'] = "'{}'".format(interval.till.isoformat())

        select = '*'
        if not candle_size.is_one_minute():
            select = 'FIRST("open") AS "open", MAX("high") AS "high", MIN("low") AS "low", LAST("close") AS "close"'

        sql = 'SELECT {} FROM "{}" WHERE '.format(select,
                                                  MEASUREMENT_CANDLES_NAME)
        where = []
        for key, value in parameters.items():
            where.append('{} {}'.format(key, value))
        sql += ' AND '.join(where)
        sql += self._get_group_by(candle_size)

        result: ResultSet = self._client.query(sql)
        data = list(result.get_points())

        return self._parse_db_result_into_candles(data, market_name, pair,
                                                  candle_size)
示例#2
0
def test_find_by_for_bigger_candles(influx_database: InfluxDBClient):
    storage = CandleInnoDbStorage(influx_database)

    time1 = datetime.datetime(2017,
                              7,
                              2,
                              0,
                              1,
                              0,
                              tzinfo=datetime.timezone.utc)
    storage.write_candles([
        Candle(DUMMY_MARKET, BTC_USD_PAIR, time1, Decimal('100'),
               Decimal('220'), Decimal('90'), Decimal('200')),
    ])

    candle = storage.find_by(market_name=DUMMY_MARKET,
                             pair=BTC_USD_PAIR,
                             candle_size=CandleSize(CANDLE_SIZE_UNIT_HOUR,
                                                    1))[0]

    assert candle.open == Decimal('100')
    assert candle.high == Decimal('220')
    assert candle.low == Decimal('90')
    assert candle.close == Decimal('200')
    assert str(candle.candle_size) == 'CandleSize: 1-hour'

    time2 = datetime.datetime(2017,
                              7,
                              2,
                              0,
                              2,
                              0,
                              tzinfo=datetime.timezone.utc)
    storage.write_candles([
        Candle(DUMMY_MARKET, BTC_USD_PAIR, time2, Decimal('0'), Decimal('320'),
               Decimal('50'), Decimal('400')),
    ])

    candle = storage.find_by(market_name=DUMMY_MARKET,
                             pair=BTC_USD_PAIR,
                             candle_size=CandleSize(CANDLE_SIZE_UNIT_HOUR,
                                                    1))[0]

    assert candle.open == Decimal('100')
    assert candle.high == Decimal('320')
    assert candle.low == Decimal('50')
    assert candle.close == Decimal('400')
    assert str(candle.candle_size) == 'CandleSize: 1-hour'
 def find_by(
     self,
     market_name: str,
     pair: Pair,
     interval: DateTimeInterval = DateTimeInterval(None, None),
     candle_size: CandleSize = CandleSize(CANDLE_SIZE_UNIT_MINUTE, 1)
 ) -> List[Candle]:
     return []
def test_subscription_can_be_found():
    storage = SubscriptionStorage()
    last_candle_subscription = LastCandleSubscription(
        '1',
        'foo_storage',
        'bar_market',
        Pair('USD', 'BTC'),
        CandleSize(CANDLE_SIZE_UNIT_MINUTE, 1)
    )
    storage.subscribe(last_candle_subscription)
    subscription_interval = DateTimeInterval(
        datetime.datetime(2018, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc),
        datetime.datetime(2018, 1, 2, 0, 0, 0, tzinfo=datetime.timezone.utc)
    )
    date_in_interval = datetime.datetime(2018, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)
    date_outside_interval = datetime.datetime(2018, 1, 3, 0, 0, tzinfo=datetime.timezone.utc)
    new_order_subscription = NewOrderSubscription(
        '1',
        'foo_storage',
        'bar_market',
        Pair('USD', 'BTC'),
        subscription_interval
    )
    storage.subscribe(new_order_subscription)

    assert storage.find_subscriptions_for_event('unknown_event_name') == []

    candle_suitable_for_subscription = Candle(
        'bar_market',
        Pair('USD', 'BTC'),
        date_in_interval,
        Decimal('11000'),
        Decimal('11000'),
        Decimal('11000'),
        Decimal('11000')
    )

    assert storage.find_subscriptions_for_event(EVENT_LAST_CANDLE_UPDATED)[0] == last_candle_subscription
    assert storage.find_subscriptions_for_event(
        EVENT_LAST_CANDLE_UPDATED,
        {'storage': 'foo_storage', 'candle': serialize_candle(candle_suitable_for_subscription)}
    )[0] == last_candle_subscription
    assert storage.find_subscriptions_for_event(
        EVENT_LAST_CANDLE_UPDATED,
        {'storage': 'gandalf', 'candle': serialize_candle(candle_suitable_for_subscription)}
    ) == []
    assert storage.find_subscriptions_for_event(
        EVENT_LAST_CANDLE_UPDATED,
        {
            'storage': 'foo_storage',
            'candle': serialize_candle(
                Candle(
                    'bar_market',
                    Pair('OMG', 'WTF'),
                    date_in_interval,
                    Decimal('11000'),
                    Decimal('11000'),
                    Decimal('11000'),
                    Decimal('11000')
                )
            )
        }
    ) == []
    assert storage.find_subscriptions_for_event(
        EVENT_LAST_CANDLE_UPDATED,
        {
            'storage': 'foo_storage',
            'candle': serialize_candle(
                Candle(
                    'wtf_market',
                    Pair('USD', 'BTC'),
                    date_in_interval,
                    Decimal('11000'),
                    Decimal('11000'),
                    Decimal('11000'),
                    Decimal('11000')
                )
            )
        }
    ) == []

    order_suitable_for_subscription = _crate_serialized_order(Pair('USD', 'BTC'), 'bar_market', date_in_interval)

    assert storage.find_subscriptions_for_event(EVENT_NEW_ORDER)[0] == new_order_subscription
    assert storage.find_subscriptions_for_event(
        EVENT_NEW_ORDER,
        {'storage': 'foo_storage', 'order': order_suitable_for_subscription}
    )[0] == new_order_subscription
    assert storage.find_subscriptions_for_event(
        EVENT_NEW_ORDER,
        {'storage': 'gandalf', 'order': order_suitable_for_subscription}
    ) == []
    assert storage.find_subscriptions_for_event(
        EVENT_NEW_ORDER,
        {
            'storage': 'foo_storage',
            'order': _crate_serialized_order(Pair('USD', 'BTC'), 'bar_market', date_outside_interval)
        }
    ) == []
    assert storage.find_subscriptions_for_event(
        EVENT_NEW_ORDER,
        {
            'storage': 'foo_storage',
            'order': _crate_serialized_order(Pair('OMG', 'WTF'), 'bar_market', date_in_interval)
        }
    ) == []
    assert storage.find_subscriptions_for_event(
        EVENT_NEW_ORDER,
        {
            'storage': 'foo_storage',
            'order': _crate_serialized_order(Pair('USD', 'BTC'), 'wtf_market', date_in_interval)
        }
    ) == []

    storage.unsubscribe(EVENT_NEW_ORDER, '2')
    assert storage.find_subscriptions_for_event(EVENT_NEW_ORDER)[0] == new_order_subscription
    storage.unsubscribe(EVENT_NEW_ORDER, '1')
    assert storage.find_subscriptions_for_event(EVENT_NEW_ORDER) == []

    storage.unsubscribe(EVENT_LAST_CANDLE_UPDATED, '2')
    assert storage.find_subscriptions_for_event(EVENT_LAST_CANDLE_UPDATED)[0] == last_candle_subscription
    storage.unsubscribe(EVENT_LAST_CANDLE_UPDATED, '1')
    assert storage.find_subscriptions_for_event(EVENT_LAST_CANDLE_UPDATED) == []
示例#5
0
 def _get_group_by(candle_size: CandleSize) -> str:
     if candle_size.is_one_minute():
         return ''
     return ' GROUP BY time({}{})'.format(candle_size.size,
                                          UNIT_MAP[candle_size.unit])
示例#6
0
def test_get_as_time_delta():
    candle_size = CandleSize(CANDLE_SIZE_UNIT_MINUTE, 15)
    assert candle_size.get_as_time_delta() == datetime.timedelta(minutes=15)
示例#7
0
def test_get_interval_for_datetime_non_divisible_values_raises_error(
        unit: str, size: int):
    with pytest.raises(AssertionError):
        CandleSize(unit, size)
示例#8
0
def test_get_interval_for_datetime(expected: str, candle_size: CandleSize,
                                   time: datetime.datetime):
    assert str(candle_size.get_interval_for_datetime(time)) == expected
示例#9
0
import pytest

from coinrat.domain.candle import CandleSize, CANDLE_SIZE_UNIT_MINUTE, CANDLE_SIZE_UNIT_HOUR, CANDLE_SIZE_UNIT_DAY


def test_get_as_time_delta():
    candle_size = CandleSize(CANDLE_SIZE_UNIT_MINUTE, 15)
    assert candle_size.get_as_time_delta() == datetime.timedelta(minutes=15)


@pytest.mark.parametrize(
    ['expected', 'candle_size', 'time'],
    [
        [
            '[2018-01-01T00:00:00+00:00, 2018-01-01T00:15:00+00:00]',
            CandleSize(CANDLE_SIZE_UNIT_MINUTE, 15),
            datetime.datetime(
                2018, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)
        ],
        [
            '[2018-01-01T00:00:00+00:00, 2018-01-01T00:15:00+00:00]',
            CandleSize(CANDLE_SIZE_UNIT_MINUTE, 15),
            datetime.datetime(
                2018, 1, 1, 0, 4, 0, tzinfo=datetime.timezone.utc)
        ],
        [
            '[2018-01-01T00:15:00+00:00, 2018-01-01T00:30:00+00:00]',
            CandleSize(CANDLE_SIZE_UNIT_MINUTE, 15),
            datetime.datetime(
                2018, 1, 1, 0, 15, 24, tzinfo=datetime.timezone.utc)
        ],