def test_should_have_latest_of_twice_appended_rate(self): cache = FxPricesCache() cache.set_rate(TickEvent('CHF_USD', get_time(), 1.1, 1.2)) cache.set_rate(TickEvent('CHF_USD', get_time(), 1.15, 1.25)) rates = cache.get_rate('CHF_USD') self.assertEqual(1.15, rates['bid']) self.assertEqual(1.25, rates['ask'])
def test_should_give_correct_rates_after_event_is_queued_using_event_loop( self): cache = self.play_event_loop( TickEvent('EUR_GBP', get_time(), 0.87, 0.88)) rates = cache.get_rate('EUR_GBP') self.assertEqual(0.87, rates['bid']) self.assertEqual(0.88, rates['ask'])
def test_should_allow_to_set_unity_fx_rate_and_have_same_units(self): ccy_limits = {'CHF': 110} rm = CcyExposureLimitRiskEvaluator('SGD', self.cache, ccy_limit=10000, ccy_limits=ccy_limits) rm.rates_cache.set_rate(TickEvent('CHF_SGD', get_time(), 1.0, 1.0)) order = OrderEvent('CHF_SGD', 105, 'buy') filtered = rm.filter_order(order) self.assertEquals(filtered.units, 105)
def test_should_allow_to_set_unity_fx_rate_and_have_less_units_when_order_size_breaches_specific_limit_on_1st_ccy( self): ccy_limits = {'CHF': 110} rm = CcyExposureLimitRiskEvaluator('SGD', self.cache, ccy_limit=10000, ccy_limits=ccy_limits) rm.rates_cache.set_rate(TickEvent('CHF_SGD', get_time(), 1.0, 1.0)) order = OrderEvent('CHF_SGD', 150, 'buy') filtered = rm.filter_order(order) self.assertEquals(filtered.units, 110)
def test_should_not_accept_tick_older_than_max_acceptable_age(self): max_tick_age = 100 cache = FxPricesCache(max_tick_age=max_tick_age) older_than_max_tick_age = get_time(-2 * max_tick_age) try: tick = TickEvent('CHF_USD', older_than_max_tick_age, 1.1, 1.2) cache.set_rate(tick) self.fail( 'FX price cache should not have accepted a tick older than [%s] seconds - [%s]' % (max_tick_age, tick)) except AssertionError: pass
def test_should_not_accept_day_old_tick(self): cache = FxPricesCache() one_day_in_seconds = 86400 one_day_ago = get_time(-1 * one_day_in_seconds) try: tick = TickEvent('CHF_USD', one_day_ago, 1.1, 1.2) cache.set_rate(tick) self.fail( 'FX price cache should not have accepted a day old tick - [%s]' % tick) except AssertionError: pass
def query(self, instrument, granularity='S5', count=50, other_params=None): rates = [] session = None try: session = requests.Session() url = self.domain + "/v1/candles" headers = {'Authorization': 'Bearer ' + self.access_token} if other_params is None: params = {} else: params = other_params.copy() params['instrument'] = instrument # params['granularity'] = granularity # params['count'] = count req = requests.Request('GET', url, headers=headers, params=params) pre = req.prepare() response = session.send(pre, stream=False, verify=False) if response.status_code != 200: exm = 'Web response not OK (%d), "%s"' % (response.status_code, response.text) raise ExceptionEvent(self, exm) try: msg = json.loads(response.text) if "instrument" in msg or "tick" in msg: instrument = msg["instrument"] for candle in msg['candles']: time = candle["time"] bid = candle["closeBid"] ask = candle["closeAsk"] rates.append(TickEvent(instrument, time, bid, ask)) except Exception as e: exm = 'Cannot convert response to json - "%s"' % e raise ExceptionEvent(self, exm) finally: if session is not None: session.close() return rates
def testTickEventHasExpectedBid(self): self.assertEquals(TickEvent(None, None, 33.34, None).bid, 33.34)
def testTickEventHasExpectedAsk(self): self.assertEquals(TickEvent(None, None, None, 123.4).ask, 123.4)
def testTickEventHasExpectedInstrument(self): self.assertEquals(TickEvent("ABC", None, None, None).instrument, "ABC")
def testTickEventHasExpectedTime(self): self.assertEquals(TickEvent(None, "12-33-45", None, None).time, "12-33-45")
def testTickEventClassExists(self): self.assertIsNotNone(TickEvent(None, None, None, None), None)
def testTickEventHasExpectedType(self): self.assertEquals(TickEvent(None, None, None, None).TYPE, "TICK")
def test_accept_day_old_tick_if_less_than_max_acceptable_age(self): one_day_in_seconds = 86400 cache = FxPricesCache(max_tick_age=2 * one_day_in_seconds) one_day_ago = get_time(-1 * one_day_in_seconds) tick = TickEvent('CHF_USD', one_day_ago, 1.1, 1.2) cache.set_rate(tick)
def assign_unity_rates(self, rm): now = get_time() rm.rates_cache.set_rate(TickEvent('CHF_USD', now, 1.0, 1.0)) rm.rates_cache.set_rate(TickEvent('EUR_USD', now, 1.0, 1.0)) rm.rates_cache.set_rate(TickEvent('SGD_USD', now, 1.0, 1.0)) rm.rates_cache.set_rate(TickEvent('CHF_SGD', now, 1.0, 1.0))
def step_impl(context, ccy, bid, ask, base_ccy): context.rates_cache.set_rate( TickEvent(ccy + '_' + base_ccy, get_time(), float(bid), float(ask)))
def step_impl(context, instrument, bid, ask): context.rates_events.put( TickEvent(instrument, get_time(), float(bid), float(ask)))
def step_impl(context, instrument, bid, ask): context.old_tick = TickEvent( instrument, get_time(-2 * context.rates_cache.max_tick_age), float(bid), float(ask)) context.rates_events.put(context.old_tick)
def test_should_have_appended_rate_tuple(self): cache = FxPricesCache() cache.set_rate(TickEvent('CHF_USD', get_time(), 1.1, 1.2)) rates = cache.get_rate_tuple('CHF_USD') self.assertEqual((1.1, 1.2), rates)
def test_can_append_rate(self): cache = FxPricesCache() cache.set_rate(TickEvent('CHF_USD', get_time(), 1.1, 1.2))
def testTickEventHasNoDefaultReceivedTime(self): self.assertIsNone(TickEvent(None, None, None, 123.4).receive_time)
def step_impl(context): context.response = None context.tick = TickEvent('CHF_USD', get_time(), 1.0, 1.0) context.rates_events.put(context.tick)
def testTickEventHasExpectedReceivedTime(self): rt = "2015-06-11T22:48:01.000000Z" self.assertEqual(rt, TickEvent(None, None, None, 123.4, rt).receive_time)
def assign_dummy_rates(self, rm): now = get_time() rm.rates_cache.set_rate(TickEvent('CHF_USD', now, 1.04, 1.05)) rm.rates_cache.set_rate(TickEvent('EUR_USD', now, 1.08, 1.09)) rm.rates_cache.set_rate(TickEvent('SGD_USD', now, 0.74, 0.75)) rm.rates_cache.set_rate(TickEvent('CHF_SGD', now, 1.04, 1.05))