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

View File

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