Implement closing positions and refuse to post rejected trades

This commit is contained in:
Mark Veidemanis 2022-12-01 20:36:58 +00:00
parent 5c2eeae043
commit 66a18a6406
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 34 additions and 2 deletions

View File

@ -117,6 +117,16 @@ class OANDAExchange(BaseExchange):
items.append(item) items.append(item)
return items return items
def close_position(self, side, symbol):
data = {
f"{side}Units": "ALL",
}
r = positions.PositionClose(
accountID=self.account_id, instrument=symbol, data=data
)
response = self.call(r)
return response
def close_all_positions(self): def close_all_positions(self):
# all_positions = self.get_all_positions() # all_positions = self.get_all_positions()
@ -124,5 +134,4 @@ class OANDAExchange(BaseExchange):
# print("POS ITER", position) # print("POS ITER", position)
r = positions.PositionClose(accountID=self.account_id) r = positions.PositionClose(accountID=self.account_id)
response = self.call(r) response = self.call(r)
print("CLOSE ALL POSITIONS", response)
return response return response

View File

@ -0,0 +1,18 @@
# Generated by Django 4.1.3 on 2022-12-01 19:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0041_alter_strategy_entry_signals_and_more'),
]
operations = [
migrations.AddField(
model_name='trade',
name='information',
field=models.JSONField(blank=True, null=True),
),
]

View File

@ -201,6 +201,7 @@ class Trade(models.Model):
trailing_stop_loss = models.FloatField(null=True, blank=True) trailing_stop_loss = models.FloatField(null=True, blank=True)
take_profit = models.FloatField(null=True, blank=True) take_profit = models.FloatField(null=True, blank=True)
status = models.CharField(max_length=255, null=True, blank=True) status = models.CharField(max_length=255, null=True, blank=True)
information = models.JSONField(null=True, blank=True)
direction = models.CharField(choices=DIRECTION_CHOICES, max_length=255) direction = models.CharField(choices=DIRECTION_CHOICES, max_length=255)
# To populate from the trade # To populate from the trade
@ -213,6 +214,10 @@ class Trade(models.Model):
self._original = self self._original = self
def post(self): def post(self):
if self.status in ["rejected", "close"]:
log.debug(f"Trade {self.id} rejected. Not posting.")
log.debug(f"Trade {self.id} information: {self.information}")
else:
return self.account.client.post_trade(self) return self.account.client.post_trade(self)
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):