Skip to content

Commit

Permalink
feat: Export instrument types (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Jan 27, 2021
1 parent ea64b96 commit abf53d4
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 233 deletions.
17 changes: 2 additions & 15 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,11 @@
"project": "./tsconfig.json",
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "typescript-sort-keys", "prettier"],
"plugins": ["@typescript-eslint", "typescript-sort-keys", "prettier", "sort-keys-fix"],
"rules": {
"@typescript-eslint/array-type": "error",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/member-ordering": [
"error",
{
"default": [
"public-instance-field",
"private-instance-field",
"public-constructor",
"private-constructor",
"public-instance-method",
"protected-instance-method",
"private-instance-method"
]
}
],
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-this-alias": "error",
Expand Down Expand Up @@ -78,6 +64,7 @@
"natural": true
}
],
"sort-keys-fix/sort-keys-fix": "warn",
"sort-vars": "error",
"space-in-parens": "error",
"strict": ["error", "global"],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"eslint": "7.18.0",
"eslint-config-prettier": "7.2.0",
"eslint-plugin-prettier": "3.3.1",
"eslint-plugin-sort-keys-fix": "1.1.1",
"eslint-plugin-typescript-sort-keys": "1.5.0",
"generate-changelog": "1.8.0",
"husky": "4.3.8",
Expand Down
80 changes: 55 additions & 25 deletions src/market/MarketAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AxiosInstance} from 'axios';
import {PricesAPI} from './prices';
import {PriceAPI} from './prices';

