or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

account.mdconvert-api.mddepth-cache.mdfutures.mdindex.mdmargin.mdmarket-data.mdrest-clients.mdstaking-mining.mdtrading.mdwebsockets.md

trading.mddocs/

0

# Trading Operations

1

2

Complete trading functionality for spot markets including order placement, management, and execution. Supports all order types with comprehensive status tracking and modification capabilities.

3

4

## Capabilities

5

6

### Order Creation

7

8

Create various types of orders with comprehensive parameter control.

9

10

```python { .api }

11

def create_order(self, **params): ...

12

def create_test_order(self, **params): ...

13

def order_limit(self, timeInForce=BaseClient.TIME_IN_FORCE_GTC, **params): ...

14

def order_limit_buy(self, timeInForce=BaseClient.TIME_IN_FORCE_GTC, **params): ...

15

def order_limit_sell(self, timeInForce=BaseClient.TIME_IN_FORCE_GTC, **params): ...

16

def order_market(self, **params): ...

17

def order_market_buy(self, **params): ...

18

def order_market_sell(self, **params): ...

19

```

20

21

#### Basic Order Examples

22

23

```python

24

from binance import Client, SIDE_BUY, SIDE_SELL, ORDER_TYPE_LIMIT, ORDER_TYPE_MARKET, TIME_IN_FORCE_GTC

25

26

client = Client(api_key='your_key', api_secret='your_secret')

27

28

# Market buy order

29

market_buy = client.order_market_buy(

30

symbol='BTCUSDT',

31

quantity=0.001

32

)

33

34

# Market sell order

35

market_sell = client.order_market_sell(

36

symbol='BTCUSDT',

37

quantity=0.001

38

)

39

40

# Limit buy order

41

limit_buy = client.order_limit_buy(

42

symbol='BTCUSDT',

43

quantity=0.001,

44

price='50000.00'

45

)

46

47

# Limit sell order

48

limit_sell = client.order_limit_sell(

49

symbol='BTCUSDT',

50

quantity=0.001,

51

price='52000.00'

52

)

53

54

# Advanced limit order with custom parameters

55

advanced_order = client.create_order(

56

symbol='BTCUSDT',

57

side=SIDE_BUY,

58

type=ORDER_TYPE_LIMIT,

59

timeInForce=TIME_IN_FORCE_GTC,

60

quantity=0.001,

61

price='49000.00',

62

newClientOrderId='my-custom-id-123'

63

)

64

```

65

66

#### Test Orders

67

68

```python

69

# Test order creation without execution

70

test_order = client.create_test_order(

71

symbol='BTCUSDT',

72

side=SIDE_BUY,

73

type=ORDER_TYPE_MARKET,

74

quantity=0.001

75

)

76

# Returns {} if order would be valid

77

```

78

79

### OCO Orders

80

81

One-Cancels-Other orders for advanced trading strategies.

82

83

```python { .api }

84

def create_oco_order(self, **params): ...

85

def order_oco_buy(self, **params): ...

86

def order_oco_sell(self, **params): ...

87

def get_open_oco_orders(self, **params): ...

88

```

89

90

#### OCO Order Examples

91

92

```python

93

# OCO sell order (take profit + stop loss)

94

oco_sell = client.order_oco_sell(

95

symbol='BTCUSDT',

96

quantity=0.001,

97

price='55000.00', # Take profit price

98

stopPrice='48000.00', # Stop loss trigger

99

stopLimitPrice='47500.00' # Stop loss limit price

100

)

101

102

# OCO buy order

103

oco_buy = client.order_oco_buy(

104

symbol='BTCUSDT',

105

quantity=0.001,

106

price='48000.00', # Limit buy price

107

stopPrice='52000.00', # Stop buy trigger

108

stopLimitPrice='52500.00' # Stop buy limit price

109

)

110

111

# Advanced OCO order

112

advanced_oco = client.create_oco_order(

113

symbol='BTCUSDT',

114

side=SIDE_SELL,

115

quantity=0.001,

116

price='55000.00',

117

stopPrice='48000.00',

118

stopLimitPrice='47500.00',

119

stopLimitTimeInForce=TIME_IN_FORCE_GTC,

120

newOrderRespType='FULL'

121

)

122

```

123

124

### Order Management

125

126

Query, modify, and cancel existing orders.

127

128

```python { .api }

129

def get_order(self, **params): ...

130

def get_all_orders(self, **params): ...

131

def get_open_orders(self, **params): ...

132

def cancel_order(self, **params): ...

133

def cancel_all_open_orders(self, **params): ...

134

def cancel_replace_order(self, **params): ...

135

```

