Skip to content

Commit

Permalink
subscribe on strikes ask bid prices
Browse files Browse the repository at this point in the history
  • Loading branch information
qc-zakir-chalimov committed May 23, 2024
1 parent a65245c commit 09f551e
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,12 @@ export class DigitalOptionsUnderlyingInstruments {
*/
private instruments: Map<number, DigitalOptionsUnderlyingInstrument> = new Map<number, DigitalOptionsUnderlyingInstrument>()

/**
* Instance of WebSocket API client.
* @private
*/
private wsApiClient: WsApiClient | undefined

/**
* Just private constructor. Use {@link DigitalOptionsUnderlyingInstruments.create create} instead.
* @internal
Expand All @@ -2561,6 +2567,7 @@ export class DigitalOptionsUnderlyingInstruments {
*/
public static async create(assetId: number, wsApiClient: WsApiClient): Promise<DigitalOptionsUnderlyingInstruments> {
const instrumentsFacade = new DigitalOptionsUnderlyingInstruments()
instrumentsFacade.wsApiClient = wsApiClient

await wsApiClient.subscribe<DigitalOptionInstrumentsInstrumentGeneratedV1>(new SubscribeDigitalOptionInstrumentsInstrumentGeneratedV1('digital-option', assetId), (event) => {
if (event.instrumentType !== 'digital-option' || event.assetId !== assetId) {
Expand Down Expand Up @@ -2596,7 +2603,7 @@ export class DigitalOptionsUnderlyingInstruments {
*/
private syncInstrumentFromEvent(msg: DigitalOptionInstrumentsInstrumentGeneratedV1) {
if (!this.instruments.has(msg.index)) {
this.instruments.set(msg.index, new DigitalOptionsUnderlyingInstrument(msg))
this.instruments.set(msg.index, new DigitalOptionsUnderlyingInstrument(msg, this.wsApiClient!))
} else {
this.instruments.get(msg.index)!.sync(msg)
}
Expand Down Expand Up @@ -2629,7 +2636,7 @@ export class DigitalOptionsUnderlyingInstruments {
*/
private syncInstrumentFromResponse(msg: DigitalOptionInstrumentsInstrumentsV1Instrument) {
if (!this.instruments.has(msg.index)) {
this.instruments.set(msg.index, new DigitalOptionsUnderlyingInstrument(msg))
this.instruments.set(msg.index, new DigitalOptionsUnderlyingInstrument(msg, this.wsApiClient!))
} else {
this.instruments.get(msg.index)!.sync(msg)
}
Expand Down Expand Up @@ -2675,9 +2682,17 @@ export class DigitalOptionsUnderlyingInstrument {
*/
public strikes: Map<string, DigitalOptionsUnderlyingInstrumentStrike> = new Map<string, DigitalOptionsUnderlyingInstrumentStrike>()


/**
* Instance of WebSocket API client.
* @private
*/
private readonly wsApiClient: WsApiClient

/**
* Creates instance from DTO.
* @param msg - Instrument data transfer object.
* @param wsApiClient
* @internal
* @private
*/
Expand Down Expand Up @@ -2731,7 +2746,8 @@ export class DigitalOptionsUnderlyingInstrument {
*/
symbol: string,
}[],
}) {
}, wsApiClient: WsApiClient) {
this.wsApiClient = wsApiClient
this.assetId = msg.assetId
this.deadtime = msg.deadtime
this.expiration = new Date(msg.expiration * 1000)
Expand Down Expand Up @@ -2777,15 +2793,15 @@ export class DigitalOptionsUnderlyingInstrument {
return new Date(this.expiration.getTime() - this.deadtime * 1000);
}

public async subscribeOnStrikesAskBidPrices(wsApiClient: WsApiClient) {
public async subscribeOnStrikesAskBidPrices() {
const request = new SubscribeTradingSettingsDigitalOptionClientPriceGeneratedV1('digital-option', this.assetId, this.index)
await wsApiClient.subscribe<DigitalOptionClientPriceGeneratedV1>(request, (event) => {
await this.wsApiClient.subscribe<DigitalOptionClientPriceGeneratedV1>(request, (event) => {
this.syncAskBidPricesFromEvent(event)
})

const checkInterval = setInterval(() => {
if (wsApiClient.currentTime.unixMilliTime >= this.expiration.getTime()) {
wsApiClient.unsubscribe<DigitalOptionClientPriceGeneratedV1>(request)
if (this.wsApiClient.currentTime.unixMilliTime >= this.expiration.getTime()) {
this.wsApiClient.unsubscribe<DigitalOptionClientPriceGeneratedV1>(request)
clearInterval(checkInterval);
}
}, 1000);
Expand Down

0 comments on commit 09f551e

Please sign in to comment.