doppelgunner-stock
Advanced tools
Comparing version 1.0.3 to 2.0.0
@@ -6,4 +6,4 @@ const http = require('http'); | ||
const request = require('superagent'); | ||
const d3dsv = require('d3-dsv'); | ||
const HPModel = require('./HPModel'); | ||
const HPCommons = require('./HPCommons'); | ||
@@ -25,11 +25,12 @@ const SC = require('./StockConstants'); | ||
stream.on('data', (data) => { | ||
histPricesArr.push(data); | ||
let str = ("" + data).trim().replace(/\s/g, ""); | ||
histPricesArr.push(str); | ||
}) | ||
.on('end', () => { | ||
let hpModel = new HPModel(histPricesArr.shift(),histPricesArr); | ||
_.forEach(hpModel.data, row => { | ||
row[0] = HPCommons | ||
.toMoment(row[0], SC.DATE_FORMAT_WSJ); | ||
let joinedArr = histPricesArr.join("\n").toLowerCase(); | ||
let result = d3dsv.csvParse(joinedArr); | ||
_.forEach(result, row => { | ||
row.date = HPCommons.toMoment(row.date, SC.DATE_FORMAT_WSJ); | ||
}); | ||
funcCallback(hpModel); | ||
funcCallback(result); | ||
}); | ||
@@ -36,0 +37,0 @@ } |
120
HPCommons.js
const _ = require('lodash'); | ||
const moment = require('moment'); | ||
const HPModel = require('./HPModel'); | ||
const SC = require('./StockConstants'); | ||
@@ -20,4 +19,3 @@ const PeaksModel = require('./PeaksModel'); | ||
getLowestPeaks: getLowestPeaks, | ||
toMoment: toMoment, | ||
@@ -29,19 +27,17 @@ divideRows: divideRows | ||
* gets the lowest peaks of certain column given rows divided by days | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {number} days | ||
* @param {string} columnName | ||
*/ | ||
function getLowestPeaks(hpModel, days, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
function getLowestPeaks(d3Model, days, columnName) { | ||
let dividedRows = divideRows(hpModel, days); | ||
let dividedRows = divideRows(d3Model, days); | ||
let lowestPeaks = []; | ||
_.forEach(dividedRows, group => { | ||
let minRow = _.minBy(group, (row) => +row[columnIndex]); | ||
let minRow = _.minBy(group, (row) => +row[columnName]); | ||
lowestPeaks.push(minRow); | ||
}); | ||
return new PeaksModel(SC.PEAK_LOWEST, days, columnName, columnIndex, lowestPeaks); | ||
return new PeaksModel(SC.PEAK_LOWEST, days, columnName, lowestPeaks); | ||
} | ||
@@ -51,19 +47,17 @@ | ||
* gets the highest peaks of certain column given rows divided by days | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {number} days | ||
* @param {string} columnName | ||
*/ | ||
function getHighestPeaks(hpModel, days, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
function getHighestPeaks(d3Model, days, columnName) { | ||
let dividedRows = divideRows(hpModel, days); | ||
let dividedRows = divideRows(d3Model, days); | ||
let highestPeaks = []; | ||
_.forEach(dividedRows, group => { | ||
let maxRow = _.maxBy(group, (row) => +row[columnIndex]); | ||
let maxRow = _.maxBy(group, (row) => +row[columnName]); | ||
highestPeaks.push(maxRow); | ||
}); | ||
return new PeaksModel(SC.PEAK_HIGHEST, days, columnName, columnIndex, highestPeaks); | ||
return new PeaksModel(SC.PEAK_HIGHEST, days, columnName, highestPeaks); | ||
} | ||
@@ -73,6 +67,6 @@ | ||
* divide rows per number of days starting from the latest | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {number} days | ||
*/ | ||
function divideRows(hpModel, days) { | ||
function divideRows(d3Model, days) { | ||
if (days < 1) return undefined; | ||
@@ -83,3 +77,3 @@ | ||
let counter = 1; | ||
_.forEach(hpModel.data, (row,index) => { | ||
_.forEach(d3Model, (row,index) => { | ||
if (counter > days) { | ||
@@ -89,3 +83,3 @@ counter = 1; | ||
group = []; | ||
} else if (index === hpModel.data.length - 1) { | ||
} else if (index === d3Model.length - 1) { | ||
group.push(row); | ||
@@ -104,7 +98,7 @@ dividedRows.push(group); | ||
* gets the time listed of the given stock security in format Y year/s and M month/s | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
*/ | ||
function getTimeListed(hpModel) { | ||
let started = getStartedDate(hpModel); | ||
let latest = getLatestDate(hpModel); | ||
function getTimeListed(d3Model) { | ||
let started = getStartedDate(d3Model); | ||
let latest = getLatestDate(d3Model); | ||
@@ -128,11 +122,9 @@ let duration = moment.duration(latest.diff(started)); | ||
* gets the average of the min and max values of a column given column name | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {string} columnName | ||
*/ | ||
function getMinMaxAverage(hpModel, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
function getMinMaxAverage(d3Model, columnName) { | ||
let min = getMinimum(hpModel, columnName); | ||
let max = getMaximum(hpModel, columnName); | ||
let min = getMinimum(d3Model, columnName); | ||
let max = getMaximum(d3Model, columnName); | ||
return (min + max) / 2; | ||
@@ -143,12 +135,10 @@ } | ||
* gets the average of the values of a column given column name | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {string} columnName | ||
*/ | ||
function getAverage(hpModel, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
function getAverage(d3Model, columnName) { | ||
return _.reduce(hpModel.data, (total, row) => { | ||
return total + +row[columnIndex]; | ||
}, 0) / hpModel.data.length; | ||
return _.reduce(d3Model, (total, row) => { | ||
return total + +row[columnName]; | ||
}, 0) / d3Model.length; | ||
} | ||
@@ -158,12 +148,10 @@ | ||
* gets the minimum value of a column given column name | ||
* @param {HPModel} hpModel | ||
* @param {[]]} d3Model | ||
* @param {string} columnName | ||
*/ | ||
function getMinimum(hpModel, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
function getMinimum(d3Model, columnName) { | ||
return _.chain(hpModel.data) | ||
return _.chain(d3Model) | ||
.map(row => { | ||
return +row[columnIndex]; | ||
return +row[columnName]; | ||
}) | ||
@@ -175,12 +163,10 @@ .min().value(); | ||
* gets the maximum value of a column given column name | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {string} columnName | ||
*/ | ||
function getMaximum(hpModel, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
function getMaximum(d3Model, columnName) { | ||
return _.chain(hpModel.data) | ||
return _.chain(d3Model) | ||
.map(row => { | ||
return +row[columnIndex]; | ||
return +row[columnName]; | ||
}) | ||
@@ -192,14 +178,11 @@ .max().value(); | ||
* gets the latest value of a column given column name | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {string} columnName | ||
*/ | ||
function getLatest(hpModel, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
return _.first(hpModel.data)[columnIndex]; | ||
function getLatest(d3Model, columnName) { | ||
return +_.first(d3Model)[columnName]; | ||
} | ||
/** | ||
* | ||
* @deprecated since version 2.0 | ||
* @param {HPModel} hpModel | ||
@@ -220,11 +203,8 @@ * @param {string} columnName | ||
* gets data given column header name | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
* @param {string} columnName | ||
*/ | ||
function getColumn(hpModel, columnName) { | ||
let columnIndex = getColumnIndex(hpModel, columnName); | ||
if (columnIndex === -1) return undefined; | ||
return _.map(hpModel.data, (row) => { | ||
return row[columnIndex]; | ||
function getColumn(d3Model, columnName) { | ||
return _.map(d3Model, (row) => { | ||
return row[columnName]; | ||
}); | ||
@@ -235,6 +215,6 @@ } | ||
* Gets the started date of the historical price | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
*/ | ||
function getStartedDate(hpModel) { | ||
return _.last(hpModel.data)[0]; | ||
function getStartedDate(d3Model) { | ||
return _.last(d3Model).date; | ||
} | ||
@@ -244,7 +224,7 @@ | ||
* Gets the latest date of the historical price | ||
* @param {HPModel} hpModel | ||
* @param {[]} d3Model | ||
*/ | ||
function getLatestDate(hpModel) { | ||
return _.first(hpModel.data)[0]; | ||
function getLatestDate(d3Model) { | ||
return _.first(d3Model).date; | ||
} | ||
@@ -7,5 +7,3 @@ | ||
StockConstants: require('./StockConstants'), | ||
HPModel: require('./HPModel'), | ||
PeaksModel: require('./PeaksModel') | ||
} |
{ | ||
"name": "doppelgunner-stock", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "stock api for node connects with WSJ as of now", | ||
@@ -20,2 +20,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"d3-dsv": "^1.0.8", | ||
"dateformat": "^3.0.2", | ||
@@ -22,0 +23,0 @@ "fast-csv": "^2.4.1", |
@@ -11,11 +11,9 @@ /** | ||
* @param {string} columnName | ||
* @param {number} columnIndex | ||
* @param {number} length | ||
* @param {[]} peaks | ||
*/ | ||
constructor(peakType, days, columnName, columnIndex, peaks) { | ||
constructor(peakType, days, columnName, peaks) { | ||
this.peakType = peakType; | ||
this.days = days; | ||
this.columnName = columnName; | ||
this.columnIndex = columnIndex; | ||
this.length = peaks.length; | ||
@@ -22,0 +20,0 @@ this.peaks = peaks; |
@@ -9,3 +9,9 @@ | ||
const _ = require('lodash'); | ||
const d3dsv = require('d3-dsv'); | ||
// let data = `date, high, low, open, close, volume | ||
// November, 10, 5, 8, 9, 1000 | ||
// December, 20, 5, 5, 3.5, 2000`; | ||
// let parsed = d3dsv.csvParse(data); | ||
// console.log(parsed.length); | ||
@@ -15,6 +21,5 @@ //TODO pass an array then encapsulate the download on load(downloader) | ||
HP.load(HP.downloadWSJ('X',false,'PH'), funcCallback); | ||
function funcCallback(hpModel) { | ||
function funcCallback(d3Model) { | ||
new Promise((resolve,reject) => { | ||
resolve(hpModel); | ||
resolve(d3Model); | ||
}) | ||
@@ -25,7 +30,14 @@ | ||
//SAMPLE of getLowestPeaks, gets peaks within range of 30 days or 1 month in close column | ||
.then(x => HPCommons.getLowestPeaks(x, 30, 'close')) | ||
.then(peaks => console.log(peaks)); | ||
// .then(x => HPCommons.getLowestPeaks(x, 150, 'close')) | ||
// .then(peaks => console.log(peaks)); | ||
//SAMPLE of getHighestPeaks | ||
// .then(x => HPCommons.getHighestPeaks(x, 150, 'close')) | ||
// .then(peaks => console.log(peaks)); | ||
//SAMPLE of divided data by days, in the example group them per 5 days | ||
// .then(x => HPCommons.divideRows(x,5)) | ||
// .then(x => { | ||
// console.log("total: " + x.length); | ||
// return HPCommons.divideRows(x,5); | ||
// }) | ||
// .then(divided => { | ||
@@ -66,7 +78,7 @@ // let toPrint = ''; | ||
// }}) | ||
// .then(d => console.log("latest:", d.latest, ",last:", d.last)); | ||
// .then(d => console.log("latest:", d.latest, ", last:", d.last)); | ||
//SAMPLE OF getColumn(hpModel, columnName) | ||
//.then(xurpas => HPCommons.getColumn(xurpas, 'date')) | ||
//.then(dateColumn => console.log(dateColumn)); | ||
// .then(xurpas => HPCommons.getColumn(xurpas, 'date')) | ||
// .then(dateColumn => console.log(dateColumn)); | ||
} | ||
@@ -73,0 +85,0 @@ |
12341
6
8
334
+ Addedd3-dsv@^1.0.8
+ Addedcommander@2.20.3(transitive)
+ Addedd3-dsv@1.2.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedrw@1.3.3(transitive)