Fix TP/SL calculation and make more tests for profit/loss

This commit is contained in:
2023-01-05 23:37:50 +00:00
parent 2dfaef324c
commit db870c39c6
3 changed files with 170 additions and 81 deletions

View File

@@ -20,34 +20,65 @@ def get_balance_hook(user_id, user_name, account_id, account_name, balance):
)
def tp_price_to_percent(tp_price, current_price, current_units, unrealised_pl):
# Is this right?
def tp_price_to_percent(tp_price, side, current_price, current_units, unrealised_pl):
"""
Determine the percent change of the TP price from the initial price.
Positive values indicate a profit, negative values indicate a loss.
"""
pl_per_unit = D(unrealised_pl) / D(current_units)
print("pl_per_unit: ", pl_per_unit)
initial_price = D(current_price) - pl_per_unit
print("initial_price: ", initial_price)
if side == "long":
initial_price = D(current_price) - pl_per_unit
else:
initial_price = D(current_price) + pl_per_unit
# Get the percent change of the TP price from the initial price.
change_percent = ((D(tp_price) - initial_price) / initial_price) * 100
print("change_percent: ", change_percent)
change_percent = ((initial_price - D(tp_price)) / initial_price) * 100
# Doesn't check direction
return abs(round(change_percent, 5))
# If the trade is long, the TP price will be lower than the initial price.
if side == "long":
change_percent *= -1
return round(change_percent, 5)
def sl_price_to_percent(sl_price, current_price, current_units, unrealised_pl):
# Is this right?
def sl_price_to_percent(sl_price, side, current_price, current_units, unrealised_pl):
"""
Determine the percent change of the SL price from the initial price.
Positive values indicate a loss, negative values indicate a profit.
This may seem backwards, but it is important to note that by default,
SL indicates a loss, and positive values should be expected.
Negative values indicate a negative loss, so a profit.
"""
pl_per_unit = D(unrealised_pl) / D(current_units)
print("pl_per_unit: ", pl_per_unit)
initial_price = D(current_price) - pl_per_unit
print("initial_price: ", initial_price)
if side == "long":
initial_price = D(current_price) - pl_per_unit
else:
initial_price = D(current_price) + pl_per_unit
# initial_price = D(current_price) - pl_per_unit
# Get the percent change of the SL price from the initial price.
change_percent = ((D(sl_price) - initial_price) / initial_price) * 100
print("change_percent: ", change_percent)
change_percent = ((initial_price - D(sl_price)) / initial_price) * 100
# Doesn't check direction
return abs(round(change_percent, 5))
# If the trade is long, the SL price will be higher than the initial price.
if side == "long":
change_percent *= -1
if side == "long":
if current_price > initial_price:
profit = True
else:
profit = False
else:
if current_price < initial_price:
profit = True
else:
profit = False
if profit:
change_percent *= -1
return round(change_percent, 5)
def convert_open_trades(open_trades):
@@ -59,11 +90,12 @@ def convert_open_trades(open_trades):
current_price = trade["price"]
current_units = trade["currentUnits"]
unrealised_pl = trade["unrealizedPL"]
side = trade["side"]
cast = {
"id": trade["id"],
"symbol": trade["symbol"],
"amount": current_units,
"side": trade["side"],
"side": side,
"state": trade["state"],
"price": current_price,
"pl": unrealised_pl,
@@ -73,7 +105,7 @@ def convert_open_trades(open_trades):
if trade["takeProfitOrder"]:
take_profit = trade["takeProfitOrder"]["price"]
take_profit_percent = tp_price_to_percent(
take_profit, current_price, current_units, unrealised_pl
take_profit, side, current_price, current_units, unrealised_pl
)
cast["take_profit"] = take_profit
@@ -83,7 +115,7 @@ def convert_open_trades(open_trades):
if trade["stopLossOrder"]:
stop_loss = trade["stopLossOrder"]["price"]
stop_loss_percent = sl_price_to_percent(
stop_loss, current_price, current_units, unrealised_pl
stop_loss, side, current_price, current_units, unrealised_pl
)
cast["stop_loss"] = stop_loss
@@ -93,7 +125,11 @@ def convert_open_trades(open_trades):
if trade["trailingStopLossOrder"]:
trailing_stop_loss = trade["trailingStopLossOrder"]["price"]
trailing_stop_loss_percent = sl_price_to_percent(
trailing_stop_loss, current_price, current_units, unrealised_pl
trailing_stop_loss,
side,
current_price,
current_units,
unrealised_pl,
)
cast["trailing_stop_loss"] = trailing_stop_loss