Stock ticker¶
If you have the VastPark Player installed, download the example IMML file below and open the demo in the VastPark Player.
Overview¶
This sample demonstrates the use of the Define element which is equivalent to scene:set(key, value) (refer to VastScript API document) and making http requests to a 3rd party website (Yahoo Finance). You can read more about it here
Instructions¶
Customise the IMML to select stocks you’re interested in or just run as is using stocks listed on the ASX.
Walkthrough¶
This IMML demonstrates the use of an Http request with LUA to display data
1 <Script Name="UpdateStocks" Language="Lua">
2 function main(obj, args)
3 request = httprequest('http://finance.yahoo.com/d/quotes.csv?s='..scene:get('stocks')..'&f=sl1c1ohgv')
4 response = scene.web:loadstring(request)
5 --reponse is returned as each stock on a new line, break into a table entry for each stock
6 stocks = explode('\r\n', response)
7
8 count = 1
9
10 --stocks is a dictionary of all listed stocks, need to explode each listing to generate a representation
11 --format: stock code, last trade,change, open, high, low, volume
12 for key,value in pairs(stocks) do
13 if(string.len(value) > 0) then
14 stock = explode(',', value)
15 _updateStock(string.sub(stock[1],2,-2), stock[2], stock[3], stock[4], stock[5], stock[6], stock[7])
16 end
17 end
18 end
The request on line 3 is what gets the stock data.
This line is how the stock data gets broken down into individual tokens
1 stocks = explode('\r\n', response)
Another Important point is the Define Element which holds a Key-Value pair. In short it's a place you can store data in IMML and have it accessible by other scripts. If you take a look at the string you can see that those letters are actually stock Symbols. Thus, extending that idea, you can add more stocks to be displayed by appending the stock symbol as a comma separated value.
1 <Define Key="stocks" Value="IPL.AX,VIL.AX,FMG.AX,RIO.AX,BHP.AX,BOQ.AX,BRM.AX,FGL.AX,NAB.AX,TAM.AX,TLS.AX,WOW.AX" />
This value is then retrieved and then appended to the http request URL in this line
1 request = httprequest('http://finance.yahoo.com/d/quotes.csv?s='..scene:get('stocks')..'&f=sl1c1ohgv')
Which then returns the data for stocks to be further processed by the scripts.