forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfutures_algo_order_examples.py
More file actions
221 lines (184 loc) · 6.08 KB
/
futures_algo_order_examples.py
File metadata and controls
221 lines (184 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
"""
Examples of how to use the Futures Algo Order API.
New Algo Order supports various conditional order types including:
- STOP / STOP_MARKET
- TAKE_PROFIT / TAKE_PROFIT_MARKET
- TRAILING_STOP_MARKET
This example demonstrates the new parameters and features available
for creating advanced algo orders with TP/SL functionality.
"""
from binance.client import Client
# Initialize the client
api_key = '<api_key>'
api_secret = '<api_secret>'
client = Client(api_key, api_secret)
def create_basic_stop_market_order():
"""Create a basic STOP_MARKET algo order"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='SELL',
type='STOP_MARKET',
quantity=0.001,
triggerPrice=40000,
workingType='CONTRACT_PRICE'
)
print("Basic Stop Market Order:", order)
return order
def create_take_profit_with_price_protect():
"""Create a TAKE_PROFIT order with price protection enabled"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='SELL',
type='TAKE_PROFIT',
quantity=0.001,
price=50000,
triggerPrice=50000,
timeInForce='GTC',
priceProtect='TRUE',
workingType='MARK_PRICE'
)
print("Take Profit with Price Protection:", order)
return order
def create_trailing_stop_market():
"""Create a TRAILING_STOP_MARKET order with activate price and callback rate"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='SELL',
type='TRAILING_STOP_MARKET',
quantity=0.001,
activatePrice=48000, # Activate when price reaches this level
callbackRate=1.0, # 1% callback rate
)
print("Trailing Stop Market:", order)
return order
def create_stop_with_stp_mode():
"""Create a STOP order with Self-Trade Prevention mode"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='BUY',
positionSide='LONG', # For hedge mode
type='STOP',
quantity=0.001,
price=42000,
triggerPrice=42000,
timeInForce='GTC',
selfTradePreventionMode='EXPIRE_MAKER'
)
print("Stop Order with STP Mode:", order)
return order
def create_take_profit_with_price_match():
"""Create a TAKE_PROFIT order with price match parameter"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='SELL',
type='TAKE_PROFIT',
quantity=0.001,
triggerPrice=50000,
timeInForce='GTC',
priceMatch='OPPONENT', # Match opponent's price
)
print("Take Profit with Price Match:", order)
return order
def create_stop_market_close_position():
"""Create a STOP_MARKET order to close all positions"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='SELL',
type='STOP_MARKET',
closePosition='true', # Close all current long positions
triggerPrice=39000,
priceProtect='TRUE'
)
print("Stop Market Close Position:", order)
return order
def create_stop_with_reduce_only():
"""Create a STOP order with reduce only mode"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='SELL',
type='STOP',
quantity=0.001,
price=41000,
triggerPrice=41000,
timeInForce='GTC',
reduceOnly='true' # Only reduce position, not increase
)
print("Stop with Reduce Only:", order)
return order
def create_with_good_till_date():
"""Create an algo order with GTD (Good Till Date) time in force"""
import time
# Set expiry to 1 hour from now (timestamp in milliseconds)
expiry_time = int((time.time() + 3600) * 1000)
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='SELL',
type='TAKE_PROFIT',
quantity=0.001,
price=50000,
triggerPrice=50000,
timeInForce='GTD',
goodTillDate=expiry_time
)
print("Order with Good Till Date:", order)
return order
def create_with_result_response():
"""Create an algo order with RESULT response type for detailed information"""
order = client.futures_create_algo_order(
symbol='BTCUSDT',
side='BUY',
type='STOP_MARKET',
quantity=0.001,
triggerPrice=42000,
newOrderRespType='RESULT' # Get detailed response
)
print("Order with RESULT response:", order)
return order
def query_algo_order(symbol, algo_id):
"""Query a specific algo order status"""
order = client.futures_get_algo_order(
symbol=symbol,
algoId=algo_id
)
print("Algo Order Status:", order)
return order
def query_open_algo_orders(symbol=None):
"""Query all open algo orders"""
if symbol:
orders = client.futures_get_open_algo_orders(symbol=symbol)
else:
orders = client.futures_get_open_algo_orders()
print("Open Algo Orders:", orders)
return orders
def cancel_algo_order(symbol, algo_id):
"""Cancel a specific algo order"""
result = client.futures_cancel_algo_order(
symbol=symbol,
algoId=algo_id
)
print("Cancel Result:", result)
return result
def cancel_all_algo_orders(symbol):
"""Cancel all open algo orders for a symbol"""
result = client.futures_cancel_all_algo_open_orders(symbol=symbol)
print("Cancel All Result:", result)
return result
if __name__ == '__main__':
# Example usage
print("=" * 50)
print("Futures Algo Order Examples")
print("=" * 50)
# Create different types of algo orders
# Uncomment the examples you want to try
# order = create_basic_stop_market_order()
# order = create_take_profit_with_price_protect()
# order = create_trailing_stop_market()
# order = create_stop_with_stp_mode()
# order = create_take_profit_with_price_match()
# order = create_with_result_response()
# Query orders
# orders = query_open_algo_orders('BTCUSDT')
# Cancel orders
# cancel_algo_order('BTCUSDT', algo_id=12345)
# cancel_all_algo_orders('BTCUSDT')