def checkhist(id): write = open(os.path.join(os.getcwd(), "hist", id + "_hist.txt"), "a") for y in dict1[id][id + "_curr"]: if not y in dict1[id][id + "_hist"]: write.write(y + "\n") print("New Story: " + y) if not vars(args)["t"]: telegrambot.sendmsg(y) sendstats(id) write.close()
def updatesl(price, sl): while True: time.sleep(20) order = self.client.get_order(symbol=self.ticker, orderId=sl["orderId"]) currprice = float( self.client.get_symbol_ticker( symbol=self.ticker)["price"]) if (order["executedQty"] == order["origQty"] or order["status"] == "CANCELED"): tbot.sendmsg("SL was canceled or filled") if (self.ticker.endswith("BTC") and order["status"] != "CANCELED"): tbot.sendmsg("selling btc for usdt") print( self.client.order_market_sell( symbol="BTCUSDT", quantity=round( float(order["executedQty"]) * currprice, 6), newOrderRespType="FULL")) break elif currprice > price: price = currprice self.client.cancel_order(symbol=self.ticker, orderId=sl["orderId"]) time.sleep(1) sl = self.client.create_order( symbol=self.ticker, type="STOP_LOSS_LIMIT", quantity=quantity, stopPrice=roundprice(price * 0.97), side="SELL", price=roundprice(price * 0.96), timeInForce="GTC") updatesl(price, sl) else: continue
def prompt(): tbot.sendmsg("Start trailing SL? (y/n)") resp = getupdates() if resp == "y": tbot.sendmsg("Starting SL") return True elif resp == "n": tbot.sendmsg("Not starting SL") return False else: print("Wrong answer") prompt()
while True: try: for x in IDs: # generating current set of links for each item in IDs for y in getlinks.getlinks(dict1[x]["url"]): dict1[x][x + "_curr"].add(y) for x in IDs: # writing current content of _hist.txt files into _hist strings read = open(os.getcwd() + r"\\hist\\" + x + "_hist.txt", "r") dict1[x][x + "_hist"] = read.read() read.close() for x in IDs: # checking if items in _curr strings are already in _hist strings write = open(os.getcwd() + r"\\hist\\" + x + "_hist.txt", "a") for y in dict1[x][x + "_curr"]: if not y in dict1[x][x + "_hist"]: write.write(y + "\n") print("New Item: " + y) telegrambot.sendmsg(y) write.close() z += 1 print(z, file=log, flush=True) time.sleep(30) print("done sleeping", file=log, flush=True) except Exception as e: print("Error, retry in 20s") print(e, file=log, flush=True) time.sleep(20)
return True elif resp == "n": tbot.sendmsg("Not starting SL") return False else: print("Wrong answer") prompt() while True: try: message = getupdates() try: client = order.Order(message) # initialize Order instance resp = client.executetransaction() print(resp) if prompt(): t = threading.Thread(target=client.setsl, args=(resp,)) t.start() except order.FormatError: tbot.sendmsg("wrong format") except order.TickerError: tbot.sendmsg("wrong ticker") except order.BinanceAPIException as e: tbot.sendmsg(e.message) except Exception as e: print(e) time.sleep(10) continue
def setsl(self, resp): try: def roundprice(price): stepsize = self.client.get_symbol_info( self.ticker)["filters"][0]["tickSize"].find("1") - 1 return round(price, stepsize) def updatesl(price, sl): while True: time.sleep(20) order = self.client.get_order(symbol=self.ticker, orderId=sl["orderId"]) currprice = float( self.client.get_symbol_ticker( symbol=self.ticker)["price"]) if (order["executedQty"] == order["origQty"] or order["status"] == "CANCELED"): tbot.sendmsg("SL was canceled or filled") if (self.ticker.endswith("BTC") and order["status"] != "CANCELED"): tbot.sendmsg("selling btc for usdt") print( self.client.order_market_sell( symbol="BTCUSDT", quantity=round( float(order["executedQty"]) * currprice, 6), newOrderRespType="FULL")) break elif currprice > price: price = currprice self.client.cancel_order(symbol=self.ticker, orderId=sl["orderId"]) time.sleep(1) sl = self.client.create_order( symbol=self.ticker, type="STOP_LOSS_LIMIT", quantity=quantity, stopPrice=roundprice(price * 0.97), side="SELL", price=roundprice(price * 0.96), timeInForce="GTC") updatesl(price, sl) else: continue price = float(resp["fills"][0]["price"]) quantity = float(resp["executedQty"]) sl = self.client.create_order(symbol=self.ticker, type="STOP_LOSS_LIMIT", quantity=quantity, stopPrice=roundprice(price * 0.97), side="SELL", price=roundprice(price * 0.96), timeInForce="GTC") print(sl) updatesl(price, sl) except BinanceAPIException as e: # main loop doesn't catch exceptions in thread tbot.sendmsg(e)
def sendstats(id): if len(dict1[id]) != 1: telegrambot.sendmsg("https://tradingview.com/chart/?symbol=" + dict1[id]["_ticker"] + "USD")