Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security Details Portfolios data #55

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1>Morningstar Connectors Demos</h1>
<li><a href="stock-timeseries/demo.html">Highcharts Stock + Morningstar TimeSeries</a></li>
<li><a href="stock-ohlcv/demo.html">Highcharts Stock + Morningstar OHLCV TimeSeries</a></li>
<li><a href="stock-securitydetails/demo.html">Highcharts Stock + Morningstar Security Details</a></li>
<li><a href="stock-securitydetails-asset-allocations/demo.html">Morningstar Security Details Asset Allocations</a></li>
<li><a href="dashboards-risk-score/demo.html">Highcharts Dashboards + Morningstar Risk Score</a></li>
</ul>
</body>
Expand Down
23 changes: 23 additions & 0 deletions demos/stock-securitydetails-asset-allocations/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script src="https://code.highcharts.com/stock/highstock.src.js"></script>
<script src="https://code.highcharts.com/connectors/morningstar/connectors-morningstar.src.js"></script>
<title>Morningstar Security Details Asset Allocations</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Morningstar Security Details Asset Allocations</h1>
<p>
Add your Postman environment file from Morningstar to start the demo:
<input type="file" id="postman-json" accept=".json,application/json" />
</p>
<div id="container"></div>
<script src="./demo.js"></script>
</body>
</html>
73 changes: 73 additions & 0 deletions demos/stock-securitydetails-asset-allocations/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
async function displayAssetAllocations (postmanJSON) {
const securityId = 'US4642898674';

const connector = new Connectors.Morningstar.SecurityDetailsConnector({

Check failure on line 4 in demos/stock-securitydetails-asset-allocations/demo.js

View workflow job for this annotation

GitHub Actions / Test eslint style

'Connectors' is not defined
pawelfus marked this conversation as resolved.
Show resolved Hide resolved
postman: {
environmentJSON: postmanJSON
},
security: {
id: securityId,
idType: 'ISIN'
},
type: 'AssetAllocations'
pawelfus marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong API option, demo doesn't work

});

await connector.load();

const typeMapping = {
'1': 'Stocks',
'2': 'Bonds',
'3': 'Cash',
'4': 'Other Instruments',
'99': 'Unclassified'
};

const chartData = connector.table.getRowObjects().map(item => ({
name: typeMapping[item.AssetAllocations_Type],
y: item.AssetAllocations_MorningstarEUR3_N
}));

Highcharts.chart('container', {
title: {
text: 'iShares Core Growth Allocation ETF (AOR) Asset Allocation'
},
subtitle: {
text: 'Type: MorningstarEUR3 | Sale Position: Net (N)'
},
series: [{
type: 'pie',
name: 'VTI Asset Allocation',
data: chartData
}]
});
}

async function handleSelectEnvironment (evt) {
const target = evt.target;
const postmanJSON = await getPostmanJSON(target);

target.parentNode.style.display = 'none';

displayAssetAllocations(postmanJSON);
}

document.getElementById('postman-json')
.addEventListener('change', handleSelectEnvironment);

async function getPostmanJSON (htmlInputFile) {
let file;
let fileJSON;

for (file of htmlInputFile.files) {
try {
fileJSON = JSON.parse(await file.text());
if (Connectors.Morningstar.isPostmanEnvironmentJSON(fileJSON)) {

Check failure on line 64 in demos/stock-securitydetails-asset-allocations/demo.js

View workflow job for this annotation

GitHub Actions / Test eslint style

'Connectors' is not defined
pawelfus marked this conversation as resolved.
Show resolved Hide resolved
break;
}
} catch (error) {
// fail silently
}
}

return fileJSON;
}
151 changes: 151 additions & 0 deletions src/SecurityDetails/Converters/AssetAllocationsConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* *
*
* (c) 2009-2024 Highsoft AS
*
* License: www.highcharts.com/license
*
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
*
* Authors:
* - Sophie Bremer
* - Pawel Lysy
* - Askel Eirik Johansson
*
* */


'use strict';


/* *
*
* Imports
*
* */


import {
SecurityDetailsConverterOptions,
SecurityDetailsMetadata
} from '../SecurityDetailsOptions';
import SecurityDetailsJSON from '../SecurityDetailsJSON';
import SecurityDetailsConverter from '../SecurityDetailsConverter';

/* *
*
* Class
*
* */


export class AssetAllocationsConverter extends SecurityDetailsConverter {


/* *
*
* Constructor
*
* */


public constructor (
options?: SecurityDetailsConverterOptions
) {
super(options);

this.metadata = {
columns: {}
};
}


/* *
*
* Properties
*
* */


public readonly metadata: SecurityDetailsMetadata;


/* *
*
* Functions
*
* */


public override parse (
options: SecurityDetailsConverterOptions
): void {
const metadata = this.metadata;
const table = this.table;
const userOptions = {
...this.options,
...options
};
const json = userOptions.json;

// Validate JSON

if (!SecurityDetailsJSON.isSecurityDetailsResponse(json)) {
throw new Error('Invalid data');
}

// Prepare table

table.deleteColumns();
table.setColumn('AssetAllocations_Type');

// Add asset allocations to table

if (json.length) {

// Update table

const securityDetails = json[0];
const assetAllocations = securityDetails.Portfolios[0].AssetAllocations;

table.setColumn('AssetAllocations_Type');

for (let i = 0; i < assetAllocations.length; i++) {
const asset = assetAllocations[i];

table.setColumn(`AssetAllocations_${asset.Type}_${asset.SalePosition}`);

for (let j = 0; j < asset.BreakdownValues.length; j++) {
table.setCell(
`AssetAllocations_${asset.Type}_${asset.SalePosition}`,
j,
asset.BreakdownValues[j].Value
);

table.setCell(
'AssetAllocations_Type',
j,
asset.BreakdownValues[j].Type
);
}
}

// Update meta data

metadata.id = securityDetails.Id;
metadata.isin = securityDetails.Isin;
}

}


}


/* *
*
* Default Export
*
* */


export default AssetAllocationsConverter;
Loading
Loading