How to accept user input in broker.application javascript

Could any one please help me to change this code so that user can enter a value for the variable Days .

Please see lines 33 to 52 in the code below.

I have tried to use my rather limited knowledge of java scripts and attempted to use the following:
daysInput = shell.InputBox("Please enter the number of days to clean zero-volume quotes:", "Input for Days");
and the a short logic to validate the entered value.

It looks however the broker object does not support (I could be totally wrong here) user input using shell.inputbox.

And to be plain simple, I do not know what I need to do to accept a user input for a number of days, once the script starts running.

I do appreciate and would be very grateful for any guidance on this,

Thank you kindly.

// Windows script to clean zero-volume quotes from AmiBroker data
// This script runs from outside of AmiBroker

function RemoveZeroVolumeQuotes(stockName, days) {
    var AB = new ActiveXObject("Broker.Application");
    var stock = AB.Stocks(stockName);
    var quotations = stock.Quotations;
    var totalQuotes = quotations.Count;

    // Approximate number of quotes per day (1-minute intervals)
    var maxQuotesPerDay = 384;

    // Adjust maxQuotesPerDay if there are fewer than expected quotes
    if (totalQuotes < days * maxQuotesPerDay) {
        maxQuotesPerDay = Math.floor(totalQuotes / days);
    }

    var removedCount = 0;

    // Loop through the last 'days' of quotes and remove those with zero volume
    for (var i = totalQuotes - 1; i >= totalQuotes - days * maxQuotesPerDay; i--) {
        var quote = quotations.Item(i);
        if (quote.Volume === 0) {
            removedCount++;
            quotations.Remove(i);
        }
    }

    AB.RefreshAll();
    return removedCount;
}

// Create a WScript.Shell object
var shell = new ActiveXObject("WScript.Shell");

// Show an input box to prompt the user for the number of days
var daysInput = shell.InputBox("Please enter the number of days to clean zero-volume quotes:", "Input for Days");

// Check if the user pressed "Cancel"
if (daysInput === null) {
    WScript.Echo("User cancelled the input.");
    WScript.Quit();
}

// Try to parse the input as an integer
var days = parseInt(daysInput, 10);

// Check if the entered value is a valid number
if (isNaN(days) || days <= 0) {
    WScript.Echo("Please enter a valid number of days.");
    WScript.Quit();
}

var totalRemovedQuotes = 0;
var stocks = [
    "360-ASX-S-AUD", 
    "A2M-ASX-S-AUD", 
    "AGL-ASX-S-AUD", 
    "ALD-ASX-S-AUD"
];

// Remove zero-volume quotes from each stock and accumulate the removed count
for (var i = 0; i < stocks.length; i++) {
    totalRemovedQuotes += RemoveZeroVolumeQuotes(stocks[i], days);
}

WScript.Echo("Cleanup Completed\n" + totalRemovedQuotes + " quotes with zero volume have been removed.");

You have total mixup of concepts.

"Broker object" is completely SEPARATE from Shell object. Shell is exposed by Windows. It has nothing to do with AmiBroker. The same way AmIBroker has nothing to do with Windows.

If you are running things inside AmiBroker you should use AmIBroker AFL functions such as AFL Function Reference - PARAMSTR to get input, not Shell.

Shell is part of Windows, not part of AmiBroker.

3 Likes

oh well, Tomasz has just responded. Adding to his post...

InputBox() is a feature of VB script, not JScript, so it won't work.

what next?
While I spent some time trying an idea, it works but its not worth it.
To make it work, I exec'd a VB script inside the JScript.

You should rather use an argument to your jscript and then

cscript.exe my_jscript.js arg1 arg2

also note, cscript is for console and wscript for windows UI.

And to test what i did, it gets even worse, because I capture in VB script, write to a file, then read in jscript and delete temp file. ( Again, it's not worth it )
I was trying to directly return from VB Script shell to JScript variable but its not working.

Thank you so very much Tomasz.

I created an AFL exploration which executes the javascript inside Amibroker and is using standard
PARAM to accept the user input.

Works very well.

Thanks again.