예제 #1
0
    def get_pair_info(self, pair: Pair) -> PairData:
        if not self.is_pair_exists(pair):
            raise PairNotExistsException(pair)

        pair_data = self._get_data[pair]

        mid = (Decimal(str(pair_data["Bid"])) +
               Decimal(str(pair_data["Ask"]))) / Decimal("2")

        try:
            ts_without_ms = pair_data["TimeStamp"].split(".")[0]
            last_trade_at = datetime.strptime(ts_without_ms,
                                              "%Y-%m-%dT%H:%M:%S")
        except ValueError:
            raise APIChangedException("TimeStamp format.")

        if pair_data["Low"] and pair_data["High"]:
            low24h = Decimal(str(pair_data["Low"]))
            high24h = Decimal(str(pair_data["High"]))
        else:
            low24h = high24h = None

        rate_open = Decimal(str(
            pair_data["PrevDay"])) if pair_data["PrevDay"] else None

        return PairData(
            pair=pair,
            rate=mid,
            rate_open=rate_open,
            low24h=low24h,
            high24h=high24h,
            last_trade_at=last_trade_at,
        )
    def get_pair_info(self, pair: Pair) -> PairData:
        if not self.is_pair_exists(pair):
            raise PairNotExistsException(pair)

        return PairData(
            pair=pair,
            rate=Decimal(str(self._get_data["rates"][pair.to_currency.code])),
            last_trade_at=datetime.fromtimestamp(self._get_data["timestamp"]),
        )
예제 #3
0
    def test_reverse_pair_data(self):
        pair_data = PairData(
            pair=Pair(ECurrency("BTC"), ECurrency("USD")),
            rate=Decimal("1") / Decimal("3"),
            last_trade_at=datetime(2019, 3, 9, 12),
            rate_open=Decimal("1") / Decimal("2"),
            low24h=Decimal("1") / Decimal("4"),
            high24h=Decimal("1") / Decimal("8"),
        )

        pair_data_reversed = PairData(
            pair=Pair(ECurrency("USD"), ECurrency("BTC")),
            rate=Decimal("3"),
            last_trade_at=datetime(2019, 3, 9, 12),
            rate_open=Decimal("2"),
            low24h=Decimal("4"),
            high24h=Decimal("8"),
        )

        self.assertEqual(reverse_pair_data(pair_data), pair_data_reversed)
 def test_get_pair_info(self):
     pair = Pair(ECurrency("USD"), ECurrency("EUR"))
     self.assertEqual(
         OpenExchangeRatesExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("0.8792"),
             rate_open=None,
             last_trade_at=datetime(2019, 3, 21, 21, 0),
         ),
     )
예제 #5
0
 def test_get_pair_info(self):
     pair = Pair(ECurrency("EUR"), ECurrency("USD"))
     self.assertEqual(
         FixerExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("1.137314"),
             rate_open=None,
             last_trade_at=datetime(2019, 3, 21, 20, 47, 5),
         ),
     )
예제 #6
0
 def test_get_pair_info(self):
     pair = Pair(ECurrency("USD"), ECurrency("SYP"))
     self.assertEqual(
         SpTodayExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("2105"),
             rate_open=None,
             last_trade_at=datetime(2019, 3, 17, 22, 14, 15, 0),
         ),
     )
예제 #7
0
    def get_pair_info(self, pair: Pair) -> PairData:
        if not self.is_pair_exists(pair):
            raise PairNotExistsException(pair)

        request_pair = f"{pair.from_currency}{pair.to_currency}".lower()

        try:
            response = requests.get(
                f"https://api.bitfinex.com/v1/pubticker/{request_pair}")
            response.raise_for_status()
            data = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            schema = {
                "type": "object",
                "properties": {
                    "mid": {
                        "type": "string"
                    },
                    "low": {
                        "type": "string"
                    },
                    "high": {
                        "type": "string"
                    },
                    "timestamp": {
                        "type": "string"
                    },
                },
                "required": ["mid", "low", "high", "timestamp"],
            }
            validate(data, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        try:
            rate = Decimal(data["mid"])
            low = Decimal(data["low"])
            high = Decimal(data["high"])
            last_trade_at = float(data["timestamp"])
        except (DecimalException, ValueError) as e:
            raise APIErrorException(e)

        return PairData(
            pair=pair,
            rate=rate,
            low24h=low,
            high24h=high,
            last_trade_at=datetime.fromtimestamp(last_trade_at),
        )
예제 #8
0
 def test_get_pair_info(self):
     pair = Pair(ECurrency("BTC"), ECurrency("THB"))
     self.assertEqual(
         SatangExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("304799.5"),
             last_trade_at=datetime(2020, 2, 17, 22, 14, 15),
             rate_open=None,
             low24h=None,
             high24h=None,
         ),
     )
