140 lines
4.5 KiB
Python
140 lines
4.5 KiB
Python
from decimal import Decimal as D
|
|
|
|
from core.lib.elastic import store_msg
|
|
|
|
|
|
def get_balance_hook(user_id, user_name, account_id, account_name, balance):
|
|
"""
|
|
Called every time the balance is fetched on an account.
|
|
Store this into Elasticsearch.
|
|
"""
|
|
store_msg(
|
|
"balances",
|
|
{
|
|
"user_id": user_id,
|
|
"user_name": user_name,
|
|
"account_id": account_id,
|
|
"account_name": account_name,
|
|
"balance": balance,
|
|
},
|
|
)
|
|
|
|
|
|
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)
|
|
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 = ((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
|
|
|
|
return round(change_percent, 5)
|
|
|
|
|
|
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)
|
|
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 = ((initial_price - D(sl_price)) / initial_price) * 100
|
|
|
|
# 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):
|
|
"""
|
|
Convert a list of open trades into a list of Trade-like objects.
|
|
"""
|
|
trades = []
|
|
for trade in 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": side,
|
|
"state": trade["state"],
|
|
"price": current_price,
|
|
"pl": unrealised_pl,
|
|
}
|
|
|
|
if "takeProfitOrder" in trade:
|
|
if trade["takeProfitOrder"]:
|
|
take_profit = trade["takeProfitOrder"]["price"]
|
|
take_profit_percent = tp_price_to_percent(
|
|
take_profit, side, current_price, current_units, unrealised_pl
|
|
)
|
|
|
|
cast["take_profit"] = take_profit
|
|
cast["take_profit_percent"] = take_profit_percent
|
|
|
|
if "stopLossOrder" in trade:
|
|
if trade["stopLossOrder"]:
|
|
stop_loss = trade["stopLossOrder"]["price"]
|
|
stop_loss_percent = sl_price_to_percent(
|
|
stop_loss, side, current_price, current_units, unrealised_pl
|
|
)
|
|
|
|
cast["stop_loss"] = stop_loss
|
|
cast["stop_loss_percent"] = stop_loss_percent
|
|
|
|
if "trailingStopLossOrder" in trade:
|
|
if trade["trailingStopLossOrder"]:
|
|
trailing_stop_loss = trade["trailingStopLossOrder"]["price"]
|
|
trailing_stop_loss_percent = sl_price_to_percent(
|
|
trailing_stop_loss,
|
|
side,
|
|
current_price,
|
|
current_units,
|
|
unrealised_pl,
|
|
)
|
|
|
|
cast["trailing_stop_loss"] = trailing_stop_loss
|
|
cast["trailing_stop_loss_percent"] = trailing_stop_loss_percent
|
|
|
|
trades.append(cast)
|
|
return trades
|