@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
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# Automated trading
|
|
2
|
+
|
|
3
|
+
Automated trading is a module your organization installs to invest **for its own account**. Your organization does not describe the orders to place: it sets a frame — a **strategy** — and the engine decides on its own when to buy and when to sell. The decision itself stays closed: no signal, no rationale and no memory of the engine is exposed.
|
|
4
|
+
|
|
5
|
+
The bot assigned to the module holds the funds: its EUR account pays for the purchases, and its custody wallets receive the assets whenever a strategy delivers them rather than holding them on the exchange. `client.autotrading` creates strategies, reads and reconfigures them, pauses, resumes or closes them, and reads the curve, the journal and the portfolio.
|
|
6
|
+
|
|
7
|
+
A strategy runs in one of two regimes, decided by `risk`: the four real-time profiles (`PRUDENT`, `BALANCED`, `DYNAMIC`, `OFFENSIVE`) buy and sell over and over and leave the assets on the exchange of the platform; `DCA` buys `installment` at every due date, holds, and has its assets delivered to the custody wallets of the bot. Whatever the regime, `capital` is a ceiling the strategy never goes past.
|
|
8
|
+
|
|
9
|
+
**The module must be ready before a strategy can be created**: installed in your organization, enabled, and served by a bot ([Bots](bot.md)) whose EUR account is funded ([Bank accounts](bank.md)). `create` and `update` answer `412` while it is not; the reads and `pause` / `resume` / `close` answer whatever the state of the module, so a strategy can always be read and closed.
|
|
10
|
+
|
|
11
|
+
**Every EUR amount of this resource is a string**, in and out — `capital`, `installment`, the guard rail values, `value`, `engaged`, `unrealised`, `realisedGains`, and the `amount` and `cash` of a journal line. The numbers are the points of a curve, `performance`, `benchmark`, the `percent` of a distribution, `trades`, `losingStreak` and `strategies`. On a write you may pass a number: the SDK converts it to a decimal string ([Amounts](../concepts.md#amounts)).
|
|
12
|
+
|
|
13
|
+
Examples use `client`, a configured `BitgenClient` ([Configuration](../configuration.md)). Wherever the API expects your organization, the SDK sends the `scope` of the client.
|
|
14
|
+
|
|
15
|
+
## Methods
|
|
16
|
+
|
|
17
|
+
| Method | What it does | Returns |
|
|
18
|
+
|---|---|---|
|
|
19
|
+
| `create(params)` | Creates a strategy | `Created` |
|
|
20
|
+
| `list(params?)` | Lists the strategies of your organization | `Page<Strategy>` |
|
|
21
|
+
| `get(strategy)` | Reads one strategy | `Strategy` |
|
|
22
|
+
| `update(strategy, params)` | Replaces the frame of a strategy | `void` |
|
|
23
|
+
| `pause(strategy)` | Stops the passes | `void` |
|
|
24
|
+
| `resume(strategy)` | Starts them again | `void` |
|
|
25
|
+
| `close(strategy)` | Closes the strategy | `void` |
|
|
26
|
+
| `history(strategy)` | Curve, performance, benchmark, figures | `StrategyHistory` |
|
|
27
|
+
| `journal(strategy, params?)` | What the strategy did | `Page<StrategyJournalEntry>` |
|
|
28
|
+
| `portfolio()` | Every live strategy, aggregated | `AutotradingPortfolio` |
|
|
29
|
+
|
|
30
|
+
TypeScript types of this resource, exported by the package: `Strategy`, `StrategyGuards`, `StrategyInput`, `StrategyListParams`, `StrategyHistory`, `StrategyRatios`, `StrategyJournalEntry`, `AutotradingPortfolio`, `StrategyRef` — the constants `StrategyState`, `StrategyRisk`, `StrategyFrequency`, `StrategyAllocation`, `StrategyClosing`, `StrategyNotifyMode`, `StrategyJournalEvent`, `StrategyExitReason`, `StrategyIdleReason` (each also a type) — plus the shared `Created`, `Page`, `PageParams`, `History`, `UserSummary` and `OrganizationSummary`.
|
|
31
|
+
|
|
32
|
+
## Create
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
client.autotrading.create(params: StrategyInput): Promise<Created>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Creates a `RUNNING` strategy, due for its first pass straight away. `name`, `capital`, `risk` and `frequency` are required — and `installment` for a `DCA`. Everything else has a default.
|
|
39
|
+
|
|
40
|
+
The booleans command and the values follow: `stopLossPercent` is read when `stopLoss` is `true`. A switch left off keeps the value you sent next to it when it is well formed, and the platform default otherwise.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { StrategyClosing, StrategyFrequency, StrategyNotifyMode, StrategyRisk } from '@bitgen/sdk'
|
|
44
|
+
|
|
45
|
+
const created = await client.autotrading.create({
|
|
46
|
+
name: 'Large cap momentum',
|
|
47
|
+
capital: '250.00',
|
|
48
|
+
risk: StrategyRisk.DYNAMIC,
|
|
49
|
+
frequency: StrategyFrequency.AUTO,
|
|
50
|
+
assets: ['BTC', 'ETH'],
|
|
51
|
+
reinvest: true,
|
|
52
|
+
closing: StrategyClosing.SELL_ALL,
|
|
53
|
+
notifyMode: StrategyNotifyMode.THRESHOLDS,
|
|
54
|
+
stopLoss: true,
|
|
55
|
+
stopLossPercent: '10',
|
|
56
|
+
takeProfit: true,
|
|
57
|
+
takeProfitPercent: '25',
|
|
58
|
+
trailingStop: false,
|
|
59
|
+
orderLimits: true,
|
|
60
|
+
maxOrderPercent: '25',
|
|
61
|
+
minOrderAmount: '10',
|
|
62
|
+
autoPause: true,
|
|
63
|
+
autoPauseLoss: '5',
|
|
64
|
+
autoPauseStreak: '3',
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
console.log(created.uuid) // '7b3d9c1e-2f4a-4b8c-9d1e-3f5a7b9c1d2e'
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`assets` is the universe the engine picks in, fifteen at most, each asset by uuid or ISO code. It must be an **array**: a bare string is refused with a `TypeError` before any request — iterated, it would reach the API as one asset per letter. **Absent or empty, the universe is the whole catalogue** and the engine chooses inside it. A `DCA` takes a dated pace, so `frequency: 'AUTO'` is refused for it, and `endDate` is read for a `DCA` only — the other profiles run until someone closes them.
|
|
71
|
+
|
|
72
|
+
The guard rails are applied by the platform without ever asking the engine: `stopLoss`, `takeProfit` and `trailingStop` sell a position, `autoPause` pauses the strategy on a bad day or a losing run, and `orderLimits` bounds the **purchases** — a sale is bounded by the sale minimum of the platform, not by these two values.
|
|
73
|
+
|
|
74
|
+
Your organization is capped on the number of strategies that are not `CLOSED`: `412 strategies_limit_reached` past it.
|
|
75
|
+
|
|
76
|
+
## List
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
client.autotrading.list(params?: StrategyListParams): Promise<Page<Strategy>>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The strategies of your organization, the most recent first. `includeClosed` also returns the `CLOSED` ones ([Query booleans](../concepts.md#query-booleans)), and `count` follows.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
const page = await client.autotrading.list({ limit: 10 })
|
|
86
|
+
|
|
87
|
+
console.log(page.count) // 1
|
|
88
|
+
console.log(page.items[0].name) // 'Large cap momentum'
|
|
89
|
+
console.log(page.items[0].engaged) // '150.00'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Get
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
client.autotrading.get(strategy: StrategyRef): Promise<Strategy>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
One strategy, by uuid or by any model that carries it — the `Created` of `create`, a `Strategy` of a list. The examples below use `'STRATEGY_UUID'` for that uuid.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { StrategyState } from '@bitgen/sdk'
|
|
102
|
+
|
|
103
|
+
const strategy = await client.autotrading.get('STRATEGY_UUID')
|
|
104
|
+
|
|
105
|
+
console.log(strategy.state) // 'RUNNING'
|
|
106
|
+
console.log(strategy.capital) // '250.00'
|
|
107
|
+
console.log(strategy.guards.trailingStopPercent) // '8'
|
|
108
|
+
console.log(strategy.bot.login) // 'bot_xTsIUym8872s@…'
|
|
109
|
+
|
|
110
|
+
if (strategy.state === StrategyState.PAUSED) {
|
|
111
|
+
console.log(strategy.idleReason) // null when a person paused it
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`assets` is `[]` when the engine picks in the whole catalogue, and `allocations` an empty list while the bot spreads the capital itself. `idleReason` says why a strategy is resting — its organization is disabled, the module is unavailable, or its bot cannot act — and is `null` when it runs normally. `updatedAt` follows the frame: a change of `state` leaves it as it is, so watch the `aitrading.setup` event rather than that field ([Webhooks](webhooks.md)).
|
|
116
|
+
|
|
117
|
+
## Update
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
client.autotrading.update(strategy: StrategyRef, params: StrategyInput): Promise<void>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Replaces the frame**, field for field, exactly as `create` reads it: a call without `assets` puts the strategy back on the whole catalogue, one without `stopLoss` switches the stop loss off. Send the whole frame every time — `installment` and `endDate` are the two fields that only a `DCA` reads.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { StrategyClosing, StrategyFrequency, StrategyNotifyMode, StrategyRisk } from '@bitgen/sdk'
|
|
127
|
+
|
|
128
|
+
await client.autotrading.update('STRATEGY_UUID', {
|
|
129
|
+
name: 'Large cap momentum',
|
|
130
|
+
capital: '400.00',
|
|
131
|
+
risk: StrategyRisk.DYNAMIC,
|
|
132
|
+
frequency: StrategyFrequency.AUTO,
|
|
133
|
+
assets: ['BTC', 'ETH'],
|
|
134
|
+
reinvest: true,
|
|
135
|
+
closing: StrategyClosing.SELL_ALL,
|
|
136
|
+
notifyMode: StrategyNotifyMode.THRESHOLDS,
|
|
137
|
+
stopLoss: true,
|
|
138
|
+
stopLossPercent: '8',
|
|
139
|
+
takeProfit: true,
|
|
140
|
+
takeProfitPercent: '25',
|
|
141
|
+
trailingStop: false,
|
|
142
|
+
orderLimits: true,
|
|
143
|
+
maxOrderPercent: '25',
|
|
144
|
+
minOrderAmount: '10',
|
|
145
|
+
autoPause: true,
|
|
146
|
+
autoPauseLoss: '5',
|
|
147
|
+
autoPauseStreak: '3',
|
|
148
|
+
})
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Three refusals read the positions the strategy holds: a ceiling under what is already engaged (`412 capital_below_engaged`), an asset left out that still holds a position (`412 asset_holds_position`), and a switch between the real-time and the `DCA` regime while a position is open (`412 regime_change_forbidden`). A `CLOSING` or `CLOSED` strategy is not reconfigured. Changing `assets` clears `allocations`; changing `frequency` moves the next pass.
|
|
152
|
+
|
|
153
|
+
## Pause, resume and close
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
client.autotrading.pause(strategy: StrategyRef): Promise<void>
|
|
157
|
+
client.autotrading.resume(strategy: StrategyRef): Promise<void>
|
|
158
|
+
client.autotrading.close(strategy: StrategyRef): Promise<void>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
`pause` stops the passes and leaves the positions as they are. `resume` starts them again and clears what had paused the strategy — its losing run and its mark of the day — so the resume holds. `close` moves the strategy to `CLOSING`: it settles position by position, following `closing`, then reaches `CLOSED` on its own.
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
await client.autotrading.pause('STRATEGY_UUID')
|
|
165
|
+
await client.autotrading.resume('STRATEGY_UUID')
|
|
166
|
+
await client.autotrading.close('STRATEGY_UUID')
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Asking for the state the strategy already has does nothing. A `CLOSING` or `CLOSED` strategy answers `412 strategy_closed`, and `412 strategy_state_changed` when another transition won the race.
|
|
170
|
+
|
|
171
|
+
## History
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
client.autotrading.history(strategy: StrategyRef): Promise<StrategyHistory>
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The curve of the strategy, its performance, the same window on BTC to compare against, and the figures of the moment. The curve is refreshed at most every five minutes; the previous one is served when it cannot be rebuilt.
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
const history = await client.autotrading.history('STRATEGY_UUID')
|
|
181
|
+
|
|
182
|
+
console.log(history.value) // '162.81'
|
|
183
|
+
console.log(history.unrealised) // '12.81'
|
|
184
|
+
console.log(history.performance.d) // 1.16
|
|
185
|
+
console.log(history.benchmark.all) // null when the window has no base to compute on
|
|
186
|
+
console.log(history.distribution[0]) // { asset: 'ETH', amount: '101.58', percent: 62.39 }
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`performance` accounts for what went in and out, so a purchase does not read as a gain, and a window with no base to compute on answers `null`. `value` leaves out an asset with no price of its own, while `engaged` stays the full cost — it is `engaged` that compares to `capital`. `distribution` carries one line per asset of the universe, the largest first — an asset with nothing in it comes last at zero, and the shares add up to 100 as soon as a position is worth something, and are all zero when the strategy holds nothing.
|
|
190
|
+
|
|
191
|
+
## Journal
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
client.autotrading.journal(strategy: StrategyRef, params?: PageParams): Promise<Page<StrategyJournalEntry>>
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
What the strategy did, the most recent first: its orders, the guard rails that fired, its pauses, its closing, and what it could not do. Paginated **inside a window of ten thousand lines** — `count` stops there ([Pagination](../concepts.md#pagination)).
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import { StrategyExitReason, StrategyJournalEvent } from '@bitgen/sdk'
|
|
201
|
+
|
|
202
|
+
const journal = await client.autotrading.journal('STRATEGY_UUID', { limit: 50 })
|
|
203
|
+
|
|
204
|
+
for (const line of journal.items) {
|
|
205
|
+
if (line.event === StrategyJournalEvent.GUARD && line.reason === StrategyExitReason.TRAILING_STOP) {
|
|
206
|
+
console.log(line.asset, line.order) // 'BTC' 'c4e6a8b0-…'
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Every line about an asset carries the position held **after** the operation; the lines that only say what the strategy became — a pause, a rest, a closing — carry no quantity. A sale fired by a guard rail writes two lines for the same order: the `guard`, which carries the reason, and the `sell`, which carries the amounts — both carry the order. An order line is rewritten when the order settles, gaining its `quantity` and its `cash` instead of adding a second line. The orders themselves are read through [`client.trading`](trading.md).
|
|
212
|
+
|
|
213
|
+
`reason` depends on the event, so it stays a plain string: the exit reasons of an order are `StrategyExitReason`, a pause says `daily_loss` or `losing_streak`, a closing `end_date` or `capital_consumed`, a rest one of the `StrategyIdleReason` values, and a failure `order_refused`, `sale_not_executed`, `insufficient_funds` or `daily_buy_limit_exceeded`.
|
|
214
|
+
|
|
215
|
+
This is the one read of the resource that queries the search cluster live: when the cluster cannot answer, the call **throws** instead of answering an empty page — the curve and the portfolio serve their previous photo instead. A failure here means the journal could not be read, not that the strategy did nothing.
|
|
216
|
+
|
|
217
|
+
## Portfolio
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
client.autotrading.portfolio(): Promise<AutotradingPortfolio>
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Every strategy of your organization that is not `CLOSED`, as one position: the same curves, and the totals. With no live strategy, `strategies` is `0`, the totals are `'0.00'`, the curves are flat at zero and `performance` is `null` on every window. One call here costs less than one history per strategy, for you and for the platform.
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
const portfolio = await client.autotrading.portfolio()
|
|
227
|
+
|
|
228
|
+
console.log(portfolio.strategies) // 1
|
|
229
|
+
console.log(portfolio.value) // '162.81' — the sum of the values of the strategies
|
|
230
|
+
console.log(portfolio.realisedGains) // '0.00'
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Webhooks
|
|
234
|
+
|
|
235
|
+
Two events tell a third party what the bot did, rather than the plumbing underneath; they are subscribed to one by one, like every other event ([Webhooks](webhooks.md)). The trading events of the platform stay silent for the orders of a bot.
|
|
236
|
+
|
|
237
|
+
| Event | When |
|
|
238
|
+
|---|---|
|
|
239
|
+
| `aitrading.setup` | The frame or the state of a strategy changed: created, reconfigured, paused, resumed, closing, closed — by a person or by its own guard rails |
|
|
240
|
+
| `aitrading.action` | An order was placed, with the reason when a guard rail or a closing placed it |
|
|
241
|
+
|
|
242
|
+
An `aitrading.action` fires when the order **opens**, so its outcome is read through [`client.trading`](trading.md). Its `amount` carries two units: EUR for a `BUY`, asset units for a `SELL`.
|
|
243
|
+
|
|
244
|
+
## Errors
|
|
245
|
+
|
|
246
|
+
In addition to the [common errors](../errors.md#common-errors):
|
|
247
|
+
|
|
248
|
+
| Status | `code` | Meaning |
|
|
249
|
+
|---|---|---|
|
|
250
|
+
| `400` | `required_index_missing::<field>` | A required field is missing or empty |
|
|
251
|
+
| `404` | `unknown_strategy` | Unknown strategy, or a strategy of another organization |
|
|
252
|
+
| `412` | `autotrading_not_installed` | Create or update: the module is not installed in your organization |
|
|
253
|
+
| `412` | `autotrading_not_enabled` | Create or update: the module is installed and not active |
|
|
254
|
+
| `412` | `autotrading_bot_not_assigned` | Create: no bot serves the module |
|
|
255
|
+
| `412` | `organization_not_enabled` | Your organization is not enabled |
|
|
256
|
+
| `412` | `strategies_limit_reached` | Create: your organization reached its number of strategies |
|
|
257
|
+
| `412` | `custody_not_enabled` | Create: a `DCA` needs the custody of your organization enabled |
|
|
258
|
+
| `412` | `capital_below_engaged` | Update: the new ceiling is under the cost of the positions held |
|
|
259
|
+
| `412` | `asset_holds_position` | Update: an asset left out still holds a position |
|
|
260
|
+
| `412` | `regime_change_forbidden` | Update: `DCA` ↔ real time while a position is open |
|
|
261
|
+
| `412` | `strategy_closed` | The strategy is `CLOSING` or `CLOSED` |
|
|
262
|
+
| `412` | `strategy_state_changed` | Pause, resume or close: another transition won the race |
|
|
263
|
+
| `416` | `invalid_capital` | `capital` is not a valid amount above zero |
|
|
264
|
+
| `416` | `invalid_installment` | `installment` is not valid, or below the purchase minimum of the platform |
|
|
265
|
+
| `416` | `installment_above_capital` | `installment` is above `capital` |
|
|
266
|
+
| `416` | `too_many_assets` | More than fifteen assets |
|
|
267
|
+
| `416` | `order_limits_contradictory` | `minOrderAmount` is above the largest purchase allowed, `capital × maxOrderPercent / 100` |
|
|
268
|
+
| `416` | `invalid_stop_loss_percent` | Not a percentage above zero and up to 100 |
|
|
269
|
+
| `416` | `invalid_take_profit_percent` | The same |
|
|
270
|
+
| `416` | `invalid_trailing_stop_percent` | The same |
|
|
271
|
+
| `416` | `invalid_max_order_percent` | The same |
|
|
272
|
+
| `416` | `invalid_min_order_amount` | Not valid, below the purchase minimum of the platform, or above `capital` |
|
|
273
|
+
| `416` | `invalid_auto_pause_loss` | Not a percentage above zero and up to 100 |
|
|
274
|
+
| `416` | `invalid_auto_pause_streak` | Not a whole number from one |
|
|
275
|
+
| `422` | `invalid_name` | `name` is empty or longer than 120 characters |
|
|
276
|
+
| `422` | `invalid_asset` | An asset is unknown or unavailable, or `assets` is not a list |
|
|
277
|
+
| `422` | `invalid_risk` | `risk` is not one of the five profiles |
|
|
278
|
+
| `422` | `invalid_frequency` | `frequency` is unknown, or `AUTO` on a `DCA` |
|
|
279
|
+
| `422` | `invalid_end_date` | `endDate` is not a day in the future |
|
|
280
|
+
| `422` | `invalid_allocation` | `allocation` is not `AUTO` |
|
|
281
|
+
| `422` | `invalid_reinvest` | `reinvest` is not a boolean value |
|
|
282
|
+
| `422` | `invalid_closing` | `closing` is not `SELL_ALL` or `KEEP` |
|
|
283
|
+
| `422` | `invalid_notify_mode` | `notifyMode` is not one of the four modes |
|
|
284
|
+
| `422` | `invalid_include_closed` | `includeClosed` is not a boolean value |
|
|
285
|
+
| `422` | `invalid_action` | The transition is not `PAUSE`, `RESUME` or `CLOSE` |
|
|
286
|
+
| `422` | `invalid_stop_loss` | The switch is not a boolean value |
|
|
287
|
+
| `422` | `invalid_take_profit` | The same |
|
|
288
|
+
| `422` | `invalid_trailing_stop` | The same |
|
|
289
|
+
| `422` | `invalid_order_limits` | The same |
|
|
290
|
+
| `422` | `invalid_auto_pause` | The same |
|
|
291
|
+
|
|
292
|
+
An empty `strategy`, or a model without a uuid, throws a `TypeError` before any request is sent.
|
|
293
|
+
|
|
294
|
+
## Related
|
|
295
|
+
|
|
296
|
+
- [Bots](bot.md) — the bot that serves the module and holds the funds; a strategy that is not `CLOSED` keeps it from being detached or closed
|
|
297
|
+
- [Bank accounts](bank.md) — the EUR account of the bot, which funds the purchases and receives the sales
|
|
298
|
+
- [Trading](trading.md) — the orders a strategy places, and what they became
|
|
299
|
+
- [Custody wallets](custody.md) — where a `DCA`, and a `KEEP` closing, deliver the assets
|
|
300
|
+
- [Amounts](../concepts.md#amounts) — why this resource answers its EUR amounts as strings
|
|
301
|
+
- [Pagination](../concepts.md#pagination) — `offset`, `limit`, `{ count, items }`, and the window of the journal
|
|
302
|
+
- [Query booleans](../concepts.md#query-booleans) — `includeClosed`
|
|
303
|
+
- [Webhooks](webhooks.md) — `aitrading.setup`, `aitrading.action`
|
|
304
|
+
- [Errors](../errors.md#common-errors) — the error body and the common codes
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Bots
|
|
2
|
+
|
|
3
|
+
A bot is a technical customer your organization runs **for its own account**: an account like any other, flagged as a bot, whose financial moves are those of a customer but whose funds belong to the organization. It is left out of the assets under management and of the customer count, it does not appear among your customers or in the journal of transactions, and it cannot sign in to the BITGEN web application — its identity is validated at creation.
|
|
4
|
+
|
|
5
|
+
A bot serves one or more **modules**, the extensions installed in your organization; a module is served by one bot at most. `client.bot` creates bots, lists them, reads one, attaches it to a module and closes it.
|
|
6
|
+
|
|
7
|
+
`list()` and `get()` answer the identity of a bot and its modules, and **nothing more** — this is a dedicated entry point, not a view of an account. The details of the account behind a bot (contact details, identity file, settings) are read through [`client.customer`](customer.md), where the `bot` field tells a bot from a customer.
|
|
8
|
+
|
|
9
|
+
Examples use `client`, a configured `BitgenClient` ([Configuration](../configuration.md)). Wherever the API expects your organization, the SDK sends the `scope` of the client.
|
|
10
|
+
|
|
11
|
+
## Methods
|
|
12
|
+
|
|
13
|
+
| Method | What it does | Returns |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `create()` | Creates a bot in your organization | `Created` |
|
|
16
|
+
| `list(params?)` | Lists the bots of your organization | `Page<Bot>` |
|
|
17
|
+
| `get(bot)` | Reads one bot | `Bot` |
|
|
18
|
+
| `assign(bot, module)` | Attaches the bot to a module | `void` |
|
|
19
|
+
| `detach(bot, module)` | Detaches the bot from a module | `void` |
|
|
20
|
+
| `modules(bot)` | Lists the modules a bot serves | `Page<BotApplication>` |
|
|
21
|
+
| `close(bot)` | Closes the bot | `void` |
|
|
22
|
+
|
|
23
|
+
TypeScript types of this resource, exported by the package: `Bot`, `BotApplication`, `BotExtension`, `BotListParams`, `BotRef`, `BotModuleRef` — the constants `BotState`, `BotApplicationState`, `ExtensionState`, `ExtensionCategory` (each also a type) — plus the shared `Created` and `Page`.
|
|
24
|
+
|
|
25
|
+
## Create
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
client.bot.create(): Promise<Created>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Takes nothing: the platform writes everything — the login, the name, a validated identity, and an account that cannot sign in. The bot is supervised by the owner of your organization, who receives the emails addressed to it.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const bot = await client.bot.create()
|
|
35
|
+
|
|
36
|
+
console.log(bot.uuid) // '9f8e7d6c-5b4a-4938-8271-605f4e3d2c1b'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Returns a `Created` — its `uuid` is the bot, and is accepted by every other method of this resource.
|
|
40
|
+
|
|
41
|
+
**Two calls create two bots**: the route carries no idempotency key, so a replay after a timeout creates a duplicate, which you then have to close.
|
|
42
|
+
|
|
43
|
+
A bot takes one seat in the customer quota of your plan and one in its bot quota, counted over the scope of your **subscription** — your organization, or the hub it belongs to when the subscription is taken there — while `list()` only ever answers the bots of your own organization: a refusal is therefore possible even when the list looks short. An organization without an active subscription answers `426 subscription_required`, a quota that is reached `426 quota_exceeded` or `426 bots_quota_exceeded`.
|
|
44
|
+
|
|
45
|
+
## List
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
client.bot.list(params?: BotListParams): Promise<Page<Bot>>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
| Parameter | Type | Description |
|
|
52
|
+
|---|---|---|
|
|
53
|
+
| `params.offset`, `params.limit` | `number` | [Pagination](../concepts.md#pagination) |
|
|
54
|
+
| `params.includeClosed` | `boolean` | Also returns the closed bots — default `false` ([Query booleans](../concepts.md#query-booleans)) |
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { BotState } from '@bitgen/sdk'
|
|
58
|
+
|
|
59
|
+
const { count, items } = await client.bot.list({ includeClosed: true })
|
|
60
|
+
|
|
61
|
+
for (const bot of items) {
|
|
62
|
+
console.log(bot.login, bot.state === BotState.ENABLED, bot.applications.length) // 'bot_xTsIUym8872s@…' true 1
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Returns a page of `Bot` ([get](#get)).
|
|
67
|
+
|
|
68
|
+
## Get
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
client.bot.get(bot: BotRef): Promise<Bot>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`bot` is the uuid of the bot, the `Created` of `create()`, or a `Bot` of `list()`. A closed bot stays readable, so that its state can be seen.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { BotApplicationState } from '@bitgen/sdk'
|
|
78
|
+
|
|
79
|
+
const bot = await client.bot.get('BOT_UUID')
|
|
80
|
+
|
|
81
|
+
for (const module of bot.applications) {
|
|
82
|
+
console.log(module.extension.name, module.state === BotApplicationState.ENABLED) // 'auto-trading' true
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Returns a `Bot`:
|
|
87
|
+
|
|
88
|
+
| Field | Description |
|
|
89
|
+
|---|---|
|
|
90
|
+
| `uuid` | The bot |
|
|
91
|
+
| `createdAt` | Epoch seconds |
|
|
92
|
+
| `state` | `BotState.ENABLED`, `BotState.FROZEN` or `BotState.CLOSED` |
|
|
93
|
+
| `isAvailable` | `false` once the bot is closed |
|
|
94
|
+
| `login` | `bot_<id>@<uuid of your organization>` — rewritten when the bot is closed |
|
|
95
|
+
| `applications` | The modules this bot serves, empty when it serves none — each a `BotApplication` ([modules](#modules)) |
|
|
96
|
+
|
|
97
|
+
An unknown uuid, an account that is not a bot, or a bot of another organization all answer `404 unknown_bot`.
|
|
98
|
+
|
|
99
|
+
## Assign
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
client.bot.assign(bot: BotRef, module: BotModuleRef): Promise<void>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`module` is the uuid of an installed application of your organization, or a `BotApplication` of `modules()`.
|
|
106
|
+
|
|
107
|
+
A module is served by one bot at most: when another bot already serves it, the call answers `412 application_already_assigned` — detach that one first. Attaching the **same** bot again succeeds and changes nothing.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
await client.bot.assign('BOT_UUID', 'MODULE_UUID')
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Resolves with nothing. A failure rejects with a `BitgenError`.
|
|
114
|
+
|
|
115
|
+
## Detach
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
client.bot.detach(bot: BotRef, module: BotModuleRef): Promise<void>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Detaching a module this bot does not serve succeeds and changes nothing. A bot must be detached from every module before it can be closed.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
await client.bot.detach('BOT_UUID', 'MODULE_UUID')
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Resolves with nothing. A failure rejects with a `BitgenError`.
|
|
128
|
+
|
|
129
|
+
## Modules
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
client.bot.modules(bot: BotRef): Promise<Page<BotApplication>>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The modules this bot serves. Not paginated: `count` is the length of the list.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { ExtensionCategory } from '@bitgen/sdk'
|
|
139
|
+
|
|
140
|
+
const { count, items } = await client.bot.modules('BOT_UUID')
|
|
141
|
+
|
|
142
|
+
for (const module of items) {
|
|
143
|
+
console.log(module.extension.label.en, module.extension.category === ExtensionCategory.AI) // 'AI trading bot' true
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Returns a page of `BotApplication`:
|
|
148
|
+
|
|
149
|
+
| Field | Description |
|
|
150
|
+
|---|---|
|
|
151
|
+
| `uuid` | The module — accepted by `assign()` and `detach()` |
|
|
152
|
+
| `createdAt` | Epoch seconds: when the module was installed |
|
|
153
|
+
| `state` | `BotApplicationState.ENABLED`, `PENDING` or `SUSPENDED` |
|
|
154
|
+
| `extension` | The extension the module runs — a `BotExtension`: `uuid`, `state` (`ExtensionState.ENABLED` or `DISABLED`), `name` (technical, e.g. `auto-trading`), `category` (`ExtensionCategory.AI`, `CGP` or `MISC`), `label` by language (`{ fr, en }`) |
|
|
155
|
+
|
|
156
|
+
## Close
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
client.bot.close(bot: BotRef): Promise<void>
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Closes the bot: its state becomes `BotState.CLOSED` and its login is rewritten. A closed bot stays readable, one by one or with `includeClosed`.
|
|
163
|
+
|
|
164
|
+
The bot must serve no module and hold no open position — see [Errors](#errors).
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
await client.bot.close('BOT_UUID')
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Resolves with nothing. A failure rejects with a `BitgenError`.
|
|
171
|
+
|
|
172
|
+
## Errors
|
|
173
|
+
|
|
174
|
+
In addition to the [common errors](../errors.md#common-errors):
|
|
175
|
+
|
|
176
|
+
| Status | `code` | Meaning |
|
|
177
|
+
|---|---|---|
|
|
178
|
+
| `404` | `unknown_bot` | Unknown bot, an account that is not a bot, or a bot of another organization |
|
|
179
|
+
| `404` | `unknown_application` | Unknown module, or a module of another organization |
|
|
180
|
+
| `412` | `organization_without_owner` | Creating a bot while your organization has no owner |
|
|
181
|
+
| `412` | `application_already_assigned` | The module is already served by another bot |
|
|
182
|
+
| `412` | `unable_to_delete_assigned_bot` | The bot still serves a module: detach it first |
|
|
183
|
+
| `412` | `unable_to_delete_has_balance` | Its EUR account is not empty |
|
|
184
|
+
| `412` | `unable_to_delete_has_open_transaction` | A transaction of the bot is still open |
|
|
185
|
+
| `412` | `unable_to_delete_has_pending_movement` | A bank movement is in progress |
|
|
186
|
+
| `412` | `unable_to_delete_has_crypto` | One of its custody wallets is not empty |
|
|
187
|
+
| `412` | `unable_to_delete_has_custody_movement` | A custody movement is in progress |
|
|
188
|
+
| `412` | `unable_to_delete_has_staking` | A staking position is open |
|
|
189
|
+
| `412` | `unable_to_delete_has_trading_order` | An order is in progress |
|
|
190
|
+
| `412` | `unable_to_delete_has_strategy` | A strategy of the automated trading module is not `CLOSED` yet ([Automated trading](autotrading.md)) |
|
|
191
|
+
| `412` | `bot_has_strategies` | `detach`: a strategy of that module is not `CLOSED` yet |
|
|
192
|
+
| `422` | `invalid_include_closed` | `includeClosed` is not a boolean value |
|
|
193
|
+
| `426` | `subscription_required` | Your organization has no active subscription |
|
|
194
|
+
| `426` | `quota_feature_missing` | The plan of your organization carries no customer quota, or no bot quota |
|
|
195
|
+
| `426` | `quota_exceeded` | The customer quota of your plan is reached |
|
|
196
|
+
| `426` | `bots_quota_exceeded` | The bot quota of your plan is reached |
|
|
197
|
+
|
|
198
|
+
## Related
|
|
199
|
+
|
|
200
|
+
- [Customers](customer.md) — the end users of your organization, and the details of the account behind a bot
|
|
201
|
+
- [Bank accounts](bank.md) — the EUR account of a bot, like that of a customer
|
|
202
|
+
- [Custody wallets](custody.md) — the crypto wallets of a bot
|
|
203
|
+
- [Trading](trading.md) — purchases and sales a bot makes for your own account
|
|
204
|
+
- [Automated trading](autotrading.md) — the strategies a bot runs; close them before detaching or closing it
|
|
205
|
+
- [Pagination](../concepts.md#pagination) — `offset`, `limit`, `{ count, items }`
|
|
206
|
+
- [Query booleans](../concepts.md#query-booleans) — `includeClosed`
|
|
207
|
+
- [Errors](../errors.md#common-errors) — the error body and the common codes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Customers
|
|
2
2
|
|
|
3
|
-
A customer is an end user of your organization on the BITGEN platform: a person (KYC) or a business (KYB) with a login, an identity file, settings and — once activated and verified — an EUR account, custody wallets, orders and staking positions. `client.customer` creates customers, lists them, and reads or updates an account.
|
|
3
|
+
A customer is an end user of your organization on the BITGEN platform: a person (KYC) or a business (KYB) with a login, an identity file, settings and — once activated and verified — an EUR account, custody wallets, orders and staking positions. `client.customer` creates customers, lists them, and reads or updates an account. It is about the end users of your organization: the **bots** it runs for its own account are left out of `list()`, and are listed by [`client.bot`](bot.md).
|
|
4
4
|
|
|
5
5
|
Examples use `client`, a configured `BitgenClient` ([Configuration](../configuration.md)). A customer is designated by a `UserRef`: their uuid, or a model carrying it — the `Created` returned by `create`, a `Customer` of `list`, an `Account` of `get` ([User references](../concepts.md#user-references)).
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Transactions
|
|
2
2
|
|
|
3
|
-
The transaction journal is the unified, read-only record of the fiat and crypto movements of your organization: bank deposits and withdrawals, custody deposits and withdrawals, and the internal legs of sales and staking. Transactions are created by the platform — never through the API — and referenced by the other resources: `bank.withdraw` and `custody.withdraw` return the uuid of the transaction they create. `client.transaction` lists and reads them.
|
|
3
|
+
The transaction journal is the unified, read-only record of the fiat and crypto movements of your organization: bank deposits and withdrawals, custody deposits and withdrawals, and the internal legs of sales and staking. Transactions are created by the platform — never through the API — and referenced by the other resources: `bank.withdraw` and `custody.withdraw` return the uuid of the transaction they create. `client.transaction` lists and reads them. The journal is about what your customers move: the transactions of your **bots** are left out of it ([Bots](bot.md)).
|
|
4
4
|
|
|
5
5
|
Examples use `client`, a configured `BitgenClient` ([Configuration](../configuration.md)).
|
|
6
6
|
|
|
@@ -129,6 +129,7 @@ Returns a `Created` — the uuid of the subscription, for `archive`, `reactivate
|
|
|
129
129
|
| Trading | `trading.buy`, `trading.sell` |
|
|
130
130
|
| Staking | `staking.requested`, `staking.status`, `staking.rewards`, `staking.claimed` |
|
|
131
131
|
| Compliance | `alert.opened`, `alert.status` |
|
|
132
|
+
| Automated trading | `aitrading.setup`, `aitrading.action` |
|
|
132
133
|
|
|
133
134
|
The `WebhookEventName` constant carries these names — `WebhookEventName.CUSTODY_SENT` for `custody.sent`, `USER_IDENTITY_STEP_VALIDATED` for `user.identity.step.validated`… — and as a type it also accepts any other string, since the catalogue may grow ([Constants](../concepts.md#constants)).
|
|
134
135
|
|
|
@@ -307,3 +308,4 @@ In addition to the [common errors](../errors.md#common-errors):
|
|
|
307
308
|
- [Custody wallets](custody.md) — `custody.wallet.created`, `custody.received`, `custody.sent`, `custody.transaction`
|
|
308
309
|
- [Trading](trading.md) — `trading.buy`, `trading.sell`
|
|
309
310
|
- [Staking](staking.md) — `staking.requested`, `staking.status`, `staking.rewards`, `staking.claimed`
|
|
311
|
+
- [Automated trading](autotrading.md) — `aitrading.setup`, `aitrading.action`
|
package/src/client.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { HttpClient } from './http.js'
|
|
2
2
|
import { ApikeysResource } from './resources/apikeys.js'
|
|
3
3
|
import { AssetResource } from './resources/asset.js'
|
|
4
|
+
import { AutotradingResource } from './resources/autotrading.js'
|
|
4
5
|
import { BankResource } from './resources/bank.js'
|
|
6
|
+
import { BotResource } from './resources/bot.js'
|
|
5
7
|
import { CoreResource } from './resources/core.js'
|
|
6
8
|
import { CustodyResource } from './resources/custody.js'
|
|
7
9
|
import { CustomerResource } from './resources/customer.js'
|
|
@@ -28,6 +30,8 @@ export class BitgenClient {
|
|
|
28
30
|
readonly staking: StakingResource
|
|
29
31
|
readonly webhooks: WebhooksResource
|
|
30
32
|
readonly apikeys: ApikeysResource
|
|
33
|
+
readonly bot: BotResource
|
|
34
|
+
readonly autotrading: AutotradingResource
|
|
31
35
|
|
|
32
36
|
constructor(config: BitgenConfig) {
|
|
33
37
|
this.http = new HttpClient(config)
|
|
@@ -42,5 +46,7 @@ export class BitgenClient {
|
|
|
42
46
|
this.staking = new StakingResource(this.http, this.core)
|
|
43
47
|
this.webhooks = new WebhooksResource(this.http)
|
|
44
48
|
this.apikeys = new ApikeysResource(this.http)
|
|
49
|
+
this.bot = new BotResource(this.http)
|
|
50
|
+
this.autotrading = new AutotradingResource(this.http)
|
|
45
51
|
}
|
|
46
52
|
}
|