Implement closing positions and refuse to post rejected trades

Mark Veidemanis 1 year ago
parent 5c2eeae043
commit 66a18a6406
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -117,6 +117,16 @@ class OANDAExchange(BaseExchange):
items.append(item)
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):
# all_positions = self.get_all_positions()
@ -124,5 +134,4 @@ class OANDAExchange(BaseExchange):
# print("POS ITER", position)
r = positions.PositionClose(accountID=self.account_id)
response = self.call(r)
print("CLOSE ALL POSITIONS", response)
return response

@ -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),
),
]

@ -201,6 +201,7 @@ class Trade(models.Model):
trailing_stop_loss = models.FloatField(null=True, blank=True)
take_profit = models.FloatField(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)
# To populate from the trade
@ -213,7 +214,11 @@ class Trade(models.Model):
self._original = self
def post(self):
return self.account.client.post_trade(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)
def delete(self, *args, **kwargs):
# close the trade

Loading…
Cancel
Save