forked from melder/trading_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
119 lines (105 loc) · 3.94 KB
/
scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from datetime import datetime
import date_helpers as dh
def jobs():
return Scheduler.get_jobs()
class Scheduler:
"""
Schedule actions on a minute-by-minute granularity basis.
Provides more flexibility than cron jobs
"""
# move jobs to yml?
# higher priority jobs on top
jobs = [
{"module": "strangler", "action": "buy", "before_close": 6, "active": False},
{"module": "iv", "action": "run", "before_close": 18, "active": False},
{
"module": "strangler",
"action": "open_sells",
"after_open": 4,
"active": False,
},
{
"module": "strangle",
"action": "log_active_strangles",
"every": 60,
"market_hours": True,
"active": False,
},
{
"module": "strangle",
"action": "eow_results",
"after_close": 5,
"active": False,
},
{
"module": "date_helpers",
"action": "expire_current_expr",
"after_close": 15,
"active": True,
},
{"module": "iv", "action": "run_condor", "before_close": 150, "active": True},
{"module": "condorer", "action": "buy", "before_close": 135, "active": True},
{
"module": "condorer",
"action": "set_sell_limits",
"after_open": 1,
"active": True,
},
#{"module": "condorer", "action": "sell", "before_close": 120, "active": True},
{
"module": "condorer_spy",
"action": "buy",
"before_expr_daily": 391,
"active": True,
},
]
@classmethod
def get_jobs(cls):
return cls().run()
def __init__(self, iso_date=None):
self.today_date_utc = iso_date or dh.today_date_utc()
self.market_open = dh.is_market_open_on(self.today_date_utc)
self.opens_at = dh.market_opens_at(self.today_date_utc)
self.closes_at = dh.market_closes_at(self.today_date_utc)
self.closes_at_next_daily = dh.market_closes_at(dh.next_expr_dailies())
self.market_minutes_remaining = -1
self.market_minutes_elapsed = -1
self.after_market_minutes_elapsed = -1
self.before_market_minutes_remaining = -1
self.market_minutes_remaining_next_daily = -1
if self.opens_at:
self.market_minutes_elapsed = int(
dh.market_seconds_between(self.opens_at, datetime.utcnow()) // 60
)
self.market_minutes_remaining = int(
dh.market_seconds_between(datetime.utcnow(), self.closes_at) // 60
)
self.after_market_minutes_elapsed = int(
max(dh.seconds_after_market_close(self.today_date_utc) // 60, 0)
)
self.before_market_minutes_remaining = int(
max(dh.seconds_before_market_open(self.today_date_utc) // 60, 0)
)
self.market_minutes_remaining_next_daily = int(
dh.market_seconds_between(datetime.utcnow(), self.closes_at_next_daily)
// 60
)
def run(self):
_jobs = []
for j in self.jobs:
if j.get("before_close") == self.market_minutes_remaining:
_jobs.append(j)
if j.get("after_close") == self.after_market_minutes_elapsed:
_jobs.append(j)
if j.get("after_open") == self.market_minutes_elapsed:
_jobs.append(j)
if j.get("before_open") == self.before_market_minutes_remaining:
_jobs.append(j)
if j.get("before_expr_daily") == self.market_minutes_remaining_next_daily:
_jobs.append(j)
if j.get("market_hours") and dh.is_market_open_now():
if self.market_minutes_elapsed % j.get("every") == 0:
_jobs.append(j)
return _jobs
if __name__ == "__main__":
print(jobs())