Cant import data from ASCII file by using JS - OLE

HI all , my code for importing ASCI data as below, but after I run the JS , it does nothing for me.
my database hasn't updated. Why?? please help. Thanks

AB= new ActiveXObject( "Broker.Application" );

AB.LoadDatabase("C:\Program Files\AmiBroker\REALTIME"); // Change Datbase

AB.Import(0, "C:\Users\user\Desktop\OPEN POSITION\AUDJPY_D1.csv","REAL_TIME_DATA.Format" );

1 Like

When using .Js code you need to enter the file paths using double slashes:

AB = new ActiveXObject( "Broker.Application" );
AB.LoadDatabase("C:\\Program Files\\AmiBroker\\REALTIME"); // Change Datbase
AB.Import(0, "C:\\Users\\user\\Desktop\\OPEN POSITION\\AUDJPY_D1.csv","REAL_TIME_DATA.Format" );
// Add this line too
AB.RefreshAll();

Here an interesting old article discussing how to use JScript to (download) and import data; while some of the content is obsolete, the general guidance is still valid.

6 Likes

The thing is that original poster used double backslashes but he did NOT use code tags as explained. Ignoring forum rules misguides other people who try to help. Code tags are required. Backslash is an escape character in this forum.

Here is what happens when you type DOUBLE BACKSLASH without code tags:

This is double backslash WITHOUT code tags: \

as can be seen there are DOUBLE backslashes yet they are seen only as SINGLE backslash.

Only if you use PROPER CODE TAGS as instructed in "how to use this site" you would get proper display

\\ - this is double backslash WITH CODE TAGS

It is really VERY EASY to format the code properly.

JUST PRESS </> BUTTON BEFORE pasting the code

pastecode3

2 Likes

Hello @beppe

your information here is very valuable to me as newbie. I have a same need and your solution matched my need.
I have another question though, is it possible to have it run every minutes using JS OLE.
I tried to use this script to no avail. I must be doing things wrong.
Please advise.
Thanks.

function fn60sec() {

var AB = new ActiveXObject( "Broker.Application" );
AB.LoadDatabase("C:\\Program Files (x86)\\AmiBroker\\Data"); // Change Datbase
AB.Import(0, "C:\\Users\\Administrator\\dataimport.csv","myimport.format" );
// Add this line too
AB.RefreshAll();

WScript.Echo("Import finished" );

}
fn60sec();
setInterval(fn60sec, 60*1000); 
 

@awicaksono AFAIK there is no setInterval() function in JScript (this is NOT JavaScript but a limited subset variant created a long time ago by Microsoft).

More in general, it is not a good idea to update data in such a way via OLE when you need very frequent updates: it is a lot better to use/write a data plugin.

Anyway, if you want to see if it works in your situation (outside of AmiBroker), you can use the Windows Task Scheduler to launch your script each minute (no need to wrap it in a function)
Ensure that your script does not use any instructions that expects user intervention (like WScript.Echo()) and that the operation can be completed before the next scheduled call.

In any case, beware that this kind of functionality, if not properly handled, can open a can of worm regarding needed permissions to run scripts and application/overlap of operations/multiple instances running, etc.

@beppe what you wrote is not entirely correct.

  1. setInterval() is NOT part of JavaScript (or JScript) language. It does NOT exist in ANY variant of JavaScript language itself. It is provided by HOST, not the language itself. It is a method provided by Window object, and that object is only available within the realms of Web Browser (because web browser exposes Window object) - which is the host for JavaScript language if you run JavaScript within the HTML web page (i.e. inside the browser).

  2. Microsoft JScript IS a JavaScript. Closely speaking it is ECMA script: https://en.wikipedia.org/wiki/ECMAScript

People mistake language with functionality provided by HOST. Host is the application that hosts JavaScript. When Web browser is a host of JavaScript it provides extra functionality (said Window object, DOM objects, etc). Windows SCRIPTING HOST, and that is what you use if you run .js with wscript.exe or cscript.exe or just double click on .js file in Windows, is not a browser, it is other kind of JavaScript host.
Windows scripting host is providing other functionality, than the browser: https://en.wikipedia.org/wiki/Windows_Script_Host (for example WScript built-in object and FileSystemObject)

Hosts can expose objects specific to host to JavaScript language. For example when JavaScript is run from within AFL formula then AFL object that is exposed by AmiBroker is available: http://www.amibroker.com/guide/a_script.html and http://www.amibroker.com/guide/a_aflcom.html

3 Likes

@beppe noted with thanks. I like to try new methods just to keep my head working with my limited ability in programming, but I think it is far beyond my ability to write plugin.
I usually do the import using batch and it is working fine so I want to try something new :slight_smile:

Using Batch http://www.amibroker.com/guide/h_batch.html is a proper solution.