[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] OLE start AB download and export vbs script



PureBytes Links

Trading Reference Links

Hi 
Please take a look at the attached vbs script.
It starts an AB process, downloads current quotes from Yahoo
and writes a text file with the past 10 quotes of the symbols
that are in the symbol.txt file. 
I would like to use AA ApplyTo & Range
but don't know how.
Thanks to dingo and Fred for there help.
Olli


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Try Online Currency Trading with GFT. Free 50K Demo. Trade 
24 Hours. Commission-Free. 
http://us.click.yahoo.com/RvFikB/9M2KAA/U1CZAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 
' Version 18.2.2006
' some infos: the program downloads quotes from Yahoo and saves them in a database
'             and it exports some tickers (in symbol.txt) into a textfile (wiso.txt) 
'             it writes alias instead of ticker symbol and has a special header and footer 
' for export only (without a download) use c:\ImportExport.vbs EXPORT 
' for download only use c:\ImportExport.vbs DOWNLOAD
' To run the program you need an registered version of AmiQuote
' 
' debug mit "CSCRIPT //X scriptname.vbs"
' VBScript Run-time Errors @
' http://msdn.microsoft.com/scripting/vbscript/doc/vsmscRunTimeErrors.htm 
' VBScript Syntax Errors @
' http://msdn.microsoft.com/scripting/vbscript/doc/vsmscSyntaxErrors.htm
' nice page http://www.msexchangefaq.de/code/vbscript.htm 
' in case there is an error message that says 'insufficient memory' look at the answer from
' Tomasz Janeczko [amibroker@xxxxxx]  Sent: Sa 28.08.2004 08:59
' 

Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const cABPath = "C:\Program Files\Amibroker\"                  ' <----- Change your path to amibroker here

Class StartStop_AB 
 Public ABPath, strComputer, Program, errReturn, sleeptime
 Private intProcessID

 Private Sub Class_Initialize
   strComputer = "."
   ABPath =  cABPath                   
   Program = "Broker.exe"
   Sleeptime = 2000
 End Sub 

 Public Sub StartAB
 ' From The Portable Script Center / Creating a Process in a Hidden Window
 ' File: scriptcenter.chm
 
   Const HIDDEN_WINDOW = 12
   Dim objWMIService, objStartup, objConfig, objProcess
   Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set objStartup = objWMIService.Get("Win32_ProcessStartup")
   Set objConfig = objStartup.SpawnInstance_
   objConfig.ShowWindow = HIDDEN_WINDOW
   Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
   errReturn = objProcess.Create(ABPath & Program, ABPath ,objConfig , intProcessID)
   If errReturn <> 0 then
     WScript.Echo "Error: " &  errReturn & " Programstop"
     WScript.Quit
   End If
   WScript.Sleep sleeptime  
   Set objProcess = nothing
   Set objConfig = nothing
   Set objStartup = nothing
   Set objWMIService = nothing
 End Sub

 Public Sub StopAB
   Dim colProcessList, objProcess, objWMIService
   Const cstrComputer = "."
   If strComputer = "" Then strComputer = cstrComputer 
   Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colProcessList = objWMIService.ExecQuery _
       ("Select * from Win32_Process Where ProcessID=" & intProcessID)
   For Each objProcess in colProcessList
     objProcess.Terminate()
   Next
   Set colProcessList = nothing
   Set objWMIService = nothing
 End Sub

 Private Sub Class_Terminate
   'Do nothing
 End Sub
End Class
' ----------------------------------------------------------------------


