def get_mkr_eth_prices(self, takes): takes_1 = filter(lambda log_take: log_take.buy_token == self.mkr_address and log_take.pay_token == self.weth_address, takes) takes_2 = filter(lambda log_take: log_take.buy_token == self.weth_address and log_take.pay_token == self.mkr_address, takes) prices_mkr_eth = list(map(lambda log_take: Price(timestamp=log_take.timestamp, price=float(log_take.take_amount / log_take.give_amount), buy_price=None, sell_price=None, volume=float(log_take.give_amount)), takes_1)) \ + list(map(lambda log_take: Price(timestamp=log_take.timestamp, price=float(log_take.give_amount / log_take.take_amount), buy_price=None, sell_price=None, volume=float(log_take.take_amount)), takes_2)) return prices_mkr_eth
def prepare_prices_for_charting(prices: List[Price], price_gap_size: int) -> List[Price]: if len(prices) == 0: return prices result = [prices[0]] for i in range(1, len(prices)): if prices[i].timestamp - prices[i - 1].timestamp > price_gap_size: result.append( Price(prices[i - 1].timestamp + 1, None, None, None, None)) result.append(prices[i]) return result
def convert_prices(self, source: List[Price], target: List[Price], operator): result = [] for price in source: matching_target = next(filter(lambda target_price: target_price.timestamp >= price.timestamp, target), None) if matching_target is not None: result.append(Price(timestamp=price.timestamp, price=operator(price.price, matching_target.price), buy_price=None, sell_price=None, volume=price.volume)) else: logging.warning(f"No matching price found at {price.timestamp}") return result
def test_granularize_prices_fills_gaps(): # given # 1518440700 = 2018-02-12 13:05:00 UTC prices = [Price(1518440700, 1.5, None, None, 10), Price(1518440760, 1.7, None, None, 14), Price(1518440880, 1.2, None, None, 18)] # when granularized_prices = granularize_prices(prices) # then assert granularized_prices == [Price(1518440700, 1.5, None, None, 10), Price(1518440760, 1.7, None, None, 14), Price(1518440820, 0, 0, 0, 0), Price(1518440880, 1.2, None, None, 18)]
def test_granularize_prices_fills_gaps_even_if_seconds_are_uneven(): # given # 1518440700 = 2018-02-12 13:05:00 UTC prices = [Price(1518440759, 1.5, None, None, 10), Price(1518440760, 1.7, None, None, 14), Price(1518440851, 1.2, None, None, 18)] # when granularized_prices = granularize_prices(prices) # then assert granularized_prices == [Price(1518440759, 1.5, None, None, 10), Price(1518440760, 1.7, None, None, 14), Price(1518440851, 1.2, None, None, 18)]
def granularize_prices(prices: list) -> list: def get_minute(ts): return int(ts / 60) granular_prices = [] last_timestamp = -1 for price in prices: if last_timestamp != -1: minute_increment = get_minute( price.timestamp) - get_minute(last_timestamp) for i in range(0, minute_increment - 1): granular_prices.append( Price(last_timestamp + 60 * (i + 1), 0.0, 0.0, 0.0, 0.0)) if minute_increment > 0: granular_prices.append(price) else: granular_prices.append(price) last_timestamp = price.timestamp return granular_prices
def test_granularize_prices_removes_duplicates(): # given # 1518440700 = 2018-02-12 13:05:00 UTC prices = [Price(1518440700, 1.5, None, None, 10), Price(1518440730, 1.51, None, None, 11), Price(1518440740, 1.52, None, None, 12), Price(1518440759, 1.53, None, None, 13), Price(1518440760, 1.7, None, None, 14), Price(1518440885, 1.2, None, None, 18), Price(1518440910, 1.1, None, None, 29),] # when granularized_prices = granularize_prices(prices) # then assert granularized_prices == [Price(1518440700, 1.5, None, None, 10), Price(1518440760, 1.7, None, None, 14), Price(1518440820, 0, 0, 0, 0), Price(1518440885, 1.2, None, None, 18)]