Improved docstrings.

This commit is contained in:
Nicolás Sánchez 2024-10-28 17:02:48 -03:00
parent a8a7bbcb03
commit d3eb8e0512
1 changed files with 298 additions and 8 deletions

306
main.py
View File

@ -1169,17 +1169,52 @@ def run_API():
###########################
def unwrapped_global_status():
'''
Returns the global status dictionary of the instance.
'''
return jsonify(global_status)
def unwrapped_return_worker_status(base,quote):
'''
Returns the individual status dictionary of the trader.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
Returns:
dict: The status dictionary of the trader.
'''
if f"{base}/{quote}" in worker_status:
return jsonify(worker_status[f"{base}/{quote}"])
return jsonify({"Error": "Worker does not exist"})
def unwrapped_return_all_worker_status():
'''
Returns the status dictionary of all traders.
Returns:
dict: The status dictionary of all traders.
'''
return jsonify(worker_status)
def unwrapped_add_pair(base,quote):
'''
Adds a trader to the instance.
Parameters:
base (str): The base currency of the trader.
quote (str): The quote currency of the trader.
Returns:
jsonified dictionary detailing the outcome of the operation.
'''
try:
#Check if the trader is already running
for x in running_instances:
@ -1221,8 +1256,20 @@ def unwrapped_add_pair(base,quote):
except Exception as e:
broker.logger.log_this(f"Exception while initializing new instance: {e}",1,f"{base}/{quote}")
return jsonify({"Error": "Error initializing new instance."})
def unwrapped_remove_pair(base,quote):
'''
Removes a trader from and instance
Parameters:
base (str): The base currency of the pair
quote (str): The quote currency of the pair
Returns:
jsonified dictionary detailing the outcome of the operation.
'''
try:
for x in running_instances:
if f"{base}/{quote}"==x.pair:
@ -1232,12 +1279,36 @@ def unwrapped_remove_pair(base,quote):
broker.logger.log_this(f"Exception while removing instance: {e}",1,f"{base}/{quote}")
return jsonify({"Error": "Halp"})
def unwrapped_restart_pair(base,quote):
'''
Restarts a trader instance
Parameters:
base (str): The base currency of the pair
quote (str): The quote currency of the pair
Returns:
jsonified dictionary detailing the outcome of the operation.
'''
if restart_pair_no_json(base,quote)==0:
return jsonify({"Success": "Pair restarted"})
return jsonify({"Error": "Halp"})
def unwrapped_import_pair(base,quote):
'''
Imports a previously running pair
Parameters:
base (str): The base currency of the pair
quote (str): The quote currency of the pair
Returns:
jsonified dictionary detailing the outcome of the operation.
'''
try:
import_instance(base+quote)
broker.add_pair_to_config(f"{base}{quote}")
@ -1248,11 +1319,20 @@ def unwrapped_import_pair(base,quote):
broker.logger.log_this(f"Exception while importing instance: {e}",1,f"{base}/{quote}")
return jsonify({"Error": "Error importing instance"})
def unwrapped_switch_to_long(base,quote,calculate_profits):
'''
Switches a pair to long mode.
If calculate_profits is 0, it does not calculate the profits
Parameters:
base (str): The base currency of the pair
quote (str): The quote currency of the pair
calculate_profits (int): 1 if you want to calculate the profits, 0 if you don't
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation
'''
ignore_old_long = int(calculate_profits)!=1
#Close trader and orders and pull info our of the orders
if f"{base}{quote}" not in broker.get_pairs():
@ -1268,10 +1348,19 @@ def unwrapped_switch_to_long(base,quote,calculate_profits):
return jsonify({"Success": "Pair switched to long mode"})
return jsonify({"Error": "Pair not found"})
def unwrapped_switch_to_short(base,quote):
'''
Switches a pair to short mode.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
Returns
jsonify: A jsonified dictionary detailing the outcome of the operation
'''
#Close trader and orders and pull info our of the orders
if f"{base}{quote}" not in broker.get_pairs():
return jsonify({"Error": "Pair not running"})
@ -1306,10 +1395,19 @@ def unwrapped_switch_to_short(base,quote):
return jsonify({"Error": "Can't initialize trader"})
return jsonify({"Error": "Halp"})
def unwrapped_load_old_long(base,quote):
'''
Loads an old_long file to the status dictionary of a trader
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
Returns
jsonify: A jsonified dictionary detailing the outcome of the operation
'''
#Load the file
try:
with open(f"{base}{quote}.oldlong") as ol:
@ -1333,10 +1431,20 @@ def unwrapped_load_old_long(base,quote):
return jsonify({"Success": "old_long file loaded to status_dict"})
return jsonify({"Error": "Pair not found"})
def unwrapped_view_old_long(base,quote,from_file):
'''
Returns the content of an old_long file
Parameters:
base (str): The base currency of the pair
quote (str): The quote currency of the pair
from_file (int): 1 if the file is to be loaded from the file system, 0 if it is to be loaded from the trader's status dictionary.
Returns:
jsonify: A jsonified dictionary containing the old_long info.
'''
try:
if int(from_file)==1:
with open(f"{base}{quote}.oldlong") as ol:
@ -1350,11 +1458,21 @@ def unwrapped_view_old_long(base,quote,from_file):
broker.logger.log_this(f"Exception while viewing old_long info: {e}",1,f"{base}/{quote}")
return jsonify({"Error": f"{e}"})
def unwrapped_add_safety_orders(base,quote,amount):
'''
Increases the amount of safety orders that a trader can use. Once the current deal is closed, the value returns to the one in the config
file.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
amount (str): The amount of safety orders to add.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation
'''
try:
for x in running_instances:
if f"{base}/{quote}"==x.pair:
@ -1372,10 +1490,20 @@ def unwrapped_add_safety_orders(base,quote,amount):
broker.logger.log_this(f"{e}",2,f"{base}/{quote}")
return jsonify({"Error": "Error adding safety orders"})
def unwrapped_mod_tp_level(base,quote,amount):
'''
Modifies the take profit percentage of a pair. It applies the new percentage only after a new TP order is sent.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
amount (str): The new take profit percentage.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation
'''
try:
for x in running_instances:
if f"{base}/{quote}"==x.pair:
@ -1386,10 +1514,18 @@ def unwrapped_mod_tp_level(base,quote,amount):
broker.logger.log_this("Error changing percentage. Ignoring...",2,f"{base}/{quote}")
return jsonify({"Error": "Error changing percentage"})
def unwrapped_mod_global_tp_level(amount):
'''
Modifies the take profit percentage of all pairs. It applies the new percentage only after a new TP order is sent.
Parameters:
amount (float): The new percentage to be used.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation
'''
for x in running_instances:
try:
x.config_dict["tp_level"]=float(amount)
@ -1398,10 +1534,19 @@ def unwrapped_mod_global_tp_level(amount):
broker.logger.log_this("Error changing percentage. Ignoring.",2)
return jsonify({"Success": "Success. The change will take effect when the next TP order is placed"})
def unwrapped_last_call(base,quote):
'''
Signals the trader to stop opening new deals once the current one is closed.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation
'''
try:
if f"{base}{quote}" in broker.get_pairs():
#read_config["pairs"].remove(base+quote)
@ -1416,10 +1561,20 @@ def unwrapped_last_call(base,quote):
except Exception:
return jsonify({"Error": "Halp"})
def unwrapped_deferred_last_call(base,quote,yyyymmdd):
'''
Programs the trader to not open new deals from a certain future date. Like a VCR.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
yyyymmdd (str): The date in YYYYMMDD format.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
year = yyyymmdd[:4]
month = yyyymmdd[4:6]
@ -1436,12 +1591,20 @@ def unwrapped_deferred_last_call(base,quote,yyyymmdd):
except Exception:
return jsonify({"Error": "Halp"})
def unwrapped_toggle_pause(base,quote):
'''
Toggles the pause flag of a trader.
When a trader is paused, no new safety orders are sent to the exchange and the take profit order is unmonitored.
Although it could be useful to close the trader if the tp order is filled anyway.
Parameters:
base (str): The base currency of the trader.
quote (str): The quote currency of the trader.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
toggle_pauses.append(f"{base}/{quote}")
for instance in running_instances:
@ -1453,9 +1616,13 @@ def unwrapped_toggle_pause(base,quote):
except Exception:
return jsonify({"Error": "Halp"})
def unwrapped_global_last_call():
'''
Signals all traders to stop opening new trades when the current ones closes.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
if broker.get_pairs!=[]:
@ -1473,12 +1640,21 @@ def unwrapped_global_last_call():
except Exception:
return jsonify({"Error": "Halp"})
def unwrapped_add_quote(base,quote,amount):
'''
Adds more funds to the deal, bringing down the average buy price in the meantime.
Adds more funds to the deal, bringing down the average buy price.
I do not recommend to use this, it's preferable to switch to short mode, but you do you.
Maybe it's more useful in a bull market's high volatility moment.
Parameters:
base (str): The base currency of the pair
quote (str): The quote currency of the pair
amount (float): The amount of quote currency to add
Returns:
json: A jsonified dictionary detailing the outcome of the operation.
'''
for x in running_instances:
if f"{base}/{quote}"==x.pair:
if x.is_short:
@ -1532,9 +1708,13 @@ def unwrapped_add_quote(base,quote,amount):
return jsonify({"Success": "Quote added successfully"})
return jsonify({"Error": "Something horrible happened :S"})
def unwrapped_missing_pairs():
'''
Returns a list of the pairs that are not running that are included in the config file
Returns:
jsonify: A jsonify object with a list of the missing pairs
'''
try:
missing_pairs = broker.get_pairs()
@ -1546,13 +1726,19 @@ def unwrapped_missing_pairs():
broker.logger.log_this(f"Exception while querying for missing pairs: {e}",1)
return jsonify({"Error": "Error fetching running pairs"})
#def unwrapped_paused_traders():
# '''
# Returns a list of paused pairs
# '''
# return jsonify({"paused_traders":global_status["paused_traders"]})
def unwrapped_toggle_cleanup(base,quote):
'''
Signals a trader to enable or disable the cleanup routine.
Parameters:
base (str): The base currency of the pair to toggle
quote (str): The quote currency of the pair to toggle
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
pair_to_toggle = f"{base}/{quote}"
for x in running_instances:
@ -1564,9 +1750,21 @@ def unwrapped_toggle_cleanup(base,quote):
except Exception as e:
broker.logger.log_this(f"Exception while toggling cleanup: {e}",1,f"{base}{quote}")
return jsonify({"Error": "Halp"})
return jsonify({"Error": "Task succesfully failed"})
return jsonify({"Error": "Task failed successfully"})
def unwrapped_toggle_autoswitch(base,quote):
'''
Signals a trader to enable or disable autoswitch.
Parameters:
base (str): The base currency of the trading pair.
quote (str): The quote currency of the trading pair.
Returns:
A jsonified dictionary detailing the outcome of the operation.
'''
try:
pair_to_toggle = f"{base}/{quote}"
for x in running_instances:
@ -1583,7 +1781,19 @@ def unwrapped_toggle_autoswitch(base,quote):
broker.logger.log_this(f"Exception while toggling autoswitch: {e}",1,f"{base}{quote}")
return jsonify({"Error": "Halp"})
def unwrapped_toggle_check_old_long_price(base,quote):
'''
Signals to the trader if it should compare the current price to the old_long price stored in the old_long dictionary.
Parameters:
base (str): The base currency of the pair.
quote (str): The quote currency of the pair.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
pair_to_toggle = f"{base}/{quote}"
for x in running_instances:
@ -1600,7 +1810,20 @@ def unwrapped_toggle_check_old_long_price(base,quote):
broker.logger.log_this(f"Exception while toggling check_old_long_price: {e}",1,f"{base}{quote}")
return jsonify({"Error": "Halp"})
def unwrapped_switch_quote_currency(base,quote,new_quote):
'''
Switches the quote currency of a trader.
Parameters:
base (str): The base currency of the trader.
quote (str): The quote currency of the trader.
new_quote (str): The new quote currency to switch to.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
pair_to_switch = f"{base}/{quote}"
for trader in running_instances:
@ -1620,13 +1843,28 @@ def unwrapped_switch_quote_currency(base,quote,new_quote):
broker.logger.log_this(f"Exception while switching quote currency: {e}",1,f"{base}{quote}")
return jsonify({"Error": "Halp"})
def unwrapped_toggle_restart():
'''
Signals to an instance if it should restart a trader if there is an error.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
new_config = broker.get_config()
new_config["attempt_to_restart"] = not new_config["attempt_to_restart"]
broker.set_config(new_config)
return jsonify({"Success": "attempt_to_restart toggled successfully"})
def unwrapped_toggle_telegram():
'''
Switches on or off the Telegram notifications
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
new_config = broker.get_config()
new_config["telegram"] = not new_config["telegram"]
broker.set_config(new_config)
@ -1634,30 +1872,71 @@ def unwrapped_toggle_telegram():
toggle = "ON" if new_config["telegram"] else "OFF"
return jsonify({"Success": f"Telegram successfully toggled {toggle}"})
def unwrapped_server_time():
'''
Returns the server time.
Returns:
jsonify: A jsonified dictionary containing the server time.
'''
return jsonify({"Time": time.time()})
def unwrapped_trader_time():
'''
Returns the time of the last trader instance lap.
Returns:
jsonify: A jsonified dictionary containing the time of the last trader instance lap.
'''
try:
return jsonify({"Time": max(x.last_time_seen for x in running_instances)})
except Exception as e:
broker.logger.log_this(f"Exception while retrieving trader_time: {e}",1)
return jsonify({"Error": str(e)})
def unwrapped_loop_wait_time(wait_time):
'''
Modifies the amount of time an instance waits between laps.
Parameters:
wait_time (int): The new amount of time to wait between laps.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
broker.set_lap_time(wait_time)
broker.logger.log_this("Done!")
return jsonify({"Success": "Lap time modified successfully"})
def unwrapped_call_wait_time(wait_time):
'''
Modifies the time between some API calls and retries.
Parameters:
wait_time (int): The new amount of time to wait between calls and retries.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
broker.set_wait_time(wait_time)
broker.logger.log_this("Done!")
return jsonify({"Success": "Call wait time modified successfully"})
def unwrapped_reload_markets():
'''
Reloads the markets from the exchange.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
broker.reload_markets()
return jsonify({"Success": "Markets reloaded successfully"})
@ -1665,7 +1944,18 @@ def unwrapped_reload_markets():
broker.logger.log_this(f"Exception while reloading markets: {e}",1)
return jsonify({"Error": "Markets couldn't be reloaded"})
def unwrapped_reload_safety_order(base,quote):
'''
Reloads the safety order of a trader.
Parameters:
base (str): The base currency of the trader.
quote (str): The quote currency of the trader.
Returns:
jsonify: A jsonified dictionary detailing the outcome of the operation.
'''
try:
for trader in running_instances:
if trader.pair==f"{base}/{quote}":