예제 #1
0
 def from_all_trades(item: dict):
     return Trade(trade_id=str(item['tid']),
                  timestamp=iso8601_to_unix(item['created_at']),
                  pair="-".join(item['book'].split('_')).upper(),
                  is_sell=item['maker_side'] == 'buy',
                  price=Wad.from_number(item['price']),
                  amount=Wad.from_number(abs(float(item['amount']))))
예제 #2
0
    def from_all_trades_message(trade: dict, pair: str, base_token: Token, previous_base_token_reserves: Wad, timestamp: int) -> Trade:
        assert (isinstance(trade, dict))
        assert (isinstance(pair, str))
        assert (isinstance(base_token, Token))
        assert (isinstance(previous_base_token_reserves, Wad))
        assert (isinstance(timestamp, int))

        if trade['token0']['id'] == base_token.address.address:
            swap_price = Wad.from_number(trade['reserve1']) / Wad.from_number(trade['reserve0'])

            is_sell = Wad.from_number(trade['reserve1']) < previous_base_token_reserves

            amount = abs(Wad.from_number(trade['reserve1']) - previous_base_token_reserves)

        else:
            swap_price = Wad.from_number(trade['reserve0']) / Wad.from_number(trade['reserve1'])

            is_sell = Wad.from_number(trade['reserve0']) < previous_base_token_reserves

            amount = abs(Wad.from_number(trade['reserve0']) - previous_base_token_reserves)

        unhashed_id = str(timestamp)
        trade_id = hashlib.sha256(unhashed_id.encode()).hexdigest()
        return Trade(trade_id=trade_id,
                     timestamp=timestamp,
                     pair=pair,
                     is_sell=is_sell,
                     price=swap_price,
                     amount=amount)
예제 #3
0
 def from_message(trade, pair: str) -> Trade:
     return Trade(trade_id=trade['orderId'],
                  timestamp=int(trade['timestamp']),
                  pair=pair,
                  is_sell=True if trade['type'] == 'ask' else False,
                  price=Wad.from_number(float(trade["price"])),
                  amount=Wad.from_number(float(trade['qty'])))
예제 #4
0
    def from_message(trade, pair: str, market_info: dict) -> Trade:
        decimal_exponent = 18 - int(market_info['quoteCurrency']['decimals'])
        price = Wad.from_number(float(trade['price']) * 10 ** decimal_exponent)

        return Trade(trade_id=trade['uuid'],
                     timestamp=int(dateutil.parser.parse(trade['createdAt']).timestamp()),
                     pair=trade["market"],
                     is_sell=True if trade['side'] == 'SELL' else False,
                     price=price,
                     amount=Wad.from_number(from_wei(abs(int(float(trade['amount']))), 'ether')))
예제 #5
0
파일: erisx.py 프로젝트: leverj/pyexchange
    def from_message(trade: dict) -> Trade:
        if isinstance(trade['time'], int):
            timestamp = trade['time']
        else:
            timestamp = int(parser.isoparse(trade['time']).timestamp())

        return Trade(trade_id=trade['trade_id'],
                     timestamp=timestamp,
                     pair=trade["contract_symbol"].replace("/", "-"),
                     is_sell=True if trade['side'] == 'SELL' else False,
                     price=Wad.from_number(trade['px']),
                     amount=Wad.from_number(abs(float(trade['qty']))))
예제 #6
0
    def from_our_trades_message(trade: dict, pair: str, base_token: Token, our_liquidity_balance: Wad, previous_base_token_reserves: Wad, timestamp: int) -> Trade:
        """
        Assumptions:
            Prices are denominated in quote token
            Amounts are denominated in the base token

            We are always long the base token, and short quote
            if the amount of TokenA is 0, then we are essentially buying additional base token in exchange for quote token

            If the base token reserves increase compared to the last block, then the trade is a buy

        Uniswap pairs can occasionally differ in base-quote terms from our expectations and price feeds.
        This requires a check to ensure that we are properly comparing asset pairs.
       """
        assert (isinstance(trade, dict))
        assert (isinstance(pair, str))
        assert (isinstance(base_token, Token))
        assert (isinstance(our_liquidity_balance, Wad))
        assert (isinstance(previous_base_token_reserves, Wad))
        assert (isinstance(timestamp, int))

        our_pool_share = our_liquidity_balance / Wad.from_number(trade['totalSupply'])

        if trade['token0']['id'] == base_token.address.address:
            swap_price = Wad.from_number(trade['reserve1']) / Wad.from_number(trade['reserve0'])

            is_sell = Wad.from_number(trade['reserve1']) < previous_base_token_reserves

            amount = our_pool_share * abs(Wad.from_number(trade['reserve1']) - previous_base_token_reserves)

        else:
            swap_price = Wad.from_number(trade['reserve0']) / Wad.from_number(trade['reserve1'])

            is_sell = Wad.from_number(trade['reserve0']) < previous_base_token_reserves

            amount = our_pool_share * abs(Wad.from_number(trade['reserve0']) - previous_base_token_reserves)

        unhashed_id = str(timestamp)
        trade_id = hashlib.sha256(unhashed_id.encode()).hexdigest()
        return Trade(trade_id=trade_id,
                     timestamp=timestamp,
                     pair=pair,
                     is_sell=is_sell,
                     price=swap_price,
                     amount=amount)
예제 #7
0
    def get_all_trades(self, pair: str, page_number: int = 1) -> List[Trade]:
        assert (isinstance(pair, str))
        assert (isinstance(page_number, int))
        assert (page_number == 1)

        result = self._http_request("GET", f"/v3/markets/{pair}/trades", {})

        return list(
            map(
                lambda item: Trade(trade_id=item['id'],
                                   timestamp=int(
                                       dateutil.parser.parse(item[
                                           'executedAt'] + 'Z').timestamp()),
                                   pair=pair,
                                   is_sell=True
                                   if item['takerSide'] == "SELL" else False,
                                   price=Wad.from_number(item['rate']),
                                   amount=Wad.from_number(item['quantity'])),
                result))
예제 #8
0
    def get_trades(self, pair: str, page_number: int = 1) -> List[Trade]:
        assert (isinstance(pair, str))
        assert (isinstance(page_number, int))
        assert (page_number == 1)

        trades = self._http_authenticated_request(
            "GET", f"/v3/orders/closed?marketSymbol={pair}")

        return list(
            map(
                lambda item: Trade(
                    trade_id=item['id'],
                    timestamp=int(
                        dateutil.parser.parse(item['createdAt'] + 'Z').
                        timestamp()),
                    pair=item['marketSymbol'],
                    is_sell=True if item['direction'] == 'SELL' else False,
                    price=Wad.from_number(item['limit']),
                    amount=Wad.from_number(item['fillQuantity'])), trades))