Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, 1 invisible), 1,086 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Trading multiple assets #460674
07/10/16 01:32
07/10/16 01:32
Joined: Jun 2016
Posts: 33
NC
C
Cstyle Offline OP
Newbie
Cstyle  Offline OP
Newbie
C

Joined: Jun 2016
Posts: 33
NC
I'm getting some pretty bizarre behavior when I trade multiple assets. If I just trade UK100, here is what the first trade looks like:

Quote:
Type Asset ID Lots Open Close Entry Exit Profit Roll ExitType
Long UK100 51201 1 3/19/2009 9:00 3/24/2009 5:01 3888 3948 7.77 -0.03 Stop


When I trade both UK100 and GER30, Here is what the trades look like:

Quote:
Name Type Asset ID Lots Open Close Entry Exit Profit Roll ExitType
PennyPicker6 Long UK100 51201 1 3/19/2009 9:00 3/24/2009 2:50 3888 4234 45.52 0 Sold
PennyPicker6 Long GER30 51302 1 3/19/2009 10:00 3/24/2009 2:50 4070 4235 16.55 0 Sold


It looks like Zorro is getting the exit price for UK100 from GER30's price data. UK100 did not trade anywhere near that price on that date, so that obviously can't be right. Any idea what could be causing behavior like that?

Last edited by Cstyle; 07/10/16 20:47.
Re: Trading multiple assets [Re: Cstyle] #460690
07/10/16 23:42
07/10/16 23:42
Joined: Jun 2016
Posts: 33
NC
C
Cstyle Offline OP
Newbie
Cstyle  Offline OP
Newbie
C

Joined: Jun 2016
Posts: 33
NC
Here is what the trades look like on the chart:



As you can see, the strategy is often exiting UK100 way outside the range that the asset normally trades in, for big wins and big losses.

Any ideas how this could be happening? I'm convinced it has something to do with trading multiple assets.

Re: Trading multiple assets [Re: Cstyle] #460709
07/11/16 12:49
07/11/16 12:49
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
You can trade as many assets as you want, but without seeing your script, I can not tell why its trades get wrong prices. There can be lots of reasons.

Re: Trading multiple assets [Re: jcl] #460712
07/11/16 16:32
07/11/16 16:32
Joined: Jun 2016
Posts: 33
NC
C
Cstyle Offline OP
Newbie
Cstyle  Offline OP
Newbie
C

Joined: Jun 2016
Posts: 33
NC
Here is the code for just a single asset, which works perfectly:

Code:
// Penny Picking strategy ///////////////////
#include <profile.c>

int TrailWhenProfitable()
{
	static bool TrailInitiated;
	var AverageOpen = 0;
	int Count = 0;
	
	
	
 	if(NumOpenLong > 0 )
	{
		var AverageOpen = 0;
		int Count = 0;
		for(open_trades)
		  if(TradeIsOpen) 
		  {
		    AverageOpen += TradePriceOpen;
		    Count++;
		  }
		  
		if(Count) 
		  AverageOpen /= Count;
	
		if (priceClose() > AverageOpen + (2 * ATR(100)) && TrailInitiated == false)
			{
				TradeStopLimit = priceClose() - 2 * ATR(100);
				TradeTrailLimit =  priceClose() - ATR(100);
				TrailInitiated = true;
				printf("  TradeStopLimit = %.3f,  TradeTrailLimit = %.3f", (var) TradeStopLimit, (var) TradeTrailLimit);
				printf("  ATR = %.3f,  priceClose = %.3f", ATR(100), priceClose());
				printf("  AverageOpen = %.3f, ltod = %f", AverageOpen, (var) ltod(ET));
				
			}
			
		if (TrailInitiated == true && ltod(ET) > 1230)
		{
			exitLong();
			printf("  ltod = %f", (var) ltod(ET));
		}
	}
	else 
		TrailInitiated = false;
   	
   	
   return 0;
   		
}

