Skip to content

Commit 41c2a41

Browse files
authored
Merge pull request #59 from deriv-com/henry/adapt-to-chart-data-change-v3
fix: change data generation according to latest final api spec
2 parents 50f6c91 + caf6f1f commit 41c2a41

File tree

5 files changed

+104
-77
lines changed

5 files changed

+104
-77
lines changed

src/components/Chart/Chart.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ export const TradeChart: React.FC = () => {
1010
triggerPopup(arg: () => void): void;
1111
}>(null);
1212

13-
// const historicalData = useMemo(() => {
14-
// const data = generateHistoricalCandles(100, 60);
15-
// return transformCandleData(data);
16-
// }, []);
13+
const historicalData1 = useMemo(() => {
14+
const data = generateHistoricalCandles(100, 60);
15+
return transformCandleData(data);
16+
}, []);
1717

1818
const historicalData = useMemo(() => {
1919
const data = generateHistoricalTicks('1HZ100V', 100);
2020
return transformTickData(data);
2121
}, []);
2222

23-
// const streamingData = useChartData({
24-
// useMockData: true,
25-
// instrumentId: '1HZ100V',
26-
// type: 'candle',
27-
// durationInSeconds: 60
28-
// });
23+
const streamingData1 = useChartData({
24+
useMockData: true,
25+
instrumentId: '1HZ100V',
26+
type: 'candle',
27+
durationInSeconds: 60
28+
});
2929