예제 #9
0
 def test_get_pair_info(self):
     pair = Pair(ECurrency("BTC"), ECurrency("THB"))
     self.assertEqual(
         BitkubExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("300353.515"),
             rate_open=None,
             low24h=Decimal("281470"),
             high24h=Decimal("304000"),
             last_trade_at=datetime(2019, 3, 17, 22, 14, 15, 0),
         ),
     )
 def test_null_high_low(self):
     pair = Pair(ECurrency("BTC"), ECurrency("USD"))
     self.assertEqual(
         BittrexExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("5132.308"),
             rate_open=Decimal("5001.301"),
             low24h=None,
             high24h=None,
             last_trade_at=datetime(2019, 4, 7, 7, 54, 34),
         ),
     )
 def test_get_pair_info(self):
     pair = Pair(ECurrency("BTC"), ECurrency("USD"))
     self.assertEqual(
         BittrexExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("3909.439"),
             rate_open=Decimal("3879.0"),
             low24h=Decimal("3773.806"),
             high24h=Decimal("3923.994"),
             last_trade_at=datetime(2019, 3, 9, 13, 47, 19, 0),
         ),
     )
예제 #12
0
 def test_get_pair_info(self):
     pair = Pair(ECurrency("BTC"), ECurrency("USD"))
     self.assertEqual(
         BitfinexExchange().get_pair_info(pair),
         PairData(
             pair=pair,
             rate=Decimal("3996.05"),
             rate_open=None,
             low24h=Decimal("3850.0"),
             high24h=Decimal("4021.0"),
             last_trade_at=datetime(2019, 3, 9, 11, 21, 12, 996645),
         ),
     )
예제 #13
0
    def get_pair_info(self, pair: Pair) -> PairData:
        if not self.is_pair_exists(pair):
            raise PairNotExistsException(pair)

        pair_data = self._get_data[pair]

        mid = (
            Decimal(str(pair_data["ask"]["price"]))
            + Decimal(str(pair_data["bid"]["price"]))
        ) / Decimal("2")

        low24h = high24h = None

        return PairData(
            pair=pair,
            rate=mid,
            low24h=low24h,
            high24h=high24h,
            last_trade_at=datetime.utcnow(),
        )
    def test_ok(self):
        pair_data = PairData(
            pair=Pair(ECurrency("BTC"), ECurrency("USD")),
            rate=Decimal("1") / Decimal("3"),
            last_trade_at=datetime(2019, 3, 9, 12),
            rate_open=Decimal("1") / Decimal("2"),
            low24h=Decimal("1") / Decimal("4"),
            high24h=Decimal("1") / Decimal("8"),
        )

        rate_obj = rate_from_pair_data(pair_data, exchange_id=1)

        inst = inspect(rate_obj)
        self.assertSetEqual(
            {c_attr.key
             for c_attr in inst.mapper.column_attrs},
            {
                "id",
                "exchange_id",
                "from_currency_id",
                "to_currency_id",
                "rate",
                "rate_open",
                "low24h",
                "high24h",
                "last_trade_at",
                "created_at",
                "modified_at",
            },
        )

        self.assertEqual(rate_obj.exchange_id, 1)
        self.assertEqual(rate_obj.from_currency.code,
                         pair_data.pair.from_currency.code)
        self.assertEqual(rate_obj.to_currency.code,
                         pair_data.pair.to_currency.code)
        self.assertEqual(rate_obj.rate, pair_data.rate)
        self.assertEqual(rate_obj.rate_open, pair_data.rate_open)
        self.assertEqual(rate_obj.low24h, pair_data.low24h)
        self.assertEqual(rate_obj.high24h, pair_data.high24h)
        self.assertEqual(rate_obj.last_trade_at, pair_data.last_trade_at)
예제 #15
0
    def get_pair_info(self, pair: Pair) -> PairData:
        if not self.is_pair_exists(pair):
            raise PairNotExistsException(pair)

        pair_data = self._get_data[pair]

        mid = (Decimal(str(pair_data["lowestAsk"])) +
               Decimal(str(pair_data["highestBid"]))) / Decimal("2")

        if pair_data["low24hr"] and pair_data["high24hr"]:
            low24h = Decimal(str(pair_data["low24hr"]))
            high24h = Decimal(str(pair_data["high24hr"]))
        else:
            low24h = high24h = None

        return PairData(
            pair=pair,
            rate=mid,
            low24h=low24h,
            high24h=high24h,
            last_trade_at=datetime.utcnow(),
        )