Assets in Loops are impacted

Posted By: Diabolus

Assets in Loops are impacted - 02/17/21 05:02

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) :

Code
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 ?
Posted By: jcl

Re: Assets in Loops are impacted - 02/17/21 13:11

Have you read the manual about the asset function, especially the remarks about the order of asset calls?
Posted By: Diabolus

Re: Assets in Loops are impacted - 02/18/21 01:52

I double checked the "asset" manual page, and the only note related to "order of asset calls" I found was something that I don't think is applicable in this case.

I believe you are referring to this remark:
The order of asset() calls matters. If BR_FLAT is not set, the bars are created from the historical ticks of the first asset. Gaps in the price history of the first asset are therefore reflected in the price data of all further assets. Price histories have normally gaps when the asset is not traded, as during the weekend or outside market hours. Therefore select first the asset with the most complete price history (for instance, a currency pair that is traded 24 hours). When a subsequent asset has a gap where the first asset has none, the gap is filled from the previous bar. This produces a different price curve and can cause indicators to behave differently in a multi-asset portfolios, dependent on asset order. Otherwise use the BR_FLAT flag or don't combine assets with different market hours.


If this is what you are referring to, I don't believe it would be applicable as in both examples, DIA was the first asset in the loop, and the one tested by itself. Aside from this, most of these would have the same trading hours, as most (with the exception of QQQ) are negotiated in the NYSE, which as mentioned wouldn't impact as it was not the first traded asset
Posted By: strimp099

Re: Assets in Loops are impacted - 02/18/21 05:54

I think this is the relevant text. You're setting LookBack after you call asset in your loop.

The place of an asset call (if any) in the script matters. All variables and flags that affect the creation of bars, such as BarPeriod, BarZone, LookBack, Detrend, StartDate, EndDate, TICKS, BarMode, UpdateDays, AssetList, History etc. must be set before calling asset(). Otherwise the simulation period is unknown at asset loading and either a default period is used, or the script will produce an Error 030 message. All parameters specific to an asset, such as Spread, Commission, etc., as well as all functions that use asset parameters or prices, such as price(), optimize(), advise(), etc. must be used after calling asset().
Posted By: Diabolus

Re: Assets in Loops are impacted - 02/19/21 04:29

Thanks for your suggestion. I've moved the LookBack to right after the BarPeriod, and before the asset call.

Unfortunately I'm still getting the same outcome (nothing changed).
Posted By: Spirit

Re: Assets in Loops are impacted - 02/20/21 11:04

When results are still wrong you have probably more bugs in your script. I'm not this good in script reading but If you cannot solve the mystery here, you can always get a support ticket and ask support. There are also valuable hints here: https://manual.zorro-project.com/trouble.htm
Posted By: ozgur

Re: Assets in Loops are impacted - 02/20/21 12:40

Try below code.

Also, I think you are supposed to use Trade variables in a TMF or a trade loop such as for(current_trades) { ... }.

Code
function run()
{
	set(LOGFILE|PLOTNOW);
	setf(PlotMode,PL_ALL);
	
	BarPeriod = 1440;
	LookBack = 201;
	
	StartDate = 20161225;
	
	//asset("DIA");
	while(asset(loop("DIA", "GLD", "IWM", "QQQ", "SPY")))
	{
		vars Close = series(priceClose());
		vars MA1 = series(SMA(Close,200));
		
		Lots = 100;
		
		if (Close[0] > MA1[0] && NumOpenLong == 0) {
			enterLong();
		}
		
		if (NumOpenLong > 0 && 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;
		plot(name,equity,NEW|AVG,BLUE);
	}
}
© 2024 lite-C Forums