Hello,
I am trying to code a trading system that use ranking of a certain metric to decide what to buy and sell.
What i want to do is to buy the stock (with 5% of equity) if its ranking is between 5 and 10 and scale in to 10% of the equity when it's between 1 and 5 in ranking.
The sell should be the direct opposite, scale out to 50% of the position if moving from 1-5 to 5-10 and sell completely when > 10.
My problem is i don't know how to set position size to match those.
I tried searching for tutorials but nothing helped my case. Here is the code snippet doing that part.
nowCase = 0;
betterLimit=5;
limit=10;
AddColumn(todayRank,"today"); //todayRank is the asset's rank of today
AddColumn(yesterdayRank,"yesterday"); //yesterdayRank is the asset's rank of yesterday
nowTrigger=0;
for( i = 0; i < BarCount; i++ )
{
if(nowTrigger==0 && todayRank[i]<=limit && todayRank[i]>betterLimit && yesterdayRank[i]>limit)
{
nowTrigger=1;
Buy[ i ] = 1;
//SetPositionSize( betterLimit, spsPercentOfEquity );
}
else if(nowTrigger==1 && todayRank[i]<=betterLimit && yesterdayRank[i]>betterLimit && yesterdayRank[i]<=limit)
{
nowTrigger= 2;
Buy[ i ] = sigScaleIn;
//SetPositionSize( limit, spsPercentOfEquity );
}
else if(nowTrigger==0 && todayRank[i]<=betterLimit )
{
nowTrigger= 2;
Buy[ i ] = 1;
//SetPositionSize( limit, spsPercentOfEquity );
}
else if(nowTrigger==2 && todayRank[i]>betterLimit && todayRank[i]<=limit && yesterdayRank[i]<=betterLimit)
{
nowTrigger= 1;
Buy[ i ] = sigScaleOut;
//SetPositionSize( betterLimit, spsPercentOfEquity );
}
else if(nowTrigger==1 && todayRank[i]>limit && yesterdayRank[i]>betterLimit && yesterdayRank[i]<=limit)
{
nowTrigger = 0;
sell[ i ] = 1;
}
else if(nowTrigger==2 && todayRank[i]>limit && yesterdayRank[i]<=betterLimit)
{
nowTrigger = 0;
Sell[ i ] = 1;
}
nowCase[i]=nowTrigger;
}
SetPositionSize( 5, spsPercentOfEquity );
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) );
SetPositionSize( 50, spsPercentOfEquity* ( Buy == sigScaleIn ) );
Regards,