diff --git a/core/forms.py b/core/forms.py index ac74392..acb911f 100644 --- a/core/forms.py +++ b/core/forms.py @@ -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: