Jump to content

Stock Market Watcher refresh problem


Recommended Posts

I am currently working on a program that gets information from googles finance site using inetget and then graphs and displays the data. I have the adding stocks function working perfectly however whenever I add the refresh function it crashes and I can't for the life of me figure out why. Any Ideas? I apologize in advance for my awful variables and my horribly inefficient coding skills :)

#NoTrayIcon
#include "guiconstants.au3"
#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>


$hGUI = GUICreate("Stocks", 505, 200)
guisetstate(@SW_SHOW)
Global $mquote[30]
global $symbol[30]
global $ticker[30]
global $label[30]
global $name[30]
global $exchangen[30]
global $pricechgn[30]
global $percentchangen[30]
$ox = 300
$oy = 195
$x = 300
$y = 195
$i = 0
$list = Guictrlcreatelistview(" Stock ", 5, 5, 60, 150)
$exchange1 = Guictrlcreatelistview(" Price USD ", 65, 5, 80, 150)
$pricechg = GUICtrlCreateListview("Pt. Change", 145, 5, 75, 150)
$percentchg = Guictrlcreatelistview("% Change", 220, 5, 75, 150)
$input = guictrlcreateinput("EX: AAPL", 5, 170, 100, 20)
$add = guictrlcreatebutton("Add", 110, 170, 50, 20)
$graph = guictrlcreatecheckbox("Graph", 200, 170, 100, 20)

_GDIPlus_Startup ()
$hPen1 = _GDIPlus_PenCreate(0xFF00C000, 2)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
_GDIPlus_GraphicsFillRect($hGraphic, 300, 5, 200, 190)
$diff = 2
$timer = timerinit()

DirCreate("C:\Stock")

While 1
    $diff = timerdiff($timer)/1000
    $x = 300 + $diff
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        DirRemove("C:\Stock", 1)
        _GDIPlus_GraphicsDispose ($hGraphic)
        _GDIPlus_Shutdown ()
        Exit
    EndIf
    If ($msg = $add) And ($i < 30) Then _addstock()
    If $msg = $graph Then
        ;graph
    EndIf
    sleep(50)
    ;If $i <> 0 Then _Refresh()
    _GDIPlus_GraphicsDrawLine($hGraphic, $ox, $oy, $x, $y, $hPen1)
    $ox = $x
    $oy = $y
WEnd