136

137

#### Order Management Examples

138

139

```python

140

# Get specific order by order ID

141

order = client.get_order(

142

symbol='BTCUSDT',

143

orderId=12345678

144

)

145

146

# Get order by client order ID

147

order = client.get_order(

148

symbol='BTCUSDT',

149

origClientOrderId='my-custom-id-123'

150

)

151

152

# Get all orders for symbol (requires account permissions)

153

all_orders = client.get_all_orders(

154

symbol='BTCUSDT',

155

limit=100 # Max 1000

156

)

157

158

# Get only open orders

159

open_orders = client.get_open_orders(symbol='BTCUSDT')

160

161

# Get all open orders across all symbols

162

all_open = client.get_open_orders()

163

164

# Cancel specific order

165

cancel_result = client.cancel_order(

166

symbol='BTCUSDT',

167

orderId=12345678

168

)

169

170

# Cancel all open orders for symbol

171

cancel_all = client.cancel_all_open_orders(symbol='BTCUSDT')

172

173

# Cancel and replace order (atomic operation)

174

replace_result = client.cancel_replace_order(

175

symbol='BTCUSDT',

176

side=SIDE_BUY,

177

type=ORDER_TYPE_LIMIT,

178

cancelReplaceMode='STOP_ON_FAILURE',

179

timeInForce=TIME_IN_FORCE_GTC,

180

quantity=0.002, # New quantity

181

price='49500.00', # New price

182

cancelOrderId=12345678 # Order to cancel

183

)

184

```

185

186

### Order Types and Parameters

187

188

#### Order Types

189

190

```python

191

# Available order types

192

ORDER_TYPE_LIMIT = "LIMIT"

193

ORDER_TYPE_MARKET = "MARKET"

194

ORDER_TYPE_STOP_LOSS = "STOP_LOSS"

195

ORDER_TYPE_STOP_LOSS_LIMIT = "STOP_LOSS_LIMIT"

196

ORDER_TYPE_TAKE_PROFIT = "TAKE_PROFIT"

197

ORDER_TYPE_TAKE_PROFIT_LIMIT = "TAKE_PROFIT_LIMIT"

198

ORDER_TYPE_LIMIT_MAKER = "LIMIT_MAKER"

199

```

200

201

#### Stop Loss Orders

202

203

```python

204

# Stop loss market order

205

stop_loss = client.create_order(

206

symbol='BTCUSDT',

207

side=SIDE_SELL,

208

type=ORDER_TYPE_STOP_LOSS,

209

quantity=0.001,

210

stopPrice='48000.00'

211

)

212

213

# Stop loss limit order

214

stop_loss_limit = client.create_order(

215

symbol='BTCUSDT',

216

side=SIDE_SELL,

217

type=ORDER_TYPE_STOP_LOSS_LIMIT,

218

timeInForce=TIME_IN_FORCE_GTC,

219

quantity=0.001,

220

price='47500.00', # Limit price

221

stopPrice='48000.00' # Trigger price

222

)

223

```

224

225

#### Take Profit Orders

226

227

```python

228

# Take profit market order

229

take_profit = client.create_order(

230

symbol='BTCUSDT',

231

side=SIDE_SELL,

232

type=ORDER_TYPE_TAKE_PROFIT,

233

quantity=0.001,

234

stopPrice='55000.00'

235

)

236

237

# Take profit limit order

238

take_profit_limit = client.create_order(

239

symbol='BTCUSDT',

240

side=SIDE_SELL,

241

type=ORDER_TYPE_TAKE_PROFIT_LIMIT,

242

timeInForce=TIME_IN_FORCE_GTC,

243

quantity=0.001,

244

price='55500.00', # Limit price

245

stopPrice='55000.00' # Trigger price

246

)

247

```

248

249

#### Post-Only Orders

250

251

```python

252

# Limit maker order (guaranteed to be maker)

253

maker_order = client.create_order(

254

symbol='BTCUSDT',

255

side=SIDE_BUY,

256

type=ORDER_TYPE_LIMIT_MAKER,

257

quantity=0.001,

258

price='49000.00'

259

)

260

```

261

262

### Time in Force Options

263

264

