Skip to content

Commit a206ed1

Browse files
committed
Add previousPriceChange and previousPriceChangePercent attributes to Quote
1 parent cc20de7 commit a206ed1

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

docs/content/releases/5.26.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**New Features**
2+
3+
* Added `previousPriceChange` and `previousPriceChangePercent` attributes to `Quote` object.

example/browser/example.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4898,6 +4898,28 @@ module.exports = (() => {
48984898
quote.priceChangePercent = priceChangePercent;
48994899
};
49004900

4901+
const _derivePreviousPriceChange = quote => {
4902+
if (quote.days.length < 1) {
4903+
return;
4904+
}
4905+
4906+
const previousPrice = quote.days[0].lastPrice;
4907+
const previousPreviousPrice = quote.days[0].previousPrice;
4908+
let priceChange = null;
4909+
let priceChangePercent = null;
4910+
4911+
if (is.number(previousPrice) && is.number(previousPreviousPrice)) {
4912+
priceChange = previousPrice - previousPreviousPrice;
4913+
4914+
if (previousPreviousPrice !== 0) {
4915+
priceChangePercent = priceChange / Math.abs(previousPreviousPrice);
4916+
}
4917+
}
4918+
4919+
quote.previousPriceChange = priceChange;
4920+
quote.previousPriceChangePercent = priceChangePercent;
4921+
};
4922+
49014923
const _processMessage = (message, options) => {
49024924
const symbol = message.symbol;
49034925

@@ -5017,6 +5039,8 @@ module.exports = (() => {
50175039
q.settlementPrice = undefined;
50185040

50195041
_derivePriceChange(q);
5042+
5043+
_derivePreviousPriceChange(q);
50205044
} else if (q.dayNum > dayNum) {
50215045
return;
50225046
}
@@ -5227,8 +5251,18 @@ module.exports = (() => {
52275251
q.highPrice = message.highPrice;
52285252
q.lowPrice = message.lowPrice;
52295253

5254+
if (is.string(message.previousDay) && is.number(message.previousPreviousPrice) && is.number(message.previousLastPrice)) {
5255+
q.days.unshift({
5256+
day: message.previousDay,
5257+
previousPrice: message.previousPreviousPrice,
5258+
lastPrice: message.previousLastPrice
5259+
});
5260+
}
5261+
52305262
_derivePriceChange(q);
52315263

5264+
_derivePreviousPriceChange(q);
5265+
52325266
_deriveRecordHighPrice(q);
52335267

52345268
_deriveRecordLowPrice(q);
@@ -6023,8 +6057,34 @@ module.exports = (() => {
60236057
*/
60246058

60256059
this.timeUtc = null;
6060+
/**
6061+
* @property {Number|null}
6062+
* @public
6063+
* @readonly
6064+
*/
6065+
60266066
this.priceChange = null;
6067+
/**
6068+
* @property {Number|null}
6069+
* @public
6070+
* @readonly
6071+
*/
6072+
60276073
this.priceChangePercent = null;
6074+
/**
6075+
* @property {Number|null}
6076+
* @public
6077+
* @readonly
6078+
*/
6079+
6080+
this.previousPriceChange = null;
6081+
/**
6082+
* @property {Number|null}
6083+
* @public
6084+
* @readonly
6085+
*/
6086+
6087+
this.previousPriceChangePercent = null;
60286088
this.days = [];
60296089
this.ticks = [];
60306090
}
@@ -7413,6 +7473,8 @@ module.exports = (() => {
74137473
if (sessions.combined.day) message.day = session.day;
74147474
if (premarket && typeof message.flag === 'undefined') message.flag = 'p';
74157475
const p = sessions.previous;
7476+
message.previousDay = p.day;
7477+
message.previousLastPrice = p.lastPrice;
74167478
message.previousPreviousPrice = p.previousPrice;
74177479
message.previousSettlementPrice = p.settlementPrice;
74187480
message.previousOpenPrice = p.openPrice;

lib/marketState/MarketState.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,29 @@ module.exports = (() => {
273273
quote.priceChangePercent = priceChangePercent;
274274
};
275275

276+
const _derivePreviousPriceChange = (quote) => {
277+
if (quote.days.length < 1) {
278+
return;
279+
}
280+
281+
const previousPrice = quote.days[0].lastPrice;
282+
const previousPreviousPrice = quote.days[0].previousPrice;
283+
284+
let priceChange = null;
285+
let priceChangePercent = null;
286+
287+
if (is.number(previousPrice) && is.number(previousPreviousPrice)) {
288+
priceChange = previousPrice - previousPreviousPrice;
289+
290+
if (previousPreviousPrice !== 0) {
291+
priceChangePercent = priceChange / Math.abs(previousPreviousPrice);
292+
}
293+
}
294+
295+
quote.previousPriceChange = priceChange;
296+
quote.previousPriceChangePercent = priceChangePercent;
297+
};
298+
276299
const _processMessage = (message, options) => {
277300
const symbol = message.symbol;
278301

@@ -393,6 +416,7 @@ module.exports = (() => {
393416
q.settlementPrice = undefined;
394417

395418
_derivePriceChange(q);
419+
_derivePreviousPriceChange(q);
396420
} else if (q.dayNum > dayNum) {
397421
return;
398422
}
@@ -579,14 +603,22 @@ module.exports = (() => {
579603
q.numberOfTrades = message.numberOfTrades;
580604
q.previousPrice = message.previousPrice;
581605
q.settlementPrice = message.settlementPrice;
606+
582607
if (message.previousSettlementPrice) {
583608
q.previousSettlementPrice = message.previousSettlementPrice;
584609
}
610+
585611
q.openPrice = message.openPrice;
586612
q.highPrice = message.highPrice;
587613
q.lowPrice = message.lowPrice;
588614

615+
if (is.string(message.previousDay) && is.number(message.previousPreviousPrice) && is.number(message.previousLastPrice)) {
616+
q.days.unshift({ day: message.previousDay, previousPrice: message.previousPreviousPrice, lastPrice: message.previousLastPrice });
617+
}
618+
589619
_derivePriceChange(q);
620+
_derivePreviousPriceChange(q);
621+
590622
_deriveRecordHighPrice(q);
591623
_deriveRecordLowPrice(q);
592624

lib/marketState/Quote.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,34 @@ module.exports = (() => {
232232
*/
233233
this.timeUtc = null;
234234

235+
/**
236+
* @property {Number|null}
237+
* @public
238+
* @readonly
239+
*/
235240
this.priceChange = null;
241+
242+
/**
243+
* @property {Number|null}
244+
* @public
245+
* @readonly
246+
*/
236247
this.priceChangePercent = null;
237248

249+
/**
250+
* @property {Number|null}
251+
* @public
252+
* @readonly
253+
*/
254+
this.previousPriceChange = null;
255+
256+
/**
257+
* @property {Number|null}
258+
* @public
259+
* @readonly
260+
*/
261+
this.previousPriceChangePercent = null;
262+
238263
this.days = [];
239264
this.ticks = [];
240265
}

lib/utilities/parse/ddf/message.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ module.exports = (() => {
257257

258258
const p = sessions.previous;
259259

260+
message.previousDay = p.day;
261+
message.previousLastPrice = p.lastPrice;
260262
message.previousPreviousPrice = p.previousPrice;
261263
message.previousSettlementPrice = p.settlementPrice;
262264
message.previousOpenPrice = p.openPrice;

0 commit comments

Comments
 (0)