I'm trying to apply an AFL function (in this case StDev, but tried others too, e.g. HHV) to a matrix.
The context is custom aggregate metrics in the backtester performance report -- other metrics show, but the one that's matrix-based simply shows a blank.
function pm_backtestWithCustomMetrics(bo) {
bo.Backtest(1);
st = bo.GetPerformanceStats(1); // get stats for all trades
totalTrades = int(st.GetValue("AllQty")); // verified to be > 0, 105 in current case
rMultiples = Matrix(totalTrades, 1, 0);
i = 0;
for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade()) {
rMultiples[i][0] = 33; // simplified with literal for debugging
i += 1;
}
// also tried using literal 5 for the end row, far < total trades
rMultiplesArray = MxGetBlock(rMultiples, 0, totalTrades - 1, 0, 0, True);
stdevVal = StDev(rMultiplesArray, totalTrades); // also tried a number like 5, < totalTrades
// and tried say HHV, also blank
// Always blank, other aggregate, non-matrix metrics show
bo.AddCustomMetric("StDev", stdevVal);
bo.ListTrades();
}
Thanks for the link. My search was finding matrix <-> array compatibility generally.
The provided solution is basically implementing StdDev in particular. But it would be nice to be able to reuse existing functions with custom user arrays.
It just so happens that my current need is std-dev (and the solution would be my fallback approach), but more generally, is my concept of wanting to create arrays and applying existing operations on them invalid?
Many thanks, Tomasz. Appreciate if you could point out what I'm doing wrong in my code, above, if you have capacity. Basically I've created a M by 1 matrix, populated rows, then used MxGetBlock, with asArray set to true. Then, I'm attempting to call the standard function StDev (but also others) on it. Empty results thus far.
I'll try to figure out again, nevertheless, at least I know it's possible, thanks for confirming. Will look at MxToString as well, missed trying that.