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
65 changes: 65 additions & 0 deletions src/TimeSeries/Converters/TimeSeriesConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* *
*
* (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 MorningstarConverter from '../../Shared/MorningstarConverter';
import MorningstarURL from '../../Shared/MorningstarURL';


/* *
*
* Class
*
* */


export abstract class TimeSeriesConverter extends MorningstarConverter {

/* *
*
* Properties
*
* */

public abstract path: string;

/* *
*
* Functions
*
* */

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public decorateURL (url: MorningstarURL) {}

}


/* *
*
* Default Export
*
* */


export default TimeSeriesConverter;
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
};
Loading