更新投资组合JSON未获取数据

发布于 2025-02-02 17:30:23 字数 2001 浏览 3 评论 0原文

我对python非常不经验 我编写了代码来存储机器人购买的所有硬币,但JSON文件上没有任何更新。 我很确定有什么问题:S JSON文件是创建的,但始终保持空白

# coins that bought by the bot since its start
coins_bought = {}
# path to the saved coins_bought file
coins_bought_file_path = "coins_bought.json"


# if saved coins_bought json file exists then load it
if os.path.isfile(coins_bought_file_path):
 with open(coins_bought_file_path) as file:
  coins_bought = json.load(file)

def trade():
 #Place Buy market orders for each volatile coin found’’’
  volume, lowest_price = convert_volume()
  orders = {}
  for coin in volume:
# only buy if the there are no active trades on the coin
    if coin not in coins_bought or coins_bought[coin] == None:
      print(f" preparing to buy {volume[coin]} {coin}") 
      info = client.get_symbol_info(coin)
      step_size = info["filters"][0]["tickSize"]
      lot_size = step_size.index("1") - 1
      # try to create a real order if the test orders did not raise an exception
      try:
        buy_limit = client.futures_create_order(
        symbol=coin,
        side="BUY",
        type="LIMIT",
        price =round(float(lowest_price[coin]["lastPrice"])*0.985,lot_size),
        timeInForce="GTC",
        quantity=round(volume[coin],lot_size)
 )
# error handling here in case position cannot be placed
      except Exception as e:
       print(e)

    else:
      print(f"Signal detected, but there is already an active trade on {coin}")
  return orders, lowest_price, volume



def update_porfolio(orders, lowest_price, volume):
 #add every coin bought to our portfolio for tracking/selling later’’’
  for coin in orders:
    coins_bought[coin] = {
  "symbol": orders[coin][0]["symbol"],
  "orderid": orders[coin][0]["orderId"],
  "timestamp": orders[coin][0]["time"],
  "bought_at": lowest_price[coin]["lastPrice"],
  "volume": volume[coin]
 }
# save the coins in a json file in the same directory
  with open(coins_bought_file_path, "w") as file:
    json.dump(coins_bought, file, indent=4)

I am very unexperienced with python
I wrote the code to store all the coins the bot buys but nothing is updated on the JSON file.
I am pretty sure something must be wrong :S
The JSON file is created but it always stays blank

# coins that bought by the bot since its start
coins_bought = {}
# path to the saved coins_bought file
coins_bought_file_path = "coins_bought.json"


# if saved coins_bought json file exists then load it
if os.path.isfile(coins_bought_file_path):
 with open(coins_bought_file_path) as file:
  coins_bought = json.load(file)

def trade():
 #Place Buy market orders for each volatile coin found’’’
  volume, lowest_price = convert_volume()
  orders = {}
  for coin in volume:
# only buy if the there are no active trades on the coin
    if coin not in coins_bought or coins_bought[coin] == None:
      print(f" preparing to buy {volume[coin]} {coin}") 
      info = client.get_symbol_info(coin)
      step_size = info["filters"][0]["tickSize"]
      lot_size = step_size.index("1") - 1
      # try to create a real order if the test orders did not raise an exception
      try:
        buy_limit = client.futures_create_order(
        symbol=coin,
        side="BUY",
        type="LIMIT",
        price =round(float(lowest_price[coin]["lastPrice"])*0.985,lot_size),
        timeInForce="GTC",
        quantity=round(volume[coin],lot_size)
 )
# error handling here in case position cannot be placed
      except Exception as e:
       print(e)

    else:
      print(f"Signal detected, but there is already an active trade on {coin}")
  return orders, lowest_price, volume



def update_porfolio(orders, lowest_price, volume):
 #add every coin bought to our portfolio for tracking/selling later’’’
  for coin in orders:
    coins_bought[coin] = {
  "symbol": orders[coin][0]["symbol"],
  "orderid": orders[coin][0]["orderId"],
  "timestamp": orders[coin][0]["time"],
  "bought_at": lowest_price[coin]["lastPrice"],
  "volume": volume[coin]
 }
# save the coins in a json file in the same directory
  with open(coins_bought_file_path, "w") as file:
    json.dump(coins_bought, file, indent=4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文