gateio / WebSocket-API

gateio WebSocket-API
57 stars 32 forks source link

Futures trading w/ leverage doesn't work (via API) #27

Open dhn0906 opened 1 year ago

dhn0906 commented 1 year ago

Please help!! I'm having trouble to trade with leverage on the BTC/USD futures via API (3Commas). The trade does happen but with only 1x leverage.

I'm trying to have 20x leverage on the BTC/USD via API (3Commas) on 50USD worth account.

I had initial inquiry on the live chat and the lady told me i'm from restricted country which I believe she is mistaken because i'm from South Korea, not North Korea. I reached out again for live support and then she told me that it cannot be addressed in live chat but asked me to submit the ticket. Then i submitted the ticket and got response that i should check here.. (very inefficient way to guide people.. )

Just for my purpose, I did try 2x leverage trade on the Web (not via API) manually, and i was able to trade with 2x leverage. So it seems like there is some API related issue..

please let me know how i could potentially solve this issue. thank you.

revilwang commented 1 year ago

Which settle currency are you in? BTC settled?

dhn0906 commented 1 year ago

yes the i'm trading based on the BTC i'm holding

revilwang commented 1 year ago

Which API endpoint you're sending your request? 3Commas's or Gate's? Can you please provide your detailed API request.

dhn0906 commented 1 year ago

sorry for the ignorance.. Not understanding what API endpoint means.. Where can i find those out?

I have a tradebot set-up in 3commas where i hooked up with Gate.IO's API to trade in Gate.IO... i'm pretty sure this is not information you are asking for.. but just trying..

revilwang commented 1 year ago

So I'm assuming you create a Gate APIv4 key with futures read/write permission enabled, and import the API key and secret to 3Commas. Is that correct? You mentioned you can trade with 20x leverage on the web. Does the web means Gate's Web UI?

dhn0906 commented 1 year ago

Yes that's correct. I have both futures read/write permission enabled. And i import that Gate.IO's API key and PW to 3commas.

I mentioned just for try i did trade 2x (not 20x, which is what I actually want ) on the Gate.IO web as see below

image

revilwang commented 1 year ago

Can you by any chance understand a little Python code? From the information I gathered. I think you might still need to contact 3Comma's to resolve the problem. So maybe I can provide you with a python code snippet, and you can edit by your own to make sure that using Gate's API, you can do the job, while in 3Commas you can't.

dhn0906 commented 1 year ago

you can consider i understand pretty much zero on Python but if you help me start (with what you mentioned above), i will try to figure things out..

revilwang commented 1 year ago

Well, in that case, I think it's be a little hard for me to help you. Setting up a Python environment does require some basic knowledge. But if you can do the job on Gate's web UI, then you should contact 3Commas to confirm them that it's their problem you can't do the same thing with 3Commas' bot.

dhn0906 commented 1 year ago

just to correct myself... i have phycharm installed through anaconda.. (i did go through some basic course on this but never did anything with it). I think some basic set-up is already in place..

dhn0906 commented 1 year ago

@revilwang , could you share with me the python code snippet you mentioned above? Thanks

@Anyone else... any other help you guys can provide?

revilwang commented 1 year ago
  1. Change the content mark as TODO
  2. Change your leverage to 2x on web UI.
  3. Run the script.
# coding: utf-8
import requests
import time
import hashlib
import hmac
import json
import sys

def gen_sign(method, url, query_string=None, payload_string=None, user='A'):
    key = ''        # TODO: api_key
    secret = ''     # TODO: api_secret

    t = time.time()
    m = hashlib.sha512()
    m.update((payload_string or "").encode('utf-8'))
    hashed_payload = m.hexdigest()
    s = '%s\n%s\n%s\n%s\n%s' % (method, url, query_string or "", hashed_payload, t)
    sign = hmac.new(secret.encode('utf-8'), s.encode('utf-8'), hashlib.sha512).hexdigest()
    return {'KEY': key, 'Timestamp': str(t), 'SIGN': sign}

if __name__ == '__main__':
    host = "https://api.gateio.ws"
    prefix = "/api/v4"
    headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}

    url = '/futures/btc/orders'
    query_param = ''
    body = json.dumps({
        "contract": "BTC_USD",
        "size": 1,   # TODO: 1 means open long with 1 contract, change to -1 to open short.
        "price": "", # TODO: input the price,
    })
    sign_headers = gen_sign('POST', prefix + url, query_param, body)
    headers.update(sign_headers)
    r = requests.request('POST', host + prefix + url, headers=headers, data=body)
    print(r.json())
dhn0906 commented 1 year ago

thanks revilwang. I guess i can run the code with adding on additional information you kindly mentioned. But in terms of "input the price", why would it be the case? I would want 3commas to send out signal to Gate.IO to trade on that particular condition's price (thus there won't be fixed price to it). Maybe i'm understanding this intention of the code differently..

revilwang commented 1 year ago

It is just an example. In 3Comma's bot, the price is, like you said, should be calculated and set with a particular condition's price.

dhn0906 commented 1 year ago

ok thanks for the clarification. Its probably too advanced for me to handle all the adjustments to above code to make it work.. But anyways thanks for your help revilwang!