10
10
from trading_calendars import get_calendar
11
11
12
12
import pytz
13
- from margot .signals .periods import DAILY
13
+
14
+ from margot .signals .order_types import MOC , MKT , LMT
14
15
from margot .data import MargotDataFrame
15
16
16
17
@@ -22,7 +23,8 @@ class BaseAlgo(object):
22
23
23
24
Args:
24
25
env (dict): a dictionary of environment variables, e.g. API keys.
25
- Overrides anything provided in sys env.
26
+ Overrides anything provided in sys env.
27
+ market (str): The ISO code for the market we will use.
26
28
27
29
Raises:
28
30
ValueError: the attribute, 'data' must be a reference to a MargotDataFrame.
@@ -31,15 +33,71 @@ class BaseAlgo(object):
31
33
32
34
"""
33
35
34
- frequency = DAILY
36
+ MOC = MOC
37
+ MKT = MKT
38
+ LMT = LMT
39
+
40
+ MONDAY = 'MON'
41
+ TUESDAY = 'TUE'
42
+ WEDNESDAY = 'WED'
43
+ THURSDAY = 'THU'
44
+ FRIDAY = 'FRI'
45
+ SATURDAY = 'SAT'
46
+ SUNDAY = 'SUN'
47
+
35
48
data = None
36
49
37
- def __init__ (self , env : dict = {}, calendar = 'XNYS' ): # noqa: D107
50
+ def __init__ (self , env : dict = {}, market = 'XNYS' ): # noqa: D107
38
51
self .env = env
39
- self .calendar = calendar
52
+ self .market = get_calendar (market )
53
+ self .when = None
40
54
if not isinstance (self .data , MargotDataFrame ):
41
55
raise ValueError ('Please set data to reference a MargotDataFrame' )
42
56
57
+ def weekday (self , dt ):
58
+ """Return a human readable three letter day of week.
59
+
60
+ Convert the Python integer representation of day of week into a string.
61
+
62
+ e.g::
63
+
64
+ 0: 'MON' (also known as self.MONDAY)
65
+
66
+ .. note::
67
+ You should always use the built in constants when passing days of
68
+ the week. e.g. self.MONDAY, self.TUESDAY, ... these map to the three
69
+ charater strings.
70
+
71
+
72
+ Args:
73
+ dt (datetime or pd.Timestamp): The datetime to check
74
+
75
+ Returns:
76
+ str: One of; 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'
77
+ """
78
+ days = {
79
+ 0 : self .MONDAY ,
80
+ 1 : self .TUESDAY ,
81
+ 2 : self .WEDNESDAY ,
82
+ 3 : self .THURSDAY ,
83
+ 4 : self .FRIDAY ,
84
+ 5 : self .SATURDAY ,
85
+ 6 : self .SUNDAY
86
+ }
87
+ return days .get (dt .weekday ())
88
+
89
+ @property
90
+ def next_close (self ):
91
+ """Return a UTC pd.Timestamp of the next close of trading session.
92
+
93
+ Returns:
94
+ pd.Timestamp: Timestamp of the next close of cash session in UTC.
95
+ """
96
+ return self .market .next_close (
97
+ getattr (self , 'when' ,
98
+ pd .Timestamp (datetime .now (tz = pytz .UTC )))
99
+ )
100
+
43
101
def signal (self ) -> list :
44
102
"""Return a list of Position objects for a given datetime."""
45
103
raise NotImplementedError ("You must implement signal" )
@@ -61,6 +119,7 @@ def simulate_signal(self, when: datetime):
61
119
list: a list of Position objects.
62
120
"""
63
121
self .data .simulate (when )
122
+ self .when = when
64
123
positions = self .signal ()
65
124
self .data .end_simulation ()
66
125
return positions
0 commit comments