Getting Single Stock Closing Price with Python and OLE

@BetaMaster In JScript, to avoid potential date ambiguities, you should pass the date as a string in the format "YYYY-MM-DD", e.g., "2025-05-22".

// This is NOT AFL but jscript

function padZero(n) {
    if (n < 10) {
        return "0" + n;
    } else {
        return "" + n;
    }
}

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

symbol = "QQQ"; 
y = 2008;
m = 2;
d = 6;
// if you get y,m,d from a JScript Date object keep in mind that months are numbered 0..11

stks = AB.Stocks;
st = stks.Item( symbol );
qts = st.Quotations;
dts = padZero(y) + "-" + padZero(m) + "-" + padZero(d); // NEED to pass as unambiguous string 
// iQty = qts.Count;
// WScript.Echo(" " + symbol + " has " + iQty + " bars - Requested date: " + dts);
qt = qts( dts);
if (qt != null)
{
	cl = qt.Close;
	WScript.Echo("The stock price for " + symbol + " on " + dts + " is: " + cl);
} 
else
{
	WScript.Echo("No stock price for " + symbol + " on " + dts);

}

This was discussed here long time ago.

I vaguely recall that Python's OLE implementation had some trouble with this type of variant conversion ((maybe I'm wrong... better try it).
If it does not work, a workaround is to perform a binary search through the quote collection (accessing each quote by numeric index) until you locate the desired date, or confirm that it’s not present.

2 Likes