zLeki / DxTrade-Api-Go

API for dxtrade that can be used with all golang applications to open, close, and manage positions with your dxtrade account. It works with all prop firms or companies that use dxtrade
MIT License
29 stars 5 forks source link

Question - python integration #3

Closed kovacicek closed 8 months ago

kovacicek commented 9 months ago

Hi,

Not sure how to contact you but via issue :) Currently I'm working on python implementation of DX trade API but simply can't pass the step after getting session token. Constantly getting Authorization required error, not sure how to put token in headers. Do you have some kind of postman collection you can share just to unblock me please. Thanks in advance, Milan

zLeki commented 9 months ago

Hi, are you using a csrf header in all your other requests?

kovacicek commented 9 months ago

Nop, do you have any example. Just using /login with username, domain and password

then not sure how to pass token with other requests, constantly getting {'errorCode': '1', 'description': 'Authorization required'}

zLeki commented 9 months ago

Yeah I can send you a snippet of my private python dxtrade api hold on

zLeki commented 9 months ago

First off all requests passed login require the X-CSRF-Token header which can be found in the html contents of the home page while using the correct authentication you received from login, I have this all written in my golang function called GetCsrf or something like that. Did this help?

Additionally before running other requests you must establish a websocket connection with the correct cookie header or else you will get a 409/403 error on all other requests, such as order execution, modify, etc

kovacicek commented 9 months ago

Unfortunately now, simply can't replicate it... can't find in documentation neither this part described how headers should look like. Why do you add headers.Add("Cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"]) if you're using simply auth

kovacicek commented 9 months ago

Do you have any Postman example which works

zLeki commented 9 months ago

I store my cookies like this self.cookies = {} and after I store it with the cookies I get from login I use it in my cookie header like this 'cookie': '; '.join([f"{key}={value}" for key, value in self.cookies.items()]),

zLeki commented 9 months ago

Heres a curl request you can paste into postman

curl --location 'https://dxtrade.ftmo.com/api/orders/single' \
--header 'content-type: application/json; charset=UTF-8' \
--header 'cookie: YOUR GENERATED COOKIE' \
--header 'x-csrf-token: YOUR GENERATED TOKEN' \
--header 'x-requested-with: XMLHttpRequest' \
--data '{"directExchange":false,"legs":[{"instrumentId":3425,"positionEffect":"OPENING","ratioQuantity":1,"symbol":"BTCUSD"}],"limitPrice":51796.35,"orderSide":"BUY","orderType":"MARKET","quantity":0.1,"requestId":"gwt-uid-1258-56353eae-e155-49b8-b8cf-f37363710ce2","timeInForce":"GTC"}'
kovacicek commented 9 months ago

Thanks a lot for your time. Thing is that I don't get any cookies generated, just session token and that is it. I've read your code, not proficient in GO but understood everything... thing is that after /login only session token and timeout are returned. Does it makes sense that I'm using Demo account, do I need to enable something in account settings or something, have no idea

zLeki commented 9 months ago

Check the response cookies OR use requests sessions with python because it must work, I have already rewritten this entire library in python. Also i'm using a demo account for this project so that's not the case

kovacicek commented 9 months ago

cool, can you please share the code if possible or publish it somewhere, I can help you with further implementation in order not to waste of time

potatoesforliferi commented 9 months ago

zleki , Is the intention to keep the python version of your code not released? I have no issues with that remaining private as its your hard work, but i think since a lot of the community are python developers, it would be nice if that version was released alongside the golang version.

A lot of people could contribute more to the python side. I personally was trying to rebuild my tradingview to MT5 bridge that I currently use for my trading But doing a tradingview to dxtrade was my goal.

kovacicek commented 9 months ago

Kudos @potatoesforliferi we share the opinion, python community is huge and tons of us can contribute and make it to perfection

kovacicek commented 9 months ago

in Header Authorization : "DXAPI {session_token}"

I'll continue with Python implementation, we can work together to cover more of the API

zLeki commented 9 months ago

Someone paid me to create it so I can't release the code publicly but i'm stating it is possible to create it, i'll create you a demo that you can use later

zLeki commented 9 months ago

Here's an example on how to close a trade


def close_trade(self, position_id, quantity, price, symbol, instrument_id):
        url = "https://dxtrade.ftmo.com/api/positions/close"
        headers = {
            'Content-Type': 'application/json; charset=UTF-8',
            'Cookie': '; '.join([f"{key}={value}" for key, value in self.cookies.items()]),
            'X-CSRF-Token': self.csrf,
            'X-Requested-With': 'XMLHttpRequest',
        }
        payload = {
            "legs": [{
                "instrumentId": instrument_id,
                "positionCode": position_id,
                "positionEffect": "CLOSING",
                "ratioQuantity": 1,
                "symbol": symbol
            }],
            "limitPrice": price,
            "orderType": "MARKET" if price == 0 else "LIMIT",
            "quantity": -quantity,
            "timeInForce": "GTC"
        }
        response = self.s.post(url, headers=headers, data=json.dumps(payload))

All you need is the cookies from login and the csrf token

zLeki commented 9 months ago

Also I can create a repository of just the login/create trade if you want

kovacicek commented 9 months ago

Thanks a lot !

zLeki commented 9 months ago

No problem. I'd appreciate a star on the repo if you could 😉😉

zLeki commented 9 months ago

I made a demo repository if you want to take a look

kpathan commented 9 months ago

@kovacicek where you able to login using RestAPI endpoint for FTMO or other prop firms? I tried to myfunded fx and its failing

zLeki commented 9 months ago

Look at my new repository it has a demo that is functional

kovacicek commented 9 months ago

It was simple at the end but dxtrade documentation is quite bad so it is better to take a loot at swagger doc. @kpathan nop, just demo, waiting for my prop to be moved from TFT to dxtrade in next day or so so will let you know after I try. What error do you get for myfunded fx

kpathan commented 9 months ago

It was simple at the end but dxtrade documentation is quite bad so it is better to take a loot at swagger doc. @kpathan nop, just demo, waiting for my prop to be moved from TFT to dxtrade in next day or so so will let you know after I try. What error do you get for myfunded fx

I am able to login using restapi, but struggling with account number and HMAC on how to generate public/private token pair.

Let me know if you like to collaborate.

kpathan commented 9 months ago

Look at my new repository it has a demo that is functional

Yes it works, and you are trying to simulate the request in browser. And this has helped me a lot. Thank you.

zLeki commented 9 months ago

No problem man