Last active
May 18, 2020 16:53
-
-
Save calvinfroedge/63ab336d6b34ac3bc8edf4c188bc83fa to your computer and use it in GitHub Desktop.
tanker dividends analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const csv=require('csvtojson') | |
const _=require('lodash'); | |
const moment = require('moment'); | |
const dateFormat = 'YYYY-MM-DD'; | |
const tickerColumn = 'TICKER'; | |
const exDivColumn = 'EX-DIV DATE'; | |
const dividendAmountColumn = 'DIVIDEND AMOUNT'; | |
(async ()=>{ | |
let files = { | |
} | |
let results = { | |
} | |
const tickers = ['DHT', 'NAT', 'TNP', 'EURN', 'FRO', 'NNA', 'STNG', 'INSW', 'TNK']; | |
const data = await Promise.all(tickers.map(ticker => csv().fromFile(`./${ticker}.csv`))); | |
tickers.map((ticker, i) => { | |
let byDate = {}; | |
data[i].map((item, j)=>{ | |
if(data[i][j+1]){ | |
item.prevClose = Number(data[i][j+1]['Close/Last'].replace('$', '')); | |
item.prevCloseFull = data[i][j+1]; | |
} | |
byDate[item.Date] = item; | |
}); | |
files[ticker] = byDate; | |
results[ticker] = []; | |
}); | |
const exDivs = await csv().fromFile('./dividends.csv'); | |
exDivs.map((item)=>{ | |
const ticker = item[tickerColumn]; | |
const divDate = item[exDivColumn]; | |
const divAmount = Number(item[dividendAmountColumn].replace('$', '')); | |
const dataForDate = files[ticker][divDate]; | |
if(dataForDate){ | |
let close = Number(dataForDate['Close/Last'].replace('$', '')); | |
const open = Number(dataForDate['Open'].replace('$', '')); | |
const prevClose = dataForDate.prevClose; | |
const expected = prevClose - divAmount; | |
const difference = Number((close - expected).toFixed(2)); | |
const pctDifference = Number((100 * (difference / divAmount)).toFixed(2)); | |
results[ticker].push([ | |
divDate, | |
divAmount, | |
prevClose, | |
expected, | |
open, | |
close, | |
difference, | |
pctDifference | |
]); | |
} | |
}); | |
let resultsToPrint = { | |
} | |
let resultKeys = ['datapoints', 'expectations', 'beatAmt']; | |
resultKeys.map(key => resultsToPrint[key] = []) | |
_.forEach(results, (result, ticker)=>{ | |
let greaterThan = 0; | |
let lessThan = 0; | |
let pcts = []; | |
let amts = []; | |
result.forEach((one)=>{ | |
let pctDifference = one[one.length-1]; | |
let amtDifference = one[one.length-2]; | |
if(pctDifference > 0){ | |
++greaterThan; | |
} else { | |
++lessThan; | |
} | |
pcts.push(pctDifference); | |
amts.push(amtDifference); | |
}); | |
// Number of datapoints for each | |
resultsToPrint.datapoints.push(`$${ticker} ${greaterThan + lessThan}`); | |
if(lessThan === 0){ | |
resultsToPrint.expectations.push(`$${ticker} 100%`); | |
} else { | |
// how often beat expectations? | |
resultsToPrint.expectations.push(`$${ticker} ${( 100 * (greaterThan/(greaterThan + lessThan))).toFixed(2)}%`); | |
} | |
// What amt beat? | |
resultsToPrint.beatAmt.push(`$${ticker} ${_.mean(amts).toFixed(2)}`); | |
}); | |
resultKeys.forEach(key => { | |
console.log(key); | |
resultsToPrint[key].forEach((item)=>{ | |
console.log(item); | |
}) | |
console.log('') | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment