Connect api working example for get account positions (open, close, account balance or equity)

Created at 13 Feb 2017, 06:49
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
mindbreaker's avatar

mindbreaker

Joined 19.07.2014

Connect api working example for get account positions (open, close, account balance or equity)
13 Feb 2017, 06:49


Hi

I need working example with connect api (only get positions, no trading api) simple and clear?

Thanks.


@mindbreaker
Replies

Spotware
13 Feb 2017, 09:08

Hi mindbreaker,

You can find a working example here https://github.com/spotware/Connect-API-ASP.Net-Sample. Let us know if you have any specific questions.


@Spotware

mindbreaker
13 Feb 2017, 11:58

Hi,

It's too high thresholds for me (MVC and ASP Net) :).

Computers were invented in order to facilitate human life and not hindering :).

I needs a simpler solution.

I do not want to wander in a clumsy documentation of c connect again.

But thanks for the help.

 


@mindbreaker

Spotware
13 Feb 2017, 12:05

Hi mindbreaker,

We tried to make the example as simple as possible. If you can explicitely state what kind of example you need (i.e. type of application, programming language) then maybe a community member could help you. 


@Spotware

mindbreaker
14 Feb 2017, 12:41 ( Updated at: 21 Dec 2023, 09:20 )

Hi Spotware,

When i create connect aplication

and want redirect to urls:

https://api.spotware.com/webserv/connect/api/profile?access_token=EpnUPzeilFN_jvS-5-7uY9O6bpymsMnaiLJKqYBMgNo

https://api.spotware.com/webserv/connect/api/tradingaccounts?access_token=EpnUPzeilFN_jvS-5-7uY9O6bpymsMnaiLJKqYBMgNo

And how add new user (cTID) when i trying with playground ... next error something went worng ...

AND HOW GET OPEN AND CLOSED POSITIONS FROM ACCOUNTS WITH ACCESS_TOKEN FROM URL ????

 

 


@mindbreaker

mindbreaker
14 Feb 2017, 12:47 ( Updated at: 21 Dec 2023, 09:20 )

and next Wooops !!!!

What is wrong with us?


@mindbreaker

Spotware
14 Feb 2017, 14:05 ( Updated at: 21 Dec 2023, 09:20 )

Hi mindbreaker,

1) We have tried both the links you provided and they work fine. Please see below

However the tokens in the provided screenshots are different. Please check your tokens.

2) If you need a new cTID, logout from your existing cTID and register a new one.

3) Below an example of how to get positions

https://sandbox-api.spotware.com/connect/tradingaccounts/89214/positions?oauth_token=test002_access_token


@Spotware

mindbreaker
14 Feb 2017, 15:23 ( Updated at: 21 Dec 2023, 09:20 )

Hi, its my browser:

3) Below an example of how to get positions

https://sandbox-api.spotware.com/connect/tradingaccounts/89214/positions?oauth_token=test002_access_token

 

But i dont want use sandbox but active account ?

What is oauth_token and how can i get my oauth_token?

 


@mindbreaker

mindbreaker
14 Feb 2017, 15:23 ( Updated at: 21 Dec 2023, 09:20 )

RE:

mindbreaker said:

Hi, its my browser:

 

3) Below an example of how to get positions

https://sandbox-api.spotware.com/connect/tradingaccounts/89214/positions?oauth_token=test002_access_token

 

But i dont want use sandbox but active account ?

What is oauth_token and how can i get my oauth_token?

 


@mindbreaker

mindbreaker
14 Feb 2017, 15:40 ( Updated at: 21 Dec 2023, 09:20 )

And from VPS browser with new token with full access:

 

 

And how allow Client to add account  to my Application

( I want get data from my clients cTrader accounts ?)

 


@mindbreaker

Spotware
14 Feb 2017, 15:49

Can you please try the following link and let us know if it works?

https://api.spotware.com/connect/profile?oauth_token=EpnUPzeilFN_jvS-5-7uY9O6bpymsMnaiLJKqYBMgNo

