• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Example on how to get Yahoo Finance Data via YQL

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Example on how to get Yahoo Finance Data via YQL

    A friend of mine told me about Yahoo's YQL. I found an example here:

    http://www.jarloo.com/get-yahoo-fina...-data-via-yql/

    - that I have moved into a NT indicator (called GetYahooFinanceDataViaYql). It is just a basic example. I'm fairly new to YQL, so I don't know, if there are any limitations to be aware of in terms of how many and how big requests you can fire off during the day.

    Although I haven't gone further with the example I think it can be expanded and further build upon, so that it be used to enrich charts with fundamental data or scan large number of symbols for fundamental data or e.g. price gaps on open. To loop over an instrument list see here:

    http://www.ninjatrader.com/support/f...instrumentlist

    Note that I during the export tried to remove unneeded references following this thread:

    http://www.ninjatrader.com/support/f...ad.php?t=40919

    So if you have problems importing the script, then I might have to give it another try.

    [Updated: I had to give it another try, based on the feedback I got from NinjaTrader_JC. Attached file was updated]
    Attached Files
    Last edited by Saxo_; 03-02-2013, 01:18 PM.

    #2
    Hello Saxo_,

    Thanks posting this, but the it appears that the packaging is missing a few things. The ".zip" is correct but the folder structure is not the way it is suppose to be. There is an extra folder so it is looking for a "GetYahooFinanceDataViaYql" instead of the Indicators folder so you would want to zip it one folder down.

    Also, the Additional Reference has not been added like the "Linq". You may see our Help Guide at the following about adding the Additional References.

    http://www.ninjatrader.com/support/h..._procedure.htm
    JCNinjaTrader Customer Service

    Comment


      #3
      Thanks JC. Extra folder removed and missing reference to Linq added. The zip-file in the original post was replaced with the updated version.

      / S.

      Comment


        #4
        I'm now trying to extract option chains from Yahoo Finance using YQL. I got the inspiration to do so from here:

        http://www.mathworks.com/matlabcentr...tionChainYQL.m

        - although I have not attempted to translate the code line by line. Furthermore I found this useful page, that enables you to build your query statements and see the result (as xml or in a tree structure ... just press the 'Test'-button on the page):

        http://developer.yahoo.com/yql/conso...0%22C%22%20%0A

        The core part of the code I have made so far looks like this (full code has been attached to the post):

        HTML Code:
        protected override void OnStartUp()
        {
        ObservableCollection<OptionQuote> OptionQuotes = new ObservableCollection<OptionQuote>();
        string[] symbols = new string[] { "AAPL" };
        //string[] tickers = new string[] { "AAPL", "MSFT" }; // Q1: How to handle more than one symbol?
        string tickers = String.Join("%2C", symbols);
        string url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.options%20where%20symbol=%22";
        url = url + tickers + "%22"; 
        url = url + "AND%20expiration%20in%20(select%20contract%20from%20yahoo.finance.option_contracts%20where%20symbol=%22";
        url = url + tickers + "%22)";
        url = url + "%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env";
        XDocument doc = XDocument.Load(url);
        var optionDictionary = doc.Descendants("option")
        // .Descendants("type")
        // .Where(e => e.Value == "C") // Q2: How to filter for e.g. 'calls' here?
        .ToDictionary( // Q3: How to store in a list with strikePrice, open int ...
        w => w.Attribute("symbol").Value,
        // w => w.Attribute("strikePrice").Value, // Q4: How to get the strikePrice?
        w => w.Attribute("type").Value );
         
        foreach(KeyValuePair<String,String> entry in optionDictionary)
        {
        var optionquote = new OptionQuote(entry.Key.ToString());
        optionquote.Type = entry.Value.ToString();
        OptionQuotes.Add(optionquote);
        }
         
        foreach(OptionQuote w in OptionQuotes)
        {
        Print(w.Symbol);
        Print(w.Type);
        }
        }
        I have managed to extract the option symbols (e.g. AAPL130316C00255000) and the option types ('C' or 'P') - but I have the following problem/question (the Q#'s below refers to the Q#'s in the out commented code lines above):

        Q1: I would like to query for many symbols in one go (which is supported) - but it seems that the xml returned looks different when I call with one symbol than when I query with more than one symbol (it has one more level when calling with more than one symbol). How can the code be written to overcome this difference?

        Q2: In the query it is possible to filter the values returned - but I would also like to be able to use Linq to query the data returned - i.e. so that I can search for 'calls' only or open interest above a certain level.

        Q3: How can I store all the information per option symbol in a dictionary - or is there a better way to capture the information (currently I can only store option symbol and option type)?

        Q4: I have not succeeded in getting the strike price, last price, change, bid, ask or open interest (I get an error when I try to access these like I do with 'type'). Can anybody show me how I can access these values in the xml?

        / S.
        Attached Files
        Last edited by Saxo_; 03-02-2013, 12:53 PM.

        Comment


          #5
          Managed to solve the issues:
          Code:
          protectedoverridevoid OnStartUp()
          {
          List<string> symbollist = new List<string>(); 
          symbollist.Add("AAPL");
          symbollist.Add("MSFT");
          string tickers = String.Join("%2C", symbollist.Select(w => "%22" + w + "%22").ToArray());
          tickers = "(" + tickers + ")";
          string url = "http://query.yahooapis.com/v1/public/yql?q=select%20option%0Afrom%20yahoo.finance.options%0Awhere%20symbol%20in";
          url = url + tickers;
          url = url + "%20%0A%0A&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
          XDocument doc = XDocument.Load(url);
           
          ObservableCollection<OptionQuote> OptionQuotes = new ObservableCollection<OptionQuote>();
          var options = doc.Descendants("option");
          foreach(var option in options)
          {
          var optionquote = new OptionQuote(option.Attribute("symbol").Value);
          optionquote.Type = option.Attribute("type").Value;
          optionquote.StrikePrice = GetDecimal(option.Element("strikePrice").Value);
          optionquote.LastPrice = GetDecimal(option.Element("lastPrice").Value);
          optionquote.OpenInt = GetDecimal(option.Element("openInt").Value);
          optionquote.LastUpdate = DateTime.Now;
          OptionQuotes.Add(optionquote);
          }
          }
          
          / S.
          Attached Files

          Comment


            #6
            Hi newbie here. How do I import and make use of .zip file in the original post?
            If I go utilities > import ninjascript archive file > select the downloaded .zip, I get a message saying
            'The assemblies 'Microsoft.Office.Interop.Excel, System.Xml.Linq have been added to use the products in the imported assembly' and another messages immediately after 'Import Failed. The Ninjascript Archive file may contain duplicate method names that already exist on your PC or there is a required file missing on either your PC or in the input archive.”

            or is there any other way of getting the very useful fundamental data from yahoo? Ninjatrader currently only support stuff like P/E ratio. Is there a way to get the rest of the data like e.g Price/Book ratio and Earnings growth from yahoo finance?

            Comment


              #7
              Not fetching options data

              I used the same code and trying to fetch options data. XDocument doc = XDocument.Load(url); is executing successfully but it is not fetching any data. Is there any specific timings at which the data comes? Anything else i need to add in the code? Please assist me on this.

              Comment


                #8
                Hello shanpalaniram,

                Thank you for writing in and welcome to the NinjaTrader Support Forum!

                NinjaTrader platform support would not provide support for Yahoo Query Language.

                This thread will be left open for those who have experience with working with YQL and NinjaScript to provide any input, however.
                Zachary G.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Ilpropio, Yesterday, 09:45 PM
                1 response
                4 views
                0 likes
                Last Post NinjaTrader_EricB  
                Started by sgordet, Yesterday, 10:31 PM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_EricB  
                Started by Revan, Yesterday, 05:44 PM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_Marco  
                Started by superg3, Yesterday, 07:57 PM
                0 responses
                4 views
                0 likes
                Last Post superg3
                by superg3
                 
                Started by Longhornmark, Yesterday, 07:06 AM
                5 responses
                37 views
                0 likes
                Last Post nkhoi
                by nkhoi
                 
                Working...
                X