I am getting the following error:
BinanceAPIException: APIError(code=-1003): Too much request weight used; current limit is 1200 request weight per 1 MINUTE. Please use the websocket for live updates to avoid polling the API.
Even though I managed to run a code for what I wanted, as below, I am quite unexperienced with python and binance api.
I understand its possible I need to change for the websocket? But i dont manage to find how to do this?
Thanks
from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager
client = Client(user_key, secret_key)
def get_price():
initial_price = {}
prices = client.get_all_tickers()
for coin in prices:
if PAIR_WITH in coin["symbol"] and all(item not in coin["symbol"] for item in FIATS):
initial_price[coin["symbol"]] = { "price": coin["price"], "time": datetime.now()}
return initial_price
def get_price24hrs():
lowest_price = {}
prices1 = client.get_ticker()
for coin in prices1:
if PAIR_WITH in coin["symbol"] and all(item not in coin["symbol"] for item in FIATS):
lowest_price[coin["symbol"]] = {"lowPrice": coin["lowPrice"], "time": datetime.now() - timedelta(minutes=1)}
return lowest_price
from math import pi
def check_below():
initial_price = get_price()
lowest_price = get_price24hrs()
volatile_coins = {}
for coin in initial_price:
if float(initial_price[coin]["price"])<=float(lowest_price[coin]["lowPrice"]):
volatile_coins[coin] = initial_price[coin]["price"]
return volatile_coins, len(volatile_coins), initial_price
def convert_volume():
volatile_coins, number_of_coins, initial_price = check_below()
lot_size = {}
volume = {}
for coin in volatile_coins:
try:
info = client.get_symbol_info(coin)
step_size = info["filters"][2]["stepSize"]
lot_size[coin] = step_size.index("1") - 1
if lot_size[coin] < 0:
lot_size[coin] = 0
except:
pass
print(initial_price[coin]["price"])
volume[coin] = float(QUANTITY) / float(initial_price[coin]["price"])
print(volume[coin])
if coin not in lot_size:
volume[coin] = float("{:.1f}".format(volume[coin]))
print(volume[coin])
else:
volume[coin] = float("{:.{}f}".format(volume[coin], lot_size[coin]))
print(volume[coin])
return volume, initial_price
def trade():
volume, last_price = convert_volume()
orders = {}
for coin in volume:
if coin not in coins_bought or coins_bought[coin] == None:
print(f" preparing to buy {volume[coin]} {coin}")
orders[coin] = client.get_all_orders(symbol=coin, limit=1)
try:
buy_limit = client.create_order(
symbol=coin,
side="BUY",
type="MARKET",
quantity=volume[coin]
)
except Exception as e:
print(e)
else:
print(f"Signal detected, but there is already an active trade on {coin}")
return orders, last_price, volume
def update_porfolio(orders, last_price, volume):
for coin in orders:
coins_bought[coin] = {
"symbol": orders[coin][0]["symbol"],
"orderid": orders[coin][0]["orderId"],
"timestamp": orders[coin][0]["time"],
"bought_at": last_price[coin]["price"],
"volume": volume[coin]
}
with open(coins_bought_file_path, "w") as file:
json.dump(coins_bought, file, indent=4)
def sell_coins():
last_price = get_price()
for coin in coins_bought:
TP = float(coins_bought[coin]["bought_at"]) + (float(coins_bought[coin]["bought_at"]) TAKE_PROFIT) / 100
SL = float(coins_bought[coin]["bought_at"]) - (float(coins_bought[coin]["bought_at"]) STOP_LOSS) / 100
if float(last_price[coin]["price"]) > TP or float(last_price[coin]["price"]) < SL:
print("TP or SL reached selling", {coins_bought[coin]["volume"]}, {coin})
else:
coins_bought[coin] = None
with open(coins_bought_file_path, "w") as file:
json.dump(coins_bought, file, indent=4)
else:
print("TP or SL not yet reached, not selling, {coin}, for now...")
if name == "main":
print("Press Ctrl-Q to stop the script")
for i in count():
orders, last_price, volume = trade()
update_porfolio(orders, last_price, volume)
sell_coins()
Hi.
I am getting the following error: BinanceAPIException: APIError(code=-1003): Too much request weight used; current limit is 1200 request weight per 1 MINUTE. Please use the websocket for live updates to avoid polling the API.
Even though I managed to run a code for what I wanted, as below, I am quite unexperienced with python and binance api. I understand its possible I need to change for the websocket? But i dont manage to find how to do this?
Thanks
from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager client = Client(user_key, secret_key)
def get_price(): initial_price = {} prices = client.get_all_tickers() for coin in prices: if PAIR_WITH in coin["symbol"] and all(item not in coin["symbol"] for item in FIATS): initial_price[coin["symbol"]] = { "price": coin["price"], "time": datetime.now()} return initial_price
def get_price24hrs(): lowest_price = {} prices1 = client.get_ticker() for coin in prices1: if PAIR_WITH in coin["symbol"] and all(item not in coin["symbol"] for item in FIATS): lowest_price[coin["symbol"]] = {"lowPrice": coin["lowPrice"], "time": datetime.now() - timedelta(minutes=1)} return lowest_price
from math import pi def check_below(): initial_price = get_price() lowest_price = get_price24hrs() volatile_coins = {}
for coin in initial_price: if float(initial_price[coin]["price"])<=float(lowest_price[coin]["lowPrice"]): volatile_coins[coin] = initial_price[coin]["price"] return volatile_coins, len(volatile_coins), initial_price
def convert_volume(): volatile_coins, number_of_coins, initial_price = check_below() lot_size = {} volume = {} for coin in volatile_coins: try: info = client.get_symbol_info(coin) step_size = info["filters"][2]["stepSize"] lot_size[coin] = step_size.index("1") - 1
return volume, initial_price
def trade(): volume, last_price = convert_volume() orders = {} for coin in volume: if coin not in coins_bought or coins_bought[coin] == None: print(f" preparing to buy {volume[coin]} {coin}") orders[coin] = client.get_all_orders(symbol=coin, limit=1)
try: buy_limit = client.create_order( symbol=coin, side="BUY", type="MARKET", quantity=volume[coin] ) except Exception as e: print(e) else: print(f"Signal detected, but there is already an active trade on {coin}") return orders, last_price, volume
def update_porfolio(orders, last_price, volume): for coin in orders: coins_bought[coin] = { "symbol": orders[coin][0]["symbol"], "orderid": orders[coin][0]["orderId"], "timestamp": orders[coin][0]["time"], "bought_at": last_price[coin]["price"], "volume": volume[coin] } with open(coins_bought_file_path, "w") as file: json.dump(coins_bought, file, indent=4)
def sell_coins(): last_price = get_price() for coin in coins_bought: TP = float(coins_bought[coin]["bought_at"]) + (float(coins_bought[coin]["bought_at"]) TAKE_PROFIT) / 100 SL = float(coins_bought[coin]["bought_at"]) - (float(coins_bought[coin]["bought_at"]) STOP_LOSS) / 100 if float(last_price[coin]["price"]) > TP or float(last_price[coin]["price"]) < SL: print("TP or SL reached selling", {coins_bought[coin]["volume"]}, {coin}) else: coins_bought[coin] = None with open(coins_bought_file_path, "w") as file: json.dump(coins_bought, file, indent=4) else: print("TP or SL not yet reached, not selling, {coin}, for now...")
if name == "main": print("Press Ctrl-Q to stop the script") for i in count(): orders, last_price, volume = trade() update_porfolio(orders, last_price, volume) sell_coins()