Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (ozgur, TipmyPip), 722 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 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: 357
D
danatrader Offline
Senior Member
danatrader  Offline
Senior Member
D

Joined: Mar 2019
Posts: 357
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