def stop(self, symbol, quantity, orderId, last_price): # If the target is not reached, stop-loss. stop_order = Orders.get_order(symbol, orderId) stopprice = self.calc(float(stop_order['price'])) lossprice = stopprice - (stopprice * self.stop_loss / 100) status = stop_order['status'] # Order status if status == 'NEW' or status == 'PARTIALLY_FILLED': if self.cancel(symbol, orderId): # Stop loss if last_price >= lossprice: sello = Orders.sell_market(symbol, quantity) print('Stop-loss, sell market, %s' % (last_price)) sell_id = sello['orderId'] if sello == True: return True else: # Wait a while after the sale to the loss. time.sleep(self.WAIT_TIME_STOP_LOSS) statusloss = sello['status'] if statusloss != 'NEW': print('Stop-loss, sold') return True else: self.cancel(symbol, sell_id) return False else: sello = Orders.sell_limit(symbol, quantity, lossprice) print('Stop-loss, sell limit, %s' % (lossprice)) time.sleep(self.WAIT_TIME_STOP_LOSS) statusloss = sello['status'] if statusloss != 'NEW': print('Stop-loss, sold') return True else: self.cancel(symbol, sell_id) return False else: print( 'Cancel did not work... Might have been sold before stop loss...' ) return True elif status == 'FILLED': self.order_id = 0 self.order_data = None print('Order filled') return True else: return False
def stop(self, symbol, quantity, orderId, last_price): # If the target is not reached, stop-loss. stop_order = Orders.get_order(symbol, orderId) stopprice = self.calc(float(stop_order['price'])) lossprice = stopprice - (stopprice * self.stop_loss / 100) status = stop_order['status'] # Order status if status == 'NEW' or status == 'PARTIALLY_FILLED': if self.cancel(symbol, orderId): # Stop loss if last_price >= lossprice: sello = Orders.sell_market(symbol, quantity) #print('Stop-loss, sell market, %s' % (last_price)) self.logger.info('Stop-loss, sell market, %s' % (last_price)) sell_id = sello['orderId'] if sello == True: return True else: # Wait a while after the sale to the loss. time.sleep(self.WAIT_TIME_STOP_LOSS) statusloss = sello['status'] if statusloss != 'NEW': print('Stop-loss, sold') self.logger.info('Stop-loss, sold') return True else: self.cancel(symbol, sell_id) return False else: sello = Orders.sell_limit(symbol, quantity, lossprice) print('Stop-loss, sell limit, %s' % (lossprice)) time.sleep(self.WAIT_TIME_STOP_LOSS) statusloss = sello['status'] if statusloss != 'NEW': print('Stop-loss, sold') return True else: self.cancel(symbol, sell_id) return False else: print('Cancel did not work... Might have been sold before stop loss...') return True elif status == 'FILLED': self.order_id = 0 self.order_data = None print('Order filled') return True else: return False
def sell(self, symbol, quantity, orderId, sell_price, last_price): ''' The specified limit will try to sell until it reaches. If not successful, the order will be canceled. ''' buy_order = Orders.get_order(symbol, orderId) if buy_order['status'] == 'FILLED' and buy_order['side'] == 'BUY': print('Buy order filled... Try sell...') else: time.sleep(self.WAIT_TIME_CHECK_BUY_SELL) if buy_order['status'] == 'FILLED' and buy_order['side'] == 'BUY': print('Buy order filled after 0.1 second... Try sell...') elif buy_order['status'] == 'PARTIALLY_FILLED' and buy_order[ 'side'] == 'BUY': print( 'Buy order partially filled... Try sell... Cancel remaining buy...' ) self.cancel(symbol, orderId) else: self.cancel(symbol, orderId) print('Buy order fail (Not filled) Cancel order...') self.order_id = 0 return sell_order = Orders.sell_limit(symbol, quantity, sell_price) sell_id = sell_order['orderId'] print('Sell order create id: %d' % sell_id) time.sleep(self.WAIT_TIME_CHECK_SELL) if sell_order['status'] == 'FILLED': print('Sell order (Filled) Id: %d' % sell_id) print('LastPrice : %.8f' % last_price) print('Profit: %%%s. Buy price: %.8f Sell price: %.8f' % (self.option.profit, float(sell_order['price']), sell_price)) self.order_id = 0 self.order_data = None return ''' If all sales trials fail, the grievance is stop-loss. ''' if self.stop_loss > 0: # If sell order failed after 5 seconds, 5 seconds more wait time before selling at loss time.sleep(self.WAIT_TIME_CHECK_SELL) if self.stop(symbol, quantity, sell_id, last_price): if Orders.get_order(symbol, sell_id)['status'] != 'FILLED': print('We apologize... Sold at loss...') else: print( 'We apologize... Cant sell even at loss... Please sell manually... Stopping program...' ) self.cancel(symbol, sell_id) exit(1) while (sell_status != 'FILLED'): time.sleep(self.WAIT_TIME_CHECK_SELL) sell_status = Orders.get_order(symbol, sell_id)['status'] lastPrice = Orders.get_ticker(symbol) print('Status: %s Current price: %.8f Sell price: %.8f' % (sell_status, lastPrice, sell_price)) print('Sold! Continue trading...') self.order_id = 0 self.order_data = None
try: self.sell_qty = self.format_quantity( float(self.all_qty - self.original_qty)) except Exception, error: self.sell_qty = 0 if quantity < self.sell_qty: quantity = self.format_quantity(float(self.sell_qty)) print('Quantity: %s' % (quantity)) print('Self Quantity: %s' % (self.sell_qty)) flago = 0 sub = quantity * 0.005 while (flago != 1): sleep(1) try: sell_id = Orders.sell_limit(symbol, quantity, sell_price)['orderId'] print('Quantity: %s' % (quantity)) except Exception, error: quantity = self.format_quantity(float(quantity - sub)) print('Quantity: %s' % (quantity)) else: flago = 1 print self.log_wrap("Order placed. Confirming...") sleep(1) print self.log_wrap("Sell order created") if Orders.get_order(symbol, sell_id)['status'] == 'FILLED': print self.log_wrap('Sell order (Filled) id: %d' % sell_id) print self.log_wrap('LastPrice : %.8f' % last_price)
def sell(self, symbol, quantity, orderId, sell_price, last_price): ''' The specified limit will try to sell until it reaches. If not successful, the order will be canceled. ''' buy_order = Orders.get_order(symbol, orderId) if buy_order['status'] == 'FILLED' and buy_order['side'] == 'BUY': print('Buy order filled... Try sell...') else: time.sleep(self.WAIT_TIME_CHECK_BUY_SELL) if buy_order['status'] == 'FILLED' and buy_order['side'] == 'BUY': print('Buy order filled after 0.1 second... Try sell...') elif buy_order['status'] == 'PARTIALLY_FILLED' and buy_order['side'] == 'BUY': print('Buy order partially filled... Try sell... Cancel remaining buy...') self.cancel(symbol, orderId) else: self.cancel(symbol, orderId) print('Buy order fail (Not filled) Cancel order...') self.order_id = 0 return sell_order = Orders.sell_limit(symbol, quantity, sell_price) sell_id = sell_order['orderId'] print('Sell order create id: %d' % sell_id) time.sleep(self.WAIT_TIME_CHECK_SELL) if sell_order['status'] == 'FILLED': print('Sell order (Filled) Id: %d' % sell_id) print('LastPrice : %.8f' % last_price) print('Profit: %%%s. Buy price: %.8f Sell price: %.8f' % (self.option.profit, float(sell_order['price']), sell_price)) self.order_id = 0 self.order_data = None return ''' If all sales trials fail, the grievance is stop-loss. ''' if self.stop_loss > 0: # If sell order failed after 5 seconds, 5 seconds more wait time before selling at loss time.sleep(self.WAIT_TIME_CHECK_SELL) if self.stop(symbol, quantity, sell_id, last_price): if Orders.get_order(symbol, sell_id)['status'] != 'FILLED': print('We apologize... Sold at loss...') else: print('We apologize... Cant sell even at loss... Please sell manually... Stopping program...') self.cancel(symbol, sell_id) exit(1) while (sell_status != 'FILLED'): time.sleep(self.WAIT_TIME_CHECK_SELL) sell_status = Orders.get_order(symbol, sell_id)['status'] lastPrice = Orders.get_ticker(symbol) print('Status: %s Current price: %.8f Sell price: %.8f' % (sell_status, lastPrice, sell_price)) print('Sold! Continue trading...') self.order_id = 0 self.order_data = None
def sell(self, symbol, quantity, orderId, sell_price, last_price): ''' The specified limit will try to sell until it reaches. If not successful, the order will be canceled. ''' buy_order = Orders.get_order(symbol, orderId) if not buy_order: print ("SERVER DELAY! Rechecking...") return if buy_order['status'] == 'FILLED' and buy_order['side'] == "BUY": print ("Buy order filled... Try sell...") else: time.sleep(self.WAIT_TIME_CHECK_BUY) buy_order = Orders.get_order(symbol, orderId) if buy_order['status'] == 'FILLED' and buy_order['side'] == "BUY": print ("Buy order filled after 0.5 second... Try sell...") elif buy_order['status'] == 'PARTIALLY_FILLED' and buy_order['side'] == "BUY": print ("Buy order partially filled... Wait 1 more second...") quantity = self.check_partial_order(symbol, orderId, sell_price) else: self.cancel(symbol, orderId) print ("Buy order fail (Not filled) Cancel order...") time.sleep(self.WAIT_TIME_BUY_SELL) buy_order = Orders.get_order(symbol, orderId) if buy_order['status'] == 'FILLED': print ("Binance server delayed! Try sell...") elif buy_order['status'] == 'PARTIALLY_FILLED': print ("Binance server delayed! Try sell...") print ("Buy order partially filled... Wait 1 more second...") quantity = self.check_partial_order(symbol, orderId, sell_price) else: self.bot_status = "cancel" return sell_id = Orders.sell_limit(symbol, quantity, sell_price)['orderId'] print ('Sell order create id: %d' % sell_id) # 5 seconds wait time before checking sell order time.sleep(self.WAIT_TIME_CHECK_SELL) if Orders.get_order(symbol, sell_id)['status'] == 'FILLED': print ('Sell order (Filled) id: %d' % sell_id) print ('LastPrice : %.8f' % last_price) print ('Profit: %%%s. Buy price: %.8f Sell price: %.8f' % (self.option.profit, float(buy_order['price']), sell_price)) self.order_id = 0 self.bot_status = "sell" return ''' If all sales trials fail, the grievance is stop-loss. ''' if self.stop_loss > 0: # If sell order failed after 5 seconds, 5 seconds more wait time before selling at loss print ('Not sold after 5 seconds, wait 5 more seconds...') time.sleep(self.WAIT_TIME_CHECK_SELL) self.stop(symbol, quantity, sell_id, sell_price) else: sell_status = 'NEW' while (sell_status != "FILLED"): time.sleep(self.WAIT_TIME_CHECK_HOLD) sell_status = Orders.get_order(symbol, sell_id)['status'] lastPrice = float(Orders.get_ticker(symbol)['lastPrice']) print ('Status: %s Current price: %s Sell price: %s' % (sell_status, lastPrice, sell_price)) print ('Sold! Continue trading...') self.order_id = 0 self.bot_status = "sell"
def stop(self, symbol, quantity, orderId, sell_price): # If the target is not reached, stop-loss. stop_order = Orders.get_order(symbol, orderId) if float(stop_order['executedQty']) > 0: quantity = self.format_quantity(float(stop_order['executedQty'])) lossprice = sell_price - (sell_price * self.stop_loss / 100) status = stop_order['status'] # Order status if status == 'NEW': if self.cancel(symbol, orderId): # Stop loss lastBid, lastAsk = Orders.get_order_book(symbol) if lastAsk <= lossprice: sello = Orders.sell_market(symbol, quantity) print ('Stop-loss, sell market, %s' % (lastAsk)) sell_id = sello['orderId'] if sello == True: return True else: # Wait a while after the sale to the loss. time.sleep(self.WAIT_TIME_STOP_LOSS) statusloss = Orders.get_order(symbol, sell_id)['status'] if statusloss != 'NEW': print ('Stop-loss, sold') return True else: return False else: sello = Orders.sell_limit(symbol, quantity, lossprice) sell_id = sello['orderId'] print ('Stop-loss, sell limit, %s' % (lossprice)) time.sleep(self.WAIT_TIME_STOP_LOSS) statusloss = Orders.get_order(symbol, sell_id)['status'] if statusloss != 'NEW': print ('Stop-loss, sold') return True else: return False else: print ('Cancel did not work... Might have been sold before stop loss...') return True elif status == 'PARTIALLY_FILLED': self.order_id = 0 print ('Sell partially filled, hold sell position to prevent dust coin. Continue trading...') time.sleep(self.WAIT_TIME_CHECK_SELL) return True elif status == 'FILLED': self.order_id = 0 print('Order filled before sell at loss!') return True else: return False
def sell(self, symbol, quantity, orderId, sell_price, last_price): ''' The specified limit will try to sell until it reaches. If not successful, the order will be canceled. ''' self.partial_status = None #time.sleep(self.WAIT_TIME_CHECK_BUY) confirm = False cancel_flag = True seconds = 0 while not confirm: sleep(1) seconds += 1 buy_order = Orders.get_order(symbol, orderId) if not buy_order: print(self.log_wrap("SERVER DELAY! Rechecking...")) return if buy_order['status'] == 'FILLED' and buy_order['side'] == "BUY": print(self.log_wrap("Buy order filled... Try sell...")) self.total_buy = self.total_buy + 1 confirm = True elif buy_order['status'] == 'PARTIALLY_FILLED' and buy_order[ 'side'] == "BUY": print( self.log_wrap( "Buy order partially filled... Wait 1 more second...")) quantity = self.check_partial_order(symbol, orderId, sell_price) confirm = True else: cancel_flag = False if seconds == self.WAIT_TIME_CHECK_BUY: confirm = True if not cancel_flag: flago = 0 while (flago != 1): try: orderId = (buy_order['orderId']) except KeyError: print(self.log_wrap("Keyerror")) else: flago = 1 self.cancel(symbol, orderId) print(self.log_wrap("Buy order fail (Not filled) Cancel order...")) sleep(5) print(self.log_wrap("Checking..")) order_status = None order_side = None check_order = Orders.get_order(symbol, orderId) flago = 0 while (flago != 1): try: order_status = (check_order['status']) except KeyError: print(self.log_wrap("Keyerror")) check_order = Orders.get_order(symbol, orderId) else: flago = 1 flago = 0 while (flago != 1): try: order_side = (check_order['side']) except KeyError: print(self.log_wrap("Keyerror")) check_order = Orders.get_order(symbol, orderId) else: flago = 1 print( self.log_wrap( 'Binance Order Status: %s, Binance Order Side: %s, Partial status: %s' % (order_status, order_side, self.partial_status))) if order_status == "CANCELED": if self.partial_status == None: self.bot_status = "cancel" self.order_id = 0 return sell_id = None try: self.all_qty = self.format_quantity( float(Orders.get_asset(symbol)['free'])) except Exception as error: self.all_qty = 0 try: self.sell_qty = self.format_quantity( float(self.all_qty - self.original_qty)) except Exception as error: self.sell_qty = 0 if quantity < self.sell_qty: quantity = self.format_quantity(float(self.sell_qty)) flago = 0 while (flago != 1): sleep(1) try: sell_id = Orders.sell_limit(symbol, quantity, sell_price)['orderId'] except Exception as error: quantity = quantity - 1 else: flago = 1 print(self.log_wrap("Order placed. Confirming...")) sleep(1) print(self.log_wrap("Sell order created")) if Orders.get_order(symbol, sell_id)['status'] == 'FILLED': print(self.log_wrap('Sell order (Filled) id: %d' % sell_id)) print(self.log_wrap('LastPrice : %.8f' % last_price)) print( self.log_wrap( 'Profit: %%%s. Buy price: %.8f Sell price: %.8f' % (self.option.profit, float( buy_order['price']), sell_price))) self.order_id = 0 self.bot_status = "sell" time.sleep(self.WAIT_TIME_CHECK_SELL) return ''' If all sales trials fail, the grievance is stop-loss. ''' if self.stop_loss > 0: flag = 0 while (flag != 1): # If sell order failed after 5 seconds, 5 seconds more wait time before selling at loss time.sleep(self.WAIT_TIME_CHECK_HOLD) ticker = Orders.get_ticker(symbol) flag1 = 0 while (flag1 != 1): try: lastPrice = float(ticker['lastPrice']) except Exception as error: ticker = Orders.get_ticker(symbol) else: flag1 = 1 break lossprice = sell_price - (sell_price * self.stop_loss / 100) print(self.log_wrap('Hold...')) print(self.log_wrap('LastPrice : %.8f' % last_price)) print(self.log_wrap('Stop-loss, sell limit, %s' % (lossprice))) if Orders.get_order(symbol, sell_id)['status'] == 'FILLED': self.order_id = 0 self.bot_status = "sell" flag = 1 if lastPrice <= lossprice: flag = 1 print(self.log_wrap('Waiting to stop loss...')) time.sleep(self.WAIT_TIME_CHECK_SELL) self.stop(symbol, quantity, sell_id, sell_price) self.total_stoploss = self.total_stoploss + 1 time.sleep(self.WAIT_TIME_STOP_LOSS) print(self.log_wrap('Sold! Continue trading...')) self.order_id = 0 self.bot_status = "sell" else: sell_status = 'NEW' while (sell_status != "FILLED"): time.sleep(self.WAIT_TIME_CHECK_HOLD) sells = Orders.get_order(symbol, sell_id) while True: try: sell_status = sells['status'] except Exception as error: sells = Orders.get_order(symbol, sell_id) else: break ticker = Orders.get_ticker(symbol) while True: try: lastPrice = float(ticker['lastPrice']) except Exception as error: ticker = Orders.get_ticker(symbol) else: print( self.log_wrap( 'Status: %s Current price: %s Sell price: %s' % (sell_status, lastPrice, sell_price))) break print(self.log_wrap('Sold! Continue trading...')) self.total_sell = self.total_sell + 1 order_id = 0 self.order_id = 0 self.bot_status = "sell" time.sleep(self.WAIT_TIME_CHECK_SELL)
def sell(self, symbol, quantity, orderId, sell_price, last_price): ''' The specified limit will try to sell until it reaches. If not successful, the order will be canceled. ''' buy_order = Orders.get_order(symbol, orderId) if buy_order['status'] == 'FILLED' and buy_order['side'] == "BUY": print("Buy order filled... Try sell...") invalidAttempts = 0 while invalidAttempts < self.INVALID_ATTEMPTS_LIMIT: # Format quantity stepsize = quantity % float(self.step_size) quantity = quantity - stepsize sell_order = Orders.sell_limit(symbol, quantity, sell_price) print('Sell order create Id: %d' % orderId) sell_id = sell_order['orderId'] if sell_order['status'] == 'FILLED': print('Sell order (Filled) Id: %d' % orderId) print('LastPrice : %.8f' % last_price) print('Profit: %%%s. Buy price: %.8f Sell price: %.8f' % (self.option.profit, float( sell_order['price']), sell_price)) self.order_id = 0 self.order_data = 0 self.INVALID_ATTEMPTS_LIMIT = 0 break if sell_id != None: time.sleep(self.WAIT_TIME_BUY_SELL) ''' If all sales trials fail, the grievance is stop-loss. ''' if self.stop_loss > 0: if self.stop(symbol, quantity, sell_id): break else: continue invalidAttempts = 0 break else: invalidAttempts += 1 continue if invalidAttempts != 0: print("invalidAttempts %s" % (invalidAttempts)) self.cancel(symbol, orderId)