https://tests.bitcoin.it/w/api.php?action=feedcontributions&user=Jwi&feedformat=atomBitcoin Wiki - User contributions [en]2024-03-29T08:48:41ZUser contributionsMediaWiki 1.30.0https://tests.bitcoin.it/w/index.php?title=MtGox/API/Streaming&diff=43078MtGox/API/Streaming2013-12-13T13:37:00Z<p>Jwi: </p>
<hr />
<div>Real time streaming data may be obtained over the streaming API, using [http://socket.io/ Socket.io] or the vanilla websocket<br />
<br />
==websocket channel list ==<br />
<br />
List of the public streaming channels :<br />
<br />
https://mtgox.com/api/2/stream/list_public<br />
<br />
==Connecting==<br />
<br />
* Host: '''websocket.mtgox.com''' or '''socketio.mtgox.com'''<br />
* Port: '''80''' or '''443''' ( ssl )<br />
* Namespace: '''/mtgox''' (Including beginning slash)<br />
<br />
The following JavaScript code establishes a connection in the browser:<br />
<source lang="javascript"><br />
<script src="https://socketio.mtgox.com/socket.io/socket.io.js"></script><br />
<script><br />
var conn = io.connect('https://socketio.mtgox.com/mtgox');<br />
conn.on('message', function(data) {<br />
// Handle incoming data object.<br />
});<br />
</script><br />
</source><br />
<br />
==the Currency parameter ==<br />
use : <br />
websocket.mtgox.com:80/mtgox?Currency=EUR<br />
<br />
and the websocket will give you updates in EUR currency<br />
<br />
if you need need more than one currency, you can add currency symbols like that : <br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD<br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD,CHF<br />
<br />
find the list of currency symbols on https://en.bitcoin.it/wiki/MtGox/API#Currency_Symbols<br />
<br />
The session ID expires after 30 seconds<br />
<br />
==Handling Events==<br />
Socket.io exposes a simple interface for handling events. Handling <tt>message</tt> events is shown above, but there are other events that may be handled:<br />
<source lang="javascript"><br />
conn.on('connect', onConnect);<br />
conn.on('disconnect', onDisconnect);<br />
conn.on('error', onError);<br />
conn.on('message', onMessage);<br />
</source><br />
<br />
==Incoming Data==<br />
Data arrives as a full object instead of as JSON text, eliminating the need to parse the data in the JavaScript handler. Messages that come across the socket to trigger the <tt>message</tt> event will contain the following minimum components:<br />
<source lang="javascript"><br />
{<br />
"op":<OPERATION_TYPE><br />
}<br />
</source><br />
<br />
The <tt>OPERATION_TYPE</tt> field may take these values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! OPERATION_TYPE !! Description<br />
|-<br />
| subscribe || Notification that the user is subscribed to a channel<br />
|-<br />
| unsubscribe || Messages will no longer arrive over the channel<br />
|-<br />
| remark || A server message, usually a warning<br />
|-<br />
| private || The operation for depth, trade, and ticker messages<br />
|-<br />
| result || The response for op:call operations<br />
|}<br />
<br />
===op:subscribe and op:unsubscribe===<br />
The subscribe and unsubscribe message data are very simple, containing the channel and the operation.<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"subscribe" OR "unsubscribe"<br />
}<br />
</source><br />
<br />
Some of the channels are:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Channel ID !! Description<br />
|-<br />
| dbf1dee9-4f2e-4a08-8cb7-748919a71b21 || Trades<br />
|-<br />
| d5f06780-30a8-4a48-a2f8-7ed181b4a13f || Ticker<br />
|-<br />
| 24e67e0d-1cad-4cc0-9e7a-f8523ef460fe || Depth<br />
|}<br />
<br />
===op:remark===<br />
The remark operation contains message and success fields.<br />
<source lang="javascript"><br />
{<br />
"op":"remark",<br />
"message":<MESSAGE FROM THE SERVER>,<br />
"success":<boolean><br />
}<br />
</source><br />
<br />
===op:private===<br />
The payloads of the <tt>op:private</tt> messages contain the real time market information. Each message follows this form:<br />
<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"private",<br />
"private":<MESSAGE_TYPE>,<br />
<MESSAGE_TYPE>:<DATA_PAYLOAD><br />
}<br />
</source><br />
<br />
The <tt>MESSAGE_TYPE</tt> field may take the values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! MESSAGE_TYPE !! Description<br />
|-<br />
| ticker || Ticker messages<br />
|-<br />
| trade || Trades, as they occur<br />
|-<br />
| depth || Orders placed or removed<br />
|-<br />
| result || The result of a websocket-encapsulated [[MtGox/API/HTTP/v1|version 1 HTTP API]] request<br />
|}<br />
<br />
====Ticker====<br />
Ticker messages contain the current inside Bid and Ask as well as daily highs, lows, and volume. The fields contained in the ticker match those defined in the version 1.0 API above. All fields contain <tt>currency</tt>, <tt>display</tt>, <tt>value</tt>, and <tt>value_int</tt> entries.<br />
<source lang="javascript"><br />
{<br />
"channel":"d5f06780-30a8-4a48-a2f8-7ed181b4a13f",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"ticker",<br />
"ticker":{<br />
"avg":{<br />
"currency":"USD",<br />
"display":"$2.26847",<br />
"value":"2.26847",<br />
"value_int":"226847"<br />
},<br />
"buy":{...},<br />
"high":{...},<br />
"last":{..},<br />
"last_local":{...},<br />
"last_orig":{...},<br />
"low":{...},<br />
"sell":{...},<br />
"vol":{<br />
"currency":"BTC",<br />
"display":"118,696.02104208",<br />
"value":"118696.02104208",<br />
"value_int":"11869602104208"<br />
},<br />
"vwap":{...}<br />
}<br />
}<br />
</source><br />
<br />
====Trade====<br />
<source lang="javascript">{<br />
"channel":"dbf1dee9-4f2e-4a08-8cb7-748919a71b21",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"trade",<br />
"trade":{<br />
"amount":2.71,<br />
"amount_int":"271000000",<br />
"date":1310279340,<br />
"item":"BTC",<br />
"price":14.43,<br />
"price_currency":"USD",<br />
"price_int":"1443000",<br />
"primary":"Y",<br />
"properties":"limit, mixed_currency",<br />
"tid":"1310279340877902",<br />
"trade_type":"bid",<br />
"type":"trade"<br />
}<br />
}<br />
</source><br />
<br />
'''trade''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| amount || the traded amount in item (BTC), float, deprecated<br />
|-<br />
| amount_int || the traded amount * 1E8<br />
|-<br />
| date || unix timestamp of trade<br />
|-<br />
| item || What was this trade about<br />
|-<br />
| price || price per unit, float, deprecated<br />
|-<br />
| price_int || price in smallest unit as integer (5 decimals of USD, 3 in case of JPY)<br />
|-<br />
| price_currency || currency in which trade was completed<br />
|-<br />
| properties || "market": the trade ate the whole order, "limit": the trade ate the order partially - some of the original order still exists<br />
|-<br />
| tid || Trade id (big integer, which is in fact trade timestamp in microseconds)<br />
|-<br />
| trade_type || Did this trade result from the execution of a bid or a ask?<br />
|-<br />
|}<br />
<br />
====Depth====<br />
<br />
Changes to the market depth data are broadcast so an up-to-date market depth can be kept by clients.<br />
<br />
<source lang="javascript">{<br />
"channel":"24e67e0d-1cad-4cc0-9e7a-f8523ef460fe",<br />
"depth":{<br />
"currency":"USD",<br />
"item":"BTC",<br />
"now":"1323644358437819",<br />
"price":"14.43",<br />
"price_int":"1443000",<br />
"total_volume_int":"849766000",<br />
"type":1,<br />
"type_str":"ask",<br />
"volume":"-2.71",<br />
"volume_int":"-271000000"<br />
},<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"depth"<br />
}</source><br />
<br />
'''depth''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| currency || the currency affected<br />
|-<br />
| item || the item (BTC)<br />
|-<br />
| price || price as a float, deprecated<br />
|-<br />
| total_volume_int || total volume at this price, after applying the depth update, can be used as a starting point before applying subsequent updates.<br />
|-<br />
| price_int || the price at which volume change happened (5 decimal for USD, 3 for JPY)<br />
|-<br />
| type || 1=ask, 2=bid. deprecated, use type_str<br />
|-<br />
| type_str || type of order at this depth, either "ask" or "bid"<br />
|-<br />
| volume || the volume change as float, deprecated<br />
|-<br />
| volume_int || volume change * 1E8<br />
<br />
|}<br />
<br />
=== op:result ===<br />
<br />
Output from [[MtGox/API/HTTP/v1|HTTP API version 1]] requests that were sent over the websocket.<br />
<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"op":"result",<br />
"result": {<br />
...<br />
}<br />
}</source><br />
<br />
"result" contains the result of the call the same as if it were called over the HTTP API. <REQUEST ID> is the ID chosen by you when the request was sent.<br />
<br />
=== Private per-account messages ===<br />
<br />
The streaming API provides a private channel for every user which receives updates for account-related activities like new orders being created. To subscribe, do:<br />
<br />
<source lang="javascript">{<br />
"op":"mtgox.subscribe",<br />
"key":<PRIVATE_IDKEY><br />
}</source><br />
<br />
<PRIVATE_IDKEY> is obtained by sending a request to [[MtGox/API/HTTP/v1#idKey|generic/private/idkey]] in the version 1 API. Either over HTTP or through the websocket using [[#Authenticated_commands|op:call]].<br />
<br />
==== user_order ====<br />
<br />
user_order is sent when a the status of a user's order is changed.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID><br />
"op": "private".<br />
"origin": "broadcast",<br />
"private": "user_order".<br />
"user_order": {<br />
"amount": {<br />
"currency": "BTC",<br />
"display": "1.00000000\U00a0BTC",<br />
"value": "1.00000000",<br />
"value_int": "100000000"<br />
},<br />
"currency": "USD",<br />
"date": "1330522328",<br />
"item": "BTC",<br />
"oid": <ORDER GUID>,<br />
"price": {<br />
"currency": "USD",<br />
"display": "$10.00000",<br />
"value": "10.00000",<br />
"value_int": "1000000",<br />
};<br />
"status": <ORDER STATUS>,<br />
"type": <ORDER TYPE>,<br />
};<br />
}</source><br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| ORDER STATUS || New status of the order. Valid states are: pending, post-pending, open, executing, invalid, or stop.<br />
|-<br />
| ORDER TYPE || bid or ask<br />
|}<br />
<br />
When an order is cancelled, the "user_order" field only contains the "oid", and none of the other fields.<br />
<br />
==== wallet ====<br />
<br />
Updates to the user's wallet balance.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID>,<br />
"op": "private",<br />
"origin": "broadcast",<br />
"private": "wallet",<br />
"wallet": {<br />
"op": <UPDATE SOURCE>,<br />
"amount": {<br />
...<br />
},<br />
"info": <UPDATE INFO>,<br />
"ref": <REFERENCE CODE>,<br />
"balance": {<br />
...<br />
}<br />
}<br />
}</source><br />
<br />
"..." values above are normal 'amount' type dictionaries containing "currency", "value", "value_int", and "display".<br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| UPDATE SOURCE || Cause of the balance update. Can be: in, out, earned, spent, withdraw, or deposit<br />
|-<br />
| UPDATE INFO || Human readable string describing the update. Same string you see in the account history on mtgox.com<br />
|-<br />
| REFERENCE CODE || Reference code for bank transfers. "null" if this isn't a bank transfer.<br />
|}<br />
<br />
Description of the different source types:<br />
{| class="wikitable"<br />
|-<br />
! Source !! Description<br />
|-<br />
| deposit || Account deposits<br />
|-<br />
| withdraw || Account withdrawals<br />
|-<br />
| earned || Amount of USD gained from a trade<br />
|-<br />
| spent || Amount of USD used for a trade<br />
|-<br />
| in || Amount of BTC gained from a trade<br />
|-<br />
| out || Amount of BTC used for a trade<br />
|-<br />
| fee || MtGox fees<br />
|}<br />
<br />
==== trade ====<br />
<br />
Trades that happen on behalf of a user whose private channel you're subscribed to issue trade messages with the same format as the [[MtGox/API/Streaming#Trade|public trades channel]] does. If you're subscribed to both, be careful not to take both into account during calculations, it might cause some hard to track down issues<br />
<br />
== Outgoing commands==<br />
=== Direct commands ===<br />
<br />
Commands that can be sent without authentication<br />
<br />
==== unsubscribe ====<br />
Stop receiving messages from a channel<br />
<br />
<source lang="javascript">{<br />
"op":"unsubscribe", <br />
"channel":<CHANNEL ID><br />
}</source><br />
<br />
Responds with an identical message to confirm<br />
==== mtgox.subscribe ====<br />
Subscribe to a channel to start receiving messages from it.<br />
<br />
<source lang="javascript">{<br />
"op": "mtgox.subscribe",<br />
"type": "ticker"<br />
}</source><br />
"type" can be ticker, trades, or depth.<br />
<br />
=== Authenticated commands ===<br />
<br />
These commands require an API key and secret pair to sign requests. Any of the [[MtGox/API/HTTP/v1|HTTP API version 1]] methods can be called. Responses are [[MtGox/API/Streaming#op:result|op:result]]<br />
<br />
<source lang="javascript">{<br />
"op":"call",<br />
"id":<REQUEST ID>,<br />
"call":<BASE-64 ENCODED SIGNED REQUEST>,<br />
"context":"mtgox.com"<br />
}</source><br />
<br />
<REQUEST ID> can be any string, it's used to identify the response as belonging to this request when an answer comes back. md5'ing your nonce is a good way to get an id. <br />
<br />
Find a few explanations about tonce and nonce on : https://en.bitcoin.it/wiki/MtGox/API/HTTP#tonce_and_nonce<br />
<br />
"call" must be a base-64 encoded string consisting of, in order: an API key, a signed copy of the request, and the request text itself.<br />
<br />
The queries themselves look like:<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"call":<HTTP API ENDPOINT>,<br />
"nonce":<REQUEST NONCE>,<br />
"params":<REQUEST PARAMETERS>,<br />
"item":"BTC"<br />
}</source><br />
<br />
<HTTP API ENDPOINT> is the last two path components of any version 1 API endpoint, for example [[#Private_info|private/info]], or [[#Cancelled_Trades|public/cancelledtrades]].<br />
<br />
<REQUEST PARAMETERS> is optional for any request that doesn't have parameters.<br />
<br />
The signing process is similar to the HTTP API, but because we can't send headers in an open websocket, the API key and signed request are simply prepended to the actual query data and base64 encoded. Reference implementations are available at https://github.com/MtGox/websocket<br />
<br />
Here's a sample of how to create a valid request in PHP 5.3:<br />
<source lang="php">$nonce = explode(' ', microtime(false));<br />
$nonce = $nonce[1].substr($nonce[0], 2, 6);<br />
$id = md5($nonce); // id can be anything to recognize this call<br />
$query = array('call' => $call, 'params' => $params, 'item' => $item, 'currency' => $currency, 'id' => $id, 'nonce' => $nonce);<br />
$query = json_encode($query);<br />
// generate signature<br />
$sign = hash_hmac('sha512', $query, base64_decode($apiSecret), true);<br />
// prefix signature to query<br />
$query = pack('H*', str_replace('-','',$apiKey)).$sign.$query;<br />
// send query<br />
$call = array('op' => 'call', 'call' => base64_encode($query), 'id' => $id, 'context' => 'mtgox.com');<br />
// $call can now be pushed out to the websocket</source><br />
<br />
== Examples ==<br />
<br />
=== Tickers ===<br />
* https://github.com/ralphtheninja/goxstream (Node.js readable stream)<br />
* https://github.com/ralphtheninja/goxtick (Node.js EventEmitter, works in the browser with browserify)<br />
* https://github.com/cronopio/hook.io-mtgox (Node.js)<br />
* https://github.com/timmolter/XChange (Java)<br />
<br />
=== Websockets ===<br />
* https://github.com/cronopio/hook.io-ws (Node.js)<br />
* https://github.com/progrium/wssh (Python)<br />
* https://github.com/sta/websocket-sharp (C#)<br />
* https://github.com/qsnjason/goxsocketjs (Node.js and HTML5 Browsers - High Level Trading API)<br />
<br />
=== REST ===<br />
* https://github.com/ameen3/node-mtgox-apiv2 (Node.js, supports v2 API)<br />
* https://bitbucket.org/pipe2grep/cryptocoinxchange (C#)<br />
<br />
=== Arbitrage ===<br />
* https://github.com/goteppo/ArBit<br />
* https://github.com/vbmithr/breakbot<br />
<br />
==See Also==<br />
<br />
* [[MtGox/API/HTTP/v1]]<br />
* [http://bitcointalk.org/index.php?topic=164404.0 MtGox API version 2: Unofficial Documentation]<br />
<br />
== References ==<br />
<references></references></div>Jwihttps://tests.bitcoin.it/w/index.php?title=MtGox/API/Streaming&diff=42996MtGox/API/Streaming2013-12-10T16:10:40Z<p>Jwi: /* Websockets */</p>
<hr />
<div>Real time streaming data may be obtained over the streaming API, using [http://socket.io/ Socket.io] or the vanilla websocket<br />
<br />
==websocket channel list ==<br />
<br />
List of the public streaming channels :<br />
<br />
https://mtgox.com/api/2/stream/list_public<br />
<br />
==Connecting==<br />
<br />
* Host: '''websocket.mtgox.com''' or '''socketio.mtgox.com'''<br />
* Port: '''80''' or '''443''' ( ssl )<br />
* Namespace: '''/mtgox''' (Including beginning slash)<br />
<br />
The following JavaScript code establishes a connection in the browser:<br />
<source lang="javascript"><br />
<script src="https://socketio.mtgox.com/socket.io/socket.io.js"></script><br />
<script><br />
var conn = io.connect('https://socketio.mtgox.com/mtgox');<br />
conn.on('message', function(data) {<br />
// Handle incoming data object.<br />
});<br />
</script><br />
</source><br />
<br />
==the Currency parameter ==<br />
use : <br />
websocket.mtgox.com:80/mtgox?Currency=EUR<br />
<br />
and the websocket will give you updates in EUR currency<br />
<br />
if you need need more than one currency, you can add currency symbols like that : <br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD<br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD,CHF<br />
<br />
find the list of currency symbols on https://en.bitcoin.it/wiki/MtGox/API#Currency_Symbols<br />
<br />
The session ID expires after 30 seconds<br />
<br />
==Handling Events==<br />
Socket.io exposes a simple interface for handling events. Handling <tt>message</tt> events is shown above, but there are other events that may be handled:<br />
<source lang="javascript"><br />
conn.on('connect', onConnect);<br />
conn.on('disconnect', onDisconnect);<br />
conn.on('error', onError);<br />
conn.on('message', onMessage);<br />
</source><br />
<br />
==Incoming Data==<br />
Data arrives as a full object instead of as JSON text, eliminating the need to parse the data in the JavaScript handler. Messages that come across the socket to trigger the <tt>message</tt> event will contain the following minimum components:<br />
<source lang="javascript"><br />
{<br />
"op":<OPERATION_TYPE><br />
}<br />
</source><br />
<br />
The <tt>OPERATION_TYPE</tt> field may take these values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! OPERATION_TYPE !! Description<br />
|-<br />
| subscribe || Notification that the user is subscribed to a channel<br />
|-<br />
| unsubscribe || Messages will no longer arrive over the channel<br />
|-<br />
| remark || A server message, usually a warning<br />
|-<br />
| private || The operation for depth, trade, and ticker messages<br />
|-<br />
| result || The response for op:call operations<br />
|}<br />
<br />
===op:subscribe and op:unsubscribe===<br />
The subscribe and unsubscribe message data are very simple, containing the channel and the operation.<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"subscribe" OR "unsubscribe"<br />
}<br />
</source><br />
<br />
Some of the channels are:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Channel ID !! Description<br />
|-<br />
| dbf1dee9-4f2e-4a08-8cb7-748919a71b21 || Trades<br />
|-<br />
| d5f06780-30a8-4a48-a2f8-7ed181b4a13f || Ticker<br />
|-<br />
| 24e67e0d-1cad-4cc0-9e7a-f8523ef460fe || Depth<br />
|}<br />
<br />
===op:remark===<br />
The remark operation contains message and success fields.<br />
<source lang="javascript"><br />
{<br />
"op":"remark",<br />
"message":<MESSAGE FROM THE SERVER>,<br />
"success":<boolean><br />
}<br />
</source><br />
<br />
===op:private===<br />
The payloads of the <tt>op:private</tt> messages contain the real time market information. Each message follows this form:<br />
<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"private",<br />
"private":<MESSAGE_TYPE>,<br />
<MESSAGE_TYPE>:<DATA_PAYLOAD><br />
}<br />
</source><br />
<br />
The <tt>MESSAGE_TYPE</tt> field may take the values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! MESSAGE_TYPE !! Description<br />
|-<br />
| ticker || Ticker messages<br />
|-<br />
| trade || Trades, as they occur<br />
|-<br />
| depth || Orders placed or removed<br />
|-<br />
| result || The result of a websocket-encapsulated [[MtGox/API/HTTP/v1|version 1 HTTP API]] request<br />
|}<br />
<br />
====Ticker====<br />
Ticker messages contain the current inside Bid and Ask as well as daily highs, lows, and volume. The fields contained in the ticker match those defined in the version 1.0 API above. All fields contain <tt>currency</tt>, <tt>display</tt>, <tt>value</tt>, and <tt>value_int</tt> entries.<br />
<source lang="javascript"><br />
{<br />
"channel":"d5f06780-30a8-4a48-a2f8-7ed181b4a13f",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"ticker",<br />
"ticker":{<br />
"avg":{<br />
"currency":"USD",<br />
"display":"$2.26847",<br />
"value":"2.26847",<br />
"value_int":"226847"<br />
},<br />
"buy":{...},<br />
"high":{...},<br />
"last":{..},<br />
"last_local":{...},<br />
"last_orig":{...},<br />
"low":{...},<br />
"sell":{...},<br />
"vol":{<br />
"currency":"BTC",<br />
"display":"118,696.02104208",<br />
"value":"118696.02104208",<br />
"value_int":"11869602104208"<br />
},<br />
"vwap":{...}<br />
}<br />
}<br />
</source><br />
<br />
====Trade====<br />
<source lang="javascript">{<br />
"channel":"dbf1dee9-4f2e-4a08-8cb7-748919a71b21",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"trade",<br />
"trade":{<br />
"amount":2.71,<br />
"amount_int":"271000000",<br />
"date":1310279340,<br />
"item":"BTC",<br />
"price":14.43,<br />
"price_currency":"USD",<br />
"price_int":"1443000",<br />
"primary":"Y",<br />
"properties":"limit, mixed_currency",<br />
"tid":"1310279340877902",<br />
"trade_type":"bid",<br />
"type":"trade"<br />
}<br />
}<br />
</source><br />
<br />
'''trade''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| amount || the traded amount in item (BTC), float, deprecated<br />
|-<br />
| amount_int || the traded amount * 1E8<br />
|-<br />
| date || unix timestamp of trade<br />
|-<br />
| item || What was this trade about<br />
|-<br />
| price || price per unit, float, deprecated<br />
|-<br />
| price_int || price in smallest unit as integer (5 decimals of USD, 3 in case of JPY)<br />
|-<br />
| price_currency || currency in which trade was completed<br />
|-<br />
| properties || "market": the trade ate the whole order, "limit": the trade ate the order partially - some of the original order still exists<br />
|-<br />
| tid || Trade id (big integer, which is in fact trade timestamp in microseconds)<br />
|-<br />
| trade_type || Did this trade result from the execution of a bid or a ask?<br />
|-<br />
|}<br />
<br />
====Depth====<br />
<br />
Changes to the market depth data are broadcast so an up-to-date market depth can be kept by clients.<br />
<br />
<source lang="javascript">{<br />
"channel":"24e67e0d-1cad-4cc0-9e7a-f8523ef460fe",<br />
"depth":{<br />
"currency":"USD",<br />
"item":"BTC",<br />
"now":"1323644358437819",<br />
"price":"14.43",<br />
"price_int":"1443000",<br />
"total_volume_int":"849766000",<br />
"type":1,<br />
"type_str":"ask",<br />
"volume":"-2.71",<br />
"volume_int":"-271000000"<br />
},<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"depth"<br />
}</source><br />
<br />
'''depth''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| currency || the currency affected<br />
|-<br />
| item || the item (BTC)<br />
|-<br />
| price || price as a float, deprecated<br />
|-<br />
| total_volume_int || total volume at this price, after applying the depth update, can be used as a starting point before applying subsequent updates.<br />
|-<br />
| price_int || the price at which volume change happened (5 decimal for USD, 3 for JPY)<br />
|-<br />
| type || 1=ask, 2=bid. deprecated, use type_str<br />
|-<br />
| type_str || type of order at this depth, either "ask" or "bid"<br />
|-<br />
| volume || the volume change as float, deprecated<br />
|-<br />
| volume_int || volume change * 1E8<br />
<br />
|}<br />
<br />
=== op:result ===<br />
<br />
Output from [[MtGox/API/HTTP/v1|HTTP API version 1]] requests that were sent over the websocket.<br />
<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"op":"result",<br />
"result": {<br />
...<br />
}<br />
}</source><br />
<br />
"result" contains the result of the call the same as if it were called over the HTTP API. <REQUEST ID> is the ID chosen by you when the request was sent.<br />
<br />
=== Private per-account messages ===<br />
<br />
The streaming API provides a private channel for every user which receives updates for account-related activities like new orders being created. To subscribe, do:<br />
<br />
<source lang="javascript">{<br />
"op":"mtgox.subscribe",<br />
"key":<PRIVATE_IDKEY><br />
}</source><br />
<br />
<PRIVATE_IDKEY> is obtained by sending a request to [[MtGox/API/HTTP/v1#idKey|generic/private/idkey]] in the version 1 API. Either over HTTP or through the websocket using [[#Authenticated_commands|op:call]].<br />
<br />
==== user_order ====<br />
<br />
user_order is sent when a the status of a user's order is changed.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID><br />
"op": "private".<br />
"origin": "broadcast",<br />
"private": "user_order".<br />
"user_order": {<br />
"amount": {<br />
"currency": "BTC",<br />
"display": "1.00000000\U00a0BTC",<br />
"value": "1.00000000",<br />
"value_int": "100000000"<br />
},<br />
"currency": "USD",<br />
"date": "1330522328",<br />
"item": "BTC",<br />
"oid": <ORDER GUID>,<br />
"price": {<br />
"currency": "USD",<br />
"display": "$10.00000",<br />
"value": "10.00000",<br />
"value_int": "1000000",<br />
};<br />
"status": <ORDER STATUS>,<br />
"type": <ORDER TYPE>,<br />
};<br />
}</source><br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| ORDER STATUS || New status of the order. Valid states are: pending, post-pending, open, executing, invalid, or stop.<br />
|-<br />
| ORDER TYPE || bid or ask<br />
|}<br />
<br />
When an order is cancelled, the "user_order" field only contains the "oid", and none of the other fields.<br />
<br />
==== wallet ====<br />
<br />
Updates to the user's wallet balance.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID>,<br />
"op": "private",<br />
"origin": "broadcast",<br />
"private": "wallet",<br />
"wallet": {<br />
"op": <UPDATE SOURCE>,<br />
"amount": {<br />
...<br />
},<br />
"info": <UPDATE INFO>,<br />
"ref": <REFERENCE CODE>,<br />
"balance": {<br />
...<br />
}<br />
}<br />
}</source><br />
<br />
"..." values above are normal 'amount' type dictionaries containing "currency", "value", "value_int", and "display".<br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| UPDATE SOURCE || Cause of the balance update. Can be: in, out, earned, spent, withdraw, or deposit<br />
|-<br />
| UPDATE INFO || Human readable string describing the update. Same string you see in the account history on mtgox.com<br />
|-<br />
| REFERENCE CODE || Reference code for bank transfers. "null" if this isn't a bank transfer.<br />
|}<br />
<br />
Description of the different source types:<br />
{| class="wikitable"<br />
|-<br />
! Source !! Description<br />
|-<br />
| deposit || Account deposits<br />
|-<br />
| withdraw || Account withdrawals<br />
|-<br />
| earned || Amount of USD gained from a trade<br />
|-<br />
| spent || Amount of USD used for a trade<br />
|-<br />
| in || Amount of BTC gained from a trade<br />
|-<br />
| out || Amount of BTC used for a trade<br />
|-<br />
| fee || MtGox fees<br />
|}<br />
<br />
==== trade ====<br />
<br />
Trades that happen on behalf of a user whose private channel you're subscribed to issue trade messages with the same format as the [[MtGox/API/Streaming#Trade|public trades channel]] does. If you're subscribed to both, be careful not to take both into account during calculations, it might cause some hard to track down issues<br />
<br />
== Outgoing commands==<br />
=== Direct commands ===<br />
<br />
Commands that can be sent without authentication<br />
<br />
==== unsubscribe ====<br />
Stop receiving messages from a channel<br />
<br />
<source lang="javascript">{<br />
"op":"unsubscribe", <br />
"channel":<CHANNEL ID><br />
}</source><br />
<br />
Responds with an identical message to confirm<br />
==== mtgox.subscribe ====<br />
Subscribe to a channel to start receiving messages from it.<br />
<br />
<source lang="javascript">{<br />
"op": "mtgox.subscribe",<br />
"type": "ticker"<br />
}</source><br />
"type" can be ticker, trades, or depth.<br />
<br />
=== Authenticated commands ===<br />
<br />
These commands require an API key and secret pair to sign requests. Any of the [[MtGox/API/HTTP/v1|HTTP API version 1]] methods can be called. Responses are [[MtGox/API/Streaming#op:result|op:result]]<br />
<br />
<source lang="javascript">{<br />
"op":"call",<br />
"id":<REQUEST ID>,<br />
"call":<BASE-64 ENCODED SIGNED REQUEST>,<br />
"context":"mtgox.com"<br />
}</source><br />
<br />
<REQUEST ID> can be any string, it's used to identify the response as belonging to this request when an answer comes back. md5'ing your nonce is a good way to get an id. <br />
<br />
Find a few explanations about tonce and nonce on : https://en.bitcoin.it/wiki/MtGox/API/HTTP#tonce_and_nonce<br />
<br />
"call" must be a base-64 encoded string consisting of, in order: an API key, a signed copy of the request, and the request text itself.<br />
<br />
The queries themselves look like:<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"call":<HTTP API ENDPOINT>,<br />
"nonce":<REQUEST NONCE>,<br />
"params":<REQUEST PARAMETERS>,<br />
"item":"BTC"<br />
}</source><br />
<br />
<HTTP API ENDPOINT> is the last two path components of any version 1 API endpoint, for example [[#Private_info|private/info]], or [[#Cancelled_Trades|public/cancelledtrades]].<br />
<br />
<REQUEST PARAMETERS> is optional for any request that doesn't have parameters.<br />
<br />
The signing process is similar to the HTTP API, but because we can't send headers in an open websocket, the API key and signed request are simply prepended to the actual query data and base64 encoded. Reference implementations are available at https://github.com/MtGox/websocket<br />
<br />
Here's a sample of how to create a valid request in PHP 5.3:<br />
<source lang="php">$nonce = explode(' ', microtime(false));<br />
$nonce = $nonce[1].substr($nonce[0], 2, 6);<br />
$id = md5($nonce); // id can be anything to recognize this call<br />
$query = array('call' => $call, 'params' => $params, 'item' => $item, 'currency' => $currency, 'id' => $id, 'nonce' => $nonce);<br />
$query = json_encode($query);<br />
// generate signature<br />
$sign = hash_hmac('sha512', $query, base64_decode($apiSecret), true);<br />
// prefix signature to query<br />
$query = pack('H*', str_replace('-','',$apiKey)).$sign.$query;<br />
// send query<br />
$call = array('op' => 'call', 'call' => base64_encode($query), 'id' => $id, 'context' => 'mtgox.com');<br />
// $call can now be pushed out to the websocket</source><br />
<br />
== Examples ==<br />
<br />
=== Tickers ===<br />
* https://github.com/ralphtheninja/goxstream (Node.js readable stream)<br />
* https://github.com/ralphtheninja/goxtick (Node.js EventEmitter, works in the browser with browserify)<br />
* https://github.com/cronopio/hook.io-mtgox (Node.js)<br />
* https://github.com/timmolter/XChange (Java)<br />
<br />
=== Websockets ===<br />
* https://github.com/cronopio/hook.io-ws (Node.js)<br />
* https://github.com/progrium/wssh (Python)<br />
* https://github.com/sta/websocket-sharp (C#)<br />
* https://github.com/qsnjason/goxsocketjs (Javascript/Node.js/HTML5 Browsers/Account and Trading API)<br />
<br />
=== REST ===<br />
* https://github.com/ameen3/node-mtgox-apiv2 (Node.js, supports v2 API)<br />
* https://bitbucket.org/pipe2grep/cryptocoinxchange (C#)<br />
<br />
=== Arbitrage ===<br />
* https://github.com/goteppo/ArBit<br />
* https://github.com/vbmithr/breakbot<br />
<br />
==See Also==<br />
<br />
* [[MtGox/API/HTTP/v1]]<br />
* [http://bitcointalk.org/index.php?topic=164404.0 MtGox API version 2: Unofficial Documentation]<br />
<br />
== References ==<br />
<references></references></div>Jwihttps://tests.bitcoin.it/w/index.php?title=MtGox/API/Streaming&diff=41646MtGox/API/Streaming2013-10-09T10:58:55Z<p>Jwi: /* Websockets */</p>
<hr />
<div>Real time streaming data may be obtained over the streaming API, using [http://socket.io/ Socket.io] or the vanilla websocket<br />
<br />
==websocket channel list ==<br />
<br />
List of the public streaming channels :<br />
<br />
https://mtgox.com/api/2/stream/list_public<br />
<br />
==Connecting==<br />
<br />
* Host: '''websocket.mtgox.com''' or '''socketio.mtgox.com'''<br />
* Port: '''80''' or '''443''' ( ssl )<br />
* Namespace: '''/mtgox''' (Including beginning slash)<br />
<br />
The following JavaScript code establishes a connection in the browser:<br />
<source lang="javascript"><br />
<script src="https://socketio.mtgox.com/socket.io/socket.io.js"></script><br />
<script><br />
var conn = io.connect('https://socketio.mtgox.com/mtgox');<br />
conn.on('message', function(data) {<br />
// Handle incoming data object.<br />
});<br />
</script><br />
</source><br />
<br />
==the Currency parameter ==<br />
use : <br />
websocket.mtgox.com:80/mtgox?Currency=EUR<br />
<br />
and the websocket will give you updates in EUR currency<br />
<br />
if you need need more than one currency, you can add currency symbols like that : <br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD<br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD,CHF<br />
<br />
find the list of currency symbols on https://en.bitcoin.it/wiki/MtGox/API#Currency_Symbols<br />
<br />
The session ID expires after 30 seconds<br />
<br />
==Handling Events==<br />
Socket.io exposes a simple interface for handling events. Handling <tt>message</tt> events is shown above, but there are other events that may be handled:<br />
<source lang="javascript"><br />
conn.on('connect', onConnect);<br />
conn.on('disconnect', onDisconnect);<br />
conn.on('error', onError);<br />
conn.on('message', onMessage);<br />
</source><br />
<br />
==Incoming Data==<br />
Data arrives as a full object instead of as JSON text, eliminating the need to parse the data in the JavaScript handler. Messages that come across the socket to trigger the <tt>message</tt> event will contain the following minimum components:<br />
<source lang="javascript"><br />
{<br />
"op":<OPERATION_TYPE><br />
}<br />
</source><br />
<br />
The <tt>OPERATION_TYPE</tt> field may take these values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! OPERATION_TYPE !! Description<br />
|-<br />
| subscribe || Notification that the user is subscribed to a channel<br />
|-<br />
| unsubscribe || Messages will no longer arrive over the channel<br />
|-<br />
| remark || A server message, usually a warning<br />
|-<br />
| private || The operation for depth, trade, and ticker messages<br />
|-<br />
| result || The response for op:call operations<br />
|}<br />
<br />
===op:subscribe and op:unsubscribe===<br />
The subscribe and unsubscribe message data are very simple, containing the channel and the operation.<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"subscribe" OR "unsubscribe"<br />
}<br />
</source><br />
<br />
Some of the channels are:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Channel ID !! Description<br />
|-<br />
| dbf1dee9-4f2e-4a08-8cb7-748919a71b21 || Trades<br />
|-<br />
| d5f06780-30a8-4a48-a2f8-7ed181b4a13f || Ticker<br />
|-<br />
| 24e67e0d-1cad-4cc0-9e7a-f8523ef460fe || Depth<br />
|}<br />
<br />
===op:remark===<br />
The remark operation contains message and success fields.<br />
<source lang="javascript"><br />
{<br />
"op":"remark",<br />
"message":<MESSAGE FROM THE SERVER>,<br />
"success":<boolean><br />
}<br />
</source><br />
<br />
===op:private===<br />
The payloads of the <tt>op:private</tt> messages contain the real time market information. Each message follows this form:<br />
<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"private",<br />
"private":<MESSAGE_TYPE>,<br />
<MESSAGE_TYPE>:<DATA_PAYLOAD><br />
}<br />
</source><br />
<br />
The <tt>MESSAGE_TYPE</tt> field may take the values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! MESSAGE_TYPE !! Description<br />
|-<br />
| ticker || Ticker messages<br />
|-<br />
| trade || Trades, as they occur<br />
|-<br />
| depth || Orders placed or removed<br />
|-<br />
| result || The result of a websocket-encapsulated [[MtGox/API/HTTP/v1|version 1 HTTP API]] request<br />
|}<br />
<br />
====Ticker====<br />
Ticker messages contain the current inside Bid and Ask as well as daily highs, lows, and volume. The fields contained in the ticker match those defined in the version 1.0 API above. All fields contain <tt>currency</tt>, <tt>display</tt>, <tt>value</tt>, and <tt>value_int</tt> entries.<br />
<source lang="javascript"><br />
{<br />
"channel":"d5f06780-30a8-4a48-a2f8-7ed181b4a13f",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"ticker",<br />
"ticker":{<br />
"avg":{<br />
"currency":"USD",<br />
"display":"$2.26847",<br />
"value":"2.26847",<br />
"value_int":"226847"<br />
},<br />
"buy":{...},<br />
"high":{...},<br />
"last":{..},<br />
"last_local":{...},<br />
"last_orig":{...},<br />
"low":{...},<br />
"sell":{...},<br />
"vol":{<br />
"currency":"BTC",<br />
"display":"118,696.02104208",<br />
"value":"118696.02104208",<br />
"value_int":"11869602104208"<br />
},<br />
"vwap":{...}<br />
}<br />
}<br />
</source><br />
<br />
====Trade====<br />
<source lang="javascript">{<br />
"channel":"dbf1dee9-4f2e-4a08-8cb7-748919a71b21",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"trade",<br />
"trade":{<br />
"amount":2.71,<br />
"amount_int":"271000000",<br />
"date":1310279340,<br />
"item":"BTC",<br />
"price":14.43,<br />
"price_currency":"USD",<br />
"price_int":"1443000",<br />
"primary":"Y",<br />
"properties":"limit, mixed_currency",<br />
"tid":"1310279340877902",<br />
"trade_type":"bid",<br />
"type":"trade"<br />
}<br />
}<br />
</source><br />
<br />
'''trade''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| amount || the traded amount in item (BTC), float, deprecated<br />
|-<br />
| amount_int || the traded amount * 1E8<br />
|-<br />
| date || unix timestamp of trade<br />
|-<br />
| item || What was this trade about<br />
|-<br />
| price || price per unit, float, deprecated<br />
|-<br />
| price_int || price in smallest unit as integer (5 decimals of USD, 3 in case of JPY)<br />
|-<br />
| price_currency || currency in which trade was completed<br />
|-<br />
| properties || "market": the trade ate the whole order, "limit": the trade ate the order partially - some of the original order still exists<br />
|-<br />
| tid || Trade id (big integer, which is in fact trade timestamp in microseconds)<br />
|-<br />
| trade_type || Did this trade result from the execution of a bid or a ask?<br />
|-<br />
|}<br />
<br />
====Depth====<br />
<br />
Changes to the market depth data are broadcast so an up-to-date market depth can be kept by clients.<br />
<br />
<source lang="javascript">{<br />
"channel":"24e67e0d-1cad-4cc0-9e7a-f8523ef460fe",<br />
"depth":{<br />
"currency":"USD",<br />
"item":"BTC",<br />
"now":"1323644358437819",<br />
"price":"14.43",<br />
"price_int":"1443000",<br />
"total_volume_int":"849766000",<br />
"type":1,<br />
"type_str":"ask",<br />
"volume":"-2.71",<br />
"volume_int":"-271000000"<br />
},<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"depth"<br />
}</source><br />
<br />
'''depth''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| currency || the currency affected<br />
|-<br />
| item || the item (BTC)<br />
|-<br />
| price || price as a float, deprecated<br />
|-<br />
| total_volume_int || total volume at this price, after applying the depth update, can be used as a starting point before applying subsequent updates.<br />
|-<br />
| price_int || the price at which volume change happened (5 decimal for USD, 3 for JPY)<br />
|-<br />
| type || 1=ask, 2=bid. deprecated, use type_str<br />
|-<br />
| type_str || type of order at this depth, either "ask" or "bid"<br />
|-<br />
| volume || the volume change as float, deprecated<br />
|-<br />
| volume_int || volume change * 1E8<br />
<br />
|}<br />
<br />
=== op:result ===<br />
<br />
Output from [[MtGox/API/HTTP/v1|HTTP API version 1]] requests that were sent over the websocket.<br />
<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"op":"result",<br />
"result": {<br />
...<br />
}<br />
}</source><br />
<br />
"result" contains the result of the call the same as if it were called over the HTTP API. <REQUEST ID> is the ID chosen by you when the request was sent.<br />
<br />
=== Private per-account messages ===<br />
<br />
The streaming API provides a private channel for every user which receives updates for account-related activities like new orders being created. To subscribe, do:<br />
<br />
<source lang="javascript">{<br />
"op":"mtgox.subscribe",<br />
"key":<PRIVATE_IDKEY><br />
}</source><br />
<br />
<PRIVATE_IDKEY> is obtained by sending a request to [[MtGox/API/HTTP/v1#idKey|generic/private/idkey]] in the version 1 API. Either over HTTP or through the websocket using [[#Authenticated_commands|op:call]].<br />
<br />
==== user_order ====<br />
<br />
user_order is sent when a the status of a user's order is changed.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID><br />
"op": "private".<br />
"origin": "broadcast",<br />
"private": "user_order".<br />
"user_order": {<br />
"amount": {<br />
"currency": "BTC",<br />
"display": "1.00000000\U00a0BTC",<br />
"value": "1.00000000",<br />
"value_int": "100000000"<br />
},<br />
"currency": "USD",<br />
"date": "1330522328",<br />
"item": "BTC",<br />
"oid": <ORDER GUID>,<br />
"price": {<br />
"currency": "USD",<br />
"display": "$10.00000",<br />
"value": "10.00000",<br />
"value_int": "1000000",<br />
};<br />
"status": <ORDER STATUS>,<br />
"type": <ORDER TYPE>,<br />
};<br />
}</source><br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| ORDER STATUS || New status of the order. Valid states are: pending, post-pending, open, executing, invalid, or stop.<br />
|-<br />
| ORDER TYPE || bid or ask<br />
|}<br />
<br />
When an order is cancelled, the "user_order" field only contains the "oid", and none of the other fields.<br />
<br />
==== wallet ====<br />
<br />
Updates to the user's wallet balance.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID>,<br />
"op": "private",<br />
"origin": "broadcast",<br />
"private": "wallet",<br />
"wallet": {<br />
"op": <UPDATE SOURCE>,<br />
"amount": {<br />
...<br />
},<br />
"info": <UPDATE INFO>,<br />
"ref": <REFERENCE CODE>,<br />
"balance": {<br />
...<br />
}<br />
}<br />
}</source><br />
<br />
"..." values above are normal 'amount' type dictionaries containing "currency", "value", "value_int", and "display".<br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| UPDATE SOURCE || Cause of the balance update. Can be: in, out, earned, spent, withdraw, or deposit<br />
|-<br />
| UPDATE INFO || Human readable string describing the update. Same string you see in the account history on mtgox.com<br />
|-<br />
| REFERENCE CODE || Reference code for bank transfers. "null" if this isn't a bank transfer.<br />
|}<br />
<br />
Description of the different source types:<br />
{| class="wikitable"<br />
|-<br />
! Source !! Description<br />
|-<br />
| deposit || Account deposits<br />
|-<br />
| withdraw || Account withdrawals<br />
|-<br />
| earned || Amount of USD gained from a trade<br />
|-<br />
| spent || Amount of USD used for a trade<br />
|-<br />
| in || Amount of BTC gained from a trade<br />
|-<br />
| out || Amount of BTC used for a trade<br />
|-<br />
| fee || MtGox fees<br />
|}<br />
<br />
==== trade ====<br />
<br />
Trades that happen on behalf of a user whose private channel you're subscribed to issue trade messages with the same format as the [[MtGox/API/Streaming#Trade|public trades channel]] does. If you're subscribed to both, be careful not to take both into account during calculations, it might cause some hard to track down issues<br />
<br />
== Outgoing commands==<br />
=== Direct commands ===<br />
<br />
Commands that can be sent without authentication<br />
<br />
==== unsubscribe ====<br />
Stop receiving messages from a channel<br />
<br />
<source lang="javascript">{<br />
"op":"unsubscribe", <br />
"channel":<CHANNEL ID><br />
}</source><br />
<br />
Responds with an identical message to confirm<br />
==== mtgox.subscribe ====<br />
Subscribe to a channel to start receiving messages from it.<br />
<br />
<source lang="javascript">{<br />
"op": "mtgox.subscribe",<br />
"type": "ticker"<br />
}</source><br />
"type" can be ticker, trades, or depth.<br />
<br />
=== Authenticated commands ===<br />
<br />
These commands require an API key and secret pair to sign requests. Any of the [[MtGox/API/HTTP/v1|HTTP API version 1]] methods can be called. Responses are [[MtGox/API/Streaming#op:result|op:result]]<br />
<br />
<source lang="javascript">{<br />
"op":"call",<br />
"id":<REQUEST ID>,<br />
"call":<BASE-64 ENCODED SIGNED REQUEST>,<br />
"context":"mtgox.com"<br />
}</source><br />
<br />
<REQUEST ID> can be any string, it's used to identify the response as belonging to this request when an answer comes back. md5'ing your nonce is a good way to get an id. <br />
<br />
Find a few explanations about tonce and nonce on : https://en.bitcoin.it/wiki/MtGox/API/HTTP#tonce_and_nonce<br />
<br />
"call" must be a base-64 encoded string consisting of, in order: an API key, a signed copy of the request, and the request text itself.<br />
<br />
The queries themselves look like:<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"call":<HTTP API ENDPOINT>,<br />
"nonce":<REQUEST NONCE>,<br />
"params":<REQUEST PARAMETERS>,<br />
"item":"BTC"<br />
}</source><br />
<br />
<HTTP API ENDPOINT> is the last two path components of any version 1 API endpoint, for example [[#Private_info|private/info]], or [[#Cancelled_Trades|public/cancelledtrades]].<br />
<br />
<REQUEST PARAMETERS> is optional for any request that doesn't have parameters.<br />
<br />
The signing process is similar to the HTTP API, but because we can't send headers in an open websocket, the API key and signed request are simply prepended to the actual query data and base64 encoded. Reference implementations are available at https://github.com/MtGox/websocket<br />
<br />
Here's a sample of how to create a valid request in PHP 5.3:<br />
<source lang="php">$nonce = explode(' ', microtime(false));<br />
$nonce = $nonce[1].substr($nonce[0], 2, 6);<br />
$id = md5($nonce); // id can be anything to recognize this call<br />
$query = array('call' => $call, 'params' => $params, 'item' => $item, 'currency' => $currency, 'id' => $id, 'nonce' => $nonce);<br />
$query = json_encode($query);<br />
// generate signature<br />
$sign = hash_hmac('sha512', $query, base64_decode($apiSecret), true);<br />
// prefix signature to query<br />
$query = pack('H*', str_replace('-','',$apiKey)).$sign.$query;<br />
// send query<br />
$call = array('op' => 'call', 'call' => base64_encode($query), 'id' => $id, 'context' => 'mtgox.com');<br />
// $call can now be pushed out to the websocket</source><br />
<br />
== Examples ==<br />
<br />
=== Tickers ===<br />
* https://github.com/ralphtheninja/goxstream (Node.js readable stream)<br />
* https://github.com/ralphtheninja/goxtick (Node.js EventEmitter, works in the browser with browserify)<br />
* https://github.com/cronopio/hook.io-mtgox (Node.js)<br />
* https://github.com/timmolter/XChange (Java)<br />
<br />
=== Websockets ===<br />
* https://github.com/cronopio/hook.io-ws (Node.js)<br />
* https://github.com/progrium/wssh (Python)<br />
* https://github.com/sta/websocket-sharp (C#)<br />
* https://github.com/qsnjason/goxsocketjs (Javascript/HTML5 Browsers)<br />
<br />
=== REST ===<br />
* https://github.com/ameen3/node-mtgox-apiv2 (Node.js, supports v2 API)<br />
* https://bitbucket.org/pipe2grep/cryptocoinxchange (C#)<br />
<br />
=== Arbitrage ===<br />
* https://github.com/goteppo/ArBit<br />
* https://github.com/vbmithr/breakbot<br />
<br />
==See Also==<br />
<br />
* [[MtGox/API/HTTP/v1]]<br />
* [http://bitcointalk.org/index.php?topic=164404.0 MtGox API version 2: Unofficial Documentation]<br />
<br />
== References ==<br />
<references></references></div>Jwihttps://tests.bitcoin.it/w/index.php?title=MtGox/API/Streaming&diff=41622MtGox/API/Streaming2013-10-09T10:09:24Z<p>Jwi: /* Websockets */</p>
<hr />
<div>Real time streaming data may be obtained over the streaming API, using [http://socket.io/ Socket.io] or the vanilla websocket<br />
<br />
==websocket channel list ==<br />
<br />
List of the public streaming channels :<br />
<br />
https://mtgox.com/api/2/stream/list_public<br />
<br />
==Connecting==<br />
<br />
* Host: '''websocket.mtgox.com''' or '''socketio.mtgox.com'''<br />
* Port: '''80''' or '''443''' ( ssl )<br />
* Namespace: '''/mtgox''' (Including beginning slash)<br />
<br />
The following JavaScript code establishes a connection in the browser:<br />
<source lang="javascript"><br />
<script src="https://socketio.mtgox.com/socket.io/socket.io.js"></script><br />
<script><br />
var conn = io.connect('https://socketio.mtgox.com/mtgox');<br />
conn.on('message', function(data) {<br />
// Handle incoming data object.<br />
});<br />
</script><br />
</source><br />
<br />
==the Currency parameter ==<br />
use : <br />
websocket.mtgox.com:80/mtgox?Currency=EUR<br />
<br />
and the websocket will give you updates in EUR currency<br />
<br />
if you need need more than one currency, you can add currency symbols like that : <br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD<br />
<br />
websocket.mtgox.com:80/mtgox?Currency=EUR,USD,CHF<br />
<br />
find the list of currency symbols on https://en.bitcoin.it/wiki/MtGox/API#Currency_Symbols<br />
<br />
The session ID expires after 30 seconds<br />
<br />
==Handling Events==<br />
Socket.io exposes a simple interface for handling events. Handling <tt>message</tt> events is shown above, but there are other events that may be handled:<br />
<source lang="javascript"><br />
conn.on('connect', onConnect);<br />
conn.on('disconnect', onDisconnect);<br />
conn.on('error', onError);<br />
conn.on('message', onMessage);<br />
</source><br />
<br />
==Incoming Data==<br />
Data arrives as a full object instead of as JSON text, eliminating the need to parse the data in the JavaScript handler. Messages that come across the socket to trigger the <tt>message</tt> event will contain the following minimum components:<br />
<source lang="javascript"><br />
{<br />
"op":<OPERATION_TYPE><br />
}<br />
</source><br />
<br />
The <tt>OPERATION_TYPE</tt> field may take these values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! OPERATION_TYPE !! Description<br />
|-<br />
| subscribe || Notification that the user is subscribed to a channel<br />
|-<br />
| unsubscribe || Messages will no longer arrive over the channel<br />
|-<br />
| remark || A server message, usually a warning<br />
|-<br />
| private || The operation for depth, trade, and ticker messages<br />
|-<br />
| result || The response for op:call operations<br />
|}<br />
<br />
===op:subscribe and op:unsubscribe===<br />
The subscribe and unsubscribe message data are very simple, containing the channel and the operation.<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"subscribe" OR "unsubscribe"<br />
}<br />
</source><br />
<br />
Some of the channels are:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Channel ID !! Description<br />
|-<br />
| dbf1dee9-4f2e-4a08-8cb7-748919a71b21 || Trades<br />
|-<br />
| d5f06780-30a8-4a48-a2f8-7ed181b4a13f || Ticker<br />
|-<br />
| 24e67e0d-1cad-4cc0-9e7a-f8523ef460fe || Depth<br />
|}<br />
<br />
===op:remark===<br />
The remark operation contains message and success fields.<br />
<source lang="javascript"><br />
{<br />
"op":"remark",<br />
"message":<MESSAGE FROM THE SERVER>,<br />
"success":<boolean><br />
}<br />
</source><br />
<br />
===op:private===<br />
The payloads of the <tt>op:private</tt> messages contain the real time market information. Each message follows this form:<br />
<br />
<source lang="javascript"><br />
{<br />
"channel":<CHANNEL_ID>,<br />
"op":"private",<br />
"private":<MESSAGE_TYPE>,<br />
<MESSAGE_TYPE>:<DATA_PAYLOAD><br />
}<br />
</source><br />
<br />
The <tt>MESSAGE_TYPE</tt> field may take the values:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! MESSAGE_TYPE !! Description<br />
|-<br />
| ticker || Ticker messages<br />
|-<br />
| trade || Trades, as they occur<br />
|-<br />
| depth || Orders placed or removed<br />
|-<br />
| result || The result of a websocket-encapsulated [[MtGox/API/HTTP/v1|version 1 HTTP API]] request<br />
|}<br />
<br />
====Ticker====<br />
Ticker messages contain the current inside Bid and Ask as well as daily highs, lows, and volume. The fields contained in the ticker match those defined in the version 1.0 API above. All fields contain <tt>currency</tt>, <tt>display</tt>, <tt>value</tt>, and <tt>value_int</tt> entries.<br />
<source lang="javascript"><br />
{<br />
"channel":"d5f06780-30a8-4a48-a2f8-7ed181b4a13f",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"ticker",<br />
"ticker":{<br />
"avg":{<br />
"currency":"USD",<br />
"display":"$2.26847",<br />
"value":"2.26847",<br />
"value_int":"226847"<br />
},<br />
"buy":{...},<br />
"high":{...},<br />
"last":{..},<br />
"last_local":{...},<br />
"last_orig":{...},<br />
"low":{...},<br />
"sell":{...},<br />
"vol":{<br />
"currency":"BTC",<br />
"display":"118,696.02104208",<br />
"value":"118696.02104208",<br />
"value_int":"11869602104208"<br />
},<br />
"vwap":{...}<br />
}<br />
}<br />
</source><br />
<br />
====Trade====<br />
<source lang="javascript">{<br />
"channel":"dbf1dee9-4f2e-4a08-8cb7-748919a71b21",<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"trade",<br />
"trade":{<br />
"amount":2.71,<br />
"amount_int":"271000000",<br />
"date":1310279340,<br />
"item":"BTC",<br />
"price":14.43,<br />
"price_currency":"USD",<br />
"price_int":"1443000",<br />
"primary":"Y",<br />
"properties":"limit, mixed_currency",<br />
"tid":"1310279340877902",<br />
"trade_type":"bid",<br />
"type":"trade"<br />
}<br />
}<br />
</source><br />
<br />
'''trade''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| amount || the traded amount in item (BTC), float, deprecated<br />
|-<br />
| amount_int || the traded amount * 1E8<br />
|-<br />
| date || unix timestamp of trade<br />
|-<br />
| item || What was this trade about<br />
|-<br />
| price || price per unit, float, deprecated<br />
|-<br />
| price_int || price in smallest unit as integer (5 decimals of USD, 3 in case of JPY)<br />
|-<br />
| price_currency || currency in which trade was completed<br />
|-<br />
| properties || "market": the trade ate the whole order, "limit": the trade ate the order partially - some of the original order still exists<br />
|-<br />
| tid || Trade id (big integer, which is in fact trade timestamp in microseconds)<br />
|-<br />
| trade_type || Did this trade result from the execution of a bid or a ask?<br />
|-<br />
|}<br />
<br />
====Depth====<br />
<br />
Changes to the market depth data are broadcast so an up-to-date market depth can be kept by clients.<br />
<br />
<source lang="javascript">{<br />
"channel":"24e67e0d-1cad-4cc0-9e7a-f8523ef460fe",<br />
"depth":{<br />
"currency":"USD",<br />
"item":"BTC",<br />
"now":"1323644358437819",<br />
"price":"14.43",<br />
"price_int":"1443000",<br />
"total_volume_int":"849766000",<br />
"type":1,<br />
"type_str":"ask",<br />
"volume":"-2.71",<br />
"volume_int":"-271000000"<br />
},<br />
"op":"private",<br />
"origin":"broadcast",<br />
"private":"depth"<br />
}</source><br />
<br />
'''depth''' contains the following:<br />
<br />
:{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| currency || the currency affected<br />
|-<br />
| item || the item (BTC)<br />
|-<br />
| price || price as a float, deprecated<br />
|-<br />
| total_volume_int || total volume at this price, after applying the depth update, can be used as a starting point before applying subsequent updates.<br />
|-<br />
| price_int || the price at which volume change happened (5 decimal for USD, 3 for JPY)<br />
|-<br />
| type || 1=ask, 2=bid. deprecated, use type_str<br />
|-<br />
| type_str || type of order at this depth, either "ask" or "bid"<br />
|-<br />
| volume || the volume change as float, deprecated<br />
|-<br />
| volume_int || volume change * 1E8<br />
<br />
|}<br />
<br />
=== op:result ===<br />
<br />
Output from [[MtGox/API/HTTP/v1|HTTP API version 1]] requests that were sent over the websocket.<br />
<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"op":"result",<br />
"result": {<br />
...<br />
}<br />
}</source><br />
<br />
"result" contains the result of the call the same as if it were called over the HTTP API. <REQUEST ID> is the ID chosen by you when the request was sent.<br />
<br />
=== Private per-account messages ===<br />
<br />
The streaming API provides a private channel for every user which receives updates for account-related activities like new orders being created. To subscribe, do:<br />
<br />
<source lang="javascript">{<br />
"op":"mtgox.subscribe",<br />
"key":<PRIVATE_IDKEY><br />
}</source><br />
<br />
<PRIVATE_IDKEY> is obtained by sending a request to [[MtGox/API/HTTP/v1#idKey|generic/private/idkey]] in the version 1 API. Either over HTTP or through the websocket using [[#Authenticated_commands|op:call]].<br />
<br />
==== user_order ====<br />
<br />
user_order is sent when a the status of a user's order is changed.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID><br />
"op": "private".<br />
"origin": "broadcast",<br />
"private": "user_order".<br />
"user_order": {<br />
"amount": {<br />
"currency": "BTC",<br />
"display": "1.00000000\U00a0BTC",<br />
"value": "1.00000000",<br />
"value_int": "100000000"<br />
},<br />
"currency": "USD",<br />
"date": "1330522328",<br />
"item": "BTC",<br />
"oid": <ORDER GUID>,<br />
"price": {<br />
"currency": "USD",<br />
"display": "$10.00000",<br />
"value": "10.00000",<br />
"value_int": "1000000",<br />
};<br />
"status": <ORDER STATUS>,<br />
"type": <ORDER TYPE>,<br />
};<br />
}</source><br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| ORDER STATUS || New status of the order. Valid states are: pending, post-pending, open, executing, invalid, or stop.<br />
|-<br />
| ORDER TYPE || bid or ask<br />
|}<br />
<br />
When an order is cancelled, the "user_order" field only contains the "oid", and none of the other fields.<br />
<br />
==== wallet ====<br />
<br />
Updates to the user's wallet balance.<br />
<br />
<source lang="javascript">{<br />
"channel": <USER CHANNEL GUID>,<br />
"op": "private",<br />
"origin": "broadcast",<br />
"private": "wallet",<br />
"wallet": {<br />
"op": <UPDATE SOURCE>,<br />
"amount": {<br />
...<br />
},<br />
"info": <UPDATE INFO>,<br />
"ref": <REFERENCE CODE>,<br />
"balance": {<br />
...<br />
}<br />
}<br />
}</source><br />
<br />
"..." values above are normal 'amount' type dictionaries containing "currency", "value", "value_int", and "display".<br />
<br />
{| class="wikitable"<br />
|-<br />
! Name !! Value<br />
|-<br />
| USER CHANNEL GUID || GUID of private user channel<br />
|-<br />
| ORDER GUID || GUID of order<br />
|-<br />
| UPDATE SOURCE || Cause of the balance update. Can be: in, out, earned, spent, withdraw, or deposit<br />
|-<br />
| UPDATE INFO || Human readable string describing the update. Same string you see in the account history on mtgox.com<br />
|-<br />
| REFERENCE CODE || Reference code for bank transfers. "null" if this isn't a bank transfer.<br />
|}<br />
<br />
Description of the different source types:<br />
{| class="wikitable"<br />
|-<br />
! Source !! Description<br />
|-<br />
| deposit || Account deposits<br />
|-<br />
| withdraw || Account withdrawals<br />
|-<br />
| earned || Amount of USD gained from a trade<br />
|-<br />
| spent || Amount of USD used for a trade<br />
|-<br />
| in || Amount of BTC gained from a trade<br />
|-<br />
| out || Amount of BTC used for a trade<br />
|-<br />
| fee || MtGox fees<br />
|}<br />
<br />
==== trade ====<br />
<br />
Trades that happen on behalf of a user whose private channel you're subscribed to issue trade messages with the same format as the [[MtGox/API/Streaming#Trade|public trades channel]] does. If you're subscribed to both, be careful not to take both into account during calculations, it might cause some hard to track down issues<br />
<br />
== Outgoing commands==<br />
=== Direct commands ===<br />
<br />
Commands that can be sent without authentication<br />
<br />
==== unsubscribe ====<br />
Stop receiving messages from a channel<br />
<br />
<source lang="javascript">{<br />
"op":"unsubscribe", <br />
"channel":<CHANNEL ID><br />
}</source><br />
<br />
Responds with an identical message to confirm<br />
==== mtgox.subscribe ====<br />
Subscribe to a channel to start receiving messages from it.<br />
<br />
<source lang="javascript">{<br />
"op": "mtgox.subscribe",<br />
"type": "ticker"<br />
}</source><br />
"type" can be ticker, trades, or depth.<br />
<br />
=== Authenticated commands ===<br />
<br />
These commands require an API key and secret pair to sign requests. Any of the [[MtGox/API/HTTP/v1|HTTP API version 1]] methods can be called. Responses are [[MtGox/API/Streaming#op:result|op:result]]<br />
<br />
<source lang="javascript">{<br />
"op":"call",<br />
"id":<REQUEST ID>,<br />
"call":<BASE-64 ENCODED SIGNED REQUEST>,<br />
"context":"mtgox.com"<br />
}</source><br />
<br />
<REQUEST ID> can be any string, it's used to identify the response as belonging to this request when an answer comes back. md5'ing your nonce is a good way to get an id. <br />
<br />
Find a few explanations about tonce and nonce on : https://en.bitcoin.it/wiki/MtGox/API/HTTP#tonce_and_nonce<br />
<br />
"call" must be a base-64 encoded string consisting of, in order: an API key, a signed copy of the request, and the request text itself.<br />
<br />
The queries themselves look like:<br />
<source lang="javascript">{<br />
"id":<REQUEST ID>,<br />
"call":<HTTP API ENDPOINT>,<br />
"nonce":<REQUEST NONCE>,<br />
"params":<REQUEST PARAMETERS>,<br />
"item":"BTC"<br />
}</source><br />
<br />
<HTTP API ENDPOINT> is the last two path components of any version 1 API endpoint, for example [[#Private_info|private/info]], or [[#Cancelled_Trades|public/cancelledtrades]].<br />
<br />
<REQUEST PARAMETERS> is optional for any request that doesn't have parameters.<br />
<br />
The signing process is similar to the HTTP API, but because we can't send headers in an open websocket, the API key and signed request are simply prepended to the actual query data and base64 encoded. Reference implementations are available at https://github.com/MtGox/websocket<br />
<br />
Here's a sample of how to create a valid request in PHP 5.3:<br />
<source lang="php">$nonce = explode(' ', microtime(false));<br />
$nonce = $nonce[1].substr($nonce[0], 2, 6);<br />
$id = md5($nonce); // id can be anything to recognize this call<br />
$query = array('call' => $call, 'params' => $params, 'item' => $item, 'currency' => $currency, 'id' => $id, 'nonce' => $nonce);<br />
$query = json_encode($query);<br />
// generate signature<br />
$sign = hash_hmac('sha512', $query, base64_decode($apiSecret), true);<br />
// prefix signature to query<br />
$query = pack('H*', str_replace('-','',$apiKey)).$sign.$query;<br />
// send query<br />
$call = array('op' => 'call', 'call' => base64_encode($query), 'id' => $id, 'context' => 'mtgox.com');<br />
// $call can now be pushed out to the websocket</source><br />
<br />
== Examples ==<br />
<br />
=== Tickers ===<br />
* https://github.com/ralphtheninja/goxstream (Node.js readable stream)<br />
* https://github.com/ralphtheninja/goxtick (Node.js EventEmitter, works in the browser with browserify)<br />
* https://github.com/cronopio/hook.io-mtgox (Node.js)<br />
* https://github.com/timmolter/XChange (Java)<br />
<br />
=== Websockets ===<br />
* https://github.com/cronopio/hook.io-ws (Node.js)<br />
* https://github.com/progrium/wssh (Python)<br />
* https://github.com/sta/websocket-sharp (C#)<br />
* https://github.com/qsnjason/goxsocketjs (Javascript)<br />
<br />
=== REST ===<br />
* https://github.com/ameen3/node-mtgox-apiv2 (Node.js, supports v2 API)<br />
* https://bitbucket.org/pipe2grep/cryptocoinxchange (C#)<br />
<br />
=== Arbitrage ===<br />
* https://github.com/goteppo/ArBit<br />
* https://github.com/vbmithr/breakbot<br />
<br />
==See Also==<br />
<br />
* [[MtGox/API/HTTP/v1]]<br />
* [http://bitcointalk.org/index.php?topic=164404.0 MtGox API version 2: Unofficial Documentation]<br />
<br />
== References ==<br />
<references></references></div>Jwi