If you want to use at active account then you should change the link to something like the following (note that you need to change the account_id and token parameters.

https://api.spotware.com/connect/tradingaccounts/account_id/positions?oauth_token=token

oauth_token is the parameter where you will set the token that will be returned to you after the client authenticates. Please the documentation here.

You don't need to allow your client to add accounts to your application. You can access his accounts after he authentices with his cTID and a valid token is returned to you.


@Spotware

mindbreaker
14 Feb 2017, 16:13

RE:

Spotware said:

Can you please try the following link and let us know if it works?

https://api.spotware.com/connect/profile?oauth_token=EpnUPzeilFN_jvS-5-7uY9O6bpymsMnaiLJKqYBMgNo

If you want to use at active account then you should change the link to something like the following (note that you need to change the account_id and token parameters.

https://api.spotware.com/connect/tradingaccounts/account_id/positions?oauth_token=token

oauth_token is the parameter where you will set the token that will be returned to you after the client authenticates. Please the documentation here.

You don't need to allow your client to add accounts to your application. You can access his accounts after he authentices with his cTID and a valid token is returned to you.

Yes, this link works.

If client authenticate with cTID,  I will get on my redirection link his oauth_token (POST or GET method) or? 

 


@mindbreaker

Spotware
14 Feb 2017, 16:24

It seems you were using an outdated version of URLs. Please find the URLs you need to use here

https://connect.spotware.com/docs/api-reference/accounts-api

Let us know if you have found those links somewhere in the documentation so we can update.

Regarding the authentication, yes as soon as the client authenticates, you will get his token on your redirection link using the get method.


@Spotware

mindbreaker
14 Feb 2017, 16:32 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Spotware said:

It seems you were using an outdated version of URLs. Please find the URLs you need to use here

https://connect.spotware.com/docs/api-reference/accounts-api

Let us know if you have found those links somewhere in the documentation so we can update.

Regarding the authentication, yes as soon as the client authenticates, you will get his token on your redirection link using the get method.

When i try acces to:

https://sandbox-connect.spotware.com/apps/auth?client_id=203_2hosJUozrOVxXNWyvAh8ftmp6Ug3vjmTP1SPBg3ytinkoiF4Te&redirect_uri=http://fxstar.eu/forex/&scope=accounts

I cant auth from my cTID Error: incorrect password

but if i trying from another (after login i got this):

 


@mindbreaker

mindbreaker
14 Feb 2017, 16:33

And this link shows

https://api.spotware.com/apps/auth?client_id=203_2hosJUozrOVxXNWyvAh8ftmp6Ug3vjmTP1SPBg3ytinkoiF4Te&redirect_uri=http://fxstar.eu/forex/&scope=accounts

Shows me:

403 Forbidden


nginx/1.6.3


@mindbreaker

Spotware
14 Feb 2017, 16:47

For the sandbox link you need to use the sandbox test account. See here

https://connect.spotware.com/docs/available-resources/test-environment

For the live environment the link should be

https://connect.spotware.com/apps/auth?client_id=203_2hosJUozrOVxXNWyvAh8ftmp6Ug3vjmTP1SPBg3ytinkoiF4Te&redirect_uri=http://fxstar.eu/forex/&scope=accounts 

Apologies, our mistake.

See here too https://connect.spotware.com/docs/api-reference/going-live

Please let us know where you found the links you used to avoid further confusion.


@Spotware

mindbreaker
14 Feb 2017, 17:20

And now how add new client accounts cTID to my aplication (playground)?


@mindbreaker

Spotware
14 Feb 2017, 17:24

You cannot and don't need to add any accounts to your application. The application works for the currenlty connected cTID. If you want to see details for a different cTID, you need to logout and connect with that cTID.


@Spotware

mindbreaker
14 Feb 2017, 17:41

I can't see positions from other cTID ? in my application?

Then how get acces to other account (as myfxbook.com gets my account info and shows charts) ?


@mindbreaker

Spotware
14 Feb 2017, 17:46

myfxbook.com and any other application that uses Connect API can see your account info only after you explicitely give it authorization to access your personal details. In the same way your application will be able to see a user's details only after that user has authorized your apprication to do so. 


@Spotware

mindbreaker
14 Feb 2017, 17:57

RE:

Spotware said:

myfxbook.com and any other application that uses Connect API can see your account info only after you explicitely give it authorization to access your personal details. In the same way your application will be able to see a user's details only after that user has authorized your apprication to do so. 

Ok, I know how i can get  :)

But what to do with this redirected code=7268724368462384628fhr3r87d2dtr28drt32rt ?

When user allow me get his positions?


@mindbreaker

mindbreaker
14 Feb 2017, 18:00

RE: RE:

How authorize user (link)?


@mindbreaker

mindbreaker
14 Feb 2017, 18:41

Just works :)  incredible.

