AutoHotkey Community

It is currently May 27th, 2012, 12:06 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: January 30th, 2011, 4:55 am 
Offline

Joined: February 23rd, 2008, 6:52 pm
Posts: 30
I have a script that helps me download data from a stock charting program.

A command needs to be sent to the program for each stock one at a time and then wait for the download to complete.

I have a simple GUI to choose symbols and date ranges.

The logic I am using is:

Click Download Button -> Download Data (using sleep to wait a specified number of seconds)

The issue I am having is that some stocks download much more quickly than others. If I would like to be able to skip to the next symbol by clicking a button on the Gui, I don't know how to do that since the script is in sleep mode.

How else can I accomplish this?

If it helps, here is the undoubtedly inefficient script :oops::

Code:
#SingleInstance force
#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, 1

FileReadLine, Xpos, NTReplayDownloader.ini, 1
FileReadLine, Ypos, NTReplayDownloader.ini, 2

ActiveDownload = 0

Gui, Add, MonthCal, x142 y40 w240 h170 vStartDate
Gui, Add, MonthCal, x392 y40 w240 h170 vEndDate
Gui, Add, ListBox, R19 vSymbol x10 y45 w120 Multi AltSubmit,

InstrumentsLoop = 0
Loop, Read, Instruments.txt
   {
     InstrumentsLoop += 1
     GuiControl, , Symbol, %A_loopReadLine%|
     Ticker%InstrumentsLoop% := A_LoopReadLine
     NumberOfSymbols := A_Index
   }

Gui, Add, Button, x300 y239 w80 h30 Default, Download
Gui, Add, Button, x390 y239 w40 h30 , Stop
;Gui, Add, Button, x435 y239 w40 h30 , Go
Gui, Add, Button, x18 y15 w100 h20 , Reload Symbols
Gui, Add, Text, x182 y20 w160 h20 +Center, Start Date
Gui, Add, Text, x432 y20 w160 h20 +Center, End Date
Gui, Add, Text, x212 y220 w90 h20 , Seconds To Wait:
Gui, Add, Edit, x302 y217 w76 h20 vWaitSeconds, 150
Gui, Add, Text, x182 y285 w408 h14 +Center vNotify,
Gui, Add, CheckBox, x395 y214 w105 h26 vShowTips checked, Show TrayTips
Gui, Add, Text, x182 y271 w408 h14 +Center vTemp, this is a BETA version, use Ctrl-Alt-Del if program hangs
Gui, Show, x%Xpos% y%Ypos% h306 w635, NT Replay Downloader
return ; Gui

ButtonStop:
BlockInput, Off
SetTimer, Countdown, Off
GuiControl,, Notify, Stopped!
Gui, Color, Default
ActiveDownload = 0
loopDate = %StartDate%
;Pause
Stopped := true
return ; ButtonStop

ButtonGo:
goto, ButtonDownload
return

ButtonReloadSymbols:
GuiControl,, Notify, Reloading
GuiControl, , Symbol, |
SymbolLoop = 0
Loop, Read, Instruments.txt
   {
     SymbolLoop += 1     
     GuiControl, , Symbol, %A_loopReadLine%|
     Ticker%SymbolLoop% := A_LoopReadLine
     NumberOfSymbols := A_Index
   }
GuiControl,, Notify, Symbols Reloaded
;msgbox % NumberOfSymbols
return ; ButtonReloadSymbols

ButtonDownload:
;msgbox, Ha!, you clicked didn't you!
Stopped := false
Gui, Submit, NoHide
calcWaitSeconds := WaitSeconds*1000

