-
Notifications
You must be signed in to change notification settings - Fork 4
/
float.js
69 lines (64 loc) · 2.17 KB
/
float.js
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
const log = console.log;
const rp = require('request-promise');
const config = require('./config.js')
const transformData = require('./src/transformData.js');
// ex usage: node float GPRO 77930000 2017-01-04
const ticker = process.argv[2];
const totalFloat = parseInt(process.argv[3]);
const startDate = process.argv[4];
const fetchDataForOneStock = (ticker) => new Promise((resolve, reject) => {
log('Getting: ', '\x1b[34m', ticker, '\x1b[0m');
return rp({ // Request data for this stock.
uri: 'https://www.alphavantage.co/query',
json: true,
qs: {
apikey: config.API_KEY,
function: 'TIME_SERIES_DAILY',
symbol: ticker,
outputsize: 'full'
},
transform: transformData // Clean up the raw data.
}).then((transformedData) => {
resolve(transformedData);
});
});
const getFloatTurnovers = (volByDate, totalFloat, startDate) => {
for(let i = 0; i < volByDate.length; i++) {
// Iterate until we find the startDate, then start calculating turnover cycles.
if(volByDate[i][0] === startDate) {
return calculateCycles(volByDate.slice(i), totalFloat);
}
}
function calculateCycles(volByDate, totalFloat) {
let remainingFloatInCycle = totalFloat;
let turnoverDates = [];
let results = {
'floatRemaining': null,
'turnovers': null
};
for(let j = 0; j < volByDate.length; j++) {
remainingFloatInCycle = remainingFloatInCycle - volByDate[j][1];
// If we've found a turnover date, add to list and reset count.
if(remainingFloatInCycle < 0) {
turnoverDates.push(volByDate[j][0])
remainingFloatInCycle += totalFloat;
}
// If this is the last day, return remaining float and list of turnover dates.
if(j === volByDate.length-1) {
results.floatRemaining = remainingFloatInCycle;
results.turnovers = turnoverDates;
}
}
return results;
}
};
fetchDataForOneStock(ticker)
.then((transformedData) => {
let volumeByDate = transformedData.map((day) => {
return [day.date, day.v];
});
return volumeByDate;
}).then((volumeByDate) => {
let result = getFloatTurnovers(volumeByDate, totalFloat, startDate);
log(result);
});