def reify_trade( cls, trade: AbstractTrade, market_state: MarketState, ) -> List[Order]: """Given an abstract trade, return a list of concrete orders that will accomplish the higher-level transaction described. """ markets = market_state.available_markets() market = format_currency_pair(trade.sell_coin, trade.buy_coin) if market not in markets: market = format_currency_pair(trade.buy_coin, trade.sell_coin) if market not in markets: raise NoMarketAvailableError( f'No market available between {trade.sell_coin} and ' f'{trade.buy_coin} and indirect trades are not yet supported' ) base, quote = split_currency_pair(market) # Price is given as base currency / quote currency price = market_state.price(market) # Order amount is given with respect to the quote currency quote_amount = market_state.estimate_value( trade.reference_coin, trade.reference_value, quote, ) # Order direction is given with respect to the quote currency if trade.sell_coin == base: # Buy quote currency; sell base currency direction = Order.Direction.BUY elif trade.sell_coin == quote: # Sell quote currency; buy quote currency direction = Order.Direction.SELL else: raise return [ Order( market, price, quote_amount, direction, OrderType.fill_or_kill, ) ]
def estimate_price(self, market_state: MarketState): ''' Sets the approximate price of the quote value, given some chart data. ''' base_price = market_state.price(self.market_name) # The price (when buying/selling) # should match the self.market_name. # So, we keep around a self.market_price to match # self.price is always in the quote currency. self.market_price = base_price # Now, we find out what price matters for our trade. # The base price is always in the base currency, # So we will need to figure out if we are trading from, # or to, this base currency. if self.buy_coin == self.market_base_currency: self.price = 1 / base_price else: self.price = base_price