Search code examples
jquerystocktwits

How do I get list of stocktwit watchlists of an user in jquery


I want to retrieve a list of all watch lists created by a user to show in my custom MVC4 application.

I could authenticate the user and could also get the watchlist feed if I knew the watchlistid. In our application, we want the user to select his existing watchlist to show the feed.


Solution

  • Starting from nowhere in Stocktwits, I found this document incredibly helpful:

    http://stocktwits.com/developers/docs/signin

    Once you've gotten the flow going, replace the AJAX call to something more like this

    $.ajax({
            url: "https://api.stocktwits.com/api/2/watchlists.json?callback=?", 
            dataType: 'jsonp',
            timeout: 5000,
            data:{ access_token: token},
            success: function(data) {
               if (data) {
               callback(data);
             }},
            error: function(error){
             //handle error
             }
    

    Now we have a watchlist. After that, you want to write a callback function to use the data you just obtained:

    var getWatchlist = function(data){
        if(!data) return undefined;     //Sanity checks are always good!
        if(data.response == undefined) return undefined;
    
          var postInfo = [];  //A list to put our results on!
    
      if(data.response.status == "401") { //Represents a bad token. Handle and return
         return;
      }
      var watchlistData = data.watchlists;
      for (var i=0; i<watchlistData.length; i++)
      {
        var watchlistId = watchlistData[i].id;
    
        var tempInfo = {watchlistId};
        postInfo.push(tempInfo);
      }
    return postInfo;    //All of your watchlists!
    }
    

    From here, use the similar function described here:

    http://stocktwits.com/developers/docs/api#watchlists-show-docs

    $.ajax({          //Note you have to put in the watchlist_id
            url: "https://api.stocktwits.com/api/2/watchlists/show/"+watchlist_id+".json?callback=?", 
            dataType: 'jsonp',
            timeout: 5000,
            data:{ access_token: token},
            success: function(data) {
               if (data) {
               callback(data);
             }},
            error: function(error){
             //handle error
             }
    

    Do the same sort of thing again with a different callback:

    var getWatchlistData = function(data){
        if(!data) return undefined;    //Sanity is still important, for some....
        if(data.response == undefined) return undefined;
    
         var postInfo = [];  //A list to put our results on!
    
      if(data.response.status == "401") { //Represents a bad token. Handle and return
         return;
      }
      var watchlistSymbolsData = data.watchlists.symbols;
      for (var i=0; i<watchlistSymbolsData.length; i++)
      {
        var watchlistTicker = watchlistData[i].symbol;
                var watchlistId = watchlistData[i].id;
        var tempInfo = {watchlistTicker, watchlistId};    //You'll probably want some more data here
        postInfo.push(tempInfo);
      }
    return postInfo;    //All of your tickers with corresponding ids!
    }
    

    Simply make a function to call the AJAX, pass in one of those get functions as the callback, and you'll be well on your way to being able to display anything you'd like.