29
29
*/
30
30
31
31
const tickers = require ( './stockList.js' ) ;
32
- const state = require ( './src/stockstate .js' ) ;
32
+ const state = require ( './src/stockData .js' ) ;
33
33
34
34
const createThrottle = require ( './src/createThrottle.js' ) ;
35
35
const throttle = createThrottle ( 1 , 1900 ) ;
@@ -61,33 +61,39 @@ const filter = process.argv[2] ? [
61
61
'signal.date === process.argv[2]'
62
62
] . join ( ' && ' ) : undefined ;
63
63
64
- ( function ( ) {
65
64
66
- // Create an array containing a promise for each ticker request.
67
- // Requests get individual catch blocks, so if one fails the rest can continue.
68
- const tickerRequests = tickers . map ( async ( ticker ) => {
69
- try {
70
- await throttle ( ) ;
71
-
72
- // Check for local data for this ticker first before retrieval.
73
- const localFile = disk . readStockDataFromDisk ( ticker ) ;
74
- let parsedData = await fetchStock ( ticker , needsFullRetrieval ( localFile ) ) ;
75
- // If local data exists, merge with fetched data.
76
- if ( localFile . lastDateRetrieved && localFile . data ) {
77
- parsedData = disk . mergeNewAndExistingData ( ticker , parsedData , localFile . data ) ;
78
- }
79
- disk . writeStockDataToDisk ( ticker , parsedData ) ;
80
-
81
- // Initialize ticker in state, and add parsed data for processing.
82
- initDataForStock ( ticker , parsedData ) ;
83
- processDataForStock ( ticker , parsedData ) ;
84
- }
85
- catch ( e ) {
86
- log ( 'warn' , e , e . stack ) ;
65
+ // Create an array containing a promise for each ticker request.
66
+ // Requests get individual catch blocks, so if one fails the rest can continue.
67
+ const tickerRequests = tickers . map ( async ( ticker ) => {
68
+ try {
69
+ await throttle ( ) ;
70
+
71
+ // Check for local data for this ticker first before retrieval.
72
+ const localFile = disk . readStockDataFromDisk ( ticker ) ;
73
+ let parsedData = await fetchStock ( ticker , needsFullRetrieval ( localFile ) ) ;
74
+
75
+ // If local data exists, merge with fetched data.
76
+ if ( localFile . lastDateRetrieved && localFile . data ) {
77
+ parsedData = disk . mergeNewAndExistingData ( ticker , parsedData , localFile . data ) ;
87
78
}
88
- } ) ;
79
+
80
+ // Write to disk.
81
+ disk . writeStockDataToDisk ( ticker , parsedData ) ;
82
+
83
+ // Initialize ticker in state, and add parsed data for processing.
84
+ initDataForStock ( ticker , parsedData ) ;
85
+
86
+ // Process data.
87
+ processDataForStock ( ticker , parsedData ) ;
88
+ }
89
+ catch ( e ) {
90
+ log ( 'warn' , e , e . stack ) ;
91
+ }
92
+ } ) ;
89
93
90
94
95
+ // Main
96
+ ( function ( ) {
91
97
// Execute all ticker requests, perform any needed retries, and apply search filter to signals.
92
98
Promise . all ( tickerRequests )
93
99
. then ( ( ) => {
@@ -140,14 +146,17 @@ function every(arr, filter) {
140
146
return result ;
141
147
}
142
148
149
+
143
150
function allResultsShort ( result ) {
144
151
return result . trade === 'short' ;
145
152
}
146
153
154
+
147
155
function allResultsLong ( result ) {
148
156
return result . trade === 'long' ;
149
157
}
150
158
159
+
151
160
// Returns true if localFile.data doesn't exist (data not fetched before), or if date ('2019-01-02') was more than 100 days ago, otherwise false.
152
161
function needsFullRetrieval ( localFile ) {
153
162
if ( ! localFile . data ) {
@@ -158,11 +167,13 @@ function needsFullRetrieval(localFile) {
158
167
return daysBetween ( mostRecentDate , currentDate ) > 100 ? true : false ;
159
168
}
160
169
170
+
161
171
function initDataForStock ( ticker , parsedData ) {
162
172
state . quotes [ ticker ] = { } ;
163
173
state . quotes [ ticker ] [ 'data' ] = parsedData ;
164
174
}
165
175
176
+
166
177
function processDataForStock ( ticker , parsedData ) {
167
178
try {
168
179
// Mark pivot highs and lows.
0 commit comments