MCPcopy Create free account
hub / github.com/Gunthersuper/Binance-Futures-Python-Examples / open_order

Function open_order

main1.py:109–153  ·  view source on GitHub ↗
(symbol, side)

Source from the content-addressed store, hash-verified

107
108# Open new order with the last price, and set TP and SL:
109def open_order(symbol, side):
110 price = float(client.ticker_price(symbol)['price'])
111 qty_precision = get_qty_precision(symbol)
112 price_precision = get_price_precision(symbol)
113 qty = round(volume/price, qty_precision)
114 if side == 'buy':
115 try:
116 resp1 = client.new_order(symbol=symbol, side='BUY', type='LIMIT', quantity=qty, timeInForce='GTC', price=price)
117 print(symbol, side, "placing order")
118 print(resp1)
119 sleep(2)
120 sl_price = round(price - price*sl, price_precision)
121 resp2 = client.new_order(symbol=symbol, side='SELL', type='STOP_MARKET', quantity=qty, timeInForce='GTC', stopPrice=sl_price)
122 print(resp2)
123 sleep(2)
124 tp_price = round(price + price * tp, price_precision)
125 resp3 = client.new_order(symbol=symbol, side='SELL', type='TAKE_PROFIT_MARKET', quantity=qty, timeInForce='GTC',
126 stopPrice=tp_price)
127 print(resp3)
128 except ClientError as error:
129 print(
130 "Found error. status: {}, error code: {}, error message: {}".format(
131 error.status_code, error.error_code, error.error_message
132 )
133 )
134 if side == 'sell':
135 try:
136 resp1 = client.new_order(symbol=symbol, side='SELL', type='LIMIT', quantity=qty, timeInForce='GTC', price=price)
137 print(symbol, side, "placing order")
138 print(resp1)
139 sleep(2)
140 sl_price = round(price + price*sl, price_precision)
141 resp2 = client.new_order(symbol=symbol, side='BUY', type='STOP_MARKET', quantity=qty, timeInForce='GTC', stopPrice=sl_price)
142 print(resp2)
143 sleep(2)
144 tp_price = round(price - price * tp, price_precision)
145 resp3 = client.new_order(symbol=symbol, side='BUY', type='TAKE_PROFIT_MARKET', quantity=qty, timeInForce='GTC',
146 stopPrice=tp_price)
147 print(resp3)
148 except ClientError as error:
149 print(
150 "Found error. status: {}, error code: {}, error message: {}".format(
151 error.status_code, error.error_code, error.error_message
152 )
153 )
154
155
156# Your current positions (returns the symbols list):

Callers 1

main1.pyFile · 0.85

Calls 2

get_qty_precisionFunction · 0.85
get_price_precisionFunction · 0.85

Tested by

no test coverage detected