VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



The code is a single file stock ticker which grabs data from yahoo and updates every 30 seconds. Yo

by Jerry Knapp (1 Submission)
Category: Internet/HTML
Compatability: VB Script
Difficulty: Unknown Difficulty
Originally Published: Mon 26th June 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

The code is a single file stock ticker which grabs data from yahoo and updates every 30 seconds. You can track up to four stocks. Looks

Rate The code is a single file stock ticker which grabs data from yahoo and updates every 30 seconds. Yo



'This sub resizes the window as soon as the app is loaded
sub window_onload

window.resizeto 220,535
window.moveto 50,50
'This part of the code sets a timer for "runme" to be run every 30 seconds
call runme()
iTimerID = window.setInterval("runme", 30000, "VBScript")
end sub

'Declairs the for initial stocks to be searched, can be changed later
stock1 = "hig"
stock2 = "schl"
stock3 = "bdx"
stock4 = "yhoo"
count = 0

'Everytime runme is run, the count  is reset
'Calls each of the stocks into the test sub
sub runme()
count = 0
call test(stock1)
call test(stock2)
call test(stock3)
call test(stock4)
end sub

Public Sub test(stock_name)
on error resume next
'This is where the stock name is added to the web address so the data can be retreived
strURL = "http://finance.yahoo.com/q?s="& stock_name

'Calls the function GetHTTPFile which will go out and retreive all the data from
'strURL and put it in the string "txt"
txt = GetHTTPFile(strURL)

'The instr function looks for a certain string in "txt" and retreives the position of that string
'in the txt, from there you can determine how far away the data is that your trying to get

number = instr(txt,"small><big><b>")
number = number + 14
current_price = Mid(txt,number,7) 
number = instr(1000,txt,"Close:")
number = number + 39
open_price = Mid(txt,number,7)

'Below is formatting to clean the data that comes in
current_price = Replace(current_price,"/"," ")
open_price = Replace(open_price,"/"," ")
current_price = Replace(current_price,"<"," ")
open_price = Replace(open_price,"<"," ")
current_price = Replace(current_price,"b"," ")
open_price = Replace(open_price,"b"," ")
current_price = Replace(current_price,"t"," ")
open_price = Replace(open_price,"t"," ")

'Trims the spaces at the end of the string
rtrim(open_price)
rtrim(current_price)

'Finds the difference in stock price and percent change
difference = ccur(current_price - open_price)
percent = formatpercent(difference / open_price)

'Determines if the change was negative or positive and changes the color to reflect that
if current_price > open_price then
current_price = "<font color =green>" & current_price & "</font>"
difference = "<font color =green>" & difference & "</font>"
percent = "<font color =green>" & percent & "</font>"
else
current_price ="<font color =red>" & current_price & "</font>"
difference ="<font color =red>" & difference & "</font>"
percent = "<font color =red>" & percent & "</font>"
end if

'the count is needed so the code knows where to put each stock in the html
'the format fuction is called to format the numbers in to better looking html code
count = count + 1
if count = 1 then
stock_1.innerhtml = format(stock_name,open_price,current_price,difference,percent)
end if
if count = 2 then
stock_2.innerhtml = format(stock_name,open_price,current_price,difference,percent)
end if
if count = 3 then
stock_3.innerhtml = format(stock_name,open_price,current_price,difference,percent)
end if
if count = 4 then
stock_4.innerhtml = format(stock_name,open_price,current_price,difference,percent)
end if


End Sub
'This function does the formatting for the stocks 
function format(stock_name,open_price,current_price,difference,percent)
format ="<br>Symbol:<b> " & stock_name & "</b></br>Prev Close:&nbsp <b>" & open_price & "</b><br> Current Price:&nbsp&nbsp<b>" & current_price & "</b><br> Changed:&nbsp<b>" & difference & "(" & percent & ") </b><br>"
end function


'This function is what goes out and gets the html page so it can be put into a string
Function GetHTTPFile(strURL)
    Dim objXML
    Dim strHTTPResponse
    Set objXML = CreateObject("Msxml2.XMLHTTP")
    Call objXML.Open("GET", strURL, False)
    Call objXML.Send()
    strHTTPResponse = objXML.responseText
    Set objXML = Nothing
    GetHTTPFile = strHTTPResponse
End Function

'This function puts up a series of imput boxes so you can change the stock symbols once the app is loaded
sub popups
stock1 =inputbox("Please enter the first stock symbol:")
stock2 =inputbox("Please enter the second stock symbol:")
stock3 =inputbox("Please enter the third stock symbol:")
stock4 =inputbox("Please enter the fourth stock symbol:")


end sub

</script>

<center>
<title>Stock Ticker App</title>
<head><b><span style="color: white; height: 20; font-size: 20pt; width: 60px; font-family: COMIC SANS MS; Filter: Glow(Color=black, Strength=2)"> My</span> <span style=" height: 20; font-size: 18pt; font-family: Bookman; Filter: Glow(Color=white, Strength=2)"> Stock Ticker</b></span></head>
<body scroll = no STYLE="filter:progid:DXImageTransform.Microsoft.Gradient
(GradientType=0, StartColorStr='#FF9966', EndColorStr='#FFFFCC')">
<hr>
<input type="button" value="Change Stocks" name="run_button2"  onClick="popups">
<span id ="stock_1"></span>
<span id ="stock_2"></span>
<span id ="stock_3"></span>
<span id ="stock_4"></span>
</center></body>

Download this snippet    Add to My Saved Code

The code is a single file stock ticker which grabs data from yahoo and updates every 30 seconds. Yo Comments

No comments have been posted about The code is a single file stock ticker which grabs data from yahoo and updates every 30 seconds. Yo. Why not be the first to post a comment about The code is a single file stock ticker which grabs data from yahoo and updates every 30 seconds. Yo.

Post your comment

Subject:
Message:
0/1000 characters