function run()
{
	StartDate = 2009;
	EndDate = 2016; 	
	LookBack = 500;
	set(LOGFILE); // log all trades
	set(TICKS);  // normally needed for TMF
	

	
	vars Price = series(price());
	vars Trend = series(LowPass(Price,500));
	

  	
	if(valley(Trend) && NumOpenLong < 4) 
	{
		var AverageOpen = 0;
		var AverageProfit = 0;
		int Count = 0;
		
		for(open_trades)
	  		if(TradeIsOpen) 
	  		{
    			AverageOpen += TradePriceOpen;
    			Count++;
  			}
  			
		if(Count) 
  			AverageOpen /= Count;
  			
  		AverageProfit = (priceClose() - AverageOpen)/AverageOpen;
  		
  			
  		switch (Count)
		{
		  case 0:
		    enterLong(TrailWhenProfitable);
		    break;
		  case 1:
		    if (AverageProfit < -0.10)
		    {
		    	enterLong(TrailWhenProfitable);
		    	printf(" enter 2nd trade ");
	    	 }
		    break;
		  case 2: 
		    if (AverageProfit < -0.15)
		    {
		    	enterLong(TrailWhenProfitable);
		    	printf(" enter 3rd trade ");
	    	 }
		    break;	  
		  case 3: 
		    if (AverageProfit < -0.20)
		    {
		    	enterLong(TrailWhenProfitable);
		    	printf(" enter 4th trade ");
	    	 }
		    break;
		  default:
		    printf("None of them! ");
		}	
  			
		
	}  // if trending

  
}



Here is the code with multiple assets which is exiting at erroneous prices:

Code:
// Penny Picking strategy ///////////////////
#include <profile.c>

int TrailWhenProfitable()
{
	bool TrailInitiated;
	static bool TrailInitiatedA;
	static bool TrailInitiatedB;
	static bool TrailInitiatedC;		
	
	while(asset(loop("UK100","GER30", "NAS100")))
	{	
	
	 	if(NumOpenLong > 0 )
		{
			var AverageOpen = 0;
			int Count = 0;
			for(open_trades)
			  if(TradeIsOpen) 
			  {
			    AverageOpen += TradePriceOpen;
			    Count++;
			  }
			  
			if(Count) 
			  AverageOpen /= Count;
			  
			  
			switch (Asset)
			{  
			  case "UK100":
			    TrailInitiated = TrailInitiatedA;
			    break;
			  case "GER30":
			    TrailInitiated = TrailInitiatedB;
			    break;
			  case "NAS100":
			    TrailInitiated = TrailInitiatedC;
			    break;
			}
		
			if (priceClose() > AverageOpen + (2 * ATR(100)) && TrailInitiated == false)
				{
					TradeStopLimit = priceClose() - 2 * ATR(100);
					TradeTrailLimit =  priceClose() - ATR(100);
					TrailInitiated = true;
					printf("  TradeStopLimit = %.3f,  TradeTrailLimit = %.3f", (var) TradeStopLimit, (var) TradeTrailLimit);
					printf("  ATR = %.3f,  priceClose = %.3f", ATR(100), priceClose());
					printf("  AverageOpen = %.3f, ltod = %f", AverageOpen, (var) ltod(ET));
					switch (Asset)
					{  
					  case "UK100":
					    printf("Asset UK100");
					    break;
					  case "GER30":
					    printf("Asset GER30");
					    break;
					  case "NAS100":
					    printf("Asset NAS100");
					    break;
					  default:
					    printf("None of them! ");
					}  // switch
					
				}
				
			if (TrailInitiated == true && ltod(ET) > 1230)
			{
				exitLong();
				printf("  ltod = %f", (var) ltod(ET));
			}
			
		}  // if NumOpenLong > 0
		else 
			TrailInitiated = false;

			
		switch (Asset)
		{  
		  case "UK100":
		    TrailInitiatedA = TrailInitiated;
		    break;
		  case "GER30":
		    TrailInitiatedB = TrailInitiated;
		    break;
		  case "NAS100":
		    TrailInitiatedC = TrailInitiated;
		    break;
		}  //  switch
			
	 
	}  // while asset loop 
	   	
	return 0;
   		
}

