Hey folks,
It seems I'm coming across an unexpected behaviour.
So I'm definitely missing something. I'd appreciate if you could point me to the issue.
When I run the same algo in an asset loop, it seems one position is interfering with another. Which I believe can be one of the two:
the use of the "TradeIs*" variables, but I'm unclear how to achieve this "only enter if there is no position open for that asset" condition
or
Capital/Margin is impacting the entry of another asset
Take this code as example (simple code just to show the behaviour) :
function run()
{
set(LOGFILE|PLOTNOW);
setf(PlotMode,PL_ALL);
StartDate = 20161225;
BarPeriod = 1440;
while(asset(loop("DIA", "GLD", "IWM", "QQQ", "SPY")))
//asset("DIA")
{
if(is(INITRUN))
{
assetHistory(Asset, FROM_AV);
}
LookBack = 201;
vars Close = series(priceClose());
vars MA1 = series(SMA(Close,200));
Lots = 100;
if (Close[0] > MA1[0] && !TradeIsOpen){
enterLong();
}
if (TradeIsOpen && TradeIsLong && Close[0] < MA1[0]){
exitLong();
}
//plots individual charts of equity for each asset
char name[40]; // string of maximal 39 characters
strcpy(name,Asset);
var equity = EquityShort+EquityLong;
if(equity != 0) plot(name,equity,NEW|AVG,BLUE);
}
}
If you run this code, you'd get the following result:
Portfolio analysis OptF ProF Win/Loss Wgt%
DIA avg .999 2.20 1/3 9.2
GLD avg .999 21.76 2/2 17.0
IWM avg .999 22.66 1/1 17.0
QQQ avg .1000 ++++ 2/0 28.8
SPY avg .999 113.26 2/1 28.0
DIA:L .999 2.20 1/3 9.2
GLD:L .999 21.76 2/2 17.0
IWM:L .999 22.66 1/1 17.0
QQQ:L .1000 ++++ 2/0 28.8
With the following individual equity plotting (equity plot from different assets):
[img]
https://ibb.co/VBFMmgP[/img]
However, if I comment the asset loop line, and uncomment the "asset('DIA')" line, without changing anything else, I get the following result:
Portfolio analysis OptF ProF Win/Loss Wgt%
DIA:L .999 2.12 4/9 100.0
with the following equity line:
[img]
https://ibb.co/prRNKnw[/img]
It seems that when running the backtest on the asset loop, it considers the whole basket as a portfolio, which one asset prevents the other from entering a position. Is this the expected behaviour ? Is there anything that I am missing so I can run each asset test (or even algo) completely independently from the other ?