Ruby programming, was: Can OLE access external data?

Can AmiBroker OLE access external data such as Metastock using the plug-in described at http://www.amibroker.com/guide/h_extsources.html ?

I wrote a number of functions that use LOCAL Amibroker database and sucessfully access
a) Stocks and Quotations objects
b) run Explorations and Backtests through AnalysisDocs object

When I switch to using MS database

dirAB = 'C:\\Program Files\\AmiBroker\\'
#db = 'Data'
db = 'MS'
ab = WIN32OLE.new("Broker.Application") # C:\Program Files (x86)\AmiBroker\Broker.exe
ab.LoadDatabase( dirAB + db )

which has the symbols at C:\Program Files\AmiBroker\MS but quotes at C:\MetaStock Data\
a) Stocks object comes back with Ticker, FullName, but Quoatations are blank
b) AnalysisDocs does not return anything

This is related to my previous topic http://forum.amibroker.com/t/import-metastock-data-via-ole/4535/7

Yes it can BUT you need to first create database in AmIBroker and configure it to use Metastock plugin. Only after doing that you can access MS data.

That would be a quick solution if I did not have the MS db created, haha! MS was of course set up, quotes come up through the plug-in in the AmiBroker GUI, and Analysis - Explorations work using same *.apx file called in the OLE automation. Please see below for how MS db was set up and that Explrorations are working in the GUI:

MS%20db%20setup

OK, but then from the screenshot I can see that Analysis is producing output, so it should work. Your problem may be caused by the fact that Current Working directory (CWD) is NOT set properly. You need to start AmiBroker from shortcut so current working directory (aka "Start In" directory) is set properly as it tries to load plugins from RELATIVE path ("Plugins") from current working directory. If CWD is not set, then it won't find plugin.

Yes, Tomasz, it is most likely CWD issue.. just like in http://forum.amibroker.com/t/amiquote-symbol-translation-table-not-loaded-through-ole-automation/3869/12 -- for now, just like there with AQ 3.29, would you also change future version of AB to "change the current working directory for you if it is set incorrectly?"

You need to start AmiBroker from shortcut so current working directory (aka "Start In" directory) is set properly as it tries to load plugins from RELATIVE path ("Plugins") from current working directory

This is how AB is invoked via OLE in my ruby script.. I'm NOT starintg it from a shortcut. I still do now know how to change CWD to C:\Windows\System32\ :

ab = WIN32OLE.new("Broker.Application")

Btw, this is how I tried to start AmiBroker in my code, doing Dir.chdir to CWD does not help:

  puts "Starting AmiBroker 32-bit"
  #Dir.chdir("C:\\Windows\\System32\\") do # change to CWD
  Dir.chdir("C:\\Program Files\\AmiBroker\\") do # change to CWD, as per Sysinternals Process Explorer
  @ab = WIN32OLE.new("Broker.Application") # C:\Program Files (x86)\AmiBroker\Broker.exe
  end
  puts "Opening #{db} database"
  @ab.LoadDatabase( dirAB + db )

Changing CWD in ruby does not affect anything but ruby itself.

As I wrote - you need to run AmiBroker from shortcut.

Alternatively use version 6.21 or higher, it first checks Plugins folder relative to "EXE" file location before going to CWD.

Thank you for the fix in AB 6.21, Tomasz.

It sounds like you are saying it will not work with ruby automation unti then.. Amibroker invoked below has incorrect Current Working Directory, so ab.AnalysisDocs or Quotations won't come back with anything through the Metastock plug-in. I am looking for an automated solution in ruby, so starting AmiBroker from a shortcut won't help, unless I could reference running instance of Broker.exe as in ab.AnalysisDocs

ab = WIN32OLE.new("Broker.Application")  # has incorrect CWD
ab.LoadDatabase( dirAB + db )
ab.AnalysisDocs.Open( dirTrading + analysisFile ) 
newAnalysis.Run( runType ) # nothing comes back as Metastock plugin not found

No, I am just saying that you have to RUN AmiBroker with correct working directory BEFORE you call OLE. Once AmiBroker instance is running, OLE would automatically connect to already running instance (with proper directory).

Connecting to already running instance "just works" without any action from your side. The only thing you need to care is to run correct bitness. So if your ruby is 32-bit you should use 32-bit app and if your ruby is 64-bit then you need to use 64-bit app. That at least how it works in scripts: http://www.amibroker.com/kb/2015/01/12/ole-automation-scripts-with-32-and-64-bit/

We are not "ruby support" here. I don't use ruby and don't know ruby. Please google to get answers:

Also:

https://ruby-doc.org/core-2.2.0/Process.html

And also
https://ruby-doc.org/stdlib-2.3.1/libdoc/win32ole/rdoc/WIN32OLE.html

(see WIN32OLE.connect if Ruby's new is not able to connect)

You need something like Process.spawn in ruby (again I am NOT USING Ruby, so all I got is from GOOGLE). Please ask on Ruby forum how to run executable with PROPER DIRECTORY set.

2 Likes

Here's the SOLUTION for future reference:

dirAB32 = 'C:\\Program Files (x86)\\AmiBroker\\'
db = 'MS'
Dir.chdir(dirAB32) do
    puts "Temporarily changed current working directory to #{Dir.getwd}"
    puts "Starting AmiBroker 32-bit"
    pid = spawn('Broker.exe')
  end
  
  puts "Current working directory is #{Dir.getwd}"
  sleep 5
  puts "Connecting to already running AB via OLE"
  ab = WIN32OLE.new("Broker.Application") # also connects to already running Broker process
  puts "Opening #{db} database"
  ab.LoadDatabase( dirAB + db )
2 Likes

So, you marked as "reference solution" your own post that is copy of what I told you step by step and you did not bother to say "thank you" to the person who spent time solving your problem.

A few words for you from ancient time:

"Nothing, Lucilius, is ours, except time. We were entrusted by nature with the ownership of this single thing, so fleeting and slippery that anyone who will can oust us from possession. What fools these mortals be! They allow the cheapest and most useless things, which can easily be replaced, to be charged in the reckoning, after they have acquired them; but they never regard themselves as in debt when they have received some of that precious commodity, – time! And yet time is the one loan which even a grateful recipient cannot repay" - Seneca Letters to Lucilius, Letter 1.

4 Likes

Thank you for your time Tomasz.