add send_order function

1. add send_order function
2. post by json param,  need to modify rest_client.py to support json field
This commit is contained in:
vigarbuaa 2019-06-06 23:13:39 +08:00
parent 8cf23cea4b
commit 21cc72ac3f
2 changed files with 16 additions and 11 deletions

View File

@ -49,6 +49,10 @@ class Request(object):
self.response = None self.response = None
self.status = RequestStatus.ready self.status = RequestStatus.ready
self.json=None
def set_json(self, json_str: dict):
self.json=json_str
def __str__(self): def __str__(self):
if self.response is None: if self.response is None:
@ -61,6 +65,7 @@ class Request(object):
"headers: {}\n" "headers: {}\n"
"params: {}\n" "params: {}\n"
"data: {}\n" "data: {}\n"
"json: {}\n"
"response:" "response:"
"{}\n".format( "{}\n".format(
self.method, self.method,
@ -70,6 +75,7 @@ class Request(object):
self.headers, self.headers,
self.params, self.params,
self.data, self.data,
self.json,
"" if self.response is None else self.response.text, "" if self.response is None else self.response.text,
) )
) )
@ -145,6 +151,7 @@ class RestClient(object):
on_failed: Callable = None, on_failed: Callable = None,
on_error: Callable = None, on_error: Callable = None,
extra: Any = None, extra: Any = None,
json_str: dict = None,
): ):
""" """
Add a new request. Add a new request.
@ -170,6 +177,8 @@ class RestClient(object):
on_error, on_error,
extra, extra,
) )
if json_str is not None:
request.set_json(json_str)
self._queue.put(request) self._queue.put(request)
return request return request
@ -253,10 +262,10 @@ class RestClient(object):
headers=request.headers, headers=request.headers,
params=request.params, params=request.params,
data=request.data, data=request.data,
json=request.json,
proxies=self.proxies, proxies=self.proxies,
) )
request.response = response request.response = response
status_code = response.status_code status_code = response.status_code
if status_code // 100 == 2: # 2xx都算成功尽管交易所都用200 if status_code // 100 == 2: # 2xx都算成功尽管交易所都用200
jsonBody = response.json() jsonBody = response.json()

View File

@ -265,26 +265,22 @@ class AlpacaRestApi(RestClient):
orderid = str(self.connect_time + self._new_order_id()) orderid = str(self.connect_time + self._new_order_id())
data = { data = {
"symbol": req.symbol, "symbol": req.symbol,
"qty":2, "qty": int(req.volume),
"side":'buy', "side": DIRECTION_VT2ALPACA[req.direction],
"type":'limit', "type": ORDERTYPE_VT2ALPACA[req.type],
"time_in_force":'opg', "time_in_force":'opg',
"limit_price":20.50 "limit_price": float(req.price)
# "side": DIRECTION_VT2BITMEX[req.direction],
# "ordType": ORDERTYPE_VT2BITMEX[req.type],
# "orderQty": int(req.volume),
# "clOrdID": orderid,
} }
order = req.create_order_data(orderid, self.gateway_name) order = req.create_order_data(orderid, self.gateway_name)
print("debug detail order: ",order)
self.add_request( self.add_request(
"POST", "POST",
"/v1/orders", "/v1/orders",
callback=self.on_send_order, callback=self.on_send_order,
data=data, #data=data,
extra=order, extra=order,
on_failed=self.on_failed, on_failed=self.on_failed,
on_error=self.on_error, on_error=self.on_error,
json_str=data,
) )
self.gateway.on_order(order) self.gateway.on_order(order)
return order.vt_orderid return order.vt_orderid