Validate strategy entries and exits

This commit is contained in:
Mark Veidemanis 2022-12-01 19:32:08 +00:00
parent 851d021af2
commit 21b9585192
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 41 additions and 0 deletions

View File

@ -165,6 +165,47 @@ class StrategyForm(RestrictedFormMixin, ModelForm):
queryset=TradingTime.objects.all(), widget=forms.CheckboxSelectMultiple
)
def clean(self):
super(StrategyForm, self).clean()
entry_signals = self.cleaned_data.get("entry_signals")
exit_signals = self.cleaned_data.get("exit_signals")
for entry in entry_signals.all():
if entry in exit_signals.all():
self._errors["entry_signals"] = self.error_class(
[
"You cannot bet against yourself. Do not use the same signal for entry and exit."
]
)
for exit in exit_signals.all():
if exit in entry_signals.all():
self._errors["exit_signals"] = self.error_class(
[
"You cannot bet against yourself. Do not use the same signal for entry and exit."
]
)
# Get all the directions for entry and exit signals
entries_set = set([x.direction for x in entry_signals.all()])
exits_set = set([x.direction for x in exit_signals.all()])
# Make sure both fields are filled before we check this
if entries_set and exits_set:
# Combine them into one set
check_set = set()
check_set.update(entries_set, exits_set)
# If the length is 1, they are all the same direction
if len(check_set) == 1:
self._errors["entry_signals"] = self.error_class(
[
"You cannot have entry and exit signals that are the same direction. At least one must be opposing."
]
)
self._errors["exit_signals"] = self.error_class(
[
"You cannot have entry and exit signals that are the same direction. At least one must be opposing."
]
)
class TradeForm(RestrictedFormMixin, ModelForm):
class Meta: