Пример #1
0
def trade(trades):
    for trade in trades:
        if trade.exchange.name in exchanges:
            last_price = exchanges[trade.exchange.name].get_last_price(trade.currency.abbreviation)
        else:
            continue

        if last_price is None: continue

        last_price = Decimal(last_price)

        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))
        except:
            raise
Пример #2
0
def trade(trades, last_price):
    if not 'ticker' in last_price: return
    if not 'last' in last_price['ticker']: return

    for trade in trades:
        try:
            # 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 Decimal(last_price['ticker']['last']) >= Decimal(trade.watch_price):
                    trade.active = False
                    trade.status = "buying"
                    trade.save()

                    response = mtgox.buy(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                    trade_log = TradeLog(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)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount))
                        # console_log(str(response))

            # 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 Decimal(last_price['ticker']['last']) <= Decimal(trade.watch_price):
                    trade.active = False
                    trade.status = "buying"
                    trade.save()

                    response = mtgox.buy(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                    trade_log = TradeLog(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)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount))
                        # console_log(str(response))

            # 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 Decimal(last_price['ticker']['last']) >= Decimal(trade.watch_price):
                        trade.active = False
                        trade.status = "buying"
                        trade.save()

                        response = mtgox.buy(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                        trade_log = TradeLog(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, related: %s)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount, trade.related.pk))
                            # console_log(str(response))

            # 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 Decimal(last_price['ticker']['last']) <= Decimal(trade.watch_price):
                        trade.active = False
                        trade.status = "buying"
                        trade.save()

                        response = mtgox.buy(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                        trade_log = TradeLog(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, related: %s)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount, trade.related.pk))
                            # console_log(str(response))

            # 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 Decimal(last_price['ticker']['last']) >= Decimal(trade.watch_price):
                    trade.active = False
                    trade.status = "selling"
                    trade.save()

                    response = mtgox.sell(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                    trade_log = TradeLog(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)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount))
                        # console_log(str(response))

            # 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 Decimal(last_price['ticker']['last']) <= Decimal(trade.watch_price):
                    trade.active = False
                    trade.status = "selling"
                    trade.save()

                    response = mtgox.sell(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                    trade_log = TradeLog(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)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount))
                        # console_log(str(response))

            # 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 Decimal(last_price['ticker']['last']) >= Decimal(trade.watch_price):
                        trade.active = False
                        trade.status = "selling"
                        trade.save()

                        response = mtgox.sell(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                        trade_log = TradeLog(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, related: %s)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount, trade.related.pk))
                            # console_log(str(response))

            # 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 Decimal(last_price['ticker']['last']) <= Decimal(trade.watch_price):
                        trade.active = False
                        trade.status = "selling"
                        trade.save()

                        response = mtgox.sell(str(Decimal(trade.price)), str(Decimal(trade.amount)))

                        trade_log = TradeLog(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, related: %s)" % (last_price['ticker']['last'], trade.watch_price, trade.price, trade.amount, trade.related.pk))
                            # console_log(str(response))
        except:
            raise
Пример #3
0
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()
            """
Пример #4
0
def check_status(trades, orders):
    for trade in trades:
        found = None
        for order in orders:
            if Decimal(trade.price) == Decimal(order["price"]):
                found = order
                break
        if found is not None:
            if trade.status == "selling" and found["type"] == "1" and found["status"] == "2":
                trade.status = "not enough funds"
                trade.save()

                trade_log = TradeLog(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 selling...")
            if trade.status == "buying" and found["type"] == "2" and found["status"] == "2":
                trade.status = "not enough funds"
                trade.save()

                trade_log = TradeLog(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 buying...")
        else:
            if trade.status == "selling":
                trade.status = "sold"
                trade.save()

                trade_log = TradeLog(trade=trade, log="sold", log_desc="Sold trade %s." % (trade.pk))
                trade_log.save()
                if (settings.bd_debug == True):
				    console_log("sold %s bitcoins for %s dollars" % (trade.amount, trade.price))
            elif trade.status == "buying":
                trade.status = "bought"
                trade.save()

                trade_log = TradeLog(trade=trade, log="bought", log_desc="Bought trade %s." % (trade.pk))
                trade_log.save()
                if (settings.bd_debug == True):
					console_log("bought %s bitcoins for %s dollars" % (trade.amount, trade.price))