Write crossfilter, asset groups and max open trades implementation and tests
This commit is contained in:
@@ -79,7 +79,6 @@ def tp_price_to_percent(tp_price, side, current_price, current_units, unrealised
|
||||
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
|
||||
|
||||
@@ -106,6 +105,7 @@ def tp_price_to_percent(tp_price, side, current_price, current_units, unrealised
|
||||
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.
|
||||
Negative values for tp_percent indicate a loss.
|
||||
"""
|
||||
pl_per_unit = D(unrealised_pl) / D(current_units)
|
||||
if side == "long":
|
||||
@@ -117,12 +117,31 @@ def tp_percent_to_price(tp_percent, side, current_price, current_units, unrealis
|
||||
change_percent = D(tp_percent) / 100
|
||||
|
||||
# Get the price of the TP percent from the initial price.
|
||||
change_price = initial_price * change_percent
|
||||
change_price = initial_price * abs(change_percent)
|
||||
# loss is true if tp_percent is:
|
||||
# - below initial_price for long
|
||||
# - above initial_price for short
|
||||
|
||||
if D(tp_percent) < D(0):
|
||||
loss = True
|
||||
else:
|
||||
loss = False
|
||||
|
||||
if side == "long":
|
||||
tp_price = initial_price - change_price
|
||||
if loss:
|
||||
tp_price = D(initial_price) - change_price
|
||||
else:
|
||||
tp_price = D(initial_price) + change_price
|
||||
else:
|
||||
tp_price = initial_price + change_price
|
||||
if loss:
|
||||
tp_price = D(initial_price) + change_price
|
||||
else:
|
||||
tp_price = D(initial_price) - change_price
|
||||
|
||||
# if side == "long":
|
||||
# tp_price = initial_price - change_price
|
||||
# else:
|
||||
# tp_price = initial_price + change_price
|
||||
|
||||
return round(tp_price, 5)
|
||||
|
||||
@@ -184,12 +203,23 @@ def sl_percent_to_price(sl_percent, side, current_price, current_units, unrealis
|
||||
change_percent = D(sl_percent) / 100
|
||||
|
||||
# Get the price of the SL percent from the initial price.
|
||||
change_price = initial_price * change_percent
|
||||
change_price = initial_price * abs(change_percent)
|
||||
|
||||
if D(sl_percent) < D(0):
|
||||
profit = True
|
||||
else:
|
||||
profit = False
|
||||
|
||||
if side == "long":
|
||||
sl_price = initial_price - change_price
|
||||
if profit:
|
||||
sl_price = D(initial_price) + change_price
|
||||
else:
|
||||
sl_price = D(initial_price) - change_price
|
||||
else:
|
||||
sl_price = initial_price + change_price
|
||||
if profit:
|
||||
sl_price = D(initial_price) - change_price
|
||||
else:
|
||||
sl_price = D(initial_price) + change_price
|
||||
|
||||
return round(sl_price, 5)
|
||||
|
||||
@@ -270,6 +300,8 @@ def open_trade_to_unified_format(trade):
|
||||
"id": trade["id"],
|
||||
"symbol": trade["symbol"],
|
||||
"amount": current_units,
|
||||
# For crossfilter
|
||||
"units": current_units,
|
||||
"side": side,
|
||||
"direction": side_to_direction(side),
|
||||
"state": trade["state"],
|
||||
|
||||
Reference in New Issue
Block a user