export interface Currency {
baseExchangeRate: number;
Expand Down Expand Up @@ -67,28 +67,37 @@ export interface Instrument {
sprintMarketsMinimumExpiryTime?: number;
stopsLimitsAllowed: boolean;
streamingPricesAvailable: boolean;
type:
| 'BUNGEE_COMMODITIES'
| 'BUNGEE_CURRENCIES'
| 'BUNGEE_INDICES'
| 'COMMODITIES'
| 'CURRENCIES'
| 'INDICES'
| 'OPT_COMMODITIES'
| 'OPT_CURRENCIES'
| 'OPT_INDICES'
| 'OPT_RATES'
| 'OPT_SHARES'
| 'RATES'
| 'SECTORS'
| 'SHARES'
| 'SPRINT_MARKET'
| 'TEST_MARKET'
| 'UNKNOWN';
unit: 'AMOUNT' | 'CONTRACTS' | 'SHARES';
type: InstrumentType;
unit: InstrumentUnit;
valueOfOnePip: string;
}

export enum InstrumentType {
BUNGEE_COMMODITIES = 'BUNGEE_COMMODITIES',
BUNGEE_CURRENCIES = 'BUNGEE_CURRENCIES',
BUNGEE_INDICES = 'BUNGEE_INDICES',
COMMODITIES = 'COMMODITIES',
CURRENCIES = 'CURRENCIES',
INDICES = 'INDICES',
OPT_COMMODITIES = 'OPT_COMMODITIES',
OPT_CURRENCIES = 'OPT_CURRENCIES',
OPT_INDICES = 'OPT_INDICES',
OPT_RATES = 'OPT_RATES',
OPT_SHARES = 'OPT_SHARES',
RATES = 'RATES',
SECTORS = 'SECTORS',
SHARES = 'SHARES',
SPRINT_MARKET = 'SPRINT_MARKET',
TEST_MARKET = 'TEST_MARKET',
UNKNOWN = 'UNKNOWN',
}

export enum InstrumentUnit {
AMOUNT = 'AMOUNT',
CONTRACTS = 'CONTRACTS',
SHARES = 'SHARES',
}

export interface MinStepDistance {
unit: string;
value: number;
Expand All @@ -115,13 +124,24 @@ export interface MaxStopOrLimitDistance {
}

export interface DealingRules {
marketOrderPreference: 'AVAILABLE_DEFAULT_OFF' | 'AVAILABLE_DEFAULT_ON' | 'NOT_AVAILABLE';
marketOrderPreference: MarketOrderPreference;
maxStopOrLimitDistance: MaxStopOrLimitDistance;
minControlledRiskStopDistance: MinControlledRiskStopDistance;
minDealSize: MinDealSize;
minNormalStopOrLimitDistance: MinNormalStopOrLimitDistance;
minStepDistance: MinStepDistance;
trailingStopsPreference: 'AVAILABLE' | 'NOT_AVAILABLE';
trailingStopsPreference: TrailingStopPreference;
}

export enum MarketOrderPreference {
AVAILABLE_DEFAULT_OFF = 'AVAILABLE_DEFAULT_OFF',
AVAILABLE_DEFAULT_ON = 'AVAILABLE_DEFAULT_ON',
NOT_AVAILABLE = 'NOT_AVAILABLE',
}

export enum TrailingStopPreference {
AVAILABLE = 'AVAILABLE',
NOT_AVAILABLE = 'NOT_AVAILABLE',
}

export interface Snapshot {
Expand All @@ -132,14 +152,24 @@ export interface Snapshot {
delayTime: number;
high: number;
low: number;
marketStatus: 'CLOSED' | 'EDITS_ONLY' | 'OFFLINE' | 'ON_AUCTION' | 'ON_AUCTION_NO_EDITS' | 'SUSPENDED' | 'TRADEABLE';
marketStatus: MarketStatus;
netChange: number;
offer: number;
percentageChange: number;
scalingFactor: number;
updateTime: string;
}

export enum MarketStatus {
CLOSED = 'CLOSED',
EDITS_ONLY = 'EDITS_ONLY',
OFFLINE = 'OFFLINE',
ON_AUCTION = 'ON_AUCTION',
ON_AUCTION_NO_EDITS = 'ON_AUCTION_NO_EDITS',
SUSPENDED = 'SUSPENDED',
TRADEABLE = 'TRADEABLE',
}

export interface MarketDetail {
dealingRules: DealingRules;
instrument: Instrument;
Expand Down Expand Up @@ -186,10 +216,10 @@ export class MarketAPI {
MARKETNAVIGATION: `/marketnavigation`,
MARKETS: `/markets`,
};
prices: PricesAPI;
price: PriceAPI;

constructor(private readonly apiClient: AxiosInstance) {
this.prices = new PricesAPI(apiClient);
this.price = new PriceAPI(apiClient);
}

/**
Expand Down
172 changes: 172 additions & 0 deletions src/market/prices/PriceAPI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import nock from 'nock';
import {APIClient} from '../../APIClient';
import {InstrumentType} from '../MarketAPI';
import {PriceAPI, Resolution} from './PriceAPI';

describe('PricesAPI', () => {
describe('getPrices', () => {
it('returns the number of price points from now', async () => {
const expectedPrices = [
{
closePrice: {ask: 13664.7, bid: 13663.8, lastTraded: null},
highPrice: {ask: 13678.1, bid: 13676.8, lastTraded: null},
lastTradedVolume: 16435,
lowPrice: {ask: 13661, bid: 13659.5, lastTraded: null},
openPrice: {ask: 13677.6, bid: 13676.1, lastTraded: null},
snapshotTime: '2021/01/26 00:00:00',
snapshotTimeUTC: '2021-01-26T00:00:00',
},
{
closePrice: {ask: 13613, bid: 13611.5, lastTraded: null},
highPrice: {ask: 13667.1, bid: 13666, lastTraded: null},
lastTradedVolume: 22061,
lowPrice: {ask: 13610.1, bid: 13609.2, lastTraded: null},
openPrice: {ask: 13664.6, bid: 13663.7, lastTraded: null},
snapshotTime: '2021/01/26 04:00:00',
snapshotTimeUTC: '2021-01-26T04:00:00',
},
{
closePrice: {ask: 13675.6, bid: 13674.1, lastTraded: null},
highPrice: {ask: 13677.1, bid: 13676.2, lastTraded: null},
lastTradedVolume: 29631,
lowPrice: {ask: 13611.8, bid: 13610.5, lastTraded: null},
openPrice: {ask: 13612.7, bid: 13611.8, lastTraded: null},
snapshotTime: '2021/01/26 08:00:00',
snapshotTimeUTC: '2021-01-26T08:00:00',
},
{
closePrice: {ask: 13742.4, bid: 13741.5, lastTraded: null},
highPrice: {ask: 13745, bid: 13744, lastTraded: null},
lastTradedVolume: 31848,
lowPrice: {ask: 13671.4, bid: 13670.5, lastTraded: null},
openPrice: {ask: 13676.1, bid: 13675.2, lastTraded: null},
snapshotTime: '2021/01/26 12:00:00',
snapshotTimeUTC: '2021-01-26T12:00:00',
},
{
closePrice: {ask: 13732.3, bid: 13731.4, lastTraded: null},
highPrice: {ask: 13743.8, bid: 13742.9, lastTraded: null},
lastTradedVolume: 12235,
lowPrice: {ask: 13718.4, bid: 13717.5, lastTraded: null},
openPrice: {ask: 13742.3, bid: 13741.4, lastTraded: null},
snapshotTime: '2021/01/26 16:00:00',
snapshotTimeUTC: '2021-01-26T16:00:00',
},
];

nock(APIClient.URL_DEMO)
.get(`${PriceAPI.URL.PRICES}/CS.D.GBPUSD.TODAY.IP?max=5&pageNumber=1&pageSize=0&resolution=HOUR_4`)
.reply(
200,
JSON.stringify({
instrumentType: InstrumentType.CURRENCIES,
metadata: {
allowance: {allowanceExpiry: 524060, remainingAllowance: 9945, totalAllowance: 10000},
pageData: {pageNumber: 1, pageSize: 20, totalPages: 1},
size: 5,
},
prices: expectedPrices,
})
);

const getPrices = await global.client.rest.market.price.getPrices('CS.D.GBPUSD.TODAY.IP', Resolution.HOUR_4, 5);
expect(getPrices.prices.length).toBe(expectedPrices.length);
expect(getPrices.instrumentType).toBe(InstrumentType.CURRENCIES);
});
});
describe('getPricesBetweenDates', () => {
it('returns prices between given dates', async () => {
const expectedPrices = [
{
closePrice: {ask: 13682.9, bid: 13681.4, lastTraded: null},
highPrice: {ask: 13698.9, bid: 13697.4, lastTraded: null},
lastTradedVolume: 22064,
lowPrice: {ask: 13675.3, bid: 13674.1, lastTraded: null},
openPrice: {ask: 13684, bid: 13681.9, lastTraded: null},
snapshotTime: '2021/01/15 00:00:00',
snapshotTimeUTC: '2021-01-15T00:00:00',
},
{
closePrice: {ask: 13663.3, bid: 13662.4, lastTraded: null},
highPrice: {ask: 13685.3, bid: 13684.3, lastTraded: null},
lastTradedVolume: 24315,
lowPrice: {ask: 13658.4, bid: 13657, lastTraded: null},
openPrice: {ask: 13682.8, bid: 13681.3, lastTraded: null},
snapshotTime: '2021/01/15 04:00:00',
snapshotTimeUTC: '2021-01-15T04:00:00',
},
{
closePrice: {ask: 13639.8, bid: 13638.9, lastTraded: null},
highPrice: {ask: 13670.6, bid: 13669.7, lastTraded: null},
lastTradedVolume: 32672,
lowPrice: {ask: 13637.9, bid: 13636.9, lastTraded: null},
openPrice: {ask: 13663.2, bid: 13662.3, lastTraded: null},
snapshotTime: '2021/01/15 08:00:00',
snapshotTimeUTC: '2021-01-15T08:00:00',
},
{
closePrice: {ask: 13584.4, bid: 13583.5, lastTraded: null},
highPrice: {ask: 13642.2, bid: 13641.3, lastTraded: null},
lastTradedVolume: 43169,
lowPrice: {ask: 13572.7, bid: 13571.8, lastTraded: null},
openPrice: {ask: 13639.3, bid: 13638.4, lastTraded: null},
snapshotTime: '2021/01/15 12:00:00',
snapshotTimeUTC: '2021-01-15T12:00:00',
},
{
closePrice: {ask: 13580.7, bid: 13579.2, lastTraded: null},
highPrice: {ask: 13607.1, bid: 13606.2, lastTraded: null},
lastTradedVolume: 23937,
lowPrice: {ask: 13575.1, bid: 13574.2, lastTraded: null},
openPrice: {ask: 13584.5, bid: 13583.6, lastTraded: null},
snapshotTime: '2021/01/15 16:00:00',
snapshotTimeUTC: '2021-01-15T16:00:00',
},
{
closePrice: {ask: 13592.1, bid: 13582.1, lastTraded: null},
highPrice: {ask: 13596.3, bid: 13589.1, lastTraded: null},
lastTradedVolume: 8749,
lowPrice: {ask: 13576.3, bid: 13574.8, lastTraded: null},
openPrice: {ask: 13580.6, bid: 13579.1, lastTraded: null},
snapshotTime: '2021/01/15 20:00:00',
snapshotTimeUTC: '2021-01-15T20:00:00',
},
];

nock(APIClient.URL_DEMO)
.get(
`${PriceAPI.URL.PRICES}/CS.D.GBPUSD.TODAY.IP?from=2021-01-15T00%3A00%3A00.000Z&pageNumber=1&pageSize=0&resolution=HOUR_4&to=2021-01-16T00%3A00%3A00.000Z`
)
.reply(
200,
JSON.stringify({
instrumentType: InstrumentType.CURRENCIES,
metadata: {
allowance: {
allowanceExpiry: 604799,
remainingAllowance: 9994,
totalAllowance: 10000,
},
pageData: {
pageNumber: 1,
pageSize: 6,
totalPages: 1,
},
size: 6,
},
prices: expectedPrices,
})
);

const getPricesBetweenDates = await global.client.rest.market.price.getPricesBetweenDates(
'CS.D.GBPUSD.TODAY.IP',
Resolution.HOUR_4,
new Date('2021-01-15T00:00:00'),
new Date('2021-01-16T00:00:00')
);

expect(getPricesBetweenDates.prices.length).toBe(expectedPrices.length);
expect(getPricesBetweenDates.instrumentType).toBe(InstrumentType.CURRENCIES);
});
});
});

0 comments on commit abf53d4

Please sign in to comment.