-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateFloatTurnover.js
100 lines (93 loc) · 3.64 KB
/
calculateFloatTurnover.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
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
/**
Calculates the percentage float turnover for a stock since a given date.
Takes a ticker, a total float number, and a calculation start date.
Accumulates the daily volume since the start date, returns the percentage of float
turnover since that date.
Example usage: node calculateFloatTurnover GPRO 77930000 2017-01-04
*/
const log = require('./src/logger.js');
const rp = require('request-promise');
const config = require('./config.js')
const parseRawData = require('./src/parseRawData.js');
const fetchDataForOneStock = require('./src/fetchDataForOneStock.js');
const ticker = process.argv[2];
const totalFloat = parseInt(process.argv[3]);
const startDate = process.argv[4];
const reverse = process.argv[5] === 'reverse' ? true : false;
const getFloatTurnovers = (volByDate, totalFloat, startDate, reverse) => {
for (let i = 0; i < volByDate.length; i++) {
// Iterate until we find the startDate, then start calculating turnover cycles.
if (volByDate[i][0] === startDate) {
const daysSubset = reverse ? volByDate.slice(0, i) : volByDate.slice(i);
return calculateCycles(daysSubset, totalFloat, reverse);
}
}
throw `${ticker} - Start date ${startDate} not found in data.`;
};
function calculateCycles(volByDate, totalFloat, reverse) {
const turnoverDates = [];
const results = {
percentFloatRemaining: null,
floatRemaining: null,
turnovers: null,
estimatedDaysRemaining: null,
averageVolPerDay: null,
};
let remainingFloatInCycle = totalFloat;
let cumulativeDailyVolume = 0;
let daysConsidered = 0;
if (reverse) {
for (let j = volByDate.length - 1; j >= 0; j--) {
const todaysDate = volByDate[j][0];
const todaysVolume = volByDate[j][1];
remainingFloatInCycle = remainingFloatInCycle - todaysVolume;
// If we've found a turnover date, add to list and reset count.
if (remainingFloatInCycle < 0) {
turnoverDates.push(todaysDate)
remainingFloatInCycle += totalFloat;
}
// If this is the last day, return remaining float and list of turnover dates.
if (j === 0) {
results.floatRemaining = remainingFloatInCycle;
results.turnovers = turnoverDates;
results.percentFloatRemaining = (remainingFloatInCycle / totalFloat) * 100;
}
}
}
else {
for (let j = 0; j < volByDate.length; j++) {
const todaysDate = volByDate[j][0];
const todaysVolume = volByDate[j][1];
remainingFloatInCycle = remainingFloatInCycle - todaysVolume;
// Accumulate volume.
cumulativeDailyVolume += todaysVolume;
daysConsidered++;
// If we've found a turnover date, add to list and reset count.
if (remainingFloatInCycle < 0) {
turnoverDates.push(todaysDate)
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;
results.percentFloatRemaining = (remainingFloatInCycle / totalFloat) * 100;
}
}
results.averageVolPerDay = cumulativeDailyVolume / daysConsidered;
results.estimatedDaysRemaining = results.floatRemaining / results.averageVolPerDay;
}
return results;
};
fetchDataForOneStock(ticker, true)
.then(transformedData => {
const volumeByDate = transformedData.map(day => {
return [day.date, day.v];
});
return volumeByDate;
})
.then(volumeByDate => {
const result = getFloatTurnovers(volumeByDate, totalFloat, startDate, reverse);
log('info', JSON.stringify(result));
})
.catch(e => log('warn', e, e.stack));