更新投资组合JSON未获取数据
我对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论