How often i need refresh users tokens ?

Thanks for help.

 

 


@mindbreaker

Spotware
15 Feb 2017, 09:38

When receiving a token you also receive an expires_in value which indicates when the token expires and when does it need to be refreshed


@Spotware

mindbreaker
15 Feb 2017, 10:19

RE:

Spotware said:

When receiving a token you also receive an expires_in value which indicates when the token expires and when does it need to be refreshed

What is it (timestamp seconds or year)?

[expiresIn] => 2628000

 

And next questions:

1) How to create New Demo Account from api (100k USD, 1:100, hedging) ?

2) How to change Demo Account Balance to initial deposit ?

2) Is it possible to create a accounts limits ?

Example:  I want allow set only positions > 1 Lot and <  3 Lot and not more than 5 positions for account or for Symbol, when i open new demo account.


@mindbreaker

Spotware
15 Feb 2017, 10:43

You need to send a post request to /connect/tradingaccounts/createdemo. See documentation https://connect.spotware.com/docs/api-reference/accounts-api

See the schema also for required and optional parameters https://api.spotware.com/connect/schema

It is not possible to set limits to the demo account


@Spotware

mindbreaker
15 Feb 2017, 11:10

RE:

NOTE: please bear in mind that access tokens have their expiry time. When you get the error message about access token expiration you should generate message to refresh the last one with refresh token request. Please see the details below.

 

After this message i need refresh token: CH_ACCESS_TOKEN_INVALID The access token provided is invalid.  or when ?

{"error":{"errorCode":"CH_ACCESS_TOKEN_INVALID","description":"Invalid access token"}}

 

What with this expire time

[expiresIn] => 2628000

 

 


@mindbreaker

Spotware
15 Feb 2017, 14:54

The expiry time is in seconds


@Spotware

mindbreaker
16 Feb 2017, 09:25

RE:

Spotware said:

The expiry time is in seconds

Hi,

Can I refresh token earlier ?

And why refresh time is only 2s when i use Accounts connect Api (in forex world is incredible it is an eternity)

When i want refresh 10 account and get from them positions it is 20 sec.

 

And why this login window (with cTID) It is not part of the application ( and open in browser - it is stupid idea and annoying - I think so stop using this platform)

Or how to disable it ?


@mindbreaker

Spotware
16 Feb 2017, 09:48

There is no reason to refresh the token earlier. The refresh time is 2628000s and not 2s. Regarding the login window, it is a typical open authentication process. Please have a look at https://en.wikipedia.org/wiki/OAuth


@Spotware

mindbreaker
16 Feb 2017, 10:08

RE:

Spotware said:

There is no reason to refresh the token earlier. The refresh time is 2628000s and not 2s. Regarding the login window, it is a typical open authentication process. Please have a look at https://en.wikipedia.org/wiki/OAuth

 

When I connect from url with Api, i can't connect often than 2s, to spotware serwers ( error 429 ) not refresh time.

Thanks again bye.


@mindbreaker

Spotware
16 Feb 2017, 10:10

Please see here

https://connect.spotware.com/docs/frequently-asked-questions

"1. Why do a get 429 error response?

The 429 error response means that the user has sent too many requests in a given amount of time. The request limit on our servers is 1 request for every two seconds so please restrict your implementation within these limits. If your application's traffic exceeds 1 request for every 2 seconds, please contact us at connect@spotware.com"


@Spotware

mindbreaker
16 Feb 2017, 18:33 ( Updated at: 21 Dec 2023, 09:20 )

SPotware connect Api error

Hi,

This connect api does not work (sometime shows positions sometime not)

