Skip to main content

Open and close a position

This recipe walks through a full trade lifecycle against production using curl. Replace credentials and IDs with your own.

1. Grab an access token

curl --request POST \
--url "https://api.onlytradeplatform.com/auth/v1/oauth2/login?remember_me=true" \
--header "Authorization: Basic $(printf '26100003:MyPassword' | base64)"

Save data.access_token from the response:

TOKEN="3d4d16824096e9c0..."

2. Confirm the account

curl --request GET \
--url https://api.onlytradeplatform.com/api/v1/accounts/me \
--header "Authorization: Bearer $TOKEN"

Check data.free_margin is enough for the trade.

3. Find an instrument

curl --request GET \
--url https://api.onlytradeplatform.com/api/v1/symbols/me \
--header "Authorization: Bearer $TOKEN"

Note the id and digits of the symbol you want (e.g. EURUSD → symbol_id 26100001).

4. Place a market order

type: 0 is a market order, side: 0 is buy, side: 1 is sell. order_price is the price you expect to fill at (the current ask for a buy, bid for a sell).

curl --request POST \
--url https://api.onlytradeplatform.com/api/v1/orders/accounts/me \
--header "Authorization: Bearer $TOKEN" \
--header "content-type: application/json" \
--data '{
"symbol_id": 26100001,
"side": 0,
"type": 0,
"volume": 0.10,
"order_price": 1.08234,
"stop_loss": null,
"take_profit": null,
"fill_policy": 0
}'

The order fills and becomes a position.

5. List open positions

curl --request GET \
--url https://api.onlytradeplatform.com/api/v1/positions/accounts/me \
--header "Authorization: Bearer $TOKEN"

Grab the id of the position you just opened (e.g. 260610016).

6. Close the position

Send the volume to close and the close price:

curl --request POST \
--url https://api.onlytradeplatform.com/api/v1/positions/260610016/accounts/me \
--header "Authorization: Bearer $TOKEN" \
--header "content-type: application/json" \
--data '{ "volume": 0.10, "order_price": 1.08620 }'

The realized P/L is applied to your balance. Verify with GET /accounts/me again or pull the deal from GET /api/v1/deals/accounts/me.

tip

Want the same flow live without a terminal? Open the API Reference, expand Orders → Create my order, and hit Try It.