3030
const streamingData = useChartData({
3131
useMockData: true,
@@ -68,7 +68,7 @@ export const TradeChart: React.FC = () => {
6868
top: 76,
6969
}}
7070
leftMargin={80}
71-
chartType="line"
71+
chartType="line" // "line", "candles", "hollow"
7272
ticksHistory={historicalData}
7373
streamingData={streamingData}
7474
/>

src/hooks/useChartData.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,30 @@ export const useChartData = ({
1919
type = "tick",
2020
durationInSeconds = 60,
2121
}: UseChartDataProps = {}) => {
22-
const openTime = useRef(Math.floor(Date.now() / 1000));
22+
const openTime = useRef(Date.now());
2323
const closeTime = useRef(openTime.current);
2424

2525
const [data, setData] = useState(() => {
2626
if (type === "candle") {
2727
return transformCandleData({
28+
instrument_id: instrumentId,
2829
candles: [
2930
{
30-
openEpochMs: openTime.current,
31+
open_epoch_ms: openTime.current.toString(),
3132
open: "911.5",
3233
high: "913.5",
3334
low: "909.8",
3435
close: "911.73",
35-
closeEpochMs: closeTime.current,
36+
close_epoch_ms: closeTime.current.toString(),
3637
},
3738
],
3839
});
3940
} else {
4041
return transformTickData({
41-
instrumentId,
42+
instrument_id: instrumentId,
4243
ticks: [
4344
{
44-
epochMs: Date.now(),
45+
epoch_ms: Date.now().toString(),
4546
ask: "911.5",
4647
bid: "911.3",
4748
price: "911.4",
@@ -63,34 +64,35 @@ export const useChartData = ({
6364
const basePrice = 911.5 + change;
6465

6566
if (type === "candle") {
66-
// Increment closeEpochMs by 1 second
67-
closeTime.current += 1;
67+
// Increment close_epoch_ms by 1 second in milliseconds
68+
closeTime.current += 1000;
6869

69-
// Check if closeEpochMs has reached the next candle period
70-
if (closeTime.current >= openTime.current + durationInSeconds) {
71-
// Increment openEpochMs by durationInSeconds
72-
openTime.current += durationInSeconds;
70+
// Check if close_epoch_ms has reached the next candle period
71+
if (closeTime.current >= openTime.current + (durationInSeconds * 1000)) {
72+
// Increment open_epoch_ms by durationInSeconds in milliseconds
73+
openTime.current += (durationInSeconds * 1000);
7374
}
7475

7576
const candleData = {
77+
instrument_id: instrumentId,
7678
candles: [
7779
{
78-
openEpochMs: openTime.current,
80+
open_epoch_ms: openTime.current.toString(),
7981
open: basePrice.toString(),
8082
high: (basePrice + 2).toString(),
8183
low: (basePrice - 1.7).toString(),
8284
close: (basePrice + 0.23).toString(),
83-
closeEpochMs: closeTime.current,
85+
close_epoch_ms: closeTime.current.toString(),
8486
},
8587
],
8688
};
8789
setData(transformCandleData(candleData));
8890
} else {
8991
const tickData = {
90-
instrumentId,
92+
instrument_id: instrumentId,
9193
ticks: [
9294
{
93-
epochMs: Date.now(),
95+
epoch_ms: Date.now().toString(),
9496
ask: (basePrice + 0.2).toString(),
9597
bid: (basePrice - 0.2).toString(),
9698
price: basePrice.toString(),

src/utils/__tests__/transformChartData.test.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ import { transformCandleData, transformTickData } from '../transformChartData';
33
describe('transformCandleData', () => {
44
it('should transform single candle data to ohlc format', () => {
55
const input = {
6+
instrument_id: 'frxUSDJPY',
67
candles: [{
7-
openEpochMs: 1644825600,
8+
open_epoch_ms: '1644825600000',
89
open: '100',
910
high: '110',
1011
low: '90',
1112
close: '105',
12-
closeEpochMs: 1644829200
13+
close_epoch_ms: '1644829200000'
1314
}]
1415
};
1516

1617
const expected = {
1718
msg_type: 'ohlc',
19+
instrument_id: 'frxUSDJPY',
1820
ohlc: {
1921
open_time: 1644825600,
2022
open: '100',
@@ -30,28 +32,30 @@ describe('transformCandleData', () => {
3032

3133
it('should transform multiple candles data to candles format', () => {
3234
const input = {
35+
instrument_id: 'frxUSDJPY',
3336
candles: [
3437
{
35-
openEpochMs: 1644825600,
38+
open_epoch_ms: '1644825600000',
3639
open: '100',
3740
high: '110',
3841
low: '90',
3942
close: '105',
40-
closeEpochMs: 1644829200
43+
close_epoch_ms: '1644829200000'
4144
},
4245
{
43-
openEpochMs: 1644829200,
46+
open_epoch_ms: '1644829200000',
4447
open: '105',
4548
high: '115',
4649
low: '95',
4750
close: '110',
48-
closeEpochMs: 1644832800
51+
close_epoch_ms: '1644832800000'
4952
}
5053
]
5154
};
5255

5356
const expected = {
5457
msg_type: 'candles',
58+
instrument_id: 'frxUSDJPY',
5559
candles: [
5660
{
5761
open_time: 1644825600,
@@ -79,9 +83,9 @@ describe('transformCandleData', () => {
7983
describe('transformTickData', () => {
8084
it('should transform single tick data to tick format', () => {
8185
const input = {
82-
instrumentId: 'R_100',
86+
instrument_id: 'R_100',
8387
ticks: [{
84-
epochMs: 1644825600000,
88+
epoch_ms: '1644825600000',
8589
ask: '100.5',
8690
bid: '100.3',
8791
price: '100.4'
@@ -90,7 +94,7 @@ describe('transformTickData', () => {
9094

9195
const expected = {
9296
msg_type: 'tick',
93-
instrumentId: 'R_100',
97+
instrument_id: 'R_100',
9498
tick: {
9599
epoch: 1644825600,
96100
ask: '100.5',
@@ -104,16 +108,16 @@ describe('transformTickData', () => {
104108

105109
it('should transform multiple ticks data to history format', () => {
106110
const input = {
107-
instrumentId: 'R_100',
111+
instrument_id: 'R_100',
108112
ticks: [
109113
{
110-
epochMs: 1644825600000,
114+
epoch_ms: '1644825600000',
111115
ask: '100.5',
112116
bid: '100.3',
113117
price: '100.4'
114118
},
115119
{
116-
epochMs: 1644825601000,
120+
epoch_ms: '1644825601000',
117121
ask: '100.6',
118122
bid: '100.4',
119123
price: '100.5'
@@ -123,7 +127,7 @@ describe('transformTickData', () => {
123127

124128
const expected = {
125129
msg_type: 'history',
126-
instrumentId: 'R_100',
130+
instrument_id: 'R_100',
127131
history: [
128132
{
129133
epoch: 1644825600,

src/utils/generateHistoricalData.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
export const generateHistoricalCandles = (count: number = 100, durationInSeconds: number = 60) => {
2-
const currentTime = Math.floor(Date.now() / 1000); // Convert to seconds
1+
export const generateHistoricalCandles = (
2+
count: number = 100,
3+
durationInSeconds: number = 60,
4+
instrumentId: string = "frxUSDJPY"
5+
) => {
6+
const currentTime = Date.now();
7+
const durationInMs = durationInSeconds * 1000;
8+
39
const candles = Array.from({ length: count }, (_, index) => {
410
const basePrice = 911.5 + (Math.random() * 10 - 5);
5-
const timeOffset = (count - 1 - index) * durationInSeconds;
11+
const timeOffset = (count - 1 - index) * durationInMs;
612
const openTime = currentTime - timeOffset;
713

814
return {
9-
openEpochMs: openTime,
15+
open_epoch_ms: openTime.toString(),
1016
open: basePrice.toString(),
1117
high: (basePrice + 2).toString(),
1218
low: (basePrice - 1.7).toString(),
1319
close: (basePrice + 0.23).toString(),
14-
closeEpochMs: openTime + durationInSeconds
20+
close_epoch_ms: (openTime + durationInMs).toString()
1521
};
1622
});
1723

1824
return {
25+
instrument_id: instrumentId,
1926
candles
2027
};
2128
};
@@ -27,15 +34,15 @@ export const generateHistoricalTicks = (instrumentId: string = '1HZ100', count:
2734
const timeOffset = (count - 1 - index) * 1000; // 1 second intervals
2835

2936
return {
30-
epochMs: (currentTime - timeOffset),
37+
epoch_ms: (currentTime - timeOffset).toString(),
3138
ask: (basePrice + 0.2).toString(),
3239
bid: (basePrice - 0.2).toString(),
3340
price: basePrice.toString()
3441
};
3542
});
3643

3744
return {
38-
instrumentId,
45+
instrument_id: instrumentId,
3946
ticks
4047
};
4148
};

0 commit comments

Comments
 (0)