Move order settings to OrderSettings

This commit is contained in:
Mark Veidemanis 2023-02-15 18:41:08 +00:00
parent 69cf8dcc10
commit b4afa32a6e
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 21 additions and 36 deletions

View File

@ -127,16 +127,10 @@ class StrategyForm(RestrictedFormMixin, ModelForm):
"risk_model", "risk_model",
"trading_times", "trading_times",
"order_settings", "order_settings",
# "order_type",
# "time_in_force",
"entry_signals", "entry_signals",
"exit_signals", "exit_signals",
"trend_signals", "trend_signals",
"enabled", "enabled",
# "take_profit_percent",
# "stop_loss_percent",
# "trailing_stop_loss_percent",
# "trade_size_percent",
) )
help_texts = { help_texts = {
@ -147,16 +141,10 @@ class StrategyForm(RestrictedFormMixin, ModelForm):
"risk_model": "The risk model to use for this strategy. Highly recommended.", "risk_model": "The risk model to use for this strategy. Highly recommended.",
"trading_times": "When the strategy will place new trades.", "trading_times": "When the strategy will place new trades.",
"order_settings": "Order settings to use for this strategy.", "order_settings": "Order settings to use for this strategy.",
# "order_type": "Market: Buy/Sell at the current market price. Limit: Buy/Sell at a specified price. Limits protect you more against market slippage.",
# "time_in_force": "The time in force controls how the order is executed.",
"entry_signals": "Callbacks received to these signals will trigger a trade.", "entry_signals": "Callbacks received to these signals will trigger a trade.",
"exit_signals": "Callbacks received to these signals will close all trades for the symbol on the account.", "exit_signals": "Callbacks received to these signals will close all trades for the symbol on the account.",
"trend_signals": "Callbacks received to these signals will limit the trading direction of the given symbol to the callback direction until further notice.", "trend_signals": "Callbacks received to these signals will limit the trading direction of the given symbol to the callback direction until further notice.",
"enabled": "Whether the strategy is enabled.", "enabled": "Whether the strategy is enabled.",
# "take_profit_percent": "The take profit will be set at this percentage above/below the entry price.",
# "stop_loss_percent": "The stop loss will be set at this percentage above/below the entry price.",
# "trailing_stop_loss_percent": "The trailing stop loss will be set at this percentage above/below the entry price. A trailing stop loss will follow the price as it moves in your favor.",
# "trade_size_percent": "Percentage of the account balance to use for each trade.",
} }
entry_signals = forms.ModelMultipleChoiceField( entry_signals = forms.ModelMultipleChoiceField(

View File

@ -1,7 +1,7 @@
# Generated by Django 4.1.6 on 2023-02-15 18:34 # Generated by Django 4.1.6 on 2023-02-15 18:34
from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -369,10 +369,6 @@ class Strategy(models.Model):
description = models.TextField(null=True, blank=True) description = models.TextField(null=True, blank=True)
account = models.ForeignKey(Account, on_delete=models.CASCADE) account = models.ForeignKey(Account, on_delete=models.CASCADE)
trading_times = models.ManyToManyField(TradingTime) trading_times = models.ManyToManyField(TradingTime)
order_type = models.CharField(
choices=TYPE_CHOICES, max_length=255, default="market"
)
time_in_force = models.CharField(choices=TIF_CHOICES, max_length=255, default="gtc")
entry_signals = models.ManyToManyField( entry_signals = models.ManyToManyField(
Signal, related_name="entry_strategies", blank=True Signal, related_name="entry_strategies", blank=True
) )
@ -383,12 +379,6 @@ class Strategy(models.Model):
Signal, related_name="trend_strategies", blank=True Signal, related_name="trend_strategies", blank=True
) )
enabled = models.BooleanField(default=False) enabled = models.BooleanField(default=False)
take_profit_percent = models.FloatField(default=1.5)
stop_loss_percent = models.FloatField(default=1.0)
trailing_stop_loss_percent = models.FloatField(default=1.0, null=True, blank=True)
# price_slippage_percent = models.FloatField(default=2.5)
# callback_price_deviation_percent = models.FloatField(default=0.5)
trade_size_percent = models.FloatField(default=0.5)
trends = models.JSONField(null=True, blank=True) trends = models.JSONField(null=True, blank=True)
asset_group = models.ForeignKey( asset_group = models.ForeignKey(
@ -443,9 +433,6 @@ class AssetGroup(models.Model):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True) description = models.TextField(null=True, blank=True)
# Dict like {"RUB": True, "USD": False}
# allowed = models.JSONField(null=True, blank=True, default=dict)
webhook_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) webhook_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
when_no_data = models.IntegerField(choices=MAPPING_CHOICES, default=7) when_no_data = models.IntegerField(choices=MAPPING_CHOICES, default=7)
@ -457,7 +444,7 @@ class AssetGroup(models.Model):
when_bearish = models.IntegerField(choices=MAPPING_CHOICES, default=3) when_bearish = models.IntegerField(choices=MAPPING_CHOICES, default=3)
def __str__(self): def __str__(self):
return f"{self.name}" return self.name
@property @property
def matches(self): def matches(self):
@ -499,3 +486,6 @@ class OrderSettings(models.Model):
stop_loss_percent = models.FloatField(default=1.0) stop_loss_percent = models.FloatField(default=1.0)
trailing_stop_loss_percent = models.FloatField(default=1.0, null=True, blank=True) trailing_stop_loss_percent = models.FloatField(default=1.0, null=True, blank=True)
trade_size_percent = models.FloatField(default=0.5) trade_size_percent = models.FloatField(default=0.5)
def __str__(self):
return self.name

