def get_balance(self): """ For future use. """ if not self.key or self.key is None: console_log("mtgox: key not set; check settings.py") return if not self.secret or self.secret is None: console_log("mtgox: secret not set; check settings.py") return params = [(u"nonce", self._create_nonce())] headers = { 'Rest-Key': self.key, 'Rest-Sign': base64.b64encode( str( hmac.new(base64.b64decode(self.secret), urllib.urlencode(params), hashlib.sha512).digest())) } response = self._send_request(self.balance_url, params, headers) if response and "result" in response and response[ "result"] == "success": return response return None
def get_order(self, trade): """ Method gets particular order. """ if not self.key or self.key is None: console_log("mtgox: key not set; check settings.py") return if not self.secret or self.secret is None: console_log("mtgox: secret not set; check settings.py") return order_type = "" if trade.buy_or_sell == True: order_type = "bid" elif trade.buy_or_sell == False: order_type = "ask" params = [("nonce", self._create_nonce()), ("order", trade.exchange_oid), ("type", order_type)] headers = { 'Rest-Key': self.key, 'Rest-Sign': base64.b64encode( str( hmac.new(base64.b64decode(self.secret), urllib.urlencode(params), hashlib.sha512).digest())) } response = self._send_request(self.order_url, params, headers) if response and u"result" in response and response[ u"result"] == u"success": order = Order() if u"trades" in response[u"return"]: order.trades = response[u"return"][u"trades"] sum_price = 0 sum_amount = 0 for exchange_trade in response[u"return"]["trades"]: if str(trade.currency) == str(exchange_trade[u"currency"]): sum_price += Decimal( exchange_trade[u"amount"][u"value"]) * Decimal( (exchange_trade[u"price"][u"value"])) sum_amount += Decimal( exchange_trade[u"amount"][u"value"]) order.sum_price = sum_price order.sum_amount = sum_amount return order elif response and u"result" in response and response[ u"result"] == u"error": return {"error": response[u"error"]} return None
def get_order(self, trade): """ Method gets particular order. """ if not self.key or self.key is None: console_log("mtgox: key not set; check settings.py") return if not self.secret or self.secret is None: console_log("mtgox: secret not set; check settings.py") return order_type = "" if trade.buy_or_sell == True: order_type = "bid" elif trade.buy_or_sell == False: order_type = "ask" params = [ ("nonce", self._create_nonce()), ("order", trade.exchange_oid), ("type", order_type) ] headers = { 'Rest-Key': self.key, 'Rest-Sign': base64.b64encode(str(hmac.new(base64.b64decode(self.secret), urllib.urlencode(params), hashlib.sha512).digest())) } response = self._send_request(self.order_url, params, headers) if response and u"result" in response and response[u"result"] == u"success": order = Order() if u"trades" in response[u"return"]: order.trades = response[u"return"][u"trades"] sum_price = 0 sum_amount = 0 for exchange_trade in response[u"return"]["trades"]: if str(trade.currency) == str(exchange_trade[u"currency"]): sum_price += Decimal(exchange_trade[u"amount"][u"value"]) * Decimal((exchange_trade[u"price"][u"value"])) sum_amount += Decimal(exchange_trade[u"amount"][u"value"]) order.sum_price = sum_price order.sum_amount = sum_amount return order elif response and u"result" in response and response[u"result"] == u"error": return {"error": response[u"error"]} return None
def sell(self, price, amount, currency): """ ask == sell """ if not self.key or self.key is None: console_log("mtgox: key not set; check settings.py") return if not self.secret or self.secret is None: console_log("mtgox: secret not set; check settings.py") return price = self._to_int_price(price, currency) amount = self._to_int_amount(amount) if not price or price is None: console_log("there is no conversion forumla for currency %s" % (currency)) return None if not amount or amount is None: return None self.sell_url["url"] = self._change_currency_url(self.sell_url["url"], currency) params = [ ("nonce", self._create_nonce()), ("amount_int", str(amount)), ("price_int", str(price)), ("type", "ask") ] headers = { 'Rest-Key': self.key, 'Rest-Sign': base64.b64encode(str(hmac.new(base64.b64decode(self.secret), urllib.urlencode(params), hashlib.sha512).digest())) } response = self._send_request(self.sell_url, params, headers) if response and u"result" in response and response[u"result"] == u"success": return response[u"return"] return None
def get_orders(self): """ Method gets open orders. """ if not self.key or self.key is None: console_log("mtgox: key not set; check settings.py") return if not self.secret or self.secret is None: console_log("mtgox: secret not set; check settings.py") return params = [ (u"nonce", self._create_nonce()) ] headers = { 'Rest-Key': self.key, 'Rest-Sign': base64.b64encode(str(hmac.new(base64.b64decode(self.secret), urllib.urlencode(params), hashlib.sha512).digest())) } response = self._send_request(self.open_orders_url, params, headers) if response and u"result" in response and response[u"result"] == u"success": return response[u"return"] return None
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 buy(self, price, amount, currency): """ bid == buy ask == sell Returns order ID if order was placed successfully. """ if not self.key or self.key is None: console_log("mtgox: key not set; check settings.py") return None if not self.secret or self.secret is None: console_log("mtgox: secret not set; check settings.py") return None price = self._to_int_price(price, currency) amount = self._to_int_amount(amount) if not price or price is None: console_log( "mtgox: there is no conversion forumla for currency %s" % (currency)) return None if not amount or amount is None: return None self.buy_url["url"] = self._change_currency_url( self.buy_url["url"], currency) params = [("nonce", self._create_nonce()), ("amount_int", str(amount)), ("price_int", str(price)), ("type", "bid")] headers = { 'Rest-Key': self.key, 'Rest-Sign': base64.b64encode( str( hmac.new(base64.b64decode(self.secret), urllib.urlencode(params), hashlib.sha512).digest())) } response = self._send_request(self.buy_url, params, headers) if response and u"result" in response and response[ u"result"] == u"success": return response[u"return"] return None
def buy(self, price, amount, currency): """ bid == buy ask == sell Returns order ID if order was placed successfully. """ if not self.key or self.key is None: console_log("bitstamp: key not set; check settings.py") return None if not self.secret or self.secret is None: console_log("bitstamp: secret not set; check settings.py") return None price = self._to_int_price(price, currency) amount = self._to_int_amount(amount) if not price or price is None: #console_log("there is no conversion forumla for currency %s" % (currency)) return None if not amount or amount is None: return None self.buy_url["url"] = self._change_currency_url(self.buy_url["url"], currency) nonce = str(self._create_nonce()) message = nonce + self.client_id + self.key signature = hmac.new(self.secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper() params = {'key': self.key, 'signature': signature, 'nonce': nonce, "amount": str(amount), "price": str(price)} response = self._send_request(self.buy_url, params) if response and u"id" in response: return response[u"id"] if response and u"error" in response: console_log("bitstamp: error returned from server with message: %s" % response[u"error"]) return None
def buy(self, price, amount, currency): """ bid == buy ask == sell Returns order ID if order was placed successfully. """ if not self.key or self.key is None: console_log("bitstamp: key not set; check settings.py") return None if not self.secret or self.secret is None: console_log("bitstamp: secret not set; check settings.py") return None price = self._to_int_price(price, currency) amount = self._to_int_amount(amount) if not price or price is None: #console_log("there is no conversion forumla for currency %s" % (currency)) return None if not amount or amount is None: return None self.buy_url["url"] = self._change_currency_url(self.buy_url["url"], currency) nonce = str(self._create_nonce()) message = nonce + self.client_id + self.key signature = hmac.new(self.secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper() params = {'key': self.key, 'signature': signature, 'nonce': nonce, "amount": str(amount), "price": str(price)} response = self._send_request(self.buy_url, params) if response and u"id" in response: return response[u"id"] if response and u"error" in response: console_log("bitstamp: error returned from server with message: %s" % response[u"error"]) return None
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
# we check for statuses of our orders all_my_trades = Trade.objects.all() my_open_orders = [] for exchange, exchange_data in exchanges.iteritems(): open_orders = exchanges[exchange].get_orders() if open_orders is not None: # when we will implement another exchange, we will need following line instead of the second following line # my_open_orders.append(open_orders) my_open_orders = open_orders if all_my_trades is not None and len(all_my_trades) > 0 and my_open_orders is not None: check_status(all_my_trades, my_open_orders) if (settings.bd_debug == True): console_log("just checked statuses of orders...") if (settings.bd_debug == True): console_log("sleeping %d seconds..." % settings.check_interval) except urllib2.URLError as err: console_log("could not connect to some url: %s" % (err)) pass except ValueError as (err): # got this error once console_log("No JSON object could be decoded ???: %s " % (err)) pass except: raise db.reset_queries()
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() """