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

Added PriceSeriesConverter #17

Merged
merged 13 commits into from
Aug 20, 2024
6 changes: 4 additions & 2 deletions src/TimeSeries/Converters/CumulativeReturnSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* */


import MorningstarConverter from '../../Shared/MorningstarConverter';
import TimeSeriesConverter from '../TimeSeriesConverter';
import { CumulativeReturnSeriesOptions } from '../TimeSeriesOptions';
import TimeSeriesJSON from '../TimeSeriesJSON';

Expand All @@ -48,7 +48,7 @@ interface CumulativeReturn {
* */


export class CumulativeReturnSeriesConverter extends MorningstarConverter {
export class CumulativeReturnSeriesConverter extends TimeSeriesConverter {


/* *
Expand Down Expand Up @@ -76,6 +76,8 @@ export class CumulativeReturnSeriesConverter extends MorningstarConverter {

public override readonly options: Required<CumulativeReturnSeriesOptions>;

public override path: string = 'timeseries/cumulativereturn';


/* *
*
Expand Down
6 changes: 4 additions & 2 deletions src/TimeSeries/Converters/DividendSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* */


import MorningstarConverter from '../../Shared/MorningstarConverter';
import TimeSeriesConverter from '../TimeSeriesConverter';
import { DividendSeriesOptions } from '../TimeSeriesOptions';
import TimeSeriesJSON from '../TimeSeriesJSON';

Expand All @@ -49,7 +49,7 @@ interface Dividend {
* */


export class DividendSeriesConverter extends MorningstarConverter {
export class DividendSeriesConverter extends TimeSeriesConverter {


/* *
Expand Down Expand Up @@ -77,6 +77,8 @@ export class DividendSeriesConverter extends MorningstarConverter {

public override readonly options: Required<DividendSeriesOptions>;

public override path: string = 'timeseries/dividend';


/* *
*
Expand Down
6 changes: 4 additions & 2 deletions src/TimeSeries/Converters/GrowthSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* */


import MorningstarConverter from '../../Shared/MorningstarConverter';
import TimeSeriesConverter from '../TimeSeriesConverter';
import { GrowthSeriesOptions } from '../TimeSeriesOptions';
import TimeSeriesJSON from '../TimeSeriesJSON';

Expand All @@ -48,7 +48,7 @@ interface Growth {
* */


export class GrowthSeriesConverter extends MorningstarConverter {
export class GrowthSeriesConverter extends TimeSeriesConverter {


/* *
Expand Down Expand Up @@ -76,6 +76,8 @@ export class GrowthSeriesConverter extends MorningstarConverter {

public override readonly options: Required<GrowthSeriesOptions>;

public override path: string = 'timeseries/growth';


/* *
*
Expand Down
177 changes: 177 additions & 0 deletions src/TimeSeries/Converters/PriceSeriesConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/* *
*
* (c) 2009-2024 Highsoft AS
*
* License: www.highcharts.com/license
*
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
*
* Authors:
* - Eskil Gjerde Sviggum
*
* */


'use strict';


/* *
*
* Imports
*
* */


import TimeSeriesConverter from '../TimeSeriesConverter';
import { PriceSeriesOptions } from '../TimeSeriesOptions';
import TimeSeriesJSON from '../TimeSeriesJSON';
import MorningstarURL from '../../Shared/MorningstarURL';


/* *
*
* Declarations
*
* */


interface Price {
Id: string;
EndDate: number;
Value: number;
}


/* *
*
* Class
*
* */


export class PriceSeriesConverter extends TimeSeriesConverter {


/* *
*
* Constructor
*
* */


public constructor (
options: PriceSeriesOptions = { type: 'Price' }
) {
super(options);

this.options = options as Required<PriceSeriesOptions>;
}


/* *
*
* Properties
*
* */


public override readonly options: Required<PriceSeriesOptions>;

public override path: string = 'timeseries/price';


/* *
*
* Functions
*
* */


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

// Validate JSON
if (!TimeSeriesJSON.isResponse(json)) {
throw new Error('Invalid data');
}

// Cumulate prices by date

const securityIds: Array<string> = [];
const sortedPrices: Array<Price> = [];

for (const security of json.TimeSeries.Security) {

if (!security.HistoryDetail) {
continue;
}

securityIds.push(security.Id);

for (const detail of security.HistoryDetail) {
sortedPrices.push({
EndDate: Date.parse(detail.EndDate),
Id: security.Id,
Value: parseFloat(detail.Value)
});
}

}

// Sort prices by date

sortedPrices.sort((a, b) => (
a.EndDate === b.EndDate ?
0 :
a.EndDate < b.EndDate ? -1 : 1
));

// Reset table

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

for (const securityId of securityIds) {
table.setColumn(securityId);
}

// Add prices to table

let currentTableDate: number = 0;
let currentTableIndex: number = -1;

for (const price of sortedPrices) {
if (currentTableDate !== price.EndDate) {
currentTableDate = price.EndDate;
table.setCell('Date', ++currentTableIndex, currentTableDate);
}
table.setCell(price.Id, currentTableIndex, price.Value);
}

}

public override decorateURL (url: MorningstarURL) {
if (this.options.priceType !== undefined) {
url.searchParams.set('priceType', this.options.priceType);
}
}


}


/* *
*
* Default Export
*
* */


export default PriceSeriesConverter;
6 changes: 4 additions & 2 deletions src/TimeSeries/Converters/RatingSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* */


import MorningstarConverter from '../../Shared/MorningstarConverter';
import TimeSeriesConverter from '../TimeSeriesConverter';
import { RatingSeriesOptions } from '../TimeSeriesOptions';
import TimeSeriesJSON from '../TimeSeriesJSON';

Expand All @@ -48,7 +48,7 @@ interface Rating {
* */


export class RatingSeriesConverter extends MorningstarConverter {
export class RatingSeriesConverter extends TimeSeriesConverter {


/* *
Expand Down Expand Up @@ -76,6 +76,8 @@ export class RatingSeriesConverter extends MorningstarConverter {

public override readonly options: Required<RatingSeriesOptions>;

public override path: string = 'timeseries/rating';


/* *
*
Expand Down
5 changes: 4 additions & 1 deletion src/TimeSeries/Converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import CumulativeReturnSeriesConverter from './CumulativeReturnSeriesConverter';
import GrowthSeriesConverter from './GrowthSeriesConverter';
import DividendSeriesConverter from './DividendSeriesConverter';
import RatingSeriesConverter from './RatingSeriesConverter';
import PriceSeriesConverter from './PriceSeriesConverter';


/* *
Expand All @@ -39,6 +40,7 @@ export * from './CumulativeReturnSeriesConverter';
export * from './GrowthSeriesConverter';
export * from './DividendSeriesConverter';
export * from './RatingSeriesConverter';
export * from './PriceSeriesConverter';


/* *
Expand All @@ -52,5 +54,6 @@ export default {
CumulativeReturnSeriesConverter,
GrowthSeriesConverter,
DividendSeriesConverter,
RatingSeriesConverter
RatingSeriesConverter,
PriceSeriesConverter
};
23 changes: 13 additions & 10 deletions src/TimeSeries/TimeSeriesConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import External from '../Shared/External';
import GrowthSeriesConverter from './Converters/GrowthSeriesConverter';
import MorningstarAPI from '../Shared/MorningstarAPI';
import MorningstarConnector from '../Shared/MorningstarConnector';
import MorningstarConverter from '../Shared/MorningstarConverter';
import MorningstarURL from '../Shared/MorningstarURL';
import CumulativeReturnSeriesConverter from './Converters/CumulativeReturnSeriesConverter';
import DividendSeriesConverter from './Converters/DividendSeriesConverter';
import TimeSeriesOptions from './TimeSeriesOptions';
import TimeSeriesRatingConverter from './Converters/RatingSeriesConverter';
import PriceSeriesConverter from './Converters/PriceSeriesConverter';
import TimeSeriesConverter from './TimeSeriesConverter';


/* *
Expand Down Expand Up @@ -63,31 +64,34 @@ export class TimeSeriesConnector extends MorningstarConnector {
...options.converter,
...options.series
});
this.path = 'timeseries/cumulativereturn';
break;

case 'Dividend':
this.converter = new DividendSeriesConverter({
...options.converter,
...options.series
});
this.path = 'timeseries/dividend';
break;

case 'Growth':
this.converter = new GrowthSeriesConverter({
...options.converter,
...options.series
});
this.path = 'timeseries/growth';
break;

case 'Rating':
this.converter = new TimeSeriesRatingConverter({
...options.converter,
...options.series
});
this.path = 'timeseries/rating';
break;

case 'Price':
this.converter = new PriceSeriesConverter({
...options.converter,
...options.series
});
break;

default:
Expand All @@ -107,15 +111,12 @@ export class TimeSeriesConnector extends MorningstarConnector {
* */


public override readonly converter: MorningstarConverter;
public override readonly converter: TimeSeriesConverter;


public override readonly options: TimeSeriesOptions;


public readonly path: string;


/* *
*
* Functions
Expand All @@ -138,7 +139,7 @@ export class TimeSeriesConnector extends MorningstarConnector {
}

const api = this.api = this.api || new MorningstarAPI(options.api);
const url = new MorningstarURL('/ecint/v1/' + this.path, api.baseURL);
const url = new MorningstarURL('/ecint/v1/' + this.converter.path, api.baseURL);

url.setSecuritiesOptions(securities);

Expand All @@ -158,6 +159,8 @@ export class TimeSeriesConnector extends MorningstarConnector {
url.searchParams.set('tax', tax);
}

this.converter.decorateURL(url);

const response = await api.fetch(url);
const json = await response.json() as unknown;

Expand Down
Loading