PnL snapshot endpoint does not work

Hi All –

after a hiatus, I’m back to working with the Tradovate API. I am using the requests library to get a snapshot of my account PnL. Here is my code:

def get_pnl():

bearer = 'Bearer '+str(main_authenticator.access_token) 

headers = {

    'Accept': 'application/json',

    'Authorization': bearer,

    }

data = '{ "accountId": "999999"}'

response = requests.post('https://demo.tradovateapi.com/v1/cashBalance/getcashbalancesnapshot', headers=headers, data=data)

response_message = response.content

print(response_message)

This returns the error: b’Invalid request: error id: 3d78e68f-7ed0-4a87-89b8-0bf884f2a196’. Anyone know what’s going on?

Maybe try “accountId” value as a number instead of a string?

1 Like

Tried that, instead of the actual data that the call is supposed to return, I just get an empty “b” frame, “b’’”

anyone else have any ideas? why would this return an empty b frame?

also have noticed the error returning only when i include a space in the header for bearer ("Bearer " instead of “Bearer”). thanks again, everyone

Not sure if you’re still looking for a solution to this, but I can share what I’ve been doing to get a cash balance snapshot. It uses the working code examples from the API tutorials, so the tvPost and getAvailableAccounts functions are imported from the services.js and storage.js helper files, respectively. You will also need to authorize your access token, etc.

let cash = await tvPost('/cashBalance/getcashbalancesnapshot', {
    accountId: getAvailableAccounts()[0].id
});

Note that this endpoint pulls from the REST API, so I would only do this once at the start of your session (it’s nice to have it immediately). In order to keep it up-to-date though, you should connect to the WebSocket and subscribe to the user/syncrequest endpoint. There is an excellent example of how this works here.

Every time you make a trade, it will update your cashBalance entity before and after you enter the position, so you should be able to use that to update your balance. If you are in the middle of a trade and want to keep a running tally, you will need to query your positions and current prices (by subscribing to the market data URL) and calculate the running P&L. This is Alexander’s solution, if you’re interested in checking it out.

1 Like