;NTdown = 0;
LoopCount = 0
Loop, parse, Symbol, |
{
   
   ;if LoopCount =  ; see http://www.autohotkey.com/docs/Variables.htm
   ;{
   ;msgbox, empty
   ;LoopCount := 1
   ;}
   
   ;msgbox % StartDate
   if (Stopped = true)
   break
   
   LoopCount := LoopCount + 1  ; Keep track where we are in the loop in case Download button gets pressed again.
   ;msgbox, %LoopCount%
   ;msgbox, %a_index%

   if (LoopCount < A_Index)
   {
      continue  ;skip this if it has already been done.
   }

   thisSymbol := A_LoopField  ; Store this position number in the next array element.
   
    NTdown := A_LoopField - 1 ; Store this position number in the next array element.
   
   if (thisSymbol <> "")
   {
      ActiveDownload = 0
      Gosub, WaitForNT
   }

} ; Symbol parsing loop

LoopCount = 0 ; see arrays in help file

SetTimer, Countdown, Off
BlockInput, Off
if (Stopped <> true) ; leave the 'Stopped!' text there if user intervened
{
   GuiControl,, Notify, Task Complete!
}
Gui, Color, Default
ActiveDownload = 0
;loopDate = %StartDate%
      
If (IsError = true)
{
   
   MsgBox, 48, Error Log, NT Replay Downloader`n`nSome data was not available`nsee NTReplayDownloader-log.txt`nin installation folder
}

Return ; ButtonDownload

WaitForNT:

if (Stopped = 1)
{
   return
}
   Gui, Submit, NoHide
   calcWaitSeconds := WaitSeconds*1000
   BlockInput, On
      
   IfWinExist, Download Replay Data
      {
         WinClose, Download Replay Data
      }
   
   if ActiveDownload = 0
      {
         GuiControlGet, SymbolText,,Symbol,Text 
         Gui, Color, FFD334
         Diff := EndDate
         Diff -= StartDate, days
         
         if EndDate < %StartDate%
         {
            BlockInput, Off
            msgbox, Please select a valid date range
            SetTimer, CountDown, Off
            BlockInput, Off
            Gui, Color, Default
            ActiveDownload = 0
            loopDate = %StartDate%
            return
         }
         
         Diff := 1000 / %Diff%
         GuiControl,, DLprogress, %Diff%
         
         if (thisSymbol = "")
         {
            BlockInput, Off
            msgbox, Please select a symbol
            SetTimer, CountDown, Off
            BlockInput, Off
            Gui, Color, Default
            ActiveDownload = 0
            loopDate = %StartDate%
            return
         }

         loopDate = %StartDate%
         StringRight, StartDay, loopDate, 2
         StringMid, StartMonth, loopDate, 5, 2
         StringLeft, StartYear, loopDate, 4

         StringRight, EndDay, EndDate, 2
         StringMid, EndMonth, EndDate, 5, 2
         StringLeft, EndYear, EndDate, 4
         
         IfWinExist, Download Replay Data
         {
            WinClose, Download Replay Data
         }
      
         ActiveDownload = 1
      }

      while loopDate <= EndDate
         {
            if (Stopped = 1)
            {
               break
            }
            
            Gui, Submit, NoHide
            calcWaitSeconds := WaitSeconds*1000
            WinWait, Control Center,
            IfWinNotActive, Control Center, , WinActivate, Control Center,
            WinWaitActive, Control Center,

            ifWinExist, Replay Data
            WinClose, Replay Data

            ifWinNotExist, Download Replay Data
            {
               Send, {ALTDOWN}f{ALTUP}ud
               WinWait, Download Replay Data,
               IfWinNotActive, Download Replay Data, , WinActivate, Download Replay Data,
               WinWaitActive, Download Replay Data,
            }
            
            Send {Tab}{Down %NTdown%}{Tab}
         
            Send %StartMonth%{Right}%StartDay%{Right}%StartYear%
            BlockInput, Off
            
            if ShowTips = 1
            {
               traytip, Downloading, % Ticker%thisSymbol% . ": " . StartMonth . "/" . StartDay . "/" . StartYear
            }

            Sleep, 1000
            Send {Tab 3}{Enter}{Tab 2}
         
            StringMid, NotifyDay, loopDate, 7, 2
            StringMid, NotifyMonth, loopDate, 5, 2
            StringLeft, NotifyYear, loopDate, 4
         
            loopDate += 1, days
            ;NOTE ABOVE CONVERTS TO FULL DATETIME FORMAT
            StringMid, StartDay, loopDate, 7, 2
            StringMid, StartMonth, loopDate, 5, 2
            StringLeft, StartYear, loopDate, 4
            StringLeft, loopDate, loopDate, 8
            BlockInput, Off

            GuiControl,, DLprogress, %Diff%
            
            SecondsRemaining := WaitSeconds
            SetTimer, Countdown, 1000
            Sleep, %calcWaitSeconds%
            
            IfWinExist, Error
            {

               FileAppend, % Ticker%thisSymbol% . "," . StartMonth . "/" . StartDay . "/" . StartYear . ", Connection to data server is busy or not available right now.", NTReplayDownloader-Log.txt
               IsError := true
               WinClose, Error
            }   
   
            IfWinExist, Replay Data, no L1
            {

               FileAppend, % Ticker%thisSymbol% . "," . StartMonth . "/" . StartDay . "/" . StartYear . ",No L1 Data (note L1 error also prevents L2 from downloading even if available)", NTReplayDownloader-Log.txt
               IsError := true
               WinClose, Replay Data
            }   
            
            IfWinExist, Replay Data, no L2
            {

               FileAppend, % Ticker%thisSymbol% . "," . StartMonth . "/" . StartDay . "/" . StartYear . ",No L2 Data, NTReplayDownloader-Log.txt"
               IsError := true
               WinClose, Replay Data
            }   
            
         } ; while loop
         
      loopDate = %StartDate%
