Ok, so i modified the script to compute the max open margin as you said by computing the max of TradeLots * MarginCost.
Here is the code:
function run()
{
BarPeriod = 1440;
StartDate = 1993;
LookBack = 200;
Capital = 10000;
var InitialMargin = 1000;
assetList("AssetsFXCMCFD.csv");
assetHistory("SPY", FROM_YAHOO);
asset("SPY");
Margin = InitialMargin * (Balance / Capital);
// Compute max open margin.
// Note: there is only one open trade, so we don't have to sum things up.
static var MaxOpenMargin = 0;
for (open_trades)
MaxOpenMargin = max(MaxOpenMargin, TradeLots * MarginCost);
vars price = series(priceClose());
var ma = SMA(price, 200);
if (NumOpenLong == 0 && price[0] > ma)
enterLong();
if (NumOpenLong > 0 && price[0] < ma)
exitLong();
if (is(EXITRUN))
printf("nMax open margin: %f", MaxOpenMargin);
}
In the EXITRUN I print the computed max open margin to the console.
When I run it it prints
Max open margin: 5134.946152
But When I look into the trade results it says:
Am I still doing something wrong here or is this a real issue?