Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active January 2, 2016 13:49
Show Gist options
  • Select an option

  • Save stephenlb/8312648 to your computer and use it in GitHub Desktop.

Select an option

Save stephenlb/8312648 to your computer and use it in GitHub Desktop.
Loading History from PubNub Mt.Gox Trades. These are resource files for a StackOverflow answer found here: http://stackoverflow.com/questions/20976433/how-to-retrieve-more-than-100-messages-from-the-history-of-a-pubnub-channel/20988208#20988208
{
"ticker.LTCGBP": "0102a446-e4d4-4082-8e83-cc02822f9172",
"ticker.LTCCNY": "0290378c-e3d7-4836-8cb1-2bfae20cc492",
"depth.BTCHKD": "049f65dc-3af3-4ffd-85a5-aac102b2a579",
"depth.BTCEUR": "057bdc6b-9f9c-44e4-bc1a-363e4443ce87",
"ticker.NMCAUD": "08c65460-cbd9-492e-8473-8507dfa66ae6",
"ticker.BTCEUR": "0bb6da8b-f6c6-4ecf-8f0d-a544ad948c15",
"depth.BTCKRW": "0c84bda7-e613-4b19-ae2a-6d26412c9f70",
"depth.BTCCNY": "0d1ecad8-e20f-459e-8bed-0bdcf927820f",
"ticker.BTCCAD": "10720792-084d-45ba-92e3-cf44d9477775",
"depth.BTCCHF": "113fec5f-294d-4929-86eb-8ca4c3fd1bed",
"ticker.LTCNOK": "13616ae8-9268-4a43-bdf7-6b8d1ac814a2",
"ticker.LTCUSD": "1366a9f3-92eb-4c6c-9ccc-492a959eca94",
"ticker.BTCBTC": "13edff67-cfa0-4d99-aa76-52bd15d6a058",
"ticker.LTCCAD": "18b55737-3f5c-4583-af63-6eb3951ead72",
"ticker.NMCCNY": "249fdefd-c6eb-4802-9f54-064bc83908aa",
"depth.BTCUSD": "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe",
"ticker.BTCCHF": "2644c164-3db7-4475-8b45-c7042efe3413",
"depth.BTCAUD": "296ee352-dd5d-46f3-9bea-5e39dede2005",
"ticker.BTCCZK": "2a968b7f-6638-40ba-95e7-7284b3196d52",
"ticker.BTCSGD": "2cb73ed1-07f4-45e0-8918-bcbfda658912",
"ticker.NMCJPY": "314e2b7a-a9fa-4249-bc46-b7f662ecbc3a",
"ticker.BTCNMC": "36189b8c-cffa-40d2-b205-fb71420387ae",
"depth.BTCINR": "414fdb18-8f70-471c-a9df-b3c2740727ea",
"depth.BTCSGD": "41e5c243-3d44-4fad-b690-f39e1dbb86a8",
"ticker.BTCLTC": "48b6886f-49c0-4614-b647-ba5369b449a9",
"ticker.LTCEUR": "491bc9bb-7cd8-4719-a9e8-16dad802ffac",
"ticker.BTCINR": "55e5feb8-fea5-416b-88fa-40211541deca",
"ticker.LTCJPY": "5ad8e40f-6df3-489f-9cf1-af28426a50cf",
"depth.BTCCAD": "5b234cc3-a7c1-47ce-854f-27aee4cdbda5",
"ticker.BTCNZD": "5ddd27ca-2466-4d1a-8961-615dedb68bf1",
"depth.BTCGBP": "60c3af1b-5d40-4d0e-b9fc-ccab433d2e9c",
"depth.BTCNOK": "66da7fb4-6b0c-4a10-9cb7-e2944e046eb5",
"depth.BTCTHB": "67879668-532f-41f9-8eb0-55e7593a5ab8",
"ticker.BTCSEK": "6caf1244-655b-460f-beaf-5c56d1f4bea7",
"ticker.BTCNOK": "7532e866-3a03-4514-a4b1-6f86e3a8dc11",
"ticker.BTCGBP": "7b842b7d-d1f9-46fa-a49c-c12f1ad5a533",
"trade.lag": "85174711-be64-4de1-b783-0628995d7914",
"depth.BTCSEK": "8f1fefaa-7c55-4420-ada0-4de15c1c38f3",
"depth.BTCDKK": "9219abb0-b50c-4007-b4d2-51d1711ab19c",
"depth.BTCJPY": "94483e07-d797-4dd4-bc72-dc98f1fd39e3",
"ticker.NMCUSD": "9aaefd15-d101-49f3-a2fd-6b63b85b6bed",
"ticker.LTCAUD": "a046600a-a06c-4ebf-9ffb-bdc8157227e8",
"ticker.BTCJPY": "a39ae532-6a3c-4835-af8c-dda54cb4874e",
"depth.BTCCZK": "a7a970cf-4f6c-4d85-a74e-ac0979049b87",
"ticker.LTCDKK": "b10a706e-e8c7-4ea8-9148-669f86930b36",
"ticker.BTCPLN": "b4a02cb3-2e2d-4a88-aeea-3c66cb604d01",
"test": "bad99f24-fa8b-4938-bfdf-0c1831fc6665",
"ticker.BTCRUB": "bd04f720-3c70-4dce-ae71-2422ab862c65",
"ticker.NMCGBP": "bf5126ba-5187-456f-8ae6-963678d0607f",
"ticker.BTCKRW": "bf85048d-4db9-4dbe-9ca3-5b83a1a4186e",
"ticker.BTCCNY": "c251ec35-56f9-40ab-a4f6-13325c349de4",
"depth.BTCNZD": "cedf8730-bce6-4278-b6fe-9bee42930e95",
"ticker.BTCHKD": "d3ae78dd-01dd-4074-88a7-b8aa03cd28dd",
"ticker.BTCTHB": "d58e3b69-9560-4b9e-8c58-b5c0f3fda5e1",
"ticker.BTCUSD": "d5f06780-30a8-4a48-a2f8-7ed181b4a13f",
"depth.BTCRUB": "d6412ca0-b686-464c-891a-d1ba3943f3c6",
"ticker.NMCEUR": "d8512d04-f262-4a14-82f2-8e5c96c15e68",
"trade.BTC": "dbf1dee9-4f2e-4a08-8cb7-748919a71b21",
"ticker.NMCCAD": "dc28033e-7506-484c-905d-1c811a613323",
"depth.BTCPLN": "e4ff055a-f8bf-407e-af76-676cad319a21",
"ticker.BTCDKK": "e5ce0604-574a-4059-9493-80af46c776b3",
"ticker.BTCAUD": "eb6aaa11-99d0-4f64-9e8c-1140872a423d"
}
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script src="mtgox-history.js"></script>
<script>(function(){
MTGOX.history.hourly({
channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
data : function(response) { console.log(JSON.stringify(response)) },
error : function() { console.log('NETWORK ERROR') }
});
MTGOX.history.full({
limit : 500, // SET AS HIGH AS NEEDED
channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
data : function(messages) { console.log(messages) },
error : function(e) { console.log("NETWORK ERROR") }
});
})();</script>
(function(){
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// INITIALIZE PUBNUB
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var pubnub = PUBNUB.init({
subscribe_key : 'sub-c-50d56e1e-2fd9-11e3-a041-02ee2ddab7fe'
});
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// MTGOX HISTORY INTERFACE
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
window.MTGOX = {
history : {
hourly : hourly,
full : full
}
};
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// GET ALL DATA FOREVER (WITH LIMIT OF COURSE)
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/*
MTGOX.history.full({
limit : 1000,
channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
data : function(messages) { console.log(messages) },
error : function(e) { console.log("NETWORK ERROR") }
});
*/
function full(args) {
var chan = args['channel'] ||'d5f06780-30a8-4a48-a2f8-7ed181b4a13f'
, callback = args['data'] || function(){}
, error = args['error'] || function(){}
, limit = +args['limit'] || 5000
, start = 0
, count = 100
, history = []
, params = {
channel : chan,
count : count,
callback : function(messages) {
var msgs = messages[0];
start = messages[1];
params.start = start;
PUBNUB.each( msgs.reverse(), function(m) {history.push(m)} );
if (history.length >= limit) return callback(history);
if (msgs.length < count) return callback(history);
count = 100;
add_messages();
},
error : function(e) {
callback(history);
error(history);
}
};
add_messages();
function add_messages() { pubnub.history(params) }
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// GET 24 HOURS IN HOURLY INCREMENTS
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/*
MTGOX.history.hourly({
channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
data : function(response) { console.log(response) },
error : function() { console.log('ERROR') }
});
*/
function hourly(setup) {
var limit = 24;
var count = 0;
var chan = setup['channel'] ||'d5f06780-30a8-4a48-a2f8-7ed181b4a13f';
var cb = setup['data'] || function(){};
var eb = setup['error'] || function(){};
var now = new Date();
now.setUTCHours(0);
now.setUTCMinutes(0);
now.setUTCSeconds(0);
now.setUTCMilliseconds(0);
var utc_now = now.getTime();
var vectors = [];
PUBNUB.each( (new Array(limit)).join(',').split(','), function( _, d ) {
var day = utc_now - 3600000 * d;
pubnub.history({
limit : 1,
channel : chan,
start : day * 10000,
error : function() { count++; eb(); },
callback : function(messages) {
// DONE?
if (++count == limit) return cb(vectors);
// ADD TIME SLICES
var res = +(((messages[0][0]||{}).ticker||{}).avg||{}).value;
res && vectors.push([ new Date(day).getUTCHours(), res ]);
// KEEP IT SORTED
vectors.sort(function(a,b){ return a[0] > b[0] && -1 || 1 });
}
})
} );
}
})();
@stephenlb
Copy link
Author

PubNub Load History More than 100 Messages

Sometimes you want to slice back in time over a linear stream of data. And often you'll want to do this at different levels of granularity. That is why PubNub Storage and Playback APIs provide maximum level of flexibility. However sometimes it ends up being a bit tricky to load data with the preferred result set.

PubNub Real-Time Network Storage and Playback API

PubNub Real-Time Network Storage and Playback

There are several considerations you may be seeking when loading transaction history over timelines that can potentially span millions of message in the transaction set. There are some great options available to you and we will cover two of them right now. The examples will be coded in JavaScript. The first example loads a summary of the data by grabbing the snapshots for the beginning of each hour for the past 24 hours. The second example shows you how to load all transactions in full detail and maximum granularity.

All Reference Files can be found on this GIST: Loading History from PubNub Mt.Gox Trades

Example PubNub Mt.Gox History JavaScript Usage

<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script src="mtgox-history.js"></script>
<script>(function(){

// LOAD HOURLY SUMMARY
MTGOX.history.hourly({
    channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
    data    : function(response) { console.log(JSON.stringify(response)) },
    error   : function()         { console.log("NETWORK ERROR")  } 
});

// LOAD ALL WITH LIMITER OPTION
MTGOX.history.full({
    limit   : 500, // SET LIMIT AS HIGH AS NEEDED TO LOAD MORE!
    channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
    data    : function(messages) { console.log(messages)        },
    error   : function(e)        { console.log("NETWORK ERROR") }
});

})();</script>

NOTE: Running MTGOX.history.hourly() method will generate a list of snapshots per hour over the last 24 hours.

NOTE: Running MTGOX.history.full() method will generate maximum resolution detail with a lot of data. You can get a full dump or partial dump as needed; and you should increase the limit parameter in order to grab more data points.

This following JavaScript file will provide you the MTGOX interface.

PubNub Mt.Gox History JavaScript Loader

//
// mtgox-history.js
//

(function(){

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// INITIALIZE PUBNUB
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var pubnub = PUBNUB.init({
    subscribe_key : 'sub-c-50d56e1e-2fd9-11e3-a041-02ee2ddab7fe'
});

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// MTGOX HISTORY INTERFACE
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
window.MTGOX = {
    history : {
        hourly : hourly,
        full   : full
    }
};

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// GET ALL DATA FOREVER (WITH LIMIT OF COURSE)
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

/*
MTGOX.history.full({
    limit   : 1000,
    channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
    data    : function(messages) { console.log(messages)        },
    error   : function(e)        { console.log("NETWORK ERROR") }
});
*/

function full(args) {
    var chan     = args['channel'] ||'d5f06780-30a8-4a48-a2f8-7ed181b4a13f'
    ,   callback = args['data']   || function(){}
    ,   error    = args['error']  || function(){}
    ,   limit    = +args['limit'] || 5000
    ,   start    = 0
    ,   count    = 100
    ,   history  = []
    ,   params   = {
            channel  : chan,
            count    : count,
            callback : function(messages) {
                var msgs = messages[0];
                start = messages[1];
                params.start = start;
                PUBNUB.each( msgs.reverse(), function(m) {history.push(m)} );

                if (history.length >= limit) return callback(history);
                if (msgs.length < count)     return callback(history);

                count = 100;
                add_messages();
            },
            error : function(e) {
                callback(history);
                error(history);
            }
        };

    add_messages();
    function add_messages() { pubnub.history(params) }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// GET 24 HOURS IN HOURLY INCREMENTS
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

/*
MTGOX.history.hourly({
    channel : 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f',
    data    : function(response) { console.log(response) },
    error   : function()         { console.log('ERROR')  } 
});
*/

function hourly(setup) {
    var limit = 24;
    var count = 0;
    var chan  = setup['channel'] ||'d5f06780-30a8-4a48-a2f8-7ed181b4a13f';
    var cb    = setup['data']    || function(){};
    var eb    = setup['error']   || function(){};
    var now   = new Date();

    now.setUTCHours(0);
    now.setUTCMinutes(0);
    now.setUTCSeconds(0);
    now.setUTCMilliseconds(0);

    var utc_now = now.getTime();
    var vectors = [];

    PUBNUB.each( (new Array(limit)).join(',').split(','), function( _, d ) {
        var day = utc_now - 3600000 * d;
        pubnub.history({
            limit    : 1,
            channel  : chan,
            start    : day * 10000,
            error    : function() { count++; eb(); },
            callback : function(messages) {
                // DONE?
                if (++count == limit) return cb(vectors);

                // ADD TIME SLICES
                var res = +(((messages[0][0]||{}).ticker||{}).avg||{}).value;
                res && vectors.push([ new Date(day).getUTCHours(), res ]);

                // KEEP IT SORTED
                vectors.sort(function(a,b){ return a[0] > b[0] && -1 || 1 });
            }
        })
    } );
}

})();

Mt.Gox PubNub Channel Listing for Tickers, Depth and Trades

The following is a list of channels provided by Mt.Gox data feed options you can use in the history channel parameter field.

{
"TICKER.ltcgbp": "0102A446-E4D4-4082-8E83-CC02822F9172",
"TICKER.ltccny": "0290378C-E3D7-4836-8CB1-2BFAE20CC492",
"DEPTH.btchkd": "049F65DC-3AF3-4FFD-85A5-AAC102B2A579",
"DEPTH.btceur": "057BDC6B-9F9C-44E4-BC1A-363E4443CE87",
"TICKER.nmcaud": "08C65460-CBD9-492E-8473-8507DFA66AE6",
"TICKER.btceur": "0BB6DA8B-F6C6-4ECF-8F0D-A544AD948C15",
"DEPTH.btckrw": "0C84BDA7-E613-4B19-AE2A-6D26412C9F70",
"DEPTH.btccny": "0D1ECAD8-E20F-459E-8BED-0BDCF927820F",
"TICKER.btccad": "10720792-084D-45BA-92E3-CF44D9477775",
"DEPTH.btcchf": "113FEC5F-294D-4929-86EB-8CA4C3FD1BED",
"TICKER.ltcnok": "13616AE8-9268-4A43-BDF7-6B8D1AC814A2",
"TICKER.ltcusd": "1366A9F3-92EB-4C6C-9CCC-492A959ECA94",
"TICKER.btcbtc": "13EDFF67-CFA0-4D99-AA76-52BD15D6A058",
"TICKER.ltccad": "18B55737-3F5C-4583-AF63-6EB3951EAD72",
"TICKER.nmccny": "249FDEFD-C6EB-4802-9F54-064BC83908AA",
"DEPTH.btcusd": "24E67E0D-1CAD-4CC0-9E7A-F8523EF460FE",
"TICKER.btcchf": "2644C164-3DB7-4475-8B45-C7042EFE3413",
"DEPTH.btcaud": "296EE352-DD5D-46F3-9BEA-5E39DEDE2005",
"TICKER.btcczk": "2A968B7F-6638-40BA-95E7-7284B3196D52",
"TICKER.btcsgd": "2CB73ED1-07F4-45E0-8918-BCBFDA658912",
"TICKER.nmcjpy": "314E2B7A-A9FA-4249-BC46-B7F662ECBC3A",
"TICKER.btcnmc": "36189B8C-CFFA-40D2-B205-FB71420387AE",
"DEPTH.btcinr": "414FDB18-8F70-471C-A9DF-B3C2740727EA",
"DEPTH.btcsgd": "41E5C243-3D44-4FAD-B690-F39E1DBB86A8",
"TICKER.btcltc": "48B6886F-49C0-4614-B647-BA5369B449A9",
"TICKER.ltceur": "491BC9BB-7CD8-4719-A9E8-16DAD802FFAC",
"TICKER.btcinr": "55E5FEB8-FEA5-416B-88FA-40211541DECA",
"TICKER.ltcjpy": "5AD8E40F-6DF3-489F-9CF1-AF28426A50CF",
"DEPTH.btccad": "5B234CC3-A7C1-47CE-854F-27AEE4CDBDA5",
"TICKER.btcnzd": "5DDD27CA-2466-4D1A-8961-615DEDB68BF1",
"DEPTH.btcgbp": "60C3AF1B-5D40-4D0E-B9FC-CCAB433D2E9C",
"DEPTH.btcnok": "66DA7FB4-6B0C-4A10-9CB7-E2944E046EB5",
"DEPTH.btcthb": "67879668-532F-41F9-8EB0-55E7593A5AB8",
"TICKER.btcsek": "6CAF1244-655B-460F-BEAF-5C56D1F4BEA7",
"TICKER.btcnok": "7532E866-3A03-4514-A4B1-6F86E3A8DC11",
"TICKER.btcgbp": "7B842B7D-D1F9-46FA-A49C-C12F1AD5A533",
"TRADE.LAG": "85174711-BE64-4DE1-B783-0628995D7914",
"DEPTH.btcsek": "8F1FEFAA-7C55-4420-ADA0-4DE15C1C38F3",
"DEPTH.btcdkk": "9219ABB0-B50C-4007-B4D2-51D1711AB19C",
"DEPTH.btcjpy": "94483E07-D797-4DD4-BC72-DC98F1FD39E3",
"TICKER.nmcusd": "9AAEFD15-D101-49F3-A2FD-6B63B85B6BED",
"TICKER.ltcaud": "A046600A-A06C-4EBF-9FFB-BDC8157227E8",
"TICKER.btcjpy": "A39AE532-6A3C-4835-AF8C-DDA54CB4874E",
"DEPTH.btcczk": "A7A970CF-4F6C-4D85-A74E-AC0979049B87",
"TICKER.ltcdkk": "B10A706E-E8C7-4EA8-9148-669F86930B36",
"TICKER.btcpln": "B4A02CB3-2E2D-4A88-AEEA-3C66CB604D01",
"TEST": "BAD99F24-FA8B-4938-BFDF-0C1831FC6665",
"TICKER.btcrub": "BD04F720-3C70-4DCE-AE71-2422AB862C65",
"TICKER.nmcgbp": "BF5126BA-5187-456F-8AE6-963678D0607F",
"TICKER.btckrw": "BF85048D-4DB9-4DBE-9CA3-5B83A1A4186E",
"TICKER.btccny": "C251EC35-56F9-40AB-A4F6-13325C349DE4",
"DEPTH.btcnzd": "CEDF8730-BCE6-4278-B6FE-9BEE42930E95",
"TICKER.btchkd": "D3AE78DD-01DD-4074-88A7-B8AA03CD28DD",
"TICKER.btcthb": "D58E3B69-9560-4B9E-8C58-B5C0F3FDA5E1",
"TICKER.btcusd": "D5F06780-30A8-4A48-A2F8-7ED181B4A13F",
"DEPTH.btcrub": "D6412CA0-B686-464C-891A-D1BA3943F3C6",
"TICKER.nmceur": "D8512D04-F262-4A14-82F2-8E5C96C15E68",
"TRADE.btc": "DBF1DEE9-4F2E-4A08-8CB7-748919A71B21",
"TICKER.nmccad": "DC28033E-7506-484C-905D-1C811A613323",
"DEPTH.btcpln": "E4FF055A-F8BF-407E-AF76-676CAD319A21",
"TICKER.btcdkk": "E5CE0604-574A-4059-9493-80AF46C776B3",
"TICKER.btcaud": "EB6AAA11-99D0-4F64-9E8C-1140872A423D"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment