@danielgroen/dxtrade-api 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -0
- package/dist/index.d.mts +129 -0
- package/dist/index.d.ts +129 -0
- package/dist/index.js +528 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +484 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# DXTrade API Library
|
|
2
|
+
|
|
3
|
+
<!-- create image from ./public/logo-dxtrade.svg -->
|
|
4
|
+
[](https://demo.dx.trade/developers/#/)
|
|
5
|
+
|
|
6
|
+
TypeScript client library for the DXTrade trading API.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install dxtrade-api
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick Start
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { DxtradeClient, OrderType, OrderSide, BROKER } from "dxtrade-api";
|
|
18
|
+
|
|
19
|
+
const client = new DxtradeClient({
|
|
20
|
+
username: "your_username",
|
|
21
|
+
password: "your_password",
|
|
22
|
+
broker: "lark",
|
|
23
|
+
accountId: "optional_account_id",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
await client.connect();
|
|
27
|
+
|
|
28
|
+
const suggestions = await client.getSymbolSuggestions("EURUSD");
|
|
29
|
+
const symbol = suggestions[0];
|
|
30
|
+
|
|
31
|
+
const order = await client.submitOrder({
|
|
32
|
+
symbol: symbol.name,
|
|
33
|
+
side: OrderSide.BUY,
|
|
34
|
+
quantity: 0.01,
|
|
35
|
+
orderType: OrderType.MARKET,
|
|
36
|
+
instrumentId: symbol.id,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
console.log(`Order ${order.orderId}: ${order.status}`);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
| Option | Type | Required | Description |
|
|
45
|
+
|---|---|---|---|
|
|
46
|
+
| `username` | `string` | Yes | DXTrade account username |
|
|
47
|
+
| `password` | `string` | Yes | DXTrade account password |
|
|
48
|
+
| `broker` | `string` | Yes | Broker identifier (e.g. `"lark"`, `"eightcap"`) |
|
|
49
|
+
| `accountId` | `string` | No | Account ID to auto-switch after login |
|
|
50
|
+
| `brokerUrls` | `Record<string, string>` | No | Custom broker URL mapping |
|
|
51
|
+
| `retries` | `number` | No | Retry count for failed requests (default: 3) |
|
|
52
|
+
| `callbacks` | `DxtradeCallbacks` | No | Event callbacks |
|
|
53
|
+
|
|
54
|
+
## Built-in Brokers
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { BROKER } from "dxtrade-api";
|
|
58
|
+
|
|
59
|
+
BROKER.LARK // "https://trade.gooeytrade.com"
|
|
60
|
+
BROKER.EIGHTCAP // "https://trader.dx-eightcap.com"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
### Session
|
|
66
|
+
|
|
67
|
+
- `client.connect()` — Login, fetch CSRF, WebSocket handshake, optional account switch
|
|
68
|
+
- `client.login()` — Authenticate with broker
|
|
69
|
+
- `client.fetchCsrf()` — Fetch CSRF token from broker page
|
|
70
|
+
- `client.switchAccount(accountId)` — Switch to a specific account
|
|
71
|
+
|
|
72
|
+
### Market Data
|
|
73
|
+
|
|
74
|
+
- `client.getSymbolSuggestions(text)` — Search for symbols
|
|
75
|
+
- `client.getSymbolInfo(symbol)` — Get instrument info (volume limits, lot size)
|
|
76
|
+
|
|
77
|
+
### Trading
|
|
78
|
+
|
|
79
|
+
- `client.submitOrder(params)` — Submit an order and wait for WebSocket confirmation
|
|
80
|
+
|
|
81
|
+
### Analytics
|
|
82
|
+
|
|
83
|
+
- `client.getAssessments(params)` — Fetch PnL assessments for a date range
|
|
84
|
+
|
|
85
|
+
## Callbacks
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const client = new DxtradeClient({
|
|
89
|
+
// ...
|
|
90
|
+
callbacks: {
|
|
91
|
+
onLogin: () => console.log("Logged in"),
|
|
92
|
+
onAccountSwitch: (id) => console.log(`Switched to ${id}`),
|
|
93
|
+
onOrderPlaced: (order) => console.log("Order placed", order),
|
|
94
|
+
onOrderUpdate: (update) => console.log("Order update", update),
|
|
95
|
+
onError: (err) => console.error(`[${err.code}] ${err.message}`),
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Examples
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
cp .env.example .env # fill in credentials
|
|
104
|
+
npm run example:connect
|
|
105
|
+
npm run example:order
|
|
106
|
+
npm run example:assessments
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## DXTrade API Docs
|
|
110
|
+
|
|
111
|
+
https://demo.dx.trade/developers/#/
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
declare const BROKER: {
|
|
2
|
+
readonly LARK: "https://trade.gooeytrade.com";
|
|
3
|
+
readonly EIGHTCAP: "https://trader.dx-eightcap.com";
|
|
4
|
+
};
|
|
5
|
+
declare function resolveBrokerUrl(broker: string, customUrls?: Record<string, string>): string;
|
|
6
|
+
|
|
7
|
+
declare const endpoints: {
|
|
8
|
+
login: (base: string) => string;
|
|
9
|
+
switchAccount: (base: string, id: string) => string;
|
|
10
|
+
suggest: (base: string, text: string) => string;
|
|
11
|
+
instrumentInfo: (base: string, symbol: string, tzOffset: number) => string;
|
|
12
|
+
submitOrder: (base: string) => string;
|
|
13
|
+
assessments: (base: string) => string;
|
|
14
|
+
websocket: (base: string) => string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
declare enum ORDER_TYPE {
|
|
18
|
+
MARKET = "MARKET",
|
|
19
|
+
LIMIT = "LIMIT",
|
|
20
|
+
STOP = "STOP"
|
|
21
|
+
}
|
|
22
|
+
declare enum SIDE {
|
|
23
|
+
BUY = "BUY",
|
|
24
|
+
SELL = "SELL"
|
|
25
|
+
}
|
|
26
|
+
declare enum ACTION {
|
|
27
|
+
OPENING = "OPENING",
|
|
28
|
+
CLOSING = "CLOSING"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare class DxtradeError extends Error {
|
|
32
|
+
code: string;
|
|
33
|
+
constructor(code: string, message: string);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface SubmitOrderParams$1 {
|
|
37
|
+
symbol: string;
|
|
38
|
+
side: SIDE;
|
|
39
|
+
quantity: number;
|
|
40
|
+
orderType: ORDER_TYPE;
|
|
41
|
+
price?: number;
|
|
42
|
+
instrumentId?: number;
|
|
43
|
+
stopLoss?: StopLossParams;
|
|
44
|
+
takeProfit?: TakeProfitParams;
|
|
45
|
+
positionEffect?: ACTION;
|
|
46
|
+
}
|
|
47
|
+
interface StopLossParams {
|
|
48
|
+
price?: number;
|
|
49
|
+
offset?: number;
|
|
50
|
+
}
|
|
51
|
+
interface TakeProfitParams {
|
|
52
|
+
price?: number;
|
|
53
|
+
offset?: number;
|
|
54
|
+
}
|
|
55
|
+
interface OrderResponse {
|
|
56
|
+
status: number;
|
|
57
|
+
data: unknown;
|
|
58
|
+
}
|
|
59
|
+
interface OrderUpdate$1 {
|
|
60
|
+
orderId: string;
|
|
61
|
+
status: string;
|
|
62
|
+
statusDescription?: string;
|
|
63
|
+
[key: string]: unknown;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface DxtradeConfig$1 {
|
|
67
|
+
username: string;
|
|
68
|
+
password: string;
|
|
69
|
+
broker: string;
|
|
70
|
+
accountId?: string;
|
|
71
|
+
brokerUrls?: Record<string, string>;
|
|
72
|
+
retries?: number;
|
|
73
|
+
debug?: boolean;
|
|
74
|
+
callbacks?: DxtradeCallbacks;
|
|
75
|
+
}
|
|
76
|
+
interface DxtradeCallbacks {
|
|
77
|
+
onError?: (error: DxtradeError) => void;
|
|
78
|
+
onLogin?: () => void;
|
|
79
|
+
onAccountSwitch?: (accountId: string) => void;
|
|
80
|
+
onOrderPlaced?: (order: OrderResponse) => void;
|
|
81
|
+
onOrderUpdate?: (order: OrderUpdate$1) => void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface SymbolSuggestion$1 {
|
|
85
|
+
id: number;
|
|
86
|
+
name: string;
|
|
87
|
+
[key: string]: unknown;
|
|
88
|
+
}
|
|
89
|
+
interface SymbolInfo$1 {
|
|
90
|
+
maxVolume: number;
|
|
91
|
+
minVolume: number;
|
|
92
|
+
volumeStep: number;
|
|
93
|
+
lotSize: number;
|
|
94
|
+
[key: string]: unknown;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface AssessmentsParams$1 {
|
|
98
|
+
from: number;
|
|
99
|
+
to: number;
|
|
100
|
+
instrument: string;
|
|
101
|
+
subtype?: string | null;
|
|
102
|
+
}
|
|
103
|
+
interface AssessmentsResponse$1 {
|
|
104
|
+
totalPL?: number;
|
|
105
|
+
[key: string]: unknown;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
declare class DxtradeClient {
|
|
109
|
+
private config;
|
|
110
|
+
private callbacks;
|
|
111
|
+
private cookies;
|
|
112
|
+
private csrf;
|
|
113
|
+
private baseUrl;
|
|
114
|
+
private retries;
|
|
115
|
+
private debug;
|
|
116
|
+
constructor(config: DxtradeConfig);
|
|
117
|
+
login(): Promise<void>;
|
|
118
|
+
fetchCsrf(): Promise<void>;
|
|
119
|
+
switchAccount(accountId: string): Promise<void>;
|
|
120
|
+
getSymbolSuggestions(text: string): Promise<SymbolSuggestion[]>;
|
|
121
|
+
getSymbolInfo(symbol: string): Promise<SymbolInfo>;
|
|
122
|
+
submitOrder(params: SubmitOrderParams): Promise<OrderUpdate>;
|
|
123
|
+
getAssessments(params: AssessmentsParams): Promise<AssessmentsResponse>;
|
|
124
|
+
connect(): Promise<void>;
|
|
125
|
+
private ensureSession;
|
|
126
|
+
private throwError;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { ACTION, type AssessmentsParams$1 as AssessmentsParams, type AssessmentsResponse$1 as AssessmentsResponse, BROKER, type DxtradeCallbacks, DxtradeClient, type DxtradeConfig$1 as DxtradeConfig, DxtradeError, ORDER_TYPE, type OrderResponse, type OrderUpdate$1 as OrderUpdate, SIDE, type StopLossParams, type SubmitOrderParams$1 as SubmitOrderParams, type SymbolInfo$1 as SymbolInfo, type SymbolSuggestion$1 as SymbolSuggestion, type TakeProfitParams, endpoints, resolveBrokerUrl };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
declare const BROKER: {
|
|
2
|
+
readonly LARK: "https://trade.gooeytrade.com";
|
|
3
|
+
readonly EIGHTCAP: "https://trader.dx-eightcap.com";
|
|
4
|
+
};
|
|
5
|
+
declare function resolveBrokerUrl(broker: string, customUrls?: Record<string, string>): string;
|
|
6
|
+
|
|
7
|
+
declare const endpoints: {
|
|
8
|
+
login: (base: string) => string;
|
|
9
|
+
switchAccount: (base: string, id: string) => string;
|
|
10
|
+
suggest: (base: string, text: string) => string;
|
|
11
|
+
instrumentInfo: (base: string, symbol: string, tzOffset: number) => string;
|
|
12
|
+
submitOrder: (base: string) => string;
|
|
13
|
+
assessments: (base: string) => string;
|
|
14
|
+
websocket: (base: string) => string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
declare enum ORDER_TYPE {
|
|
18
|
+
MARKET = "MARKET",
|
|
19
|
+
LIMIT = "LIMIT",
|
|
20
|
+
STOP = "STOP"
|
|
21
|
+
}
|
|
22
|
+
declare enum SIDE {
|
|
23
|
+
BUY = "BUY",
|
|
24
|
+
SELL = "SELL"
|
|
25
|
+
}
|
|
26
|
+
declare enum ACTION {
|
|
27
|
+
OPENING = "OPENING",
|
|
28
|
+
CLOSING = "CLOSING"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare class DxtradeError extends Error {
|
|
32
|
+
code: string;
|
|
33
|
+
constructor(code: string, message: string);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface SubmitOrderParams$1 {
|
|
37
|
+
symbol: string;
|
|
38
|
+
side: SIDE;
|
|
39
|
+
quantity: number;
|
|
40
|
+
orderType: ORDER_TYPE;
|
|
41
|
+
price?: number;
|
|
42
|
+
instrumentId?: number;
|
|
43
|
+
stopLoss?: StopLossParams;
|
|
44
|
+
takeProfit?: TakeProfitParams;
|
|
45
|
+
positionEffect?: ACTION;
|
|
46
|
+
}
|
|
47
|
+
interface StopLossParams {
|
|
48
|
+
price?: number;
|
|
49
|
+
offset?: number;
|
|
50
|
+
}
|
|
51
|
+
interface TakeProfitParams {
|
|
52
|
+
price?: number;
|
|
53
|
+
offset?: number;
|
|
54
|
+
}
|
|
55
|
+
interface OrderResponse {
|
|
56
|
+
status: number;
|
|
57
|
+
data: unknown;
|
|
58
|
+
}
|
|
59
|
+
interface OrderUpdate$1 {
|
|
60
|
+
orderId: string;
|
|
61
|
+
status: string;
|
|
62
|
+
statusDescription?: string;
|
|
63
|
+
[key: string]: unknown;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface DxtradeConfig$1 {
|
|
67
|
+
username: string;
|
|
68
|
+
password: string;
|
|
69
|
+
broker: string;
|
|
70
|
+
accountId?: string;
|
|
71
|
+
brokerUrls?: Record<string, string>;
|
|
72
|
+
retries?: number;
|
|
73
|
+
debug?: boolean;
|
|
74
|
+
callbacks?: DxtradeCallbacks;
|
|
75
|
+
}
|
|
76
|
+
interface DxtradeCallbacks {
|
|
77
|
+
onError?: (error: DxtradeError) => void;
|
|
78
|
+
onLogin?: () => void;
|
|
79
|
+
onAccountSwitch?: (accountId: string) => void;
|
|
80
|
+
onOrderPlaced?: (order: OrderResponse) => void;
|
|
81
|
+
onOrderUpdate?: (order: OrderUpdate$1) => void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface SymbolSuggestion$1 {
|
|
85
|
+
id: number;
|
|
86
|
+
name: string;
|
|
87
|
+
[key: string]: unknown;
|
|
88
|
+
}
|
|
89
|
+
interface SymbolInfo$1 {
|
|
90
|
+
maxVolume: number;
|
|
91
|
+
minVolume: number;
|
|
92
|
+
volumeStep: number;
|
|
93
|
+
lotSize: number;
|
|
94
|
+
[key: string]: unknown;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface AssessmentsParams$1 {
|
|
98
|
+
from: number;
|
|
99
|
+
to: number;
|
|
100
|
+
instrument: string;
|
|
101
|
+
subtype?: string | null;
|
|
102
|
+
}
|
|
103
|
+
interface AssessmentsResponse$1 {
|
|
104
|
+
totalPL?: number;
|
|
105
|
+
[key: string]: unknown;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
declare class DxtradeClient {
|
|
109
|
+
private config;
|
|
110
|
+
private callbacks;
|
|
111
|
+
private cookies;
|
|
112
|
+
private csrf;
|
|
113
|
+
private baseUrl;
|
|
114
|
+
private retries;
|
|
115
|
+
private debug;
|
|
116
|
+
constructor(config: DxtradeConfig);
|
|
117
|
+
login(): Promise<void>;
|
|
118
|
+
fetchCsrf(): Promise<void>;
|
|
119
|
+
switchAccount(accountId: string): Promise<void>;
|
|
120
|
+
getSymbolSuggestions(text: string): Promise<SymbolSuggestion[]>;
|
|
121
|
+
getSymbolInfo(symbol: string): Promise<SymbolInfo>;
|
|
122
|
+
submitOrder(params: SubmitOrderParams): Promise<OrderUpdate>;
|
|
123
|
+
getAssessments(params: AssessmentsParams): Promise<AssessmentsResponse>;
|
|
124
|
+
connect(): Promise<void>;
|
|
125
|
+
private ensureSession;
|
|
126
|
+
private throwError;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { ACTION, type AssessmentsParams$1 as AssessmentsParams, type AssessmentsResponse$1 as AssessmentsResponse, BROKER, type DxtradeCallbacks, DxtradeClient, type DxtradeConfig$1 as DxtradeConfig, DxtradeError, ORDER_TYPE, type OrderResponse, type OrderUpdate$1 as OrderUpdate, SIDE, type StopLossParams, type SubmitOrderParams$1 as SubmitOrderParams, type SymbolInfo$1 as SymbolInfo, type SymbolSuggestion$1 as SymbolSuggestion, type TakeProfitParams, endpoints, resolveBrokerUrl };
|