Skip to content

Commit

Permalink
chore: Add isActiveEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
andredezzy committed Apr 29, 2021
1 parent e4ff598 commit fdea5b2
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 5 deletions.
25 changes: 25 additions & 0 deletions examples/iqoption/isActiveEnabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import '../../loadEnv'

import { Hemes, sleep } from '@hemes/core'
import { IQOptionProvider } from '@hemes/iqoption'

async function run() {
const hemes = new Hemes(IQOptionProvider).getProvider()

const account = await hemes.logIn({
email: String(process.env.TEST_IQOPTION_ACCOUNT_EMAIL),
password: String(process.env.TEST_IQOPTION_ACCOUNT_PASSWORD),
})

await sleep(5000)

const isEnabled = await account.isActiveEnabled(
'EURUSD',
'binary-option',
'm1'
)

console.log(isEnabled)
}

run()
2 changes: 1 addition & 1 deletion examples/iqoption/typescript/getActiveProfit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function run() {

await sleep(5000)

const profit = await account.getActiveProfit('EURUSD', 'binary-option', 'm1')
const profit = await account.getActiveProfit('EURUSD', 'digital-option')

console.log(profit)
}
Expand Down
25 changes: 25 additions & 0 deletions examples/iqoption/typescript/isActiveEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import '../../loadEnv'

import { Hemes, sleep } from '@hemes/core'
import { IQOptionProvider, BaseIQOptionProvider } from '@hemes/iqoption'

async function run() {
const hemes = new Hemes(IQOptionProvider).getProvider<BaseIQOptionProvider>()

const account = await hemes.logIn({
email: String(process.env.TEST_IQOPTION_ACCOUNT_EMAIL),
password: String(process.env.TEST_IQOPTION_ACCOUNT_PASSWORD),
})

await sleep(5000)

const isEnabled = await account.isActiveEnabled(
'EURUSD',
'binary-option',
'm1'
)

console.log(isEnabled)
}

run()
75 changes: 71 additions & 4 deletions packages/iqoption/lib/IQOptionAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import {
InstrumentType,
} from './types'
import { getActiveId } from './utils/getActiveId'
import { getFixedMilliseconds } from './utils/getFixedMilliseconds'
import { GetBalancesRequest } from './websocket/events/requests/GetBalances'
import { GetInitializationDataRequest } from './websocket/events/requests/GetInitializationData'
import { GetTopAssetsRequest } from './websocket/events/requests/GetTopAssets'
import { GetUnderlyingListRequest } from './websocket/events/requests/GetUnderlyingList'
import { GetBalancesResponse } from './websocket/events/responses/GetBalances'
import { GetInitializationDataResponse } from './websocket/events/responses/GetInitializationData'
import { GetTopAssetsResponse } from './websocket/events/responses/GetTopAssets'
import { GetUnderlyingListResponse } from './websocket/events/responses/GetUnderlyingList'
import { Profile, ProfileResponse } from './websocket/events/responses/Profile'
import { WebSocketClient } from './websocket/WebSocketClient'

Expand Down Expand Up @@ -49,10 +52,13 @@ export class IQOptionAccount implements BaseIQOptionAccount {
): Promise<number> {
const activeId = getActiveId(active)

if (instrumentType === 'binary-option') {
if (
instrumentType === 'binary-option' ||
instrumentType === 'turbo-option'
) {
let instrument: 'binary' | 'turbo' = 'binary'

if (expirationPeriod[0] === 'm1') {
if (expirationPeriod[0] === 'm1' || instrumentType === 'turbo-option') {
instrument = 'turbo'
}

Expand All @@ -70,7 +76,7 @@ export class IQOptionAccount implements BaseIQOptionAccount {

const commission = activeInfo.option.profit.commission

return 100 - commission
return Math.round(100 - commission)
}

await this.webSocket.send(GetTopAssetsRequest, {
Expand All @@ -91,6 +97,67 @@ export class IQOptionAccount implements BaseIQOptionAccount {
throw new Error('Active asset not found')
}

return findAsset.spot_profit.value
return Math.round(findAsset.spot_profit.value)
}

public async isActiveEnabled<Type extends InstrumentType>(
active: Active,
instrumentType: Type,
...expirationPeriod: Type extends 'binary-option' ? [ExpirationPeriod] : []
): Promise<boolean> {
const activeId = getActiveId(active)

if (
instrumentType === 'binary-option' ||
instrumentType === 'turbo-option'
) {
let instrument: 'binary' | 'turbo' = 'binary'

if (expirationPeriod[0] === 'm1' || instrumentType === 'turbo-option') {
instrument = 'turbo'
}

await this.webSocket.send(GetInitializationDataRequest)

const initializationData = await this.webSocket.waitFor(
GetInitializationDataResponse
)

const activeInfo = initializationData?.msg[instrument].actives[activeId]

if (!activeInfo) {
throw new Error('Active info not found')
}

if (activeInfo.enabled) {
return !activeInfo.is_suspended
}

return false
}

await this.webSocket.send(GetUnderlyingListRequest, {
type: 'digital-option',
})

const underlyingList = await this.webSocket.waitFor(
GetUnderlyingListResponse
)

const findAsset = underlyingList?.msg.underlying.find(
asset => asset.active_id === activeId
)

if (!findAsset) {
throw new Error('Active asset not found')
}

const checkIsEnabled = findAsset.schedule.some(
item =>
getFixedMilliseconds(item.open) < Date.now() &&
getFixedMilliseconds(item.close) > Date.now()
)

return checkIsEnabled
}
}
5 changes: 5 additions & 0 deletions packages/iqoption/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface BaseIQOptionAccount {
instrumentType: Type,
...expirationPeriod: Type extends 'binary-option' ? [ExpirationPeriod] : []
): Promise<number>
isActiveEnabled<Type extends InstrumentType>(
active: Active,
instrumentType: Type,
...expirationPeriod: Type extends 'binary-option' ? [ExpirationPeriod] : []
): Promise<boolean>
}

export interface WebSocketEvent<Message = any> {
Expand Down
9 changes: 9 additions & 0 deletions packages/iqoption/lib/utils/getFixedMilliseconds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function getFixedMilliseconds(value: number): number {
const parsedValue = String(value)

if (parsedValue.length === 10) {
return Number(parsedValue + '000')
}

return value
}

0 comments on commit fdea5b2

Please sign in to comment.