function run()
{
	StartDate = 2009;
	EndDate = 2016; 	
	LookBack = 500;
	set(LOGFILE); // log all trades
	set(TICKS);  // normally needed for TMF
	
	while(asset(loop("UK100","GER30","NAS100")))
	{
	
		vars Price = series(price());
		vars Trend = series(LowPass(Price,500));
		
	
	  	
		if(valley(Trend) && NumOpenLong < 4) 
		{
			var AverageOpen = 0;
			var AverageProfit = 0;
			int Count = 0;
			
			for(open_trades)
		  		if(TradeIsOpen) 
		  		{
	    			AverageOpen += TradePriceOpen;
	    			Count++;
	  			}
	  			
			if(Count) 
	  			AverageOpen /= Count;
	  			
	  		AverageProfit = (priceClose() - AverageOpen)/AverageOpen;
	  		
	  			
	  		switch (Count)
			{
			  case 0:
			    enterLong(TrailWhenProfitable);
			    break;
			  case 1:
			    if (AverageProfit < -0.10)
			    {
			    	enterLong(TrailWhenProfitable);
			    	printf(" enter 2nd trade ");
		    	 }
			    break;
			  case 2: 
			    if (AverageProfit < -0.15)
			    {
			    	enterLong(TrailWhenProfitable);
			    	printf(" enter 3rd trade ");
		    	 }
			    break;	  
			  case 3: 
			    if (AverageProfit < -0.20)
			    {
			    	enterLong(TrailWhenProfitable);
			    	printf(" enter 4th trade ");
		    	 }
			    break;
			  default:
			    printf("None of them! ");
			}	// switch count
  			
		
		}  // if trending
	
	}  // while asset loop
  
}  // run


Re: Trading multiple assets [Re: Cstyle] #460713
07/11/16 17:30
07/11/16 17:30
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Ok, it's clear that this script won't give you much joy. The computer just did what you told it to do. You're setting trade parameters to prices of a different asset.

I don't know why you do asset() calls in a TMF, but if you do, you must switch back the asset to the asset of the trade afterwards. Also don't tamper with trade variables unless you know precisely what and why you're doing that. If the purpose was just setting a trailing stop, there are several examples in the manual.


Re: Trading multiple assets [Re: jcl] #460723
07/11/16 23:31
07/11/16 23:31
Joined: Jun 2016
Posts: 33
NC
C
Cstyle Offline OP
Newbie
Cstyle  Offline OP
Newbie
C

Joined: Jun 2016
Posts: 33
NC
Originally Posted By: jcl
If the purpose was just setting a trailing stop, there are several examples in the manual.


It's a little more complex than that. First of all, if the trade goes against me, I increase my position, lowering the average entry price. A trail is only set once priceClose moves above the average entry price.

The code works perfectly for just one asset, as I showed in the previous post. I'm doing something wrong though when I add in multiple assets.

In addition, it still doesn't make sense to me why Zorro would ever have me exit a trade at a price significantly higher than the high of that day, or significantly below the low of a day. Regardless how flawed my code is, that shouldn't happen. No matter what I did in live trading I could never sell my position way above the high of the day, so in that manner zorro is not accurately simulating live action. Just look at that image I posted earlier!

Re: Trading multiple assets [Re: Cstyle] #460733
07/12/16 06:57
07/12/16 06:57
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
You can absolutely produce such an image with your script when you overwrite trade parameters with data from wrong assets. Programming is not foolproof - you can do anything with any variable, no matter if it makes sense or not. Garbage in, garbage out.

Your single asset TMF makes some sense to me, but I do not understand the purpose of your multi asset version. Delete it and start over. Think about what parameters you want to change - then program just that, preferably without "while", "loop", "asset", or similar stuff, and without overwriting other trades. If you are not sure how to do it, just ask here. if you get strange results, you can normally easily find out why - use the single step mode and watch all variables in question.

Re: Trading multiple assets [Re: jcl] #460863
07/17/16 20:53
07/17/16 20:53
Joined: Jun 2016
Posts: 33
NC
C
Cstyle Offline OP
Newbie
Cstyle  Offline OP
Newbie
C

Joined: Jun 2016
Posts: 33
NC
Originally Posted By: jcl
Your single asset TMF makes some sense to me, but I do not understand the purpose of your multi asset version. Delete it and start over. Think about what parameters you want to change - then program just that, preferably without "while", "loop", "asset", or similar stuff, and without overwriting other trades.


