Skip to content

Commit

Permalink
[OHLCV Converter] Add option to use Close for Open, High and Low when…
Browse files Browse the repository at this point in the history
… Volume is zero (#28)

* Add converter option `replaceZeroWithCloseValue` to OHLCVSeriesOptions
* Add `replaceZeroWithCloseValue` to demo
* Add unit test
  • Loading branch information
Eskils authored Sep 3, 2024
1 parent de782d1 commit 39930ce
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
1 change: 1 addition & 0 deletions demos/stock-ohlcv/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ async function displayOHLCV (postmanJSON) {
postman: {
environmentJSON: postmanJSON
},
replaceZeroWithCloseValue: true,
series: {
type: 'OHLCV'
},
Expand Down
14 changes: 12 additions & 2 deletions src/TimeSeries/Converters/OHLCVSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,20 @@ export class OHLCVSeriesConverter extends TimeSeriesConverter {
const sortedOHLCVItems: Array<OHLCV> = [];

for (const ohlcvItem of json) {
const [date, /* Open */ , /* High */, /* Low */, close, volume] = ohlcvItem;
let [/* Date */, open, high, low] = ohlcvItem;


if (volume === 0 && userOptions.replaceZeroWithCloseValue) {
open = close;
high = close;
low = close;
}

sortedOHLCVItems.push({
Id: securityIds[0],
Date: ohlcvItem[0],
Value: [ohlcvItem[1], ohlcvItem[2], ohlcvItem[3], ohlcvItem[4], ohlcvItem[5]]
Date: date,
Value: [open, high, low, close, volume]
});
}

Expand Down
6 changes: 4 additions & 2 deletions src/TimeSeries/TimeSeriesConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import MorningstarConnector from '../Shared/MorningstarConnector';
import MorningstarURL from '../Shared/MorningstarURL';
import CumulativeReturnSeriesConverter from './Converters/CumulativeReturnSeriesConverter';
import DividendSeriesConverter from './Converters/DividendSeriesConverter';
import TimeSeriesOptions from './TimeSeriesOptions';
import TimeSeriesOptions, { OHLCVSeriesOptions } from './TimeSeriesOptions';
import TimeSeriesRatingConverter from './Converters/RatingSeriesConverter';
import PriceSeriesConverter from './Converters/PriceSeriesConverter';
import TimeSeriesConverter from './TimeSeriesConverter';
Expand Down Expand Up @@ -99,7 +99,9 @@ export class TimeSeriesConnector extends MorningstarConnector {
this.converter = new OHLCVSeriesConverter({
...options.converter,
...options.series,
securities: options.securities
securities: options.securities,
replaceZeroWithCloseValue: (options as OHLCVSeriesOptions)
.replaceZeroWithCloseValue
});
break;

Expand Down
11 changes: 11 additions & 0 deletions src/TimeSeries/TimeSeriesOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ export interface OHLCVSeriesOptions extends TimeSeriesConverterOptions {
*/
type: 'OHLCV';

/**
* When this property is `true`, open, high and low are replaced with
* the close value if the volume is zero.
*
* If volume is zero, open high low are zero too. If you do not prefer this
* behavior, you can enable this property.
*
* @default false
*/
replaceZeroWithCloseValue?: boolean;

/**
* Security to retrieve.
*/
Expand Down
68 changes: 68 additions & 0 deletions test/unit-tests/TimeSeries/OHLCV.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,71 @@ export async function ohlcvLoad (
);

}

export function ohlcvReplaceZeroWithClose () {
const data = [
[1722816000000, 0, 0, 0, 14754.871, 0],
[1722816000001, 0, 0, 0, 14754.871, 1]
];

const expectedFirstRow = [1722816000000, 14754.871, 14754.871, 14754.871, 14754.871, 0];
const expectedSecondRow = [1722816000001, 0, 0, 0, 14754.871, 1];

const converter = new MC.TimeSeriesConverters.OHLCVSeriesConverter({
type: 'OHLCV',
replaceZeroWithCloseValue: true,
securities: [
{
id: 'XIUSA000O2',
idType: 'SECID'
}
],
json: data
});

converter.parse({ type: 'OHLCV' });

const dataTable = converter.getTable();

Assert.deepEqual(
dataTable.getRow(0),
expectedFirstRow,
'Should replace o, h, l with c when v is 0'
);

Assert.deepEqual(
dataTable.getRow(1),
expectedSecondRow,
'Should not replace o, h, l with c when v is not 0'
);
}

export function ohlcvDoNotReplaceZeroWithClose () {
const data = [
[1722816000000, 0, 0, 0, 14754.871, 0]
];

const expectedRow = [1722816000000, 0, 0, 0, 14754.871, 0];

const converter = new MC.TimeSeriesConverters.OHLCVSeriesConverter({
type: 'OHLCV',
replaceZeroWithCloseValue: false,
securities: [
{
id: 'XIUSA000O2',
idType: 'SECID'
}
],
json: data
});

converter.parse({ type: 'OHLCV' });

const dataTable = converter.getTable();

Assert.deepEqual(
dataTable.getRow(0),
expectedRow,
'Should not replace o, h, l with c when v is 0'
);
}

0 comments on commit 39930ce

Please sign in to comment.