TL;DR: I'm having troubles designing my tradebot because bitstamp API doesn't provide enough information; I'm asking for help and/or requesting this information to be provided through the API
Bitstamp allows to do JSON/HTTP-based API trading using
https://www.bitstamp.net/api/function/
The tradebot that I'm about to create needs to know the current balance and the currently open orders at a synchronised(!) point in time.
The API allows to get the current balance using /api/balance/ and the currently open orders using /api/open_orders/. But this is awkward and doesn't work properly because:
- The access is not synchronized: It is possible that a trade happens just inbetween the API calls for balance/open orders, causing the returned information to be inaccurate (I've already seen losses due to this effect while testing)
- The list returned by /api/open_orders/ can be quite large; it's wasting bandwidth, which is rare at my site. (and I need to download this list quite often)
To work around these problems, I'm trying the following approach:
The bot maintains an offline list of all open orders that it has posted to the market using /api/buy/ and /api/sell/; it also keeps track of canceled orders.
The bot keeps itself up-to-date about all user transactions using /api/user_transactions/. This allows my bot to know the current balance before and after every transaction.
Now I'm trying to match the user transactions to the open orders, i.e. the bot tries to figure out what user transaction happened due to what open order.
If this matching can be done, then I'm happy because then the bot could know about open orders and balance at any point in time (i.e. before and after every transaction).
However, this matching can be quite tricky. Let me show you an example where I think it's impossible:
* The bot initializes in a clean state, i.e. without any open orders and without pending transactions
* The bot calls /api/buy/ to buy 2 BTC at 122 USD each; saving the returned order id in variable X
* The bot calls /api/buy/ to buy 2 BTC at 120 USD each; saving the returned order id in variable Y
* The bot calls /api/cancel_order/ to cancel order X, the API indicates that canceling was successful
* The bot calls /api/user_transactions/ and receives one transaction with the following information:
BTC: +1
USD: -120
Now, the tricky question is: What open orders does the bot have after this transaction??
I know about at least two possibilities:
1.: After the bot canceled order X, someone else sold 1 BTC, which partially fulfilled order Y. Consequently, after the transaction, there's one remaining open order: buy 1 BTC at 120 USD each
2.: Even before the bot posted order X, there already was someone else's order to sell 1 BTC at 120 USD each, causing order X to immediately match against it. So order X was only "buy 1 BTC at 122 USD each" while it was canceled. Consequently, after the transaction, there's one remaining order: buy 2 BTC at 120 USD each
So my bot can't know what open order caused the received transaction
This makes it impossible to maintain an offline list of all open orders posted by the bot.
It would be
a lot easier to code this stuff if the user transactions returned by /api/user_transactions/ would contain an additional entry about what open order's id issued this transaction.
In the example above it would clearly indicate order id X or order id Y, allowing the bot to maintain an offline list of open orders.
Consider this post as being a feature request