DCAv2/utils/recreate_orders.py

83 lines
3.0 KiB
Python

'''
If the broker cancelled all your orders just because (THANK YOU KUCOIN), this script will resend all the orders and change the status dictionary values accordingly.
Note: since version 2024.07.08, both the safety and the take profit order are included in the status file; this will make resending the orders easier.
'''
import ccxt
import json
import sys
def gib_so_size(starting_order_size: float, so_number: int, scaling_factor: float) -> float:
'''
Returns the correct safety order size depending on the number
Scaling factor example: 5% = 0.0105
'''
order_size = starting_order_size
for _ in range(so_number):
order_size = order_size*scaling_factor*100
return order_size
with open(f"../configs/kucoin.json") as k:
config_file = json.load(k)
exchange_class = getattr(ccxt, "kucoin")
exchange = exchange_class({
"apiKey": config_file["key"],
"secret": config_file["secret"],
"password": config_file["password"],
"timeout": 30000,
"enableRateLimit": True
})
pair = sys.argv[1]
with open(f"../status/{pair}.status") as f:
status_contents = json.loads(f.read())
with open(f"../configs/{pair}.json") as c:
config = json.loads(c.read())
if not config["is_short"]:
buy_order_amount = gib_so_size(status_contents["order_size"],status_contents["so_amount"]+1,config["safety_order_scale"]) #Next safety order
buy_order_price = status_contents["next_so_price"] #Next safety order price
sell_order_amount = status_contents["base_bought"] #Take profit order
sell_order_price = status_contents["take_profit_price"] #Take profit order price
else:
sell_order_amount = gib_so_size(status_contents["order_size"],status_contents["so_amount"]+1,config["safety_order_scale"]) #Next safety order
sell_order_price = status_contents["next_so_price"] #Next safety order price
buy_order_amount = status_contents["base_bought"] #Take profit order
buy_order_price = status_contents["take_profit_price"] #Take profit order price
print(f"Pair: {pair}")
pair_mode = "Short" if config["is_short"] else "Long"
print(f"Mode: {pair_mode}")
print(f"Buy order amount: {buy_order_amount}")
print(f"Buy order price: {buy_order_price}")
print(f"Sell order amount: {sell_order_amount}")
print(f"Sell order price: {sell_order_price}")
input("Proceed? ")
print("Sending buy order")
buy_order = exchange.create_order(config["pair"],"limit","buy",buy_order_amount,buy_order_price)
print(f"Buy order id: {buy_order['id']}")
print("Sending sell order")
sell_order = exchange.create_order(config["pair"],"limit","sell",sell_order_amount,sell_order_price)
print(f"Sell order id: {sell_order['id']}")
if not config["is_short"]:
status_contents["tp_order_id"] = sell_order["id"]
status_contents["so_order_id"] = buy_order["id"]
else:
status_contents["tp_order_id"] = buy_order["id"]
status_contents["so_order_id"] = sell_order["id"]
json_object = json.dumps(status_contents, indent=4)
with open(f"../status/{pair}.status","w") as f:
f.write(json_object)