Class FileObject
  Private ForReading, ForWriting, ForAppending
  Private fso, WshShell, oAB
  Public ABPath, database, Path, outputfilename, current, symbolfilename, NofQuotes

  Private Sub Class_initialize
     ABPath = cABPath                                                  
     database = "Yahoo_DEU"                                            ' <- Change your databasename here
     outputfilename = "wiso.txt"      	                               ' <- Change your output filename here                                 
     symbolfilename = "symbol.txt"                                     ' <- filename with symbols for export
     current = 1                                                       ' <- if 0 = historical quotes, 1 = current quotes
     NofQuotes = 10                                                    ' <- Change the number of quotes to export here

     Set fso = CreateObject("Scripting.FileSystemObject")
     Set WshShell = WScript.CreateObject("WScript.Shell")
     Set oAB = CreateObject("Broker.Application")
     path = fso.GetAbsolutePathName(".") & "\"
  End Sub
  
  Private Sub Class_terminate
    Set fso = nothing
    Set WshShell = nothing
    Set oAB = nothing
  End Sub

  Private Function FileExist(filename)
    FileExist = fso.FileExists(filename)
  End Function

  Private Sub FileDelete(filename)
    Dim f
    If fso.FileExists(filename) Then
       Set f = fso.GetFile(filename)
       f.delete
       Set f = nothing
    End If
  End Sub

  Private Function ReadFile(filename)
    Dim f, strContents
    Set f = fso.OpenTextFile( filename , ForReading)           
    strContents = f.ReadAll
    f.Close
    ReadFile = strContents
    Set f = nothing
  End Function

  Private Function ReadFile2Arr(filename)
    Dim f, i, aFields()
    If FileExist(filename) Then
        Set f = fso.OpenTextFile (filename, ForReading)
    Else 
       WScript.Echo "Can not find " & filename & " Programhalt"
       WScript.Quit
    End If
    i = 0
    Do Until f.atEndOfStream
      Redim Preserve aFields(i)
      aFields(i) = f.ReadLine
      i = i + 1
    Loop
    f.Close
    ReadFile2Arr = aFields
    set f = nothing
  End Function

  Public Sub ExportAB  
    Dim  f, oStocks, oStock, oQuote, oAA
    Dim filenames, ticker, Qty, i
    If Not oAB.LoadDatabase(ABPath & database) Then
    	 WScript.Echo "Couldn't find the database: " & ABPath
    	 WScript.Echo "Programhalt"
    	 WScript.Quit
    End If  
    FileDelete(path & outputfilename)
    Set f = fso.OpenTextFile(outputfilename, ForWriting, true)
    f.WriteLine("VERSION;1;2")
    f.WriteLine("KURSE")
    filenames = ReadFile2Arr(path & symbolfilename)
    For each ticker in filenames
      WScript.Echo "Export of " & ticker & " start"
      set oStocks = oAB.Stocks
      set oStock = oStocks.Item(cstr(ticker))   
      ' Set oAA = oAB.Analysis;
      ' oAA.RangeMode = 2;
      ' oAA.RangeN = 10;
      ' oAA.ClearFilters();
      ' oAA.ApplyTo = 2;
      ' oAA.Filter(0, "watchlist") = 63; 
      ' WScript.Echo oStocks.Count
       Qty = oStock.Quotations.Count
      ' WScript.Echo "Qty: " & Qty
      If Qty > NofQuotes Then
         For i= Qty - NofQuotes to Qty-1
           Set oQuote = oStock.Quotations(i) 
           ' WScript.Echo oStock.Alias & ";" & CStr(oQuote.Date) & ";" & _
           '              oQuote.Volume & ";" & oQuote.Open & ";" & _ 
           '              oQuote.High & ";" & oQuote.Low & ";" & oQuote.Close 
            f.WriteLine  oStock.Alias & ";" & CStr(oQuote.Date) & ";;" & _
                         oQuote.Volume & ";" & oQuote.Open & ";" & _ 
                         oQuote.High & ";" & oQuote.Low & ";" & oQuote.Close 
          Set oQuote = nothing       
        Next
      End If  
      set oStock = nothing
      set oStocks = nothing 
    Next
    f.WriteLine("ENDE")
    f.Close
    set f = nothing
  End Sub
 
  Public Sub Download_YAHOO 
    Dim AQ
    Set AQ = CreateObject("AmiQuote.Document")
    If (oAB.LoadDatabase(ABPath & database)) Then 
      AQ.AutoImport = true ' no automatic import
      If current Then 
        AQ.Source = 1 ' Yahoo Current
      Else 
        AQ.Source = 0 ' Yahoo Historical
      End If
      AQ.GetSymbolsFromAmiBroker
      AQ.Download  ' starts download
      while( AQ.DownloadInProgress OR AQ.ImportInProgress )
         WScript.sleep 5000 ' wait 5 seconds while AmiQuote is downloading 
      wend
      AQ.Import ' // starts download
      while( AQ.ImportInProgress )	
         WScript.sleep 5000'  wait 5 seconds while AmiQuote is importing
      wend
      WScript.echo "Download and import complete"
      WScript.Echo "Wait 10 seconds"
      oAB.RefreshAll
      WScript.sleep 5000
      oAB.SaveDatabase
      WScript.sleep 5000
    End If
    Set AQ = nothing   
  End Sub

End Class
' --------------------------- Main --------------------------------------
  Dim oF , AB
  If WScript.Arguments.Count > 0 Then
    Set AB = new StartStop_AB
    AB.StartAB
    Set oF = new FileObject
    If UCase(WScript.Arguments.Item(0))="EXPORT" Then oF.ExportAB
    If UCase(WScript.Arguments.Item(0))="DOWNLOAD" Then oF.Download_YAHOO
    If UCase(WScript.Arguments.Item(0))="BOTH" Then 
       oF.Download_YAHOO
       WScript.Sleep 10000  ' Wait 10 sec till the database is saved
       oF.ExportAB  
    End If 
    AB.StopAB
    Set oF = nothing
    Set AB = nothing    
  Else
     WScript.Echo "Commandline options:"
     WScript.Echo " EXPORT (c:\ImportExport.vbs EXPORT) "
     WScript.Echo "     exports the last 10 quotes to a file called wiso.txt"
     WScript.Echo " DOWNLOAD (c:\ImportExport.vbs DOWNLOAD)"
     WScript.Echo "     downloads from Yahoo the current quotes"
     WScript.Echo " BOTH (c:\ImportExport.vbs BOTH)"
     WScript.Echo "     does EXPORT and DOWNLOAD "
  End If

'------------------------------------------------------------------------------------------------------------------
' From: Tomasz Janeczko [amibroker@xxxxxx]
' Sent: Sa 28.08.2004 08:59
' Subject: Re: [amibroker] Insufficient memory to perform operation
'
'Hello,
'
'Please go to Tools->Preferences "Data" tab and enter 11 into "in-memory cache" field and you should have no problems.
'
'Best regards,
'Tomasz Janeczko
'amibroker.com
'----- Original Message -----
'From: "aequalsz" <aequalsz@xxxxxxxxx>
'To: <amibroker@xxxxxxxxxxxxxxx>
'Sent: Saturday, August 28, 2004 2:39 AM
'Subject: [amibroker] Insufficient memory to perform operation
'
'
'> Downloaded the entire US stock database from Yahoo and was running the
'> script file, Cleanup.js when I got the message above about
'> insufficient memory.  Are there any AB settings or Windows XP or
'> system settings to fix this problem?  Currently have a Gigabyte of RAM
'> using an Athlon 64 3000 CPU with Windows XP home.  TIA. 
'> 
'> Aequalsz