Send empty data (but i've got open positions )


@mindbreaker

mindbreaker
16 Feb 2017, 18:44

Spotware connect Api error

Is the broker has the ability to block access to positions from connectApi and FIX ?


@mindbreaker

mindbreaker
16 Feb 2017, 18:51

It works. It shows positions (not this account :D)


@mindbreaker

mindbreaker
16 Feb 2017, 18:56

Last question:

Is it possible refresh allowed and connected accounts to application cTID (if i add next account to my playground i dont see new account on old access token) ?

Or maybe i need refresh old token?

Bye, works fine.


@mindbreaker

mindbreaker
17 Feb 2017, 12:44

Hi Spotware,

how get closed positions with connect Accounts Api ?

And why

[executionPrice] => 0

in second array [1] ?

                (
                    [dealId] => 19075744
                    [positionId] => 7980418
                    [orderId] => 12787961
                    [tradeSide] => BUY
                    [volume] => 100000
                    [filledVolume] => 100000
                    [symbolName] => GBPUSD
                    [commission] => -3
                    [executionPrice] => 1.2428
                    [baseToUsdConversionRate] => 1.2428
                    [marginRate] => 1.17765
                    [channel] => cAlgo
                    [label] => 
                    [comment] => 
                    [createTimestamp] => 1487151222993
                    [executionTimestamp] => 1487151223210
                    [positionCloseDetails] => 
                )

            [1] => Array
                (
                    [dealId] => 19075666
                    [positionId] => 7980418
                    [orderId] => 12787961
                    [tradeSide] => BUY
                    [volume] => 100000
                    [filledVolume] => 0
                    [symbolName] => GBPUSD
                    [commission] => 0
                    [executionPrice] => 0
                    [baseToUsdConversionRate] => 1.2428
                    [marginRate] => 1.17736
                    [channel] => cAlgo
                    [label] => 
                    [comment] => 
                    [createTimestamp] => 1487151007973
                    [executionTimestamp] => 1487151008187
                    [positionCloseDetails] => 
                )

How work deals ?


@mindbreaker

mindbreaker
17 Feb 2017, 15:08 ( Updated at: 21 Dec 2023, 09:20 )

Spotware account error data connect api deals

Hi when i get deals with connect Api i've got error on one account on second not (on this same script)???

 


@mindbreaker

mindbreaker
17 Feb 2017, 16:39

// Parse a Number from Exponential Notation
Double.Parse(string);
or
decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);
or
decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Any);

 


@mindbreaker

mindbreaker
18 Feb 2017, 10:51

Hi,

here connect Api Accounts Api, working example:

https://github.com/fxstar/connectAPI

Bye


@mindbreaker

mindbreaker
23 Feb 2017, 14:24

Hi Spotware,

Is it possible to add cTraderID and creation timestamp or connected account list to accetToken response:

{"accessToken":"sdfsdfsdA55Rnu29g7YdLotTNWTSM","tokenType":"bearer","expiresIn":2628000,"refreshToken":"LcusdfghtypPmJ8m4PP8OG6jNVVoFwHymT1mFaQ","errorCode":null,"access_token":"sdfsdfsdA55Rnu29g7YdLotTNWTSM","refresh_token":"LcusdfghtypPmJ8m4PP8OG6jNVVoFwHymT1mFaQ","expires_in":2628000}

Bye.


@mindbreaker

mindbreaker
23 Feb 2017, 14:32

RE:

mindbreaker said:

Hi Spotware,

Is it possible to add cTraderID and creation timestamp or connected account list to accetToken response:

{"accessToken":"sdfsdfsdA55Rnu29g7YdLotTNWTSM","tokenType":"bearer","expiresIn":2628000,"refreshToken":"LcusdfghtypPmJ8m4PP8OG6jNVVoFwHymT1mFaQ","errorCode":null,"access_token":"sdfsdfsdA55Rnu29g7YdLotTNWTSM","refresh_token":"LcusdfghtypPmJ8m4PP8OG6jNVVoFwHymT1mFaQ","expires_in":2628000}

Bye.

Ok i get from:

https://api.spotware.com/connect/profile?access_token=sdfsd328r7r2r32r-23rr23r-23r2sef-fsdfsdVa_GEYe


@mindbreaker