@bitgen/sdk 1.0.8 → 1.1.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/CHANGELOG.md +13 -0
- package/README.md +3 -1
- package/dist/cjs/client.d.ts +4 -0
- package/dist/cjs/client.d.ts.map +1 -1
- package/dist/cjs/client.js +6 -0
- package/dist/cjs/client.js.map +1 -1
- package/dist/cjs/constants.d.ts +118 -0
- package/dist/cjs/constants.d.ts.map +1 -1
- package/dist/cjs/constants.js +93 -1
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +14 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/resources/autotrading.d.ts +44 -0
- package/dist/cjs/resources/autotrading.d.ts.map +1 -0
- package/dist/cjs/resources/autotrading.js +116 -0
- package/dist/cjs/resources/autotrading.js.map +1 -0
- package/dist/cjs/resources/bot.d.ts +31 -0
- package/dist/cjs/resources/bot.d.ts.map +1 -0
- package/dist/cjs/resources/bot.js +54 -0
- package/dist/cjs/resources/bot.js.map +1 -0
- package/dist/cjs/types/autotrading.d.ts +196 -0
- package/dist/cjs/types/autotrading.d.ts.map +1 -0
- package/dist/cjs/types/autotrading.js +3 -0
- package/dist/cjs/types/autotrading.js.map +1 -0
- package/dist/cjs/types/bot.d.ts +45 -0
- package/dist/cjs/types/bot.d.ts.map +1 -0
- package/dist/cjs/types/bot.js +3 -0
- package/dist/cjs/types/bot.js.map +1 -0
- package/dist/cjs/types/index.d.ts +13 -3
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/client.d.ts +4 -0
- package/dist/esm/client.d.ts.map +1 -1
- package/dist/esm/client.js +6 -0
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/constants.d.ts +118 -0
- package/dist/esm/constants.d.ts.map +1 -1
- package/dist/esm/constants.js +92 -0
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/resources/autotrading.d.ts +44 -0
- package/dist/esm/resources/autotrading.d.ts.map +1 -0
- package/dist/esm/resources/autotrading.js +112 -0
- package/dist/esm/resources/autotrading.js.map +1 -0
- package/dist/esm/resources/bot.d.ts +31 -0
- package/dist/esm/resources/bot.d.ts.map +1 -0
- package/dist/esm/resources/bot.js +50 -0
- package/dist/esm/resources/bot.js.map +1 -0
- package/dist/esm/types/autotrading.d.ts +196 -0
- package/dist/esm/types/autotrading.d.ts.map +1 -0
- package/dist/esm/types/autotrading.js +2 -0
- package/dist/esm/types/autotrading.js.map +1 -0
- package/dist/esm/types/bot.d.ts +45 -0
- package/dist/esm/types/bot.d.ts.map +1 -0
- package/dist/esm/types/bot.js +2 -0
- package/dist/esm/types/bot.js.map +1 -0
- package/dist/esm/types/index.d.ts +13 -3
- package/dist/esm/types/index.d.ts.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +1 -1
- package/readme/concepts.md +3 -3
- package/readme/errors.md +1 -1
- package/readme/resource/autotrading.md +304 -0
- package/readme/resource/bot.md +207 -0
- package/readme/resource/customer.md +1 -1
- package/readme/resource/transaction.md +1 -1
- package/readme/resource/webhooks.md +2 -0
- package/src/client.ts +6 -0
- package/src/constants.ts +131 -0
- package/src/index.ts +28 -0
- package/src/resources/autotrading.ts +132 -0
- package/src/resources/bot.ts +58 -0
- package/src/types/autotrading.ts +218 -0
- package/src/types/bot.ts +48 -0
- package/src/types/index.ts +45 -3
- package/src/version.ts +1 -1
package/src/constants.ts
CHANGED
|
@@ -50,6 +50,24 @@ export const BankDirection = Object.freeze({
|
|
|
50
50
|
/** The values of `BankDirection` */
|
|
51
51
|
export type BankDirection = (typeof BankDirection)[keyof typeof BankDirection]
|
|
52
52
|
|
|
53
|
+
/** States of a module a bot serves — `BotApplication.state` */
|
|
54
|
+
export const BotApplicationState = Object.freeze({
|
|
55
|
+
ENABLED: 'ENABLED',
|
|
56
|
+
PENDING: 'PENDING',
|
|
57
|
+
SUSPENDED: 'SUSPENDED',
|
|
58
|
+
} as const)
|
|
59
|
+
/** The values of `BotApplicationState` */
|
|
60
|
+
export type BotApplicationState = (typeof BotApplicationState)[keyof typeof BotApplicationState]
|
|
61
|
+
|
|
62
|
+
/** States of a bot — `Bot.state`, compare it with `BotState.ENABLED` */
|
|
63
|
+
export const BotState = Object.freeze({
|
|
64
|
+
ENABLED: 'ENABLED',
|
|
65
|
+
CLOSED: 'CLOSED',
|
|
66
|
+
FROZEN: 'FROZEN',
|
|
67
|
+
} as const)
|
|
68
|
+
/** The values of `BotState` */
|
|
69
|
+
export type BotState = (typeof BotState)[keyof typeof BotState]
|
|
70
|
+
|
|
53
71
|
/** States of a connector — `Core.state`; also the `state` filter of `client.core.list` */
|
|
54
72
|
export const CoreState = Object.freeze({
|
|
55
73
|
ENABLED: 'ENABLED',
|
|
@@ -86,6 +104,23 @@ export const CustomerState = Object.freeze({
|
|
|
86
104
|
/** The values of `CustomerState` */
|
|
87
105
|
export type CustomerState = (typeof CustomerState)[keyof typeof CustomerState]
|
|
88
106
|
|
|
107
|
+
/** Categories of an extension — `BotExtension.category` */
|
|
108
|
+
export const ExtensionCategory = Object.freeze({
|
|
109
|
+
AI: 'AI',
|
|
110
|
+
CGP: 'CGP',
|
|
111
|
+
MISC: 'MISC',
|
|
112
|
+
} as const)
|
|
113
|
+
/** The values of `ExtensionCategory` */
|
|
114
|
+
export type ExtensionCategory = (typeof ExtensionCategory)[keyof typeof ExtensionCategory]
|
|
115
|
+
|
|
116
|
+
/** States of an extension — `BotExtension.state` */
|
|
117
|
+
export const ExtensionState = Object.freeze({
|
|
118
|
+
ENABLED: 'ENABLED',
|
|
119
|
+
DISABLED: 'DISABLED',
|
|
120
|
+
} as const)
|
|
121
|
+
/** The values of `ExtensionState` */
|
|
122
|
+
export type ExtensionState = (typeof ExtensionState)[keyof typeof ExtensionState]
|
|
123
|
+
|
|
89
124
|
/** `KYC` (a person) or `KYB` (a business) — `Identity.mode` */
|
|
90
125
|
export const IdentityMode = Object.freeze({
|
|
91
126
|
KYC: 'KYC',
|
|
@@ -180,6 +215,100 @@ export const StakingPositionState = Object.freeze({
|
|
|
180
215
|
/** The values of `StakingPositionState` */
|
|
181
216
|
export type StakingPositionState = (typeof StakingPositionState)[keyof typeof StakingPositionState]
|
|
182
217
|
|
|
218
|
+
/** States of an automated-trading strategy — `Strategy.state` */
|
|
219
|
+
export const StrategyState = Object.freeze({
|
|
220
|
+
RUNNING: 'RUNNING',
|
|
221
|
+
PAUSED: 'PAUSED',
|
|
222
|
+
CLOSING: 'CLOSING',
|
|
223
|
+
CLOSED: 'CLOSED',
|
|
224
|
+
} as const)
|
|
225
|
+
/** The values of `StrategyState` */
|
|
226
|
+
export type StrategyState = (typeof StrategyState)[keyof typeof StrategyState]
|
|
227
|
+
|
|
228
|
+
/** Risk profile of a strategy — the four first buy and sell in real time, `DCA` buys at every due date and holds */
|
|
229
|
+
export const StrategyRisk = Object.freeze({
|
|
230
|
+
PRUDENT: 'PRUDENT',
|
|
231
|
+
BALANCED: 'BALANCED',
|
|
232
|
+
DYNAMIC: 'DYNAMIC',
|
|
233
|
+
OFFENSIVE: 'OFFENSIVE',
|
|
234
|
+
DCA: 'DCA',
|
|
235
|
+
} as const)
|
|
236
|
+
/** The values of `StrategyRisk` */
|
|
237
|
+
export type StrategyRisk = (typeof StrategyRisk)[keyof typeof StrategyRisk]
|
|
238
|
+
|
|
239
|
+
/** Pace of a strategy — `AUTO` is real time; a `DCA` takes a dated pace */
|
|
240
|
+
export const StrategyFrequency = Object.freeze({
|
|
241
|
+
AUTO: 'AUTO',
|
|
242
|
+
DAILY: 'DAILY',
|
|
243
|
+
WEEKLY: 'WEEKLY',
|
|
244
|
+
MONTHLY: 'MONTHLY',
|
|
245
|
+
} as const)
|
|
246
|
+
/** The values of `StrategyFrequency` */
|
|
247
|
+
export type StrategyFrequency = (typeof StrategyFrequency)[keyof typeof StrategyFrequency]
|
|
248
|
+
|
|
249
|
+
/** Who spreads the capital over the universe — only `AUTO` is accepted on a write */
|
|
250
|
+
export const StrategyAllocation = Object.freeze({
|
|
251
|
+
AUTO: 'AUTO',
|
|
252
|
+
MANUAL: 'MANUAL',
|
|
253
|
+
} as const)
|
|
254
|
+
/** The values of `StrategyAllocation` */
|
|
255
|
+
export type StrategyAllocation = (typeof StrategyAllocation)[keyof typeof StrategyAllocation]
|
|
256
|
+
|
|
257
|
+
/** What a closing does with the positions — `KEEP` delivers them to the custody wallets of the bot */
|
|
258
|
+
export const StrategyClosing = Object.freeze({
|
|
259
|
+
SELL_ALL: 'SELL_ALL',
|
|
260
|
+
KEEP: 'KEEP',
|
|
261
|
+
} as const)
|
|
262
|
+
/** The values of `StrategyClosing` */
|
|
263
|
+
export type StrategyClosing = (typeof StrategyClosing)[keyof typeof StrategyClosing]
|
|
264
|
+
|
|
265
|
+
/** Which emails the supervisor of the bot receives — a strategy stuck for want of funds is reported whatever this says */
|
|
266
|
+
export const StrategyNotifyMode = Object.freeze({
|
|
267
|
+
EVERY_ORDER: 'EVERY_ORDER',
|
|
268
|
+
THRESHOLDS: 'THRESHOLDS',
|
|
269
|
+
DAILY: 'DAILY',
|
|
270
|
+
NEVER: 'NEVER',
|
|
271
|
+
} as const)
|
|
272
|
+
/** The values of `StrategyNotifyMode` */
|
|
273
|
+
export type StrategyNotifyMode = (typeof StrategyNotifyMode)[keyof typeof StrategyNotifyMode]
|
|
274
|
+
|
|
275
|
+
/** What a line of the journal of a strategy is — `StrategyJournalEntry.event` */
|
|
276
|
+
export const StrategyJournalEvent = Object.freeze({
|
|
277
|
+
BUY: 'buy',
|
|
278
|
+
SELL: 'sell',
|
|
279
|
+
GUARD: 'guard',
|
|
280
|
+
PAUSED: 'paused',
|
|
281
|
+
CLOSING: 'closing',
|
|
282
|
+
CLOSED: 'closed',
|
|
283
|
+
IDLE: 'idle',
|
|
284
|
+
RESUMED: 'resumed',
|
|
285
|
+
DELIVERING: 'delivering',
|
|
286
|
+
DELIVERED: 'delivered',
|
|
287
|
+
DUST: 'dust',
|
|
288
|
+
FAILED: 'failed',
|
|
289
|
+
} as const)
|
|
290
|
+
/** The values of `StrategyJournalEvent` */
|
|
291
|
+
export type StrategyJournalEvent = (typeof StrategyJournalEvent)[keyof typeof StrategyJournalEvent]
|
|
292
|
+
|
|
293
|
+
/** Why a guard rail or a closing placed an order — the `reason` of an `aitrading.action` delivery, `null` when the engine decided */
|
|
294
|
+
export const StrategyExitReason = Object.freeze({
|
|
295
|
+
STOP_LOSS: 'stop_loss',
|
|
296
|
+
TAKE_PROFIT: 'take_profit',
|
|
297
|
+
TRAILING_STOP: 'trailing_stop',
|
|
298
|
+
CLOSED: 'closed',
|
|
299
|
+
} as const)
|
|
300
|
+
/** The values of `StrategyExitReason` */
|
|
301
|
+
export type StrategyExitReason = (typeof StrategyExitReason)[keyof typeof StrategyExitReason]
|
|
302
|
+
|
|
303
|
+
/** Why a strategy is resting — `Strategy.idleReason`, `null` when it runs normally */
|
|
304
|
+
export const StrategyIdleReason = Object.freeze({
|
|
305
|
+
ORGANIZATION_DISABLED: 'organization_disabled',
|
|
306
|
+
EXTENSION_UNAVAILABLE: 'extension_unavailable',
|
|
307
|
+
BOT_UNAVAILABLE: 'bot_unavailable',
|
|
308
|
+
} as const)
|
|
309
|
+
/** The values of `StrategyIdleReason` */
|
|
310
|
+
export type StrategyIdleReason = (typeof StrategyIdleReason)[keyof typeof StrategyIdleReason]
|
|
311
|
+
|
|
183
312
|
/** States of a webhook subscription (`Subscriber.state`) and of an event of the catalogue (`WebhookType.state`) — `ARCHIVED` ones are left out of `client.webhooks.list` unless `includeArchived` */
|
|
184
313
|
export const SubscriberState = Object.freeze({
|
|
185
314
|
ENABLED: 'ENABLED',
|
|
@@ -276,6 +405,8 @@ export const WebhookEventName = Object.freeze({
|
|
|
276
405
|
STAKING_CLAIMED: 'staking.claimed',
|
|
277
406
|
ALERT_OPENED: 'alert.opened',
|
|
278
407
|
ALERT_STATUS: 'alert.status',
|
|
408
|
+
AITRADING_SETUP: 'aitrading.setup',
|
|
409
|
+
AITRADING_ACTION: 'aitrading.action',
|
|
279
410
|
} as const)
|
|
280
411
|
/** Known event names — the catalogue may grow: any string is accepted */
|
|
281
412
|
export type WebhookEventName = (typeof WebhookEventName)[keyof typeof WebhookEventName] | (string & {})
|
package/src/index.ts
CHANGED
|
@@ -10,9 +10,13 @@ export {
|
|
|
10
10
|
ApikeyState,
|
|
11
11
|
AssetState,
|
|
12
12
|
BankDirection,
|
|
13
|
+
BotApplicationState,
|
|
14
|
+
BotState,
|
|
13
15
|
CoreState,
|
|
14
16
|
CoreType,
|
|
15
17
|
CustomerState,
|
|
18
|
+
ExtensionCategory,
|
|
19
|
+
ExtensionState,
|
|
16
20
|
IdentityMode,
|
|
17
21
|
IdentityState,
|
|
18
22
|
Locale,
|
|
@@ -22,6 +26,15 @@ export {
|
|
|
22
26
|
StakingMovementKind,
|
|
23
27
|
StakingMovementState,
|
|
24
28
|
StakingPositionState,
|
|
29
|
+
StrategyAllocation,
|
|
30
|
+
StrategyClosing,
|
|
31
|
+
StrategyExitReason,
|
|
32
|
+
StrategyFrequency,
|
|
33
|
+
StrategyIdleReason,
|
|
34
|
+
StrategyJournalEvent,
|
|
35
|
+
StrategyNotifyMode,
|
|
36
|
+
StrategyRisk,
|
|
37
|
+
StrategyState,
|
|
25
38
|
SubscriberState,
|
|
26
39
|
TradingDirection,
|
|
27
40
|
TransactionDirection,
|
|
@@ -108,4 +121,19 @@ export type {
|
|
|
108
121
|
Apikey,
|
|
109
122
|
ApikeyLog,
|
|
110
123
|
ApikeyListParams,
|
|
124
|
+
BotExtension,
|
|
125
|
+
BotApplication,
|
|
126
|
+
Bot,
|
|
127
|
+
BotRef,
|
|
128
|
+
BotModuleRef,
|
|
129
|
+
BotListParams,
|
|
130
|
+
StrategyGuards,
|
|
131
|
+
Strategy,
|
|
132
|
+
StrategyInput,
|
|
133
|
+
StrategyListParams,
|
|
134
|
+
StrategyRatios,
|
|
135
|
+
StrategyHistory,
|
|
136
|
+
StrategyJournalEntry,
|
|
137
|
+
AutotradingPortfolio,
|
|
138
|
+
StrategyRef,
|
|
111
139
|
} from './types/index.js'
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { HttpClient } from '../http.js'
|
|
2
|
+
import type {
|
|
3
|
+
AutotradingPortfolio,
|
|
4
|
+
Created,
|
|
5
|
+
Page,
|
|
6
|
+
PageParams,
|
|
7
|
+
Strategy,
|
|
8
|
+
StrategyHistory,
|
|
9
|
+
StrategyInput,
|
|
10
|
+
StrategyJournalEntry,
|
|
11
|
+
StrategyListParams,
|
|
12
|
+
StrategyRef,
|
|
13
|
+
} from '../types/index.js'
|
|
14
|
+
import { amount, assetId, idOf, pathSegment } from '../utils.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The automated-trading module: the organization sets a frame — a strategy — and the engine decides on
|
|
18
|
+
* its own when to buy and when to sell. The decision itself stays closed: no signal and no rationale is
|
|
19
|
+
* exposed. The bot assigned to the module holds the funds, its EUR account pays for the purchases
|
|
20
|
+
* (`client.bot`, `client.bank`).
|
|
21
|
+
*
|
|
22
|
+
* `create` and `update` need the module installed, enabled and served by a bot; the reads and
|
|
23
|
+
* `pause` / `resume` / `close` answer whatever the state of the module, so a strategy can always be read
|
|
24
|
+
* and closed.
|
|
25
|
+
*
|
|
26
|
+
* **Every EUR amount of this resource is a string**, in and out — `capital`, `installment`, the guard
|
|
27
|
+
* rail values, `value`, `engaged`, `unrealised`, `realisedGains`, the `amount` and `cash` of a journal
|
|
28
|
+
* line. The numbers are the points of a curve, `performance`, `benchmark`, `percent`, `trades`,
|
|
29
|
+
* `losingStreak` and `strategies`.
|
|
30
|
+
*/
|
|
31
|
+
export class AutotradingResource {
|
|
32
|
+
constructor(private readonly http: HttpClient) {}
|
|
33
|
+
|
|
34
|
+
/** Creates a `RUNNING` strategy, due for its first pass straight away */
|
|
35
|
+
async create(params: StrategyInput): Promise<Created> {
|
|
36
|
+
return await this.http.post('/extension/autotrading/strategy', strategyBody(params))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Strategies of the organization, the most recent first — closed ones with `includeClosed` */
|
|
40
|
+
async list(params: StrategyListParams = {}): Promise<Page<Strategy>> {
|
|
41
|
+
return await this.http.get('/extension/autotrading/strategies', { ...params })
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** One strategy by uuid, or by model */
|
|
45
|
+
async get(strategy: StrategyRef): Promise<Strategy> {
|
|
46
|
+
return await this.http.get(`/extension/autotrading/strategy/${this.segment(strategy)}`)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Replaces the frame of the strategy, field for field: send the whole frame every time */
|
|
50
|
+
async update(strategy: StrategyRef, params: StrategyInput): Promise<void> {
|
|
51
|
+
await this.http.put(`/extension/autotrading/strategy/${this.segment(strategy)}`, strategyBody(params))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Stops the passes and leaves the positions as they are — the state it already has is a no-op */
|
|
55
|
+
async pause(strategy: StrategyRef): Promise<void> {
|
|
56
|
+
await this.transition(strategy, 'PAUSE')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Starts the passes again and clears what had paused the strategy: its losing run and its mark of the day */
|
|
60
|
+
async resume(strategy: StrategyRef): Promise<void> {
|
|
61
|
+
await this.transition(strategy, 'RESUME')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Closes the strategy: it settles position by position, following `closing`, then reaches `CLOSED` on its own */
|
|
65
|
+
async close(strategy: StrategyRef): Promise<void> {
|
|
66
|
+
await this.transition(strategy, 'CLOSE')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Curve of the strategy, its performance, the same window on BTC, and the figures of the moment */
|
|
70
|
+
async history(strategy: StrategyRef): Promise<StrategyHistory> {
|
|
71
|
+
return await this.http.get(`/extension/autotrading/strategy/${this.segment(strategy)}/history`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What the strategy did, the most recent first — paginated inside a window of ten thousand lines */
|
|
75
|
+
async journal(strategy: StrategyRef, params: PageParams = {}): Promise<Page<StrategyJournalEntry>> {
|
|
76
|
+
return await this.http.get(`/extension/autotrading/strategy/${this.segment(strategy)}/journal`, { ...params })
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Every strategy of the organization that is not `CLOSED`, as one position: the same curves, and the totals */
|
|
80
|
+
async portfolio(): Promise<AutotradingPortfolio> {
|
|
81
|
+
return await this.http.get('/extension/autotrading/portfolio')
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private async transition(strategy: StrategyRef, action: 'PAUSE' | 'RESUME' | 'CLOSE'): Promise<void> {
|
|
85
|
+
await this.http.patch(`/extension/autotrading/strategy/${this.segment(strategy)}`, { action })
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private segment(strategy: StrategyRef): string {
|
|
89
|
+
return pathSegment(idOf(strategy, 'strategy'), 'strategy')
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The frame as the API reads it: amounts and percentages as strings, assets as uuid or ISO code */
|
|
94
|
+
function strategyBody(params: StrategyInput): Record<string, unknown> {
|
|
95
|
+
const body: Record<string, unknown> = {
|
|
96
|
+
name: params.name,
|
|
97
|
+
capital: amount(params.capital),
|
|
98
|
+
risk: params.risk,
|
|
99
|
+
frequency: params.frequency,
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (params.assets !== undefined) {
|
|
103
|
+
// A string is iterable too: without this guard `assets: 'BTC'` would reach the API as one asset per letter
|
|
104
|
+
if (!Array.isArray(params.assets)) {
|
|
105
|
+
throw new TypeError('assets must be an array of assets, each a uuid or an ISO code, or a model')
|
|
106
|
+
}
|
|
107
|
+
body.assets = params.assets.map((asset) => assetId(asset))
|
|
108
|
+
}
|
|
109
|
+
if (params.endDate !== undefined) body.endDate = params.endDate
|
|
110
|
+
if (params.allocation !== undefined) body.allocation = params.allocation
|
|
111
|
+
if (params.reinvest !== undefined) body.reinvest = params.reinvest
|
|
112
|
+
if (params.closing !== undefined) body.closing = params.closing
|
|
113
|
+
if (params.notifyMode !== undefined) body.notifyMode = params.notifyMode
|
|
114
|
+
|
|
115
|
+
for (const [key, value] of [
|
|
116
|
+
['stopLoss', params.stopLoss], ['takeProfit', params.takeProfit], ['trailingStop', params.trailingStop],
|
|
117
|
+
['orderLimits', params.orderLimits], ['autoPause', params.autoPause],
|
|
118
|
+
] as const) {
|
|
119
|
+
if (value !== undefined) body[key] = value
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
for (const [key, value] of [
|
|
123
|
+
['installment', params.installment], ['stopLossPercent', params.stopLossPercent],
|
|
124
|
+
['takeProfitPercent', params.takeProfitPercent], ['trailingStopPercent', params.trailingStopPercent],
|
|
125
|
+
['maxOrderPercent', params.maxOrderPercent], ['minOrderAmount', params.minOrderAmount],
|
|
126
|
+
['autoPauseLoss', params.autoPauseLoss], ['autoPauseStreak', params.autoPauseStreak],
|
|
127
|
+
] as const) {
|
|
128
|
+
if (value !== undefined) body[key] = amount(value)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return body
|
|
132
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { HttpClient } from '../http.js'
|
|
2
|
+
import type { Bot, BotApplication, BotListParams, BotModuleRef, BotRef, Created, Page } from '../types/index.js'
|
|
3
|
+
import { idOf, pathSegment } from '../utils.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The technical customers the organization runs for its own account, and the modules they serve.
|
|
7
|
+
* A bot is left out of the assets under management and of the customer count, it does not appear
|
|
8
|
+
* among the customers or in the journal of transactions, and it cannot sign in.
|
|
9
|
+
*
|
|
10
|
+
* `list` and `get` answer the identity of a bot and its modules, and nothing more: the details of the
|
|
11
|
+
* account behind it are read through `client.customer`.
|
|
12
|
+
*/
|
|
13
|
+
export class BotResource {
|
|
14
|
+
constructor(private readonly http: HttpClient) {}
|
|
15
|
+
|
|
16
|
+
/** Creates a bot in the organization — takes nothing: the platform writes the login, the name and a validated identity */
|
|
17
|
+
async create(): Promise<Created> {
|
|
18
|
+
return await this.http.post('/bot')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Bots of the organization, each with the modules it serves — closed ones with `includeClosed` */
|
|
22
|
+
async list(params: BotListParams = {}): Promise<Page<Bot>> {
|
|
23
|
+
return await this.http.get('/bot', { ...params })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One bot by uuid, or by model — a closed bot stays readable; the same fields as `list` */
|
|
27
|
+
async get(bot: BotRef): Promise<Bot> {
|
|
28
|
+
return await this.http.get(`/bot/${this.segment(bot)}`)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Attaches the bot to a module. A module is served by one bot at most; the same bot again is a no-op */
|
|
32
|
+
async assign(bot: BotRef, module: BotModuleRef): Promise<void> {
|
|
33
|
+
await this.http.patch(`/bot/${this.segment(bot)}/assign/${this.moduleSegment(module)}`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Detaches the bot from a module — a module it does not serve is a no-op */
|
|
37
|
+
async detach(bot: BotRef, module: BotModuleRef): Promise<void> {
|
|
38
|
+
await this.http.delete(`/bot/${this.segment(bot)}/assign/${this.moduleSegment(module)}`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The modules this bot serves — not paginated: `count` is the length of the list */
|
|
42
|
+
async modules(bot: BotRef): Promise<Page<BotApplication>> {
|
|
43
|
+
return await this.http.get(`/bot/${this.segment(bot)}/assign`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Closes the bot. It must serve no module and hold no open position */
|
|
47
|
+
async close(bot: BotRef): Promise<void> {
|
|
48
|
+
await this.http.delete(`/bot/${this.segment(bot)}`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private segment(bot: BotRef): string {
|
|
52
|
+
return pathSegment(idOf(bot, 'bot'), 'bot')
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private moduleSegment(module: BotModuleRef): string {
|
|
56
|
+
return pathSegment(idOf(module, 'module'), 'module')
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
StrategyState,
|
|
3
|
+
StrategyRisk,
|
|
4
|
+
StrategyFrequency,
|
|
5
|
+
StrategyAllocation,
|
|
6
|
+
StrategyClosing,
|
|
7
|
+
StrategyNotifyMode,
|
|
8
|
+
StrategyJournalEvent,
|
|
9
|
+
StrategyExitReason,
|
|
10
|
+
StrategyIdleReason,
|
|
11
|
+
} from '../constants.js'
|
|
12
|
+
import type {
|
|
13
|
+
StrategyState,
|
|
14
|
+
StrategyRisk,
|
|
15
|
+
StrategyFrequency,
|
|
16
|
+
StrategyAllocation,
|
|
17
|
+
StrategyClosing,
|
|
18
|
+
StrategyNotifyMode,
|
|
19
|
+
StrategyJournalEvent,
|
|
20
|
+
StrategyIdleReason,
|
|
21
|
+
} from '../constants.js'
|
|
22
|
+
import type { Amount, AssetInput, History, OrganizationSummary, UserSummary } from './index.js'
|
|
23
|
+
|
|
24
|
+
/** The guard rails of a strategy, switch by switch, with the value each one uses — percentages and EUR amounts as strings */
|
|
25
|
+
export interface StrategyGuards {
|
|
26
|
+
stopLoss: boolean
|
|
27
|
+
/** Fall from the cost that sells, as a percentage */
|
|
28
|
+
stopLossPercent: string
|
|
29
|
+
takeProfit: boolean
|
|
30
|
+
/** Gain over the cost that sells, as a percentage */
|
|
31
|
+
takeProfitPercent: string
|
|
32
|
+
trailingStop: boolean
|
|
33
|
+
/** Fall under the highest point that sells, as a percentage */
|
|
34
|
+
trailingStopPercent: string
|
|
35
|
+
/** Bounds the purchases — a sale is bounded by the sale minimum of the platform */
|
|
36
|
+
orderLimits: boolean
|
|
37
|
+
/** Largest purchase, as a percentage of `capital` */
|
|
38
|
+
maxOrderPercent: string
|
|
39
|
+
/** Floor of a purchase, in EUR */
|
|
40
|
+
minOrderAmount: string
|
|
41
|
+
autoPause: boolean
|
|
42
|
+
/** Daily loss that pauses, as a percentage of `capital` */
|
|
43
|
+
autoPauseLoss: string
|
|
44
|
+
/** Losing sales in a row that pauses */
|
|
45
|
+
autoPauseStreak: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A strategy: the frame the organization sets, the state it is in, and its figures of the moment.
|
|
50
|
+
*
|
|
51
|
+
* Every EUR amount of this resource is a **string** — `capital`, `installment`, the guard rail values,
|
|
52
|
+
* `engaged`. The numbers are `trades` and `losingStreak`.
|
|
53
|
+
*/
|
|
54
|
+
export interface Strategy {
|
|
55
|
+
uuid: string
|
|
56
|
+
/** When the frame last changed (epoch seconds) — a change of `state` leaves it as it is */
|
|
57
|
+
updatedAt: number
|
|
58
|
+
createdAt: number
|
|
59
|
+
state: StrategyState
|
|
60
|
+
organization: OrganizationSummary
|
|
61
|
+
/** The bot that holds the funds */
|
|
62
|
+
bot: UserSummary
|
|
63
|
+
name: string
|
|
64
|
+
/** The universe, as ISO codes — empty when the engine picks in the whole catalogue */
|
|
65
|
+
assets: string[]
|
|
66
|
+
/** Ceiling of the strategy, in EUR: never gone past, whatever the balance of the bot */
|
|
67
|
+
capital: string
|
|
68
|
+
/** Amount of one due date, in EUR — `null` outside `DCA` */
|
|
69
|
+
installment: string | null
|
|
70
|
+
allocation: StrategyAllocation
|
|
71
|
+
/** Weights of the universe — an empty list while the bot spreads the capital itself */
|
|
72
|
+
allocations: Record<string, string> | []
|
|
73
|
+
/** Whether the gains go back to work */
|
|
74
|
+
reinvest: boolean
|
|
75
|
+
risk: StrategyRisk
|
|
76
|
+
frequency: StrategyFrequency
|
|
77
|
+
/** Last day of the strategy (epoch seconds) — `null` when it has none */
|
|
78
|
+
endDate: number | null
|
|
79
|
+
closing: StrategyClosing
|
|
80
|
+
guards: StrategyGuards
|
|
81
|
+
notifyMode: StrategyNotifyMode
|
|
82
|
+
/** When the strategy was closed (epoch seconds) — `null` while it is open */
|
|
83
|
+
closedAt: number | null
|
|
84
|
+
/** When its next pass is due (epoch seconds) */
|
|
85
|
+
nextRunAt: number | null
|
|
86
|
+
/** Why the strategy is resting — `null` when it runs normally */
|
|
87
|
+
idleReason: StrategyIdleReason | null
|
|
88
|
+
/** Losing sales in a row, reset by a winning sale and by a resume */
|
|
89
|
+
losingStreak: number
|
|
90
|
+
/** Gains of the sales already settled, in EUR, losses included: negative when the strategy is behind */
|
|
91
|
+
realisedGains: string
|
|
92
|
+
/** Orders settled since the strategy started */
|
|
93
|
+
trades: number
|
|
94
|
+
/** Cost of the positions it holds, in EUR: this is what compares to `capital` */
|
|
95
|
+
engaged: string
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The frame of a strategy. `update` **replaces** it field for field: a call without `assets` puts the
|
|
100
|
+
* strategy back on the whole catalogue, one without `stopLoss` switches the stop loss off — send the
|
|
101
|
+
* whole frame every time. The booleans command and the values follow: `stopLossPercent` is read when
|
|
102
|
+
* `stopLoss` is `true`.
|
|
103
|
+
*/
|
|
104
|
+
export interface StrategyInput {
|
|
105
|
+
/** 120 characters at most */
|
|
106
|
+
name: string
|
|
107
|
+
/** Ceiling of the strategy, in EUR */
|
|
108
|
+
capital: Amount
|
|
109
|
+
risk: StrategyRisk
|
|
110
|
+
/** A `DCA` takes a dated pace, so `AUTO` is refused for it */
|
|
111
|
+
frequency: StrategyFrequency
|
|
112
|
+
/** The universe, 15 at most, each asset by uuid or ISO code — absent or empty, the engine picks in the whole catalogue */
|
|
113
|
+
assets?: AssetInput[]
|
|
114
|
+
/** Amount of one due date, in EUR — required for `DCA`, ignored for the other profiles */
|
|
115
|
+
installment?: Amount
|
|
116
|
+
/** Last day of the strategy, `YYYY-MM-DD` — read for `DCA` only */
|
|
117
|
+
endDate?: string
|
|
118
|
+
/** Only `AUTO` is accepted: the bot alone spreads the capital over the universe */
|
|
119
|
+
allocation?: Extract<StrategyAllocation, 'AUTO'>
|
|
120
|
+
/** `true` by default: the gains go back to work */
|
|
121
|
+
reinvest?: boolean
|
|
122
|
+
/** `SELL_ALL` by default */
|
|
123
|
+
closing?: StrategyClosing
|
|
124
|
+
/** `THRESHOLDS` by default */
|
|
125
|
+
notifyMode?: StrategyNotifyMode
|
|
126
|
+
stopLoss?: boolean
|
|
127
|
+
/** Percentage above zero and up to 100 */
|
|
128
|
+
stopLossPercent?: Amount
|
|
129
|
+
takeProfit?: boolean
|
|
130
|
+
/** Percentage, same shape */
|
|
131
|
+
takeProfitPercent?: Amount
|
|
132
|
+
trailingStop?: boolean
|
|
133
|
+
/** Percentage, same shape */
|
|
134
|
+
trailingStopPercent?: Amount
|
|
135
|
+
/** Bounds the purchases of the strategy */
|
|
136
|
+
orderLimits?: boolean
|
|
137
|
+
/** Largest purchase, as a percentage of `capital` */
|
|
138
|
+
maxOrderPercent?: Amount
|
|
139
|
+
/** Floor of a purchase, in EUR */
|
|
140
|
+
minOrderAmount?: Amount
|
|
141
|
+
autoPause?: boolean
|
|
142
|
+
/** Daily loss that pauses, as a percentage of `capital` */
|
|
143
|
+
autoPauseLoss?: Amount
|
|
144
|
+
/** Losing sales in a row that pauses, a whole number from one */
|
|
145
|
+
autoPauseStreak?: Amount
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface StrategyListParams {
|
|
149
|
+
offset?: number
|
|
150
|
+
limit?: number
|
|
151
|
+
/** Also returns the `CLOSED` strategies — default `false` */
|
|
152
|
+
includeClosed?: boolean
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** One percentage per window, `null` when the window has no base to compute on */
|
|
156
|
+
export type StrategyRatios = Record<'d' | 'w' | 'm' | 'y' | 'all', number | null>
|
|
157
|
+
|
|
158
|
+
/** The curve of a strategy, its performance, the same window on BTC to compare against, and the figures of the moment */
|
|
159
|
+
export interface StrategyHistory {
|
|
160
|
+
/** Value of the positions over time — each curve starts when the strategy was created */
|
|
161
|
+
history: History
|
|
162
|
+
/** Return over each window: it accounts for what went in and out, so a purchase does not read as a gain */
|
|
163
|
+
performance: StrategyRatios
|
|
164
|
+
/** Move of BTC over the same windows */
|
|
165
|
+
benchmark: StrategyRatios
|
|
166
|
+
/** What the positions are worth now, in EUR: an asset with no price of its own is left out */
|
|
167
|
+
value: string
|
|
168
|
+
/** Cost of the positions, in EUR */
|
|
169
|
+
engaged: string
|
|
170
|
+
/** `value` less the cost of the priced positions */
|
|
171
|
+
unrealised: string
|
|
172
|
+
realisedGains: string
|
|
173
|
+
trades: number
|
|
174
|
+
/** Weight of each asset of the universe, the largest first — an asset with nothing in it comes last at zero,
|
|
175
|
+
* and the shares add up to 100 as soon as a position is worth something, and are all zero when the strategy holds nothing */
|
|
176
|
+
distribution: { asset: string, amount: string, percent: number }[]
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* One line of what a strategy did. Every line about an asset carries the position held **after** the
|
|
181
|
+
* operation; the lines that only say what the strategy became carry no quantity.
|
|
182
|
+
*/
|
|
183
|
+
export interface StrategyJournalEntry {
|
|
184
|
+
/** When the line was written (epoch seconds) */
|
|
185
|
+
date: number | null
|
|
186
|
+
event: StrategyJournalEvent | null
|
|
187
|
+
/**
|
|
188
|
+
* What the line is about, when it has one — `stop_loss`, `take_profit` or `trailing_stop` on a `guard`
|
|
189
|
+
* (see `StrategyExitReason`); `daily_loss` or `losing_streak` on a `paused`; `end_date` or
|
|
190
|
+
* `capital_consumed` on a `closing`; `SELL_ALL` or `KEEP` on a `closed`; an idle reason on an `idle`;
|
|
191
|
+
* `order_refused`, `sale_not_executed`, `insufficient_funds` or `daily_buy_limit_exceeded` on a `failed`
|
|
192
|
+
*/
|
|
193
|
+
reason: string | null
|
|
194
|
+
/** ISO code of the asset of the line */
|
|
195
|
+
asset: string | null
|
|
196
|
+
/** EUR for a `buy`, asset units for a `sell`, as a string */
|
|
197
|
+
amount: string | null
|
|
198
|
+
/** Position held in that asset after the operation, as a string */
|
|
199
|
+
quantity: string | null
|
|
200
|
+
/** Money of the settlement, in EUR: what a purchase cost, what a sale brought in */
|
|
201
|
+
cash: string | null
|
|
202
|
+
/** The order behind the line — `null` on a line that has no order */
|
|
203
|
+
order: string | null
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Every strategy of the organization that is not `CLOSED`, as one position */
|
|
207
|
+
export interface AutotradingPortfolio {
|
|
208
|
+
history: History
|
|
209
|
+
performance: StrategyRatios
|
|
210
|
+
benchmark: StrategyRatios
|
|
211
|
+
/** How many strategies the totals cover */
|
|
212
|
+
strategies: number
|
|
213
|
+
/** Sum of the values of the strategies, in EUR */
|
|
214
|
+
value: string
|
|
215
|
+
engaged: string
|
|
216
|
+
unrealised: string
|
|
217
|
+
realisedGains: string
|
|
218
|
+
}
|
package/src/types/bot.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type { BotState, BotApplicationState, ExtensionState, ExtensionCategory } from '../constants.js'
|
|
2
|
+
import type { BotState, BotApplicationState, ExtensionState, ExtensionCategory } from '../constants.js'
|
|
3
|
+
|
|
4
|
+
/** The extension a module runs — `label` is the display name by language (`{ fr, en }`) */
|
|
5
|
+
export interface BotExtension {
|
|
6
|
+
uuid: string
|
|
7
|
+
state: ExtensionState
|
|
8
|
+
/** Technical name, e.g. `auto-trading` */
|
|
9
|
+
name: string
|
|
10
|
+
category: ExtensionCategory
|
|
11
|
+
label: Record<string, string>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A module the bot serves: an installed application of the organization and the extension it runs */
|
|
15
|
+
export interface BotApplication {
|
|
16
|
+
uuid: string
|
|
17
|
+
/** When the module was installed (epoch seconds) */
|
|
18
|
+
createdAt: number
|
|
19
|
+
state: BotApplicationState
|
|
20
|
+
extension: BotExtension
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A technical customer the organization runs for its own account: an account like any other, flagged `bot`,
|
|
25
|
+
* left out of the assets under management and of the customer count, absent from the customers and from the
|
|
26
|
+
* journal of transactions, and unable to sign in.
|
|
27
|
+
*
|
|
28
|
+
* `bot.list` and `bot.get` answer the identity of the bot and its modules, and nothing more — the details of
|
|
29
|
+
* the account behind it (contact details, identity file, settings) are read through `client.customer`.
|
|
30
|
+
*/
|
|
31
|
+
export interface Bot {
|
|
32
|
+
uuid: string
|
|
33
|
+
createdAt: number
|
|
34
|
+
state: BotState
|
|
35
|
+
/** `false` once the bot is closed */
|
|
36
|
+
isAvailable: boolean
|
|
37
|
+
/** `bot_<id>@<uuid of the organization>` — rewritten when the bot is closed */
|
|
38
|
+
login: string
|
|
39
|
+
/** The modules this bot serves, empty when it serves none */
|
|
40
|
+
applications: BotApplication[]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface BotListParams {
|
|
44
|
+
offset?: number
|
|
45
|
+
limit?: number
|
|
46
|
+
/** Also returns the closed bots — default `false` */
|
|
47
|
+
includeClosed?: boolean
|
|
48
|
+
}
|