1 | const Market = require('../broker/robinhood/Market');
|
2 | const schedule = require('node-schedule');
|
3 | const moment = require('moment');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | class Scheduler {
|
9 |
|
10 | |
11 |
|
12 |
|
13 |
|
14 |
|
15 | constructor(f) {
|
16 | this.f = f;
|
17 | this.job = null;
|
18 | }
|
19 |
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | onMarketOpen(offset) {
|
27 | const _this = this;
|
28 | if (!offset) offset = 0;
|
29 | return new Promise((resolve, reject) => {
|
30 | if (_this.job !== null) reject(new Error("You must cancel this job before scheduling it again!"));
|
31 | else Market.getByMIC("XNYS").then(nyse => {
|
32 | nyse.getNextOpen().then(next => {
|
33 | let date = new Date(next.getTime() + offset);
|
34 | if (moment(date).isBefore(moment())) {
|
35 | date = moment(date).add('1', 'day').toDate();
|
36 | }
|
37 | _this.job = schedule.scheduleJob(date, (invocationDate) => {
|
38 | _this.f();
|
39 | let date = moment(invocationDate);
|
40 | schedule.scheduleJob(date.add('1', 'day'), _this.f);
|
41 | });
|
42 | resolve(_this.job.nextInvocation().toDate());
|
43 | })
|
44 | });
|
45 | })
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | onMarketClose(offset) {
|
55 | const _this = this;
|
56 | if (!offset) offset = 0;
|
57 | return new Promise((resolve, reject) => {
|
58 | if (_this.job !== null) reject(new Error("You must cancel this job before scheduling it again!"));
|
59 | else Market.getByMIC("XNYS").then(nyse => {
|
60 | nyse.getNextClose().then(next => {
|
61 | let date = new Date(next.getTime() + offset);
|
62 | if (moment(date).isBefore(moment())) {
|
63 | date = moment(date).add('1', 'day').toDate();
|
64 | }
|
65 | _this.job = schedule.scheduleJob(date, (invocationDate) => {
|
66 | _this.f();
|
67 | let date = moment(invocationDate);
|
68 | schedule.scheduleJob(date.add('1', 'day'), _this.f);
|
69 | });
|
70 | resolve(_this.job.nextInvocation().toDate());
|
71 | })
|
72 | });
|
73 | });
|
74 | }
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | every(minutes, extended) {
|
83 | const _this = this;
|
84 | return new Promise((resolve, reject) => {
|
85 | if (_this.job !== null) reject(new Error("You must cancel this job before scheduling it again!"));
|
86 | else {
|
87 | _this.job = schedule.scheduleJob("*/" + minutes + " * * * 1-5", () => {
|
88 | Market.getByMIC("XNYS").then(nyse => {
|
89 | if (nyse.isOpenNow()) _this.f();
|
90 | else if (extended && nyse.isExtendedOpenNow()) _this.f();
|
91 | })
|
92 | });
|
93 | resolve(_this.job.nextInvocation().toDate());
|
94 | }
|
95 | });
|
96 | }
|
97 |
|
98 | |
99 |
|
100 |
|
101 |
|
102 | cancel() {
|
103 | if (this.job === null) return new Error("This job has not been scheduled yet.");
|
104 | else {
|
105 | this.job.cancel();
|
106 | this.job = null;
|
107 | }
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 | getNext() {
|
116 | if (this.job === null) return new Error("This job has not been scheduled yet.");
|
117 | else return this.job.nextInvocation();
|
118 | }
|
119 |
|
120 | }
|
121 |
|
122 | module.exports = Scheduler; |
\ | No newline at end of file |