Gamestudio Links
Zorro Links
Newest Posts
IG plugin initial release
by sodiumchloride. 09/22/20 20:53
Using STL in Zorro strategies
by AndrewAMD. 09/22/20 17:01
Rubbish data from Yahoo
by DanielX. 09/22/20 14:37
assert
by Grat. 09/22/20 12:14
MT4 - use more Zorro script
by Grat. 09/22/20 06:49
Single advise model for portfolio
by jmlocatelli. 09/21/20 19:41
AUM Magazine
Latest Screens
The Space Between
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Who's Online Now
4 registered members (Dooley, kvm, AndrewAMD, jmlocatelli), 445 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
NewBorn, 3dvg, secretagentman, distribution, EditeY
18515 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Help! How do set positions size for each side in a pairs trade.. #481095
08/06/20 13:59
08/06/20 13:59
Joined: Dec 2019
Posts: 4
London
G
good2bme Offline OP
Guest
good2bme  Offline OP
Guest
G

Joined: Dec 2019
Posts: 4
London
Hi Everyone,

Currently trying to build out my first Zorro strategy and settled on developing a pairs trading strategy pitting Aussie Dollar (Y) against gold (X).

I am trying to allocate weighted position size to the Gold based on the ratio of the average true range of Y to X. I have also set the Capital to 100,000 and RiskAllocation to 10000.


Code
	  if(NumOpenTotal < maxOpen)  {
        if(crossUnder(stocNorm, lowerThreshold)) // buying the spread (long Y, short X)
        {
	 asset(Y);
            Risk = RiskAllocation;
            enterLong();
            asset(X);
	    Risk = RiskAllocation *(AtrY/AtrX);
            enterShort();
        }
        if(crossOver(stocNorm, upperThreshold)) // shorting the spread (short Y, long X)
        {
           asset(Y);
            Risk = RiskAllocation;
            enterShort();
            asset(X);
	   Risk = RiskAllocation *(AtrY/AtrX);
            enterLong();
        }
     }


At first i thought it was because capital was too small, but even making a 100 million and Risk = 100k the log still show only one lot per pair.

Code
[126: Fri 10-02-05 15:40] 10000074 0 10/6  (1061.10)
[127: Mon 10-02-08 15:40] 10000074 0 10/6  (1066.16)
[128: Tue 10-02-09 15:40] 10000074 0 10/6  (1075.11)
[129: Wed 10-02-10 15:40] 10000074 0 10/6  (1066.41)
[AUD/USD::L12902] Long 1@0.872119  at 15:40:00
[XAU/USD::S12903] Short 1@1066.77  at 15:40:00



Any suggestion, comments or feedback would be most appreciated.

Re: Help! How do set positions size for each side in a pairs trade.. [Re: good2bme] #481118
08/07/20 07:49
08/07/20 07:49
Joined: Mar 2019
Posts: 249
D
danatrader Offline
Member
danatrader  Offline
Member
D

Joined: Mar 2019
Posts: 249
Why not post the complete script, then others can just copy & paste to check on it, makes life easier, and the increses chances others helping out.

Re: Help! How do set positions size for each side in a pairs trade.. [Re: danatrader] #481148
08/07/20 14:18
08/07/20 14:18
Joined: Dec 2019
Posts: 4
London
G
good2bme Offline OP
Guest
good2bme  Offline OP
Guest
G

Joined: Dec 2019
Posts: 4
London
Originally Posted by danatrader
Why not post the complete script, then others can just copy & paste to check on it, makes life easier, and the increses chances others helping out.


fair Point.

the full script below.

Code


#define Y "AUD/USD"
#define X "XAU/USD"

function run() 
{
 
 set(PLOTNOW,LOGFILE);
 BarPeriod = 1440;	
 StartDate = 20100101;
 EndDate   = 20101231; 
 Capital   = 100000;
 LotAmount  = 1;
  
 int period = 6;
 int ATRperiod = 20;
 int RiskAllocation = 10000;
 int MidUpper = 55;
 int MidLower = 45; 
 int upperThreshold = 90;
 int lowerThreshold = 10; 
 int maxOpen = 2;
 
 LOOKBACK = max(ATRperiod,period);


	asset(Y);
	 vars percntReturnsY = series(((priceClose(0)-priceClose(1))/priceClose(0))*100);
	 vars stocY    = series((priceClose(period)- priceLow(period))/(priceHigh(period)-priceLow(period)));
	 var AtrY      =  ATR(ATRperiod);
	 
	asset(X);
	vars percntReturnsX = series(((priceClose(0)-priceClose(1))/priceClose(0))*100);
	vars stocX    = series((priceClose(period)- priceLow(period))/(priceHigh(period)-priceLow(period)));	 
	var AtrX      =  ATR(ATRperiod);

	vars stocDiff = series(stocY[0] - stocX[0]);
	vars ZScore   = series(zscore(stocDiff[0], period));
	vars stocNorm = series((normalize(stocDiff[0], period)*0.5)+50);

	var  yLots =  max(round(RiskAllocation/(priceClose(0)*LotAmount)),1);
	var  xLots = max(round((RiskAllocation/(priceClose(0)*LotAmount))*(AtrY/AtrX)),1);

	
// -------------------------------
// Exit all trades  on friday at 6pm
// -------------------------------	
	if(dow() == FRIDAY && hour() >= 18) {   exitLong("*");  exitShort("*");}

// -------------------------------
// trade logic 
// -------------------------------
    // exit on cross midpoints
    if(crossOver(stocNorm, MidLower) or crossUnder(stocNorm, MidUpper)) 
    {
        asset(X);
        exitLong(); exitShort();
        asset(Y);
        exitLong(); exitShort();
    }
	


	  if(NumOpenTotal < maxOpen)  {
        if(crossUnder(stocNorm, lowerThreshold)) // buying the spread (long Y, short X)
        {
	   asset(Y);
	   Lots = yLots;
            enterLong();

            asset(X);
	    Lots = xLots;
            enterShort();
        }
        if(crossOver(stocNorm, upperThreshold)) // shorting the spread (short Y, long X)
        {
		asset(Y);
		Lots = yLots;
		enterShort();

		asset(X);
		Lots = xLots;
		enterLong();
        }
     }

}


Last edited by good2bme; 08/07/20 22:16. Reason: Solved my issue
Re: Help! How do set positions size for each side in a pairs trade.. [Re: good2bme] #481159
08/07/20 22:25
08/07/20 22:25
Joined: Dec 2019
Posts: 4
London
G
good2bme Offline OP
Guest
good2bme  Offline OP
Guest
G

Joined: Dec 2019
Posts: 4
London
I solved my issue by including the following lines.
Code
 	var  yLots =  max(round(RiskAllocation/(priceClose(0)*LotAmount)),1);
	var  xLots = max(round((RiskAllocation/(priceClose(0)*LotAmount))*(AtrY/AtrX)),1);


I used the priceclose multiple by LotAmount, to find out the cost a lot in the account currency, i then divided my riskallocation capital by that lot value to give the current number of lots i could by them rounded to ensure i was buying whole lots. I added the max function around to ensure that i was buying at least 1 lot.

I hope my explanation helps someone in the future.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1