Move more checks from market into checks library

This commit is contained in:
2023-02-17 07:20:28 +00:00
parent da67177a18
commit dd3b3521d9
8 changed files with 274 additions and 87 deletions

View File

@@ -1,4 +1,3 @@
from datetime import datetime
from decimal import Decimal as D
from core.exchanges import common
@@ -172,7 +171,7 @@ def get_tp_sl(direction, strategy, price, round_to=None):
return cast
def get_price_bound(direction, strategy, price, current_price):
def get_price_bound(direction, strategy, current_price):
"""
Get the price bound for a given price using the slippage from the strategy.
* Check that the price of the callback is within the callback price deviation of the
@@ -187,25 +186,6 @@ def get_price_bound(direction, strategy, price, current_price):
:return: Price bound
"""
# Convert the callback price deviation to a ratio
if strategy.risk_model is not None:
callback_price_deviation_as_ratio = D(
strategy.risk_model.callback_price_deviation_percent
) / D(100)
else:
callback_price_deviation_as_ratio = D(0.5) / D(100)
log.debug(f"Callback price deviation as ratio: {callback_price_deviation_as_ratio}")
maximum_price_deviation = D(current_price) * D(callback_price_deviation_as_ratio)
# Ensure the current price is within price_slippage_as_ratio of the callback price
if abs(current_price - price) <= maximum_price_deviation:
log.debug("Current price is within price deviation of callback price")
else:
log.error("Current price is not within price deviation of callback price")
log.debug(f"Difference: {abs(current_price - price)}")
return None
# Convert the maximum price slippage to a ratio
if strategy.risk_model is not None:
price_slippage_as_ratio = D(strategy.risk_model.price_slippage_percent) / D(100)
@@ -277,15 +257,8 @@ def execute_strategy(callback, strategy, func):
# Only check times for entries. We can always exit trades and set trends.
if func == "entry":
# Check if we can trade now!
now_utc = datetime.utcnow()
trading_times = strategy.trading_times.all()
if not trading_times:
log.error("No trading times set for strategy")
return
matches = [x.within_range(now_utc) for x in trading_times]
if not any(matches):
log.debug("Not within trading time range")
within_trading_times = checks.within_trading_times(strategy)
if not within_trading_times:
return
# Don't touch the account if it's disabled.
@@ -338,8 +311,17 @@ def execute_strategy(callback, strategy, func):
log.debug(f"Callback price: {price}")
log.debug(f"Current price: {current_price}")
# Check callback price deviation
within_callback_price_deviation = checks.within_callback_price_deviation(
strategy, price, current_price
)
if not within_callback_price_deviation:
log.debug("Not within callback price deviation")
return
# Calculate price bound and round to the display precision
price_bound = get_price_bound(direction, strategy, price, current_price)
# Also enforces max price slippage
price_bound = get_price_bound(direction, strategy, current_price)
if not price_bound:
return
price_bound = round(price_bound, display_precision)
@@ -390,34 +372,9 @@ def execute_strategy(callback, strategy, func):
return
# Check if we are trading against the trend
if strategy.trend_signals.exists():
if strategy.trends is None:
log.debug("Refusing to trade with no trend signals received")
sendmsg(
user,
f"Refusing to trade {symbol} with no trend signals received",
title="Trend not ready",
)
return
if symbol not in strategy.trends:
log.debug("Refusing to trade asset without established trend")
sendmsg(
user,
f"Refusing to trade {symbol} without established trend",
title="Trend not ready",
)
return
else:
if strategy.trends[symbol] != direction:
log.debug("Refusing to trade against the trend")
sendmsg(
user,
f"Refusing to trade {symbol} against the trend",
title="Trend rejection",
)
return
else:
log.debug(f"Trend check passed for {symbol} - {direction}")
within_trends = checks.within_trends(strategy, symbol, direction)
if not within_trends:
return
type = strategy.order_settings.order_type
@@ -435,18 +392,6 @@ def execute_strategy(callback, strategy, func):
protection = get_tp_sl(
direction, strategy, current_price, round_to=display_precision
)
# protection_cast = {}
# if "sl" in protection:
# protection_cast["stop_loss"] = float(round(protect
# ion["sl"], display_precision))
# if "tp" in protection:
# protection_cast["take_profit"] = float(
# round(protection["tp"], display_precision)
# )
# if "tsl" in protection:
# protection_cast["trailing_stop_loss"] = float(
# round(protection["tsl"], display_precision)
# )
# Create object, note that the amount is rounded to the trade precision
amount_rounded = float(round(trade_size_in_base, trade_precision))