Skip to content

Commit

Permalink
feat: Add market navigation (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Dec 7, 2020
1 parent de28347 commit 459d826
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,30 @@ const session = await client.rest.login.createSession('your-username', 'your-pas
console.info(`Your client ID is "${session.clientId}".`);
```

## Useful links
## Documentation

- [IG API Companion](https://labs.ig.com/sample-apps/api-companion/index.html)
- [IG REST Trading API Reference](https://labs.ig.com/rest-trading-api-reference)
- [IG API Companion](https://labs.ig.com/sample-apps/api-companion/index.html)

## Maintainers

[![Benny Neugebauer on Stack Exchange][stack_exchange_bennyn_badge]][stack_exchange_bennyn_url]

## Contributing

Contributions, issues and feature requests are welcome!

Feel free to check the [issues page](https://github.com/bennycode/ig-trading-api/issues).

## License

This project is [MIT](./LICENSE) licensed.

## ⭐️ Show your support ⭐️

[Please leave a star](https://github.com/bennycode/ig-trading-api/stargazers) if you find this project useful.

[1]: https://www.npmjs.com/package/ig-trading-api
[2]: https://bennycode.com/ig-trading-api
[stack_exchange_bennyn_badge]: https://stackexchange.com/users/flair/203782.png?theme=default
[stack_exchange_bennyn_url]: https://stackexchange.com/users/203782/benny-neugebauer?tab=accounts
64 changes: 64 additions & 0 deletions src/market/MarketAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,68 @@ describe('MarketAPI', () => {
expect(marketSearch.markets[2].updateTimeUTC).toBe('16:30:00');
});
});

describe('getMarketCategories', () => {
it('returns all top-level nodes (market categories) in the market navigation hierarchy', async () => {
nock(APIClient.URL_DEMO)
.get(MarketAPI.URL.MARKETNAVIGATION)
.query(true)
.reply(
200,
JSON.stringify({
markets: null,
nodes: [
{id: '404243', name: 'IPOs'},
{id: '88877247', name: 'Shares - Euronext Dublin (Ireland)'},
{id: '118179919', name: 'Shares - NZX (New Zealand)'},
{id: '186563295', name: 'Weekend Markets'},
],
})
);

const marketCategories = await global.client.rest.market.getMarketCategories();
const marketNodes = marketCategories.nodes!;
expect(marketNodes.length).toBe(4);
expect(marketNodes[marketNodes.length - 1].name).toBe('Weekend Markets');
});

it('returns all sub-nodes of the given node in the market navigation hierarchy', async () => {
const nodeId = '138425500';

nock(APIClient.URL_DEMO)
.get(`${MarketAPI.URL.MARKETNAVIGATION}/${nodeId}`)
.reply(
200,
JSON.stringify({
markets: [
{
bid: null,
delayTime: 0,
epic: 'EZ.D.WEWGREY.Month2.IP',
expiry: 'DEC-20',
high: 15.0,
instrumentName: 'WeWork IPO Market Cap ($Bn)',
instrumentType: 'SHARES',
lotSize: 10,
low: 14.0,
marketStatus: 'TRADEABLE',
netChange: 0.0,
offer: null,
otcTradeable: true,
percentageChange: 0.0,
scalingFactor: 1,
streamingPricesAvailable: true,
updateTime: '-3600000',
updateTimeUTC: '00:00:00',
},
],
nodes: null,
})
);

const marketCategories = await global.client.rest.market.getMarketCategories(nodeId);
expect(marketCategories.nodes).toBeNull();
expect(marketCategories.markets![0].marketStatus).toBe('TRADEABLE');
});
});
});
23 changes: 23 additions & 0 deletions src/market/MarketAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {AxiosInstance} from 'axios';

export interface MarketNode {
id: string;
name: string;
}

export interface MarketNavigation {
markets?: Market[];
nodes?: MarketNode[];
}

export interface Market {
bid?: number;
delayTime: number;
Expand All @@ -25,6 +35,7 @@ export interface MarketSearch {

export class MarketAPI {
static readonly URL = {
MARKETNAVIGATION: `/marketnavigation`,
MARKETS: `/markets`,
};

Expand All @@ -41,4 +52,16 @@ export class MarketAPI {
const response = await this.apiClient.get<MarketSearch>(resource);
return response.data;
}

/**
* Returns all nodes (market categories) in the market navigation hierarchy.
*
* @see https://labs.ig.com/rest-trading-api-reference/service-detail?id=550
* @see https://labs.ig.com/rest-trading-api-reference/service-detail?id=544
*/
async getMarketCategories(nodeId?: string): Promise<MarketNavigation> {
const resource = nodeId ? `${MarketAPI.URL.MARKETNAVIGATION}/${nodeId}` : MarketAPI.URL.MARKETNAVIGATION;
const response = await this.apiClient.get<MarketNavigation>(resource);
return response.data;
}
}

0 comments on commit 459d826

Please sign in to comment.