Func _addstock()
    $finexchange = ""
    $symbol[$i] = guictrlread($input)
    InetGet("http://www.google.com/finance?q=" & $symbol[$i], "C:\Stock\" & $i & ".txt", 1)
    $file = fileread("C:\Stock\" & $i & ".txt")
    $readdata = StringInStr($file, "s:" & chr(34) & $symbol[$i])
    $data = Stringtrimleft($file, $readdata)
    $stripdata = StringTrimRight($data, (stringlen($data)- 100))
    $mquote = stringsplit($stripdata, ":" & chr(34), 1)
    $finalname = StringTrimRight($mquote[2], 3) ; final name of symbol
    $exchange = stringsplit($mquote[3], "")
    for $x = 0 to (UBound($exchange) - 1) Step 1
        if (StringIsUpper($exchange[$x])) Then
            $finexchange = $finexchange & $exchange[$x] ;final exchange
        EndIf
    Next
    $finalprice = StringTrimRight($mquote[4], 4)
    $finalchangepts = StringTrimRight($mquote[6], 4)
    $finalpercent = StringTrimRight($mquote[7], 5) & "%"
    $name[$i] = guictrlcreatelistviewitem($finalname, $list)
    $exchangen[$i] = guictrlcreatelistviewitem($finalprice, $exchange1)
    $pricechgn[$i] = guictrlcreatelistviewitem($finalchangepts, $pricechg)
    $percentchangen[$i] = guictrlcreatelistviewitem($finalpercent, $percentchg)
    $color = stringsplit($finalchangepts, "")
    if $color[1] = "+" then guictrlsetcolor($label[$i], 0x00C000)
    if $color[1] = "-" then guictrlsetcolor($label[$i], 0xFF0000)
    $pr = 90/($finalprice*.1)
    $y = 100
    $i += 1
EndFunc

Func _Refresh()
    For $k = 0 to $i Step 1
        $finexchange = ""
        InetGet("http://www.google.com/finance?q=" & $symbol[$k], "C:\Stock\" & $k & ".txt", 1, 1)
        if fileexists("C:\Stock\" & $k & ".txt") then
            $file = fileread("C:\Stock\" & $k & ".txt")
            $readdata = StringInStr($file, "s:" & chr(34) & $symbol[$k])
            $data = Stringtrimleft($file, $readdata)
            $stripdata = StringTrimRight($data, (stringlen($data)- 100))
            $mquote = stringsplit($stripdata, ":" & chr(34), 1)
            $finalname = StringTrimRight($mquote[2], 3) ; final name of symbol
            $exchange = stringsplit($mquote[3], "")
            for $x = 0 to (UBound($exchange) - 1) Step 1
                if (StringIsUpper($exchange[$x])) Then
                    $finexchange = $finexchange & $exchange[$x] ;final exchange
                EndIf
            Next
            $finalprice = StringTrimRight($mquote[4], 4)
            $finalchangepts = StringTrimRight($mquote[6], 4)
            $finalpercent = StringTrimRight($mquote[7], 5) & "%"
            guictrlsetdata($name[$k], $finalname)
            guictrlsetdata($exchangen[$k], $finalprice)
            guictrlsetdata($pricechgn[$k], $finalchangepts)
            guictrlsetdata($percentchangen[$k], $finalpercent)
        EndIf
    Next
EndFunc
Edited by LimeSeed
global $warming = true
Link to comment
Share on other sites

@LimeSeed

I'm sure that the problem comes from the google website.

For the same request from your script you can have 2 different responses.

This shows the response from $stripdata var :

;Normal response
:"AAPL",e:"NASDAQ",p:"332.04",cs:"chg",c:"+5.69",cp:"1.74",cap:"305.90B"}],ct:""},up:"chg",down:"chr

;Bad response (which causes the error)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<h

In order to fix that, I have added an ubound check to avoid the error and it works :

#NoTrayIcon
#include "guiconstants.au3"
#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>


$hGUI = GUICreate("Stocks", 505, 200)
GUISetState(@SW_SHOW)
Global $mquote[30]
Global $symbol[30]
Global $ticker[30]
Global $label[30]
Global $name[30]
Global $exchangen[30]
Global $pricechgn[30]
Global $percentchangen[30]
$ox = 300
$oy = 195
$x = 300
$y = 195
$i = 0
$list = GUICtrlCreateListView(" Stock ", 5, 5, 60, 150)
$exchange1 = GUICtrlCreateListView(" Price USD ", 65, 5, 80, 150)
$pricechg = GUICtrlCreateListView("Pt. Change", 145, 5, 75, 150)
$percentchg = GUICtrlCreateListView("% Change", 220, 5, 75, 150)
$input = GUICtrlCreateInput("EX: AAPL", 5, 170, 100, 20)
$add = GUICtrlCreateButton("Add", 110, 170, 50, 20)
$graph = GUICtrlCreateCheckbox("Graph", 200, 170, 100, 20)

_GDIPlus_Startup()
$hPen1 = _GDIPlus_PenCreate(0xFF00C000, 2)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
_GDIPlus_GraphicsFillRect($hGraphic, 300, 5, 200, 190)
$diff = 2
$timer = TimerInit()

DirCreate("C:\Stock")

While 1
    $diff = TimerDiff($timer) / 1000
    $x = 300 + $diff
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        DirRemove("C:\Stock", 1)
        _GDIPlus_GraphicsDispose($hGraphic)
        _GDIPlus_Shutdown()
        Exit
    EndIf
    If ($msg = $add) And ($i < 30) Then _addstock()
    If $msg = $graph Then
        ;graph
    EndIf
    Sleep(50)
    If $i <> 0 Then _Refresh()
    _GDIPlus_GraphicsDrawLine($hGraphic, $ox, $oy, $x, $y, $hPen1)
    $ox = $x
    $oy = $y
WEnd

Func _addstock()
    $finexchange = ""
    $symbol[$i] = GUICtrlRead($input)
    InetGet("http://www.google.com/finance?q=" & $symbol[$i], "C:\Stock\" & $i & ".txt", 1)
    $file = FileRead("C:\Stock\" & $i & ".txt")
    $readdata = StringInStr($file, "s:" & Chr(34) & $symbol[$i])
    $data = StringTrimLeft($file, $readdata)
    $stripdata = StringTrimRight($data, (StringLen($data) - 100))
    $mquote = StringSplit($stripdata, ":" & Chr(34), 1)

    If UBound($mquote) = 12 Then
        $finalname = StringTrimRight($mquote[2], 3) ; final name of symbol
        $exchange = StringSplit($mquote[3], "")
        For $x = 0 to (UBound($exchange) - 1) Step 1
            if (StringIsUpper($exchange[$x])) Then
                $finexchange = $finexchange & $exchange[$x] ;final exchange
            EndIf
        Next
        $finalprice = StringTrimRight($mquote[4], 4)
        $finalchangepts = StringTrimRight($mquote[6], 4)
        $finalpercent = StringTrimRight($mquote[7], 5) & "%"
        $name[$i] = GUICtrlCreateListViewItem($finalname, $list)
        $exchangen[$i] = GUICtrlCreateListViewItem($finalprice, $exchange1)
        $pricechgn[$i] = GUICtrlCreateListViewItem($finalchangepts, $pricechg)
        $percentchangen[$i] = GUICtrlCreateListViewItem($finalpercent, $percentchg)
        $color = StringSplit($finalchangepts, "")
        If $color[1] = "+" Then GUICtrlSetColor($label[$i], 0x00C000)
        If $color[1] = "-" Then GUICtrlSetColor($label[$i], 0xFF0000)
        $pr = 90 / ($finalprice * .1)
        $y = 100
        $i += 1
    Else
        MsgBox(48, "", "Bad sock name !")
    EndIf
EndFunc   ;==>_addstock

Func _Refresh()
    For $k = 0 To $i Step 1
        $finexchange = ""
        InetGet("http://www.google.com/finance?q=" & $symbol[$k], "C:\Stock\" & $k & ".txt", 1, 1)
        If FileExists("C:\Stock\" & $k & ".txt") Then
            $file = FileRead("C:\Stock\" & $k & ".txt")
            $readdata = StringInStr($file, "s:" & Chr(34) & $symbol[$k])
            $data = StringTrimLeft($file, $readdata)
            $stripdata = StringTrimRight($data, (StringLen($data) - 100))
            $mquote = StringSplit($stripdata, ":" & Chr(34), 1)

            If UBound($mquote) = 12 Then
                $finalname = StringTrimRight($mquote[2], 3) ; final name of symbol
                $exchange = StringSplit($mquote[3], "")
                For $x = 0 to (UBound($exchange) - 1) Step 1
                    if (StringIsUpper($exchange[$x])) Then
                        $finexchange = $finexchange & $exchange[$x] ;final exchange
                    EndIf
                Next
                $finalprice = StringTrimRight($mquote[4], 4)
                $finalchangepts = StringTrimRight($mquote[6], 4)
                $finalpercent = StringTrimRight($mquote[7], 5) & "%"
                GUICtrlSetData($name[$k], $finalname)
                GUICtrlSetData($exchangen[$k], $finalprice)
                GUICtrlSetData($pricechgn[$k], $finalchangepts)
                GUICtrlSetData($percentchangen[$k], $finalpercent)
            EndIf
        EndIf
    Next
EndFunc   ;==>_Refresh

Br, FireFox

Link to comment
Share on other sites

Thanks so much @FireFox however the ubound check didn't work and as of now I ended up pausing the script until the download is complete which slows the script down but its the only way I can get it to work. Perhaps it is because i am in china with miserably slow internet? With the ubound check all i get is the msgbox that says error! bad stock name

global $warming = true
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...