OLE import using ruby

I wrote a ruby function to invoke Import(), it runs, returns code 0 or OK, but no data is actually imported. I can import the data manually just fine. I already looked through pointer sin similar https://forum.amibroker.com/t/ole-import-with-vbscript/9559

Environment:
64 bit ruby 2.6.5
64 bit AmiBroker 6.30.5
Windows 8
*.rb file is under C:\Dropbox\Trading, executing it from that directory's cmd window

Ruby code:

import_file = "C:\\Program Files\\AmiBroker\\AmiQuote\\Download\\^RUT.aqh"
  format_file = "C:\\Program Files\\AmiBroker\\Formats\\aqh.format"
  puts "Starting AmiBroker" # C:\Program Files\AmiBroker\Broker.exe 
  ab = WIN32OLE.new("Broker.Application") # C:\Program Files\AmiBroker\Broker.exe
  dirAB = 'C:\\Program Files\\AmiBroker\\'
  db = 'MS'
  ab.LoadDatabase( dirAB + db )
  stks = ab.Stocks()
  qty = stks.Count
  puts "Opening #{db} database with #{qty} pre-existing symbols"
  
  puts "Importing #{import_file} using #{format_file}"
  imp = ab.Import( 0, import_file, format_file )   # Actual Import
  # https://forum.amibroker.com/t/ole-import-with-vbscript/9559/10
  # ' Return codes for ole calls ' 0=ok, 4=no file, 2/3=bad data errors - see import.log for more info
  if imp == 0
    returnCodeExplanation = "OK"
  elsif imp == 4
    returnCodeExplanation = "Import file NOT found"
  elsif imp == 2 || imp == 3
    returnCodeExplanation = "Bad data errors - see import.log for more info"
  else
    returnCodeExplanation = "Unknown"
  end
  puts "Return code: " + imp.to_s + " or " + returnCodeExplanation
  
  puts "Saving #{db} database"   
  ab.SaveDatabase()  # When closing AmiBroker, it says saving database, and only then are db files actually updated.
  puts "Quitting AmiBroker"
  ab.Quit()

Ruby code output:

Starting AmiBroker
Opening MS database with 46980 pre-existing symbols
Importing C:\Program Files\AmiBroker\AmiQuote\Download^RUT.aqh using C:\Program Files\AmiBroker\Formats\aqh.format
Return code: 0 or OK
Saving MS database
Quitting AmiBroker

If you get 46980 bars being returned, the OLE part is fine but not importing.

  1. Make sure no instance of AB is running when you run the ruby script

  2. also, try running your script with Administrator privileges and check incase there is a rights/permission issue

If you get 46980 bars being returned, the OLE part is fine but not importing.
Correct, it can read that many symbols in my db. I use AmiBroker through the OLE in other ruby functions successfully, just not Import()

Make sure no instance of AB is running when you run the ruby script
I'm already doing
system("taskkill /t /f /im Broker.exe")
before earlier code shown.

also, try running your script with Administrator privileges and check incase there is a rights/permission issue
I tried cmd Administrator, it does not help.

Your code says database "MS" - are you by chance trying to import to database that is driven by METASTOCK plugin?
Plugin-driven databases are read-only, see: http://www.amibroker.com/guide/h_workspace.html

If not, maybe it imports, but does not save. To check: do NOT call Quit() and check if instance launched via OLE

  • if imported data are there (before saving)
  • if Help->About shows "registered to: your name"
  • check import.log file - import may return 0 (OK), even if no data were added

Thank you for getting back to me! Yes, in this example I was importing to Metastock database, but the particular symbol I'm trying to import data to has "Use only local database = Yes." When I manually do "Import ASCII" through the UI, then the data does persist for my symbol, even if I restart AB. I have run the same OLE import against my "Data" database, which is all local not MS plugin, with same result, no import happens. OLE import does not write anything to import.log

When testing against my local 'Data' database:

  1. I'm specifying both inputs, the import file and the format file, as full paths, using two backslashes -- is that as expected? OLE Import comes back as return code 0, all OK, as in first import file found.
  import_file = "C:\\Program Files\\AmiBroker\\AmiQuote\\Download\\^RUT.aqh"
  format_file = "C:\\Program Files\\AmiBroker\\Formats\\aqh.format"
  1. If my first input, the import file, is not found, OLE Import comes back with return code 4, or import file not found -- is there any validation for finding the format file?
  2. Is there any chance that either of the inputs is still being looked for as a relative path, to either the directory my script runs from, Amibroker install directory, or working directory of ruby?

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.