Write protection check tests

This commit is contained in:
2023-02-17 17:05:52 +00:00
parent 1dbb3fcf79
commit 67117f0978
7 changed files with 172 additions and 41 deletions

View File

@@ -103,6 +103,30 @@ def tp_price_to_percent(tp_price, side, current_price, current_units, unrealised
return round(change_percent, 5)
def tp_percent_to_price(tp_percent, side, current_price, current_units, unrealised_pl):
"""
Determine the price of the TP percent from the initial price.
"""
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 = D(tp_percent) / 100
# Get the price of the TP percent from the initial price.
change_price = initial_price * change_percent
if side == "long":
tp_price = initial_price - change_price
else:
tp_price = initial_price + change_price
return round(tp_price, 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.
@@ -146,6 +170,30 @@ def sl_price_to_percent(sl_price, side, current_price, current_units, unrealised
return round(change_percent, 5)
def sl_percent_to_price(sl_percent, side, current_price, current_units, unrealised_pl):
"""
Determine the price of the SL percent from the initial price.
"""
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 SL price from the initial price.
change_percent = D(sl_percent) / 100
# Get the price of the SL percent from the initial price.
change_price = initial_price * change_percent
if side == "long":
sl_price = initial_price - change_price
else:
sl_price = initial_price + change_price
return round(sl_price, 5)
def annotate_trade_tp_sl_percent(trade):
"""
Annotate the trade with the TP and SL percent.
@@ -228,6 +276,8 @@ def open_trade_to_unified_format(trade):
"current_price": current_price,
"pl": unrealised_pl,
}
if "openTime" in trade:
cast["open_time"] = trade["openTime"]
# Add some extra fields, sometimes we have already looked up the
# prices and don't need to call convert_trades_to_usd
# This is mostly for tests, but it can be useful in other places.