Created
June 1, 2018 12:19
-
-
Save Kirens/c0dc49be3d44f53e23d6aa873efd4c57 to your computer and use it in GitHub Desktop.
Avanza tools
This file contains hidden or 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
| // ==UserScript== | |
| // @name Avanza tools | |
| // @version 1 | |
| // @grant none | |
| // ==/UserScript== | |
| HTMLCollection.prototype.toArray = function(){ return Array.from(this) } | |
| const realConcat = Array.prototype.concat | |
| Array.prototype.concat = function() { | |
| if(arguments.length) return realConcat.apply(this, arguments) | |
| else return realConcat.apply([], this) | |
| } | |
| const getInterestingTables = ()=> | |
| document.getElementsByTagName('table').toArray() | |
| .map(t => ({table: t, name: t.caption && t.caption.firstElementChild.textContent.trim()})) | |
| .filter(t=>t.name) | |
| .reduce((acc, {name, table}) => ({...acc, [name]: table}), {}) | |
| const getTableColIdx = name => table => | |
| table.tHead.rows[0].cells.toArray() | |
| .map((c, i) => c.textContent.trim() == name && i) | |
| .filter(v=>v!==false)[0] | |
| const colObject = colMap => table => | |
| table.tBodies.toArray() | |
| .map(t => | |
| t.rows.toArray() | |
| .map(r => arrToObj(['idx', colMap])(r.cells.toArray())) | |
| ) | |
| .concat() | |
| const arrToObj = ([kind, mapper]) => a => a.reduce((acc, v, i) => { | |
| if(kind === 'string') acc[v[mapper.name]] = mapper.fn(v) | |
| else if(kind === 'idx' && mapper[i]) acc[mapper[i].name] = mapper[i].fn(v) | |
| return acc | |
| }, {}) | |
| const trimedName = e => e.textContent.replace(/\s+/g, ' ').trim() | |
| const parseNumber = n => +n.replace(/[^\d.,]/g, '').replace(/,/g, '.') | |
| const trimParse = e => parseNumber(trimedName(e)) | |
| const nameCol = getTableColIdx('Värdepapper') | |
| const valCol = getTableColIdx('Markn.värde') | |
| const nameValueCol = t => ({[nameCol(t)]: {name: 'name', fn: trimedName}, [valCol(t)]: {name: 'value', fn: trimParse}}) | |
| const avkastning = t => colObject(nameValueCol(t))(t) | |
| //console.log(arrToObj(['string', {name: 'name', fn: o=>o.value}])(avkastning(getInterestingTables().Fonder))) | |
| const räntefonder = ['KPA Etisk Räntefond', 'Swedbank Robur Penningmarknadsfond', 'Spiltan Räntefond Sverige', 'AMF Räntefond Lång', 'SPP Obligationsfond', 'Handelsbanken Realräntefond', 'Öhman Obligationsfond A'] | |
| const getBondPercentage = ()=>{ | |
| const values = avkastning(getInterestingTables().Fonder) | |
| const bonds = values.filter(v => räntefonder.indexOf(v.name) !== -1).reduce((acc, v) => acc + v.value, 0) | |
| const other = values.filter(v => räntefonder.indexOf(v.name) === -1).reduce((acc, v) => acc + v.value, 0) | |
| return bonds/(bonds+other) | |
| } | |
| const percentageString = v => Math.round(v*10000)/100 + '%' | |
| const addPercentage = p => { | |
| const foot = getInterestingTables().Fonder.tFoot.rows[0] | |
| foot.cells[0].colSpan = 2 | |
| foot.insertCell(1).textContent = p | |
| const newRow = document.getElementsByTagName('table')[0].tBodies[0].insertRow() | |
| newRow.insertCell().textContent = 'Andel räntefonder' | |
| const pCell = newRow.insertCell() | |
| pCell.textContent = p | |
| pCell.style.textAlign = 'right' | |
| } | |
| addPercentage(percentageString(getBondPercentage())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment