App Programming‎ > ‎

Stock App

Making a Simple App - A Stock Quote App
   The API we will use 
Yahoo as well as many other sites, has an API for stocks.I did a Google search on "Yahoo stock api" and among the first links were: 

There are so many options and different information that we can get from a stock that it is amazing. Let us just start simple - we want the stock price:

    Yahoo Finance API
    - CSV API - URL Formats - Listing of Properties to get - 

 
    The URL to use is:
                http://finance.yahoo.com/d/quotes.csv?s=<stock symbol>&f=<property function>

where:
        the API address is "finance.yahoo.com/d/"
        the data will be CSV - quotes.csv?
        the <stock symbol> is the stock ticker name
          e.gfor IBM the stock symbol is s=IBM
        the <property> is what property you want to see
            e.g. 
                property    function
                    bid                b
                    change         c1
                    change in % c
                    day's high     h
                    day's low       g
                    name            n
                    notes            n4
                    price             p
                    revenue        s6
                    volume         v
                    year high      k
                    year low       j
                    year range   w
            (there are more, these are just the more common ones)
                    
        I asked for the price - &f=p  (you can ask for more than just one)


    I came up with this API url to get price of IBM stock

            http://finance.yahoo.com/d/quotes.csv?s=IBM&f=p

    where:
         the API address is "finance.yahoo.com/d/"
        the data will be CSV - quotes.csv?
        the stock symbol is s=IBM
        I asked for the price - &f=p

    if you paste that string into a browser, you will get the data returned to you in a CSV (Excel) file. If you open it up, you will see the stock name and price.

        Example 1 - just the price
                http://finance.yahoo.com/d/quotes.csv?s=MFST&f=p
                gives this: 
                        28.55
                (s=MSFT and p is for price)

        Example 2 - the name and the price
                http://finance.yahoo.com/d/quotes.csv?s=GE&f=np
                gives this: 
                        "GENERAL ELEC CO",28.55
                (s=GE and  n for name and p for price)

        Example 3 - even more information...
                http://finance.yahoo.com/d/quotes.csv?s=GE&f=nkqwxyr1l9t5p4 
                gives this: 
                        "GENERAL ELEC CO",32.98,"Jun 26","21.30 - 32.98","NYSE",2.66,"Jul 25",28.55,"Jul 3","-0.21%"

                (s=GE and name(n), year high (k), etc)

        Example 4 - more than 1 stock

                http://finance.yahoo.com/d/quotes.csv?s=GE+MFST&f=np  - will give us General Electric and Microsoft stocks
                gives this: 
                        "GENERAL ELEC CO",28.55
                        "Microsoft CP      ",46.85

        Example 5 - more info, multiple stocks

                http://finance.yahoo.com/d/quotes.csv?s=BBDB.TO+NT.TO+GE+MSFT&f=snl1d1t1ohgdr

                will return:
                        

But we want to put this in our app and make a really nice graphical interface. We do not want to keep having to open a browser, type in the request and save the file, then open up Excel to see that data. So we do the following:


    Now You Do It: 

1. Simple Stock App with a button for each company (IBM, GE, MSFT, etc)
2. Stock App with text field for you to type in any stock symbol


1. Stock App with a button for each company

Lets write our first App, step by step...

  • Open a new mainstack, and put a text field and a button on it.
  • Name the text field "myData"
  • Name the button "IBM" with the following code:

on mouseUp
       put url("http://finance.yahoo.com/d/quotes.csv?s=IBM&f=np") into field "myData"
end mouseUp

  Now try it out. You should see the price and full name for the stock symbol "IBM"

This is a good place to start. You see the data as you get it back from the website - finance.yahoo.com


    Let's make it look more professional by not showing the raw data and moving the data to separate fields.

    Add a field called "price" and a field called "name"
    Add code to the button's script to move the data to these new fields. 

    We will us the following to get the info:
         put url("http://finance.yahoo.com/d/quotes.csv?s=GE&f=np") into x

     It will give us back info like this:
           "GENERAL ELEC CO",28.55
    which we put into x
        
    each item is separated by a comma.
        item 1 = "GENERAL ELECTRIC CO"
        item 2 = 28.65:
   
    so we need to put each into it's own field     

The complete code should now be:

on mouseUp
       put url("http://finance.yahoo.com/d/quotes.csv?s=IBM&f=np") into x
       put item 1 of x into field "name"
       put item 2 of x into field "price"
end mouseUp

  Run it and you should see the following (I changed the text formatting of the fields so they would look better)

Add a "Clear" or "Reset" button with the following code to clear all fields:

on mouseUp
   put empty into field "name"
   put empty into field "price"
   put empty into field "high"
   put empty into field "low"
end mouseUp



To add more stocks by cloning the "IBM" button, hold down the "Ctrl" key on the keyboard and drag off a copy of the "IBM" button.
Open up that second "IBM" button and change the name to "Apple". Then edit the script for that button and change "IBM" to "aapl"

Test it and make sure that it works:




Now that you know how the incoming data looks and it works, we do not need the field "myData" any more.

You can:
    1. Hide it by unchecking the "visible" checkbox in it's property Inspector" , move it away, or cover it up with other fields
 
or

   2. Delete the field "myData" and put the data into a variable "x". 

   Change the code on the buttons to put the data into a variable "x" instead of the field "myData"

on mouseUp
       put url("http://finance.yahoo.com/d/quotes.csv?s=IBM&f=np") into x
       put item 1 of x into field "name"
       put item 2 of x into field "price"
end mouseUp

    
The next step is to add more stocks. Your options are:
    1. add more buttons - one for each stock
    2. have a drop-down box with a list of stocks
    3. Ask the user for a stock symbol, then use that in your url

 ...more advanced links:
Gummy-stuff - which shows the codes/options that you can ask for in a table (see copyright issue)
Google - which has loads of documentation



A Student's Sample Stock Quote App
This is an app or widget that check the price of your favorite stocks by clicking on the button

1. One student's version: create the following:

    add buttons for every stock that you want to look up (AAPL=Apple, GM=General Motors, etc)
    add 4 label fields with contents: "Name:", "Bid:", "High:" and  "Low:" respectively
    add 4 text entry fields for the following: Name, Bid, High, Low
    


The following code is what is on the Apple (AAPL) button:

on mouseUp

   local x
   put url "http://finance.yahoo.com/d/quotes.csv?s=AAPL&f=nbgh" into x
   put item 1 of line 1 of x into the field "NameAns"
   put item 2 of line 1 of x into the field "BidAns"
   put item 3 of line 1 of x into the field "HighAns"
   put item 4 of line 1 of x into the field "LowAns"   

end mouseUp

           Add the same code to the other buttons for the other stocks

            That's it, modify it as you wish with fields, graphics and other enhancements


3. Custom Look ups

If you want the ability to type your own stocks in (instead of having set  buttons for fixed stocks), you can create an input field

Add a new "text entry field" , which we will call "stock".
Add another button called "Get Price" and add the script:

on mouseUp
   put field "stock" into y
   put url("http://finance.yahoo.com/d/quotes.csv?s=" & y & "&f=np") into x
   put item 1 of x into field "theName"
   put item 2 of x into field "thePrice"
end mouseUp


or


Comments