def test_candle(): candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('1000'), Decimal('2000'), Decimal('3000'), Decimal('4000')) assert '2017-01-01T00:00:00+00:00 O:1000.00000000 H:2000.00000000 L:3000.00000000 C:4000.00000000 ' + \ '| CandleSize: 1-minute' == str(candle) assert candle.is_bearish() is False assert candle.is_bullish() is True
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 test_candle_body_size(): candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('1000'), Decimal('2000'), Decimal('3000'), Decimal('4000')) assert candle.body_size() == Decimal('3000') candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('8000'), Decimal('2000'), Decimal('3000'), Decimal('4000')) assert candle.body_size() == Decimal('4000') candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('8000'), Decimal('10000'), Decimal('5000'), Decimal('8000')) assert candle.body_size() == Decimal('0')
def _create_candle_from_raw(self, pair: Pair, candles_data: Dict) -> Candle: return Candle( self._market_name, pair, datetime.fromtimestamp(candles_data['time']).astimezone( timezone.utc), Decimal(candles_data['open']), Decimal(candles_data['high']), Decimal(candles_data['low']), Decimal(candles_data['close']))
def _create_candle_from_raw_ticker_data(self, pair: Pair, candle: Dict[str, str]) -> Candle: return Candle( self.name, pair, dateutil.parser.parse( candle['T']).replace(tzinfo=datetime.timezone.utc), Decimal(candle['O']), Decimal(candle['H']), Decimal(candle['L']), Decimal(candle['C']))
def test_candle_wicks(): candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('8000'), Decimal('10000'), Decimal('5000'), Decimal('8000')) assert candle.has_upper_wick() is True assert candle.has_lower_wick() is True candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('8000'), Decimal('8000'), Decimal('8000'), Decimal('8000')) assert candle.has_upper_wick() is False assert candle.has_lower_wick() is False
def _create_dummy_candle(minute: int = 0, close: int = 8300) -> Candle: return Candle( DUMMY_MARKET, BTC_USD_PAIR, datetime.datetime(2017, 7, 2, 0, minute, 0, tzinfo=datetime.timezone.utc), Decimal('8000'), Decimal('8100'), Decimal('8200'), Decimal(close))
def test_candle_to_heikin_ashi(): previous_ha_candle = HeikinAshiCandle( '', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('2500'), Decimal('4000'), Decimal('1000'), Decimal('2500'), ) candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('3000'), Decimal('3500'), Decimal('2500'), Decimal('4000')) current_ha_candle = candle_to_heikin_ashi(candle, previous_ha_candle) assert str(current_ha_candle) == '2017-01-01T00:00:00+00:00 ' \ + 'O:2500.00000000 H:3500.00000000 L:2500.00000000 C:3250.00000000 | CandleSize: 1-minute (Heikin-Ashi)'
def test_candle_export_import(): pair = Pair('USD', 'BTC') candle = Candle( 'dummy_market', pair, datetime.datetime(2017, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), Decimal('1000'), Decimal('2000'), Decimal('3000'), Decimal('4000') ) storage = flexmock() storage.should_receive('find_by').and_return([candle]).once() storage.should_receive('write_candles').once() exporter = CandleExporter(storage) file_name = os.path.dirname(__file__) + '_orders.json' exporter.export_to_file(file_name, 'dummy_market', pair) expected = [{ 'market': 'dummy_market', 'pair': 'USD_BTC', 'time': '2017-01-01T00:00:00+00:00', 'open': "1000.00000000", 'high': "2000.00000000", 'low': "3000.00000000", 'close': "4000.00000000", 'size': '1-minute', }] with open(file_name) as json_file: assert json.load(json_file) == expected exporter.import_from_file(file_name) os.remove(file_name)
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) == []
def test_bearish_bullish(): candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('1000'), Decimal('2000'), Decimal('3000'), Decimal('4000')) assert candle.is_bearish() is False assert candle.is_bullish() is True candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('8000'), Decimal('2000'), Decimal('3000'), Decimal('4000')) assert candle.is_bearish() is True assert candle.is_bullish() is False candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('8000'), Decimal('10000'), Decimal('5000'), Decimal('8000')) assert candle.is_bearish() is False assert candle.is_bullish() is False
import datetime from decimal import Decimal from flexmock import flexmock from coinrat.domain.market import MarketException from coinrat_bittrex.synchronizer import BittrexSynchronizer from coinrat.domain.pair import Pair from coinrat.domain.candle import Candle from coinrat.event.event_emitter import EventEmitter BTC_USD_PAIR = Pair('USD', 'BTC') DUMMY_CANDLE = Candle( '', BTC_USD_PAIR, datetime.datetime(2017, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), Decimal('8000'), Decimal('8000'), Decimal('8000'), Decimal('8000')) def test_synchronize_success(): market = flexmock(name='yolo_market') market.should_receive('get_candles').and_return( [DUMMY_CANDLE, DUMMY_CANDLE]) market.should_receive('get_last_minute_candles').and_return(DUMMY_CANDLE) storage = flexmock(name='yolo_storage') storage.should_receive('write_candles').times(2) synchronizer = BittrexSynchronizer(market, storage, create_emitter_mock(), delay=0,
def test_create_initial_heikin_ashi_candle(): candle = Candle('', Pair('USD', 'BTC'), DUMMY_DATE, Decimal('2000'), Decimal('4500'), Decimal('1000'), Decimal('3000')) ha_candle = create_initial_heikin_ashi_candle(candle) assert str(ha_candle) == '2017-01-01T00:00:00+00:00 ' \ + 'O:2500.00000000 H:4500.00000000 L:1000.00000000 C:2625.00000000 | CandleSize: 1-minute (Heikin-Ashi)'