Implement trading time limits
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from datetime import timedelta
|
||||
|
||||
import stripe
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
@@ -25,13 +27,13 @@ TIF_CHOICES = (
|
||||
("ioc", "IOC (Immediate Or Cancel)"),
|
||||
)
|
||||
DAY_CHOICES = (
|
||||
("monday", "Monday"),
|
||||
("tuesday", "Tuesday"),
|
||||
("wednesday", "Wednesday"),
|
||||
("thursday", "Thursday"),
|
||||
("friday", "Friday"),
|
||||
("saturday", "Saturday"),
|
||||
("sunday", "Sunday"),
|
||||
(1, "Monday"),
|
||||
(2, "Tuesday"),
|
||||
(3, "Wednesday"),
|
||||
(4, "Thursday"),
|
||||
(5, "Friday"),
|
||||
(6, "Saturday"),
|
||||
(7, "Sunday"),
|
||||
)
|
||||
|
||||
|
||||
@@ -225,13 +227,63 @@ class TradingTime(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=255)
|
||||
description = models.TextField(null=True, blank=True)
|
||||
start_day = models.CharField(choices=DAY_CHOICES, max_length=255)
|
||||
end_day = models.CharField(choices=DAY_CHOICES, max_length=255)
|
||||
start_day = models.IntegerField(choices=DAY_CHOICES)
|
||||
end_day = models.IntegerField(choices=DAY_CHOICES)
|
||||
start_time = models.TimeField()
|
||||
end_time = models.TimeField()
|
||||
|
||||
def within_range(self, ts):
|
||||
"""
|
||||
Check if the specified time is within the configured trading times.
|
||||
:param ts: Timestamp
|
||||
:type ts: datetime
|
||||
:return: whether or not the time is within the trading range
|
||||
:rtype: bool
|
||||
"""
|
||||
start_day = self.start_day
|
||||
end_day = self.end_day
|
||||
# Check the day is between the start and end day
|
||||
if not start_day <= ts.weekday() + 1 <= end_day:
|
||||
return False
|
||||
|
||||
start_time = self.start_time
|
||||
end_time = self.end_time
|
||||
|
||||
# Get what the start time would be this week
|
||||
ts_monday = ts - timedelta(days=ts.weekday())
|
||||
|
||||
# Now we need to add our day of week to monday
|
||||
# Let's set the offset now since it's off by one
|
||||
offset_start = start_day - 1
|
||||
# Datetime: monday=0, tuesday=1, us: monday=1, tuesday=2, so we need to subtract
|
||||
# one from ours to not be off by one
|
||||
offset_end = end_day - 1
|
||||
|
||||
# Now we can add the offset to the monday
|
||||
start = ts_monday + timedelta(days=offset_start)
|
||||
start = start.replace(
|
||||
hour=start_time.hour,
|
||||
minute=start_time.minute,
|
||||
second=start_time.second,
|
||||
microsecond=start_time.microsecond,
|
||||
)
|
||||
end = ts_monday + timedelta(days=offset_end)
|
||||
end = end.replace(
|
||||
hour=end_time.hour,
|
||||
minute=end_time.minute,
|
||||
second=end_time.second,
|
||||
microsecond=end_time.microsecond,
|
||||
)
|
||||
# Check if the ts is between the start and end times
|
||||
# ts must be more than start and less than end
|
||||
return ts >= start and ts <= end
|
||||
return True
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ({self.start_day} at {self.start_time} - {self.end_day} at {self.end_time})"
|
||||
return (
|
||||
f"{self.name} ({self.get_start_day_display()} at {self.start_time} -"
|
||||
f"{self.get_end_day_display()} at {self.end_time})"
|
||||
)
|
||||
|
||||
|
||||
class Strategy(models.Model):
|
||||
@@ -239,7 +291,7 @@ class Strategy(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
description = models.TextField(null=True, blank=True)
|
||||
account = models.ForeignKey(Account, on_delete=models.CASCADE)
|
||||
trading_times = models.ManyToManyField(TradingTime, blank=True)
|
||||
trading_times = models.ManyToManyField(TradingTime)
|
||||
order_type = models.CharField(
|
||||
choices=TYPE_CHOICES, max_length=255, default="market"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user