def check_pump(): print("Looking for Buy Opportunities...") threading.Thread(target=check_sell).start() while True: try: open_orders = db.query(Trade).all() for symbol in symbols: r2 = requests.get( "https://api.binance.com/api/v1/ticker/24hr?symbol=" + symbol).json() prices[symbol] = '{0:.8f}'.format( round(float(r2['lastPrice']), 8)) pct_change = float(r2['priceChangePercent']) if round(float(prices[symbol]), 8) < float(prices[symbol])*(1 + expected_change_buy/100) \ and pct_change > expected_change_buy: # print(pct_change, expected_change_buy) current_symbol = symbol if current_symbol not in open_orders: min_price, min_Qty = calculate_min(current_symbol) temp_price = float( prices[symbol]) * final_buy_price_change final_buy_price = temp_price - (temp_price % float(min_price)) temp_quantity = buy_quantity_btc / float( final_buy_price) quantity = round((temp_quantity - ((temp_quantity % float(min_Qty)))), 8) try: trade = Trade.find(symbol) if trade is None: order = client.order_limit_buy( # Place order for buy symbol=current_symbol, recvWindow=1000, quantity='{0:.3f}'.format(float(quantity)), price='{0:.8f}'.format( float(final_buy_price))) print("Buy: " + symbol + ' at: ' + str('{0:.8f}'.format( float(prices[symbol]) * final_buy_price_change)) + " from " + str(prices[symbol]) + " Due to change % " + str(pct_change)) trade = Trade.get_or_create(symbol) trade.price = str('{0:.8f}'.format( float(prices[symbol]))) trade.orderId = order['orderId'] trade.quantity = str('{0:.3f}'.format( float(quantity))) db.commit() check_buy_status(symbol) else: continue except Exception as e: # print(e) pass time.sleep(5) except Exception as e: print(e)
def readmail(volume): time.sleep(1.5) m = imaplib.IMAP4_SSL(imap) m.login(user, pwd) m.select('"' + folder + '"') resp, items = m.search(None, "NOT SEEN SUBJECT tradingview") items = items[0].split() for emailid in items: resp, data = m.fetch(emailid, "(RFC822)") email_body = data[0][1] mail = email.message_from_bytes(email_body) ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') nonce = generate_nonce() direction = mail['Subject'].split()[3] if direction == "Buy": signal = "0" else: signal = "1" pair = mail['Subject'].split()[2] try: if "Close" not in direction: setup = Trade.get_or_create(pair) m.store(emailid, '+FLAGS', '\Seen') log(st + ' ' + direction + ' Triggered on ' + pair) if hedging == "0": if setup.nonce is not None: cnr(signal, volume, pair, nonce) else: trade(signal, volume, pair, nonce) else: trade(signal, volume, pair, nonce) # Close Trade else: direction = mail['Subject'].split()[4] setup = Trade.find(pair) if setup is not None: if setup.signal == direction: m.store(emailid, '+FLAGS', '\Seen') close(signal, volume, pair) log(st + " Closed trade on " + pair) except Exception as e: log(e)