Help! How do set positions size for each side in a pairs trade..

Posted By: good2bme

Help! How do set positions size for each side in a pairs trade.. - 08/06/20 13:59

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.
Posted By: danatrader

Re: Help! How do set positions size for each side in a pairs trade.. - 08/07/20 07:49

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.
Posted By: good2bme

Re: Help! How do set positions size for each side in a pairs trade.. - 08/07/20 14:18

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();
        }
     }

}

Posted By: good2bme

Re: Help! How do set positions size for each side in a pairs trade.. - 08/07/20 22:25

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.
© 2024 lite-C Forums