View File

@ -67,7 +67,7 @@ def get_trade_size_in_base(direction, account, strategy, cash_balance, base):
""" """
# Convert the trade size in percent to a ratio # Convert the trade size in percent to a ratio
trade_size_as_ratio = D(strategy.trade_size_percent) / D(100) trade_size_as_ratio = D(strategy.order_settings.trade_size_percent) / D(100)
log.debug(f"Trade size as ratio: {trade_size_as_ratio}") log.debug(f"Trade size as ratio: {trade_size_as_ratio}")
# Multiply with cash balance to get the trade size in the account's # Multiply with cash balance to get the trade size in the account's
@ -146,16 +146,23 @@ def get_tp_sl(direction, strategy, price, round_to=None):
:return: Take profit and stop loss prices :return: Take profit and stop loss prices
""" """
cast = {} cast = {}
if strategy.take_profit_percent != 0: if strategy.order_settings.take_profit_percent != 0:
cast["take_profit"] = get_tp(direction, strategy.take_profit_percent, price) cast["take_profit"] = get_tp(
direction, strategy.order_settings.take_profit_percent, price
)
if strategy.stop_loss_percent != 0: if strategy.order_settings.stop_loss_percent != 0:
cast["stop_loss"] = get_sl(direction, strategy.stop_loss_percent, price) cast["stop_loss"] = get_sl(
direction, strategy.order_settings.stop_loss_percent, price
)
# Look up the TSL if required by the strategy # Look up the TSL if required by the strategy
if strategy.trailing_stop_loss_percent != 0: if strategy.order_settings.trailing_stop_loss_percent != 0:
cast["trailing_stop_loss"] = get_sl( cast["trailing_stop_loss"] = get_sl(
direction, strategy.trailing_stop_loss_percent, price, return_var=True direction,
strategy.order_settings.trailing_stop_loss_percent,
price,
return_var=True,
) )
if round_to: if round_to:
@ -412,7 +419,7 @@ def execute_strategy(callback, strategy, func):
else: else:
log.debug(f"Trend check passed for {symbol} - {direction}") log.debug(f"Trend check passed for {symbol} - {direction}")
type = strategy.order_type type = strategy.order_settings.order_type
# Get the account's balance in the native account currency # Get the account's balance in the native account currency
cash_balance = strategy.account.client.get_balance() cash_balance = strategy.account.client.get_balance()
@ -450,7 +457,7 @@ def execute_strategy(callback, strategy, func):
signal=signal, signal=signal,
symbol=symbol, symbol=symbol,
type=type, type=type,
time_in_force=strategy.time_in_force, time_in_force=strategy.order_settings.time_in_force,
# amount_fiat=amount_fiat, # amount_fiat=amount_fiat,
amount=amount_rounded, amount=amount_rounded,
# price=price_bound, # price=price_bound,