```python

265

# Available time in force options

266

TIME_IN_FORCE_GTC = "GTC" # Good Till Cancelled

267

TIME_IN_FORCE_IOC = "IOC" # Immediate Or Cancel

268

TIME_IN_FORCE_FOK = "FOK" # Fill Or Kill

269

TIME_IN_FORCE_GTX = "GTX" # Good Till Crossing (Post Only)

270

TIME_IN_FORCE_GTD = "GTD" # Good Till Date

271

272

# Examples

273

ioc_order = client.order_limit_buy(

274

symbol='BTCUSDT',

275

quantity=0.001,

276

price='50000.00',

277

timeInForce=TIME_IN_FORCE_IOC

278

)

279

280

fok_order = client.order_limit_buy(

281

symbol='BTCUSDT',

282

quantity=0.001,

283

price='50000.00',

284

timeInForce=TIME_IN_FORCE_FOK

285

)

286

```

287

288

### Order Response Types

289

290

Control the amount of information returned from order operations.

291

292

```python

293

# Response type options

294

ORDER_RESP_TYPE_ACK = "ACK" # Minimal response

295

ORDER_RESP_TYPE_RESULT = "RESULT" # Standard response

296

ORDER_RESP_TYPE_FULL = "FULL" # Full response with fills

297

298

# Example with full response

299

full_response_order = client.create_order(

300

symbol='BTCUSDT',

301

side=SIDE_BUY,

302

type=ORDER_TYPE_MARKET,

303

quantity=0.001,

304

newOrderRespType=ORDER_RESP_TYPE_FULL

305

)

306

```

307

308

### Advanced Trading Features

309

310

#### Self-Trade Prevention

311

312

```python

313

# Prevent self-trading

314

order = client.create_order(

315

symbol='BTCUSDT',

316

side=SIDE_BUY,

317

type=ORDER_TYPE_LIMIT,

318

quantity=0.001,

319

price='50000.00',

320

selfTradePreventionMode='EXPIRE_TAKER'

321

)

322

```

323

324

#### Iceberg Orders

325

326

```python

327

# Iceberg order (only show partial quantity)

328

iceberg_order = client.create_order(

329

symbol='BTCUSDT',

330

side=SIDE_BUY,

331

type=ORDER_TYPE_LIMIT,

332

timeInForce=TIME_IN_FORCE_GTC,

333

quantity=1.0, # Total quantity

334

price='50000.00',

335

icebergQty=0.1 # Visible quantity

336

)

337

```

338

339

### Order Status and Fills

340

341

#### Order Status Values

342

343

```python

344

ORDER_STATUS_NEW = "NEW"

345

ORDER_STATUS_PARTIALLY_FILLED = "PARTIALLY_FILLED"

346

ORDER_STATUS_FILLED = "FILLED"

347

ORDER_STATUS_CANCELED = "CANCELED"

348

ORDER_STATUS_PENDING_CANCEL = "PENDING_CANCEL"

349

ORDER_STATUS_REJECTED = "REJECTED"

350

ORDER_STATUS_EXPIRED = "EXPIRED"

351

```

352

353

#### Checking Order Status

354

355

```python

356

order = client.get_order(symbol='BTCUSDT', orderId=12345678)

357

358

print(f"Order status: {order['status']}")

359

print(f"Executed quantity: {order['executedQty']}")

360

print(f"Remaining quantity: {float(order['origQty']) - float(order['executedQty'])}")

361

362

if order['status'] == ORDER_STATUS_FILLED:

363

print(f"Average fill price: {order['cummulativeQuoteQty']}")

364

```

365

366

### Rate Limits and Order Count

367

368

Monitor API usage and order limits.

369

370

```python { .api }

371

def get_current_order_count(self, **params): ...

372

```

373

374

```python

375

# Check current order count usage

376

order_count = client.get_current_order_count()

377

print(f"Order count usage: {order_count}")

378

379

# The response includes rate limit information

380

# [

381

# {

382

# "rateLimitType": "ORDERS",

383

# "interval": "SECOND",

384

# "intervalNum": 10,

385

# "limit": 50,

386

# "count": 5

387

# },

388

# {

389

# "rateLimitType": "ORDERS",

390

# "interval": "DAY",

391

# "intervalNum": 1,

392

# "limit": 160000,

393

# "count": 1200

394

# }

395

# ]

396

```

397

398

### Trade Prevention and Compliance

399

400

```python { .api }

401

def get_prevented_matches(self, **params): ...

402

```

403

404

```python

405

# Get prevented matches (self-trading prevention)

406

prevented = client.get_prevented_matches(

407

symbol='BTCUSDT',

408

preventedMatchId=123456

409

)

410

```

411

412

This comprehensive trading functionality enables full control over order execution and management in spot markets, with support for all Binance order types and advanced trading features.