My strategy is dependent on the average entry price of all of the trades for a single asset. How do I get that without a loop?

Thanks!

Re: Trading multiple assets [Re: Cstyle] #460865
07/18/16 07:27
07/18/16 07:27
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline
Senior Member
boatman  Offline
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
Rather than a while(asset(loop( ....))), try for(open_trades) or for(current_trades). There are examples of each in the manual, but post here if you get stuck!

Last edited by boatman; 07/18/16 07:30.
Re: Trading multiple assets [Re: boatman] #460895
07/20/16 00:35
07/20/16 00:35
Joined: Jun 2016
Posts: 33
NC
C
Cstyle Offline OP
Newbie
Cstyle  Offline OP
Newbie
C

Joined: Jun 2016
Posts: 33
NC
I have it working perfectly now. Thanks everyone for your help!

Code:
// Penny Picking strategy ///////////////////
#include <profile.c>

	
int TrailWhenProfitable()
{

	var AverageOpen = 0;
	int Count = 0;
	
	
	
 	if(NumOpenLong > 0 )
	{
		var AverageOpen = 0;
		int Count = 0;
		for(current_trades)
		  if(TradeIsOpen) 
		  {
		    AverageOpen += TradePriceOpen;
		    Count++;
		  }
		  
		if(Count) 
		  AverageOpen /= Count;
	
		if (priceClose() > AverageOpen + (2 * ATR(100)) && TradeStopLimit <= 0)
			{
				TradeStopLimit = priceClose() - 2 * ATR(100);
				TradeTrailLimit =  priceClose() - ATR(100);
				printf("  TradeStopLimit = %.3f,  TradeTrailLimit = %.3f", (var) TradeStopLimit, (var) TradeTrailLimit);
				printf("  ATR = %.3f,  priceClose = %.3f", ATR(100), priceClose());
				printf("  AverageOpen = %.3f, ltod = %f", AverageOpen, (var) ltod(ET));
				
			}
			
		if (TradeStopLimit > 0 && ltod(ET) > 1230)
		{
			exitLong();
			printf("  ltod = %f", (var) ltod(ET));
		}
	}
  
   	
   	
   return 0;
   		
}

function run()
{
	StartDate = 2009;
	EndDate = 2016; 	
	LookBack = 500;
	set(LOGFILE); // log all trades
	set(TICKS);  // normally needed for TMF
	
	Trail = 0;
	Stop = 0;
	
	while(asset(loop("UK100","GER30","NAS100")))
	{
	
		vars Price = series(price());
		vars Trend = series(LowPass(Price,500));
		
	
	  	
		if(valley(Trend) && NumOpenLong < 4) 
		{
			var AverageOpen = 0;
			var AverageProfit = 0;
			int Count = 0;
			
			for(current_trades)
		  		if(TradeIsOpen) 
		  		{
	    			AverageOpen += TradePriceOpen;
	    			Count++;
	  			}
	  			
			if(Count) 
	  			AverageOpen /= Count;
	  			
	  		if(AverageOpen > 0)
	  			AverageProfit = (priceClose() - AverageOpen)/AverageOpen;
	  		
	  			
	  		switch (Count)
			{
			  case 0:
			    enterLong(TrailWhenProfitable);
			    break;
			  case 1:
			    if (AverageProfit < -0.10)
			    {
			    	enterLong(TrailWhenProfitable);
			    	printf(" enter 2nd trade ");
			    	printf(" AverageOpen %.3f ", AverageOpen);
		    	 }
			    break;
			  case 2: 
			    if (AverageProfit < -0.15)
			    {
			    	enterLong(TrailWhenProfitable);
			    	printf(" enter 3rd trade ");
		    	 }
			    break;	  
			  case 3: 
			    if (AverageProfit < -0.20)
			    {
			    	enterLong(TrailWhenProfitable);
			    	printf(" enter 4th trade ");
		    	 }
			    break;
			  default:
			    printf("None of them! ");
			}	// switch count
  			
		
		}  // if trending
	
	}  // while asset loop
  
}  // run



Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1