def get_context_data(self, **kwargs): # validated by URL pattern amount = Decimal(self.kwargs.pop("amount")) depth = mtgox.market(USD).getDepth() orders = self.get_orders(depth) sought = Amount(amount, BTC) obtained = Amount(0, BTC) orders_from = [] order_idx = 0 # get all of the orders we could be purchasing from while obtained < sought: order = orders[order_idx] xrt = order.exchange_rate # we're always trying to buy/sell BTC, so make sure the amounts # are consistent order_btc_amount = xrt.convert(order.to_amount, BTC) obtained = obtained + order_btc_amount orders_from.append(order) # get average of all orders from which the purchase will be constructed # as a Decimal avg = self.get_avg(orders=orders_from) total = float(amount * avg) # add commission value = (total * settings.BTCX_COMMISSION) + total ctx = {"rate": {"value": value, "currency": "USD", "amount": float(amount)}} return ctx
def get_xrt(self): xrt = mtgox.market(USD).getTicker().sell # returns a Decimal type return xrt.exchange_rate