diff --git a/core/forms.py b/core/forms.py index 3814f9c..ae38201 100644 --- a/core/forms.py +++ b/core/forms.py @@ -111,13 +111,22 @@ class SignalForm(RestrictedFormMixin, ModelForm): class AccountForm(RestrictedFormMixin, ModelForm): class Meta: model = Account - fields = ("name", "exchange", "api_key", "api_secret", "sandbox", "risk_model") + fields = ( + "name", + "exchange", + "api_key", + "api_secret", + "sandbox", + "enabled", + "risk_model", + ) help_texts = { "name": "Name of the account. Informational only.", "exchange": "The exchange to use for this account.", "api_key": "The API key or username for the account.", "api_secret": "The API secret or password/token for the account.", "sandbox": "Whether to use the sandbox/demo or not.", + "enabled": "Whether the account is enabled.", "risk_model": "The risk model to use for this account.", } diff --git a/core/migrations/0050_account_enabled.py b/core/migrations/0050_account_enabled.py new file mode 100644 index 0000000..ae5e08a --- /dev/null +++ b/core/migrations/0050_account_enabled.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.4 on 2023-01-01 15:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0049_rename_riskmodel_account_risk_model'), + ] + + operations = [ + migrations.AddField( + model_name='account', + name='enabled', + field=models.BooleanField(default=True), + ), + ] diff --git a/core/models.py b/core/models.py index ad887cb..c0ada3c 100644 --- a/core/models.py +++ b/core/models.py @@ -109,6 +109,7 @@ class Account(models.Model): api_key = models.CharField(max_length=255) api_secret = models.CharField(max_length=255) sandbox = models.BooleanField(default=False) + enabled = models.BooleanField(default=True) supported_symbols = models.JSONField(default=list) instruments = models.JSONField(default=list) currency = models.CharField(max_length=255, null=True, blank=True) diff --git a/core/trading/market.py b/core/trading/market.py index f73018f..bffa842 100644 --- a/core/trading/market.py +++ b/core/trading/market.py @@ -286,6 +286,13 @@ def execute_strategy(callback, strategy, func): log.debug("Not within trading time range") return + # Don't touch the account if it's disabled. + # We still want to set trends, though. + if func in ("entry", "exit"): + if not strategy.account.enabled: + log.debug("Account is disabled, exiting") + return + # Instruments supported by the account if not strategy.account.instruments: strategy.account.update_info() diff --git a/core/views/positions.py b/core/views/positions.py index b29f0c6..250b7ff 100644 --- a/core/views/positions.py +++ b/core/views/positions.py @@ -53,7 +53,8 @@ class Positions(LoginRequiredMixin, OTPRequiredMixin, ObjectList): if account_id: self.extra_context["account_id"] = account_id items = [] - accounts = Account.objects.filter(user=self.request.user) + # Only get enabled accounts for positions + accounts = Account.objects.filter(user=self.request.user, enabled=True) for account in accounts: try: positions = account.client.get_all_positions() diff --git a/core/views/profit.py b/core/views/profit.py index fac2377..b87300b 100644 --- a/core/views/profit.py +++ b/core/views/profit.py @@ -22,7 +22,8 @@ class Profit(LoginRequiredMixin, OTPRequiredMixin, ObjectList): def get_queryset(self, **kwargs): items = [] - accounts = Account.objects.filter(user=self.request.user) + # Only get enabled accounts + accounts = Account.objects.filter(user=self.request.user, enabled=True) for account in accounts: try: details = account.client.get_account()