Return ; WaitForNT

Countdown:
if (SecondsRemaining >= 1)
{
   SecondsRemaining := SecondsRemaining - 1
}
else
{
   SecondsRemaining := 0
}
GuiControl,, Notify, % "Sent " . ticker%thisSymbol% . ": " . NotifyMonth . "/" . NotifyDay . "/" . NotifyYear . " -> " . SecondsRemaining . "."
Return ; Countdown

GuiClose:
gosub, WriteToFile
ExitApp
Return

WriteToFile:
FileDelete, NTReplayDownloader.ini
WinGetPos,X,Y,W,H,NT Replay Downloader
FileAppend,%X%`n%Y%, NTReplayDownloader.ini
Return ; WriteToFile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2011, 7:12 am 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6074
Location: San Diego, California
Your script is long with several unknowns, but it compiles :D

>>The issue I am having is that some stocks download much more quickly than others. If I would like to be able to skip to the next symbol by clicking a button on the Gui, I don't know how to do that since the script is in sleep mode.

>>How else can I accomplish this?
This appears to be the sleep you are talking about, but notice that you are already using settimer -- to update your gui display!!
Code:
SetTimer, Countdown, 1000
Sleep, %calcWaitSeconds%


Is the 'downloader' a program, or does it use a webbrowser?

If the downloader is a program, then you shuld be able to use 'Window Spy' (in you AHk folder) to get more information on the controls. This may be helpful in the next step.

If you were sitting right there, how could you determine that the download is finished?
Could the settimer routine also 'see' what you 'see'?

If it can:
Instead of a sleep, use a loop that keeps looping because a variable is 1
when the settimer routine 'sees' the download is done, it sets the variable=0.

The main part of your program (that is looping) stops looping and continues with the next step.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2011, 5:40 pm 
Offline

Joined: February 23rd, 2008, 6:52 pm
Posts: 30
Quote:
If you were sitting right there, how could you determine that the download is finished?

Yes

Quote:
Could the settimer routine also 'see' what you 'see'?

not easily, Windowspy can't 'see' the text

Quote:
Instead of a sleep, use a loop that keeps looping because a variable is 1
when the settimer routine 'sees' the download is done, it sets the variable=0.

I used settimer and assigned the variable change to a button and it is working to my satisfaction, thanks much!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Yahoo [Bot] and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group