def check_status(trades, orders): for trade in trades: found = None for order in orders: if str(trade.exchange_oid) == str(order[u"oid"]): found = order break if found is not None: if trade.status == "selling" and found[u"status"] == u"invalid": trade.status = "not enough funds" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="not enough funds", log_desc="Not enough funds for trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("not enoguh funds for sell at price: %s, amount: %s, currency: %s, trade: %s" % (trade.price, trade.amount, trade.currency.abbreviation, trade.pk)) if trade.status == "buying" and found[u"status"] == u"invalid": trade.status = "not enough funds" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="not enough funds", log_desc="Not enough funds for trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("not enoguh funds for buy at price: %s, amount: %s, currency: %s, trade: %s" % (trade.price, trade.amount, trade.currency.abbreviation, trade.pk)) else: if trade.status == "selling": trade.status = "sold" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="sold", log_desc="Sold trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("sold %s bitcoins at %s %s" % (trade.amount, trade.price, trade.currency.abbreviation)) elif trade.status == "buying": trade.status = "bought" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="bought", log_desc="Bought trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("bought %s bitcoins at %s %s" % (trade.amount, trade.price, trade.currency.abbreviation)) """
def trade(trades): #last prices for every exchange last_prices_exchanges = {} for trade in trades: if trade.exchange.name in exchanges and trade.exchange.name not in last_prices_exchanges: last_price = exchanges[trade.exchange.name].get_last_price(trade.currency.abbreviation) last_prices_exchanges[trade.exchange.name] = last_price elif trade.exchange.name in exchanges: last_price = last_prices_exchanges[trade.exchange.name] else: continue if last_price is None: continue try: watch_price = Decimal(trade.watch_price) # we are BUYING, when last price is higher or equal to watch price (lp_higher == True) and there is no related "sell" order if trade.active == True and trade.lp_higher == True and trade.buy_or_sell == True and trade.related is None: if last_price >= watch_price: response = exchanges[trade.exchange.name].buy(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "buying" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="buying", log_desc="Buying %s." % (trade.pk)) trade_log.save() if settings.bd_debug == True: console_log("buying, when last price (%s) is higher or equal than watch price (%s) and there is no related sell order (buying at price: %s, amount: %s, currency: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation)) # we are BUYING, when last price is lower or equal to watch price (lp_higher == False) and there is no related "sell" order elif trade.active == True and trade.lp_higher == False and trade.buy_or_sell == True and trade.related is None: if last_price <= watch_price: response = exchanges[trade.exchange.name].buy(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "buying" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="buying", log_desc="Buying %s." % (trade.pk)) trade_log.save() if settings.bd_debug == True: console_log("buying, when last price (%s) is lower or equal that watch price (%s) and there is no related sell order (buying at price: %s, amount: %s, currency: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation)) # we are BUYING, when last price is higher or equal to watch price (lp_higher == True) and related "sell" order has been fully "sold" elif trade.active == True and trade.lp_higher == True and trade.buy_or_sell == True and trade.related is not None: if trade.related.status == "sold" and trade.status == "waiting": if last_price >= watch_price: response = exchanges[trade.exchange.name].buy(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "buying" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="buying", log_desc="Buying %s (related %s sold)." % (trade.pk, trade.related.pk)) trade_log.save() if settings.bd_debug == True: console_log("buying, when last price (%s) is higher or equal to watch price (%s) and related sell order was sold (buying at price: %s, amount: %s, currency: %s, related: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation, trade.related.pk)) # we are BUYING, when last price is lower or equal to watch price (lp_hihger == False) and related "sell" order has been fully "sold" elif trade.active == True and trade.lp_higher == False and trade.buy_or_sell == True and trade.related is not None: if trade.related.status == "sold" and trade.status == "waiting": if last_price <= watch_price: response = exchanges[trade.exchange.name].buy(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "buying" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="buying", log_desc="Buying %s (related %s sold)." % (trade.pk, trade.related.pk)) trade_log.save() if settings.bd_debug == True: console_log("buying, when last price (%s) is lower or equal to watch price (%s) and related sell order was sold (buying at price: %s, amount: %s, currency: %s, related: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation, trade.related.pk)) # we are SELLING, when last price is higher or equal to watch price (lp_higher == True) and there is no related "buy" order elif trade.active == True and trade.lp_higher == True and trade.buy_or_sell == False and trade.related is None: if last_price >= watch_price: response = exchanges[trade.exchange.name].sell(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "selling" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="selling", log_desc="Selling %s." % (trade.pk)) trade_log.save() if settings.bd_debug == True: console_log("selling, when last price (%s) is higher or equal to watch price (%s) and there is no related buy order (selling at price: %s, amount: %s, currency: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation)) # we are SELLING, when last price is lower or equal to watch price (lp_higher == False) and there is no related "buy" order elif trade.active == True and trade.lp_higher == False and trade.buy_or_sell == False and trade.related is None: if last_price <= watch_price: response = exchanges[trade.exchange.name].sell(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "selling" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="selling", log_desc="Selling %s." % (trade.pk)) trade_log.save() if settings.bd_debug == True: console_log("selling, when last price (%s) is lower or equal to watch price (%s) and there is no related buy order (selling at price: %s, amount: %s, currency: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation)) # we are SELLING, when last price is higher or equal to watch price (lp_higher == True) and related "buy" order has been fully "bought" elif trade.active == True and trade.lp_higher == True and trade.buy_or_sell == False and trade.related is not None: if trade.related.status == "bought" and trade.status == "waiting": if last_price >= watch_price: response = exchanges[trade.exchange.name].sell(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "selling" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="selling", log_desc="Selling %s (related %s bought)." % (trade.pk, trade.related.pk)) trade_log.save() if settings.bd_debug == True: console_log("selling, when last price (%s) is higher or equal to watch price (%s) and related buy was bought (selling at price: %s, amount: %s, currency: %s, related: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation, trade.related.pk)) # we are SELLING, when last price is lower or equal to watch price and related "buy" order has been fully "bought" elif trade.active == True and trade.lp_higher == False and trade.buy_or_sell == False and trade.related is not None: if trade.related.status == "bought" and trade.status == "waiting": if last_price <= watch_price: response = exchanges[trade.exchange.name].sell(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "selling" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="selling", log_desc="Selling %s (related %s bought)." % (trade.pk, trade.related.pk)) trade_log.save() if settings.bd_debug == True: console_log("selling, when last price (%s) is lower or equal to watch price (%s) and related buy was bought (selling at price: %s, amount: %s, currency: %s, related: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation, trade.related.pk)) elif trade.active == True and trade.lp_higher == False and trade.buy_or_sell == False and trade.related is not None: if trade.related.status == "bought" and trade.status == "waiting": if last_price <= watch_price: response = exchanges[trade.exchange.name].sell(trade.price, trade.amount, trade.currency.abbreviation) if response and response is not None: trade.active = False trade.status = "selling" trade.exchange_oid = response trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="selling", log_desc="Selling %s (related %s bought)." % (trade.pk, trade.related.pk)) trade_log.save() if settings.bd_debug == True: console_log("selling, when last price (%s) is lower or equal to watch price (%s) and related buy was bought (selling at price: %s, amount: %s, currency: %s, related: %s)" % (last_price, trade.watch_price, trade.price, trade.amount, trade.currency.abbreviation, trade.related.pk)) except: raise
def check_status(trades, orders): for trade in trades: found = None for order in orders: if str(trade.exchange_oid) == str(order[u"oid"]): found = order break if found is not None: if trade.status == "selling" and found[u"status"] == u"invalid": trade.status = "not enough funds" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="not enough funds", log_desc="Not enough funds for trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("not enoguh funds for sell at price: %s, amount: %s, currency: %s, trade: %s" % (trade.price, trade.amount, trade.currency.abbreviation, trade.pk)) if trade.status == "buying" and found[u"status"] == u"invalid": trade.status = "not enough funds" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="not enough funds", log_desc="Not enough funds for trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("not enoguh funds for buy at price: %s, amount: %s, currency: %s, trade: %s" % (trade.price, trade.amount, trade.currency.abbreviation, trade.pk)) else: if trade.status == "selling": trade.status = "sold" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="sold", log_desc="Sold trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("sold %s bitcoins at %s %s" % (trade.amount, trade.price, trade.currency.abbreviation)) elif trade.status == "buying": trade.status = "bought" trade.save() trade_log = TradeLog(created_by=trade.user, trade=trade, log="bought", log_desc="Bought trade %s." % (trade.pk)) trade_log.save() if (settings.bd_debug == True): console_log("bought %s bitcoins at %s %s" % (trade.amount, trade.price, trade.currency.abbreviation)) if trade.exchange_oid is not None and trade.completed == False and (trade.status == "buying" or trade.status == "bought" or trade.status == "selling" or trade.status == "sold"): if (settings.bd_debug == True): if trade.status == "buying" or trade.status == "selling": console_log("trade %s at price %s, amount %s and currency %s is still not being completed, so we will check for completed transactions" % (trade.pk, trade.price, trade.amount, trade.currency.abbreviation)) elif trade.status == "bought" or trade.status == "sold": console_log("trade %s at price %s, amount %s and currency %s was fully executed, but we do not have a final sum of money we got/spent for the trade, so we will do this right now" % (trade.pk, trade.price, trade.amount, trade.currency.abbreviation)) exchanges[trade.exchange.name].order = None exchanges[trade.exchange.name].order = exchanges[trade.exchange.name].get_order(trade) # isinstance not working properly, so we "hack" a little bit if hasattr(exchanges[trade.exchange.name].order, "sum_price") and hasattr(exchanges[trade.exchange.name].order, "sum_amount"): trade.total_price = exchanges[trade.exchange.name].order.sum_price trade.total_amount = exchanges[trade.exchange.name].order.sum_amount if (trade.status == "bought" or trade.status == "sold"): if (settings.bd_debug == True): console_log("trade %s at price %s, amount %s and currency %s completed" % (trade.pk, trade.price, trade.amount, trade.currency.abbreviation)) trade.completed = True trade.save() """