Fix profit/loss calculation
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from decimal import Decimal as D
|
||||
|
||||
from core.lib.elastic import store_msg
|
||||
from core.trading.market import to_currency
|
||||
|
||||
|
||||
def get_balance_hook(user_id, user_name, account_id, account_name, balance):
|
||||
@@ -34,9 +35,22 @@ def tp_price_to_percent(tp_price, side, current_price, current_units, unrealised
|
||||
# Get the percent change of the TP price from the initial price.
|
||||
change_percent = ((initial_price - D(tp_price)) / initial_price) * 100
|
||||
|
||||
# If the trade is long, the TP price will be lower than the initial price.
|
||||
if side == "long":
|
||||
change_percent *= -1
|
||||
if D(tp_price) < initial_price:
|
||||
loss = True
|
||||
else:
|
||||
loss = False
|
||||
else:
|
||||
if D(tp_price) > initial_price:
|
||||
loss = True
|
||||
else:
|
||||
loss = False
|
||||
|
||||
# if we are in loss on the short side, we want to show a negative
|
||||
if loss:
|
||||
change_percent = 0 - abs(change_percent)
|
||||
else:
|
||||
change_percent = abs(change_percent)
|
||||
|
||||
return round(change_percent, 5)
|
||||
|
||||
@@ -65,23 +79,22 @@ def sl_price_to_percent(sl_price, side, current_price, current_units, unrealised
|
||||
# change_percent *= -1
|
||||
|
||||
if side == "long":
|
||||
if D(current_price) > initial_price:
|
||||
if D(sl_price) > initial_price:
|
||||
profit = True
|
||||
else:
|
||||
profit = False
|
||||
else:
|
||||
if D(current_price) < initial_price:
|
||||
if D(sl_price) < initial_price:
|
||||
profit = True
|
||||
else:
|
||||
profit = False
|
||||
|
||||
# if we are in profit on the short side, we want to show a negative loss
|
||||
if profit and side == "short":
|
||||
# change_percent *= -1
|
||||
print("CHANGE PERCENT: ", change_percent)
|
||||
print("PROFIT", profit)
|
||||
|
||||
if profit:
|
||||
change_percent = 0 - abs(change_percent)
|
||||
# if we are in loss on the long side, we want to show a positive loss
|
||||
if not profit and side == "long":
|
||||
# change_percent *= -1
|
||||
else:
|
||||
change_percent = abs(change_percent)
|
||||
|
||||
return round(change_percent, 5)
|
||||
@@ -143,3 +156,9 @@ def convert_open_trades(open_trades):
|
||||
|
||||
trades.append(cast)
|
||||
return trades
|
||||
|
||||
|
||||
def convert_trades_to_usd(account, trades):
|
||||
"""
|
||||
Convert a list of trades to USD.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user