Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (monk12, Quad), 830 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Assets in Loops are impacted #482516
02/17/21 05:02
02/17/21 05:02
Joined: Jan 2021
Posts: 5
D
Diabolus Offline OP
Newbie
Diabolus  Offline OP
Newbie
D

Joined: Jan 2021
Posts: 5
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 ?

Re: Assets in Loops are impacted [Re: Diabolus] #482519
02/17/21 13:11
02/17/21 13:11
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
Have you read the manual about the asset function, especially the remarks about the order of asset calls?

Re: Assets in Loops are impacted [Re: Diabolus] #482524
02/18/21 01:52
02/18/21 01:52
Joined: Jan 2021
Posts: 5
D
Diabolus Offline OP
Newbie
Diabolus  Offline OP
Newbie
D

Joined: Jan 2021
Posts: 5
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

Re: Assets in Loops are impacted [Re: Diabolus] #482525
02/18/21 05:54
02/18/21 05:54
Joined: Jan 2021
Posts: 22
Singapore
S
strimp099 Offline
Newbie
strimp099  Offline
Newbie
S

Joined: Jan 2021
Posts: 22
Singapore
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().

Last edited by strimp099; 02/18/21 06:45.
Re: Assets in Loops are impacted [Re: Diabolus] #482533
02/19/21 04:29
02/19/21 04:29
Joined: Jan 2021
Posts: 5
D
Diabolus Offline OP
Newbie
Diabolus  Offline OP
Newbie
D

Joined: Jan 2021
Posts: 5
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).

Re: Assets in Loops are impacted [Re: Diabolus] #482537
02/20/21 11:04
02/20/21 11:04
Joined: Sep 2003
Posts: 929
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 929
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

Re: Assets in Loops are impacted [Re: Diabolus] #482538
02/20/21 12:40
02/20/21 12:40
Joined: Dec 2019
Posts: 53
ozgur Offline
Junior Member
ozgur  Offline
Junior Member

Joined: Dec 2019
Posts: 53
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);
	}
}

Last edited by ozgur; 02/20/21 12:41.

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1