Answer the question
In order to leave comments, you need to log in
Sending a request with the same body gives different results, how to fix it?
I form the request body like this:
price = self.get_price()
body = '{' + f'"currency_pair":"{pair}","type":"limit","account":"spot","side":"{side}","amount":"{quantity}","price":"{price}","time_in_force":"gtc"' + '}'
{"currency_pair":"BNB_USDT","type":"limit","account":"spot","side":"buy","amount":"0.019",
"price":"516.2708"," time_in_force":"gtc"}
{'status': 405, 'label': 'METHOD_NOT_ALLOWED', 'message': ''}
price = 516.2708 # self.get_price()
body = '{' + f'"currency_pair":"{pair}","type":"limit","account":"spot","side":"{side}","amount":"{quantity}","price":"{price}","time_in_force":"gtc"' + '}'
{"currency_pair":"BNB_USDT","type":"limit","account":"spot","side":"buy","amount":"0.019",
"price":"516.2708"," time_in_force":"gtc"}
{'id': '109317501746', 'text': 'apiv4', 'create_time': '1641032772', 'update_time': '1641032772', 'create_time_ms': 1641032772621, ... }
get_price()
takes the price string from the site, converts it to a float and returns the valuehost = "https://api.gateio.ws"
prefix = "/api/v4"
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
url = ''
def get_url(self, param=None, host=None):
if host is None:
host = self.host
if param is None:
return host + self.prefix + self.url
return host + self.prefix + self.url + "?" + param
def get_price(self) -> float:
return float(self.get_price_by_str())
def get_price_by_str(self):
self.url = '/spot/order_book'
query_param = f'currency_pair={self.pair}'
r = self.send_request(query_param)
print(r)
return r["bids"][0][0]
def send_request(self, param: str = None, body: dict = None, method='GET', authorization=False):
headers = self.headers
data = None
if body is not None:
data = json.dumps(body)
if authorization is True:
headers.update(self.gen_sign(method, param, data))
r = requests.request(method, self.get_url(param), headers=headers, data=data)
return r.json()
Answer the question
In order to leave comments, you need to log in
import json
body = json.dumps({"currency_pair":pair, "type":"limit", "account": "spot", "side": side, "amount":quantity, "price":price, "time_in_force": "gtc"})
>>> type("2.46"); type(2.46)
<class 'str'>
<class 'float'>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question