Hello,
I am trying to implement scaling out 50% of the position when a target is hit but i get un expected results.
here is my code
higher=Ref(H,-1);
lower=Ref(L,-1);
dif=higher-lower;
Buy = C>=higher;
//AddColumn(Buy,"Buy");
Filter=C;
//sigScaleOut1=
Sell=0;
buyingPrice=0;
diff=0;
tp=0;
sellingPrice=0;
sellingPrice2=0;
ok=0;
tp1=0;
for (i=0;i<BarCount;i++)
{
//ok[i]=99;
if(tp==0 AND Buy[i]==1)
{
buyingPrice= BuyPrice[i];
ok[i]=1;
tp=1;
diff=dif[i];
sellingPrice=lower[i];
sellingPrice2[i]=lower[i];
tp1[i]=1;
}
else if(tp==1 AND C[i]>=(buyingPrice+diff))
{
ok[i]=2;
Buy[i]=sigScaleOut;
tp=2;
sellingPrice=lower[i];
tp1[i]=1;
}
else if(tp==2 AND C[i]>=(buyingPrice+(2*diff)))
{
Sell[i]=1;
tp=0;
ok[i]=3;
Buy[i]=0;
}
if(C[i]<=sellingPrice OR C[i]<=lower[i])
{
Sell[i]=1;
ok[i]=4;
sellingPrice=0;
tp=0;
Buy[i]=0;
}
BuyPrice[i]=buyingPrice;
}
SetPositionSize(100, spsPercentOfEquity );
SetPositionSize( 50, IIf( Buy == sigScaleOut, spsPercentOfPosition, spsNoChange ) );
when a scaleout signal is triggered the backtester exists un expected number of shares
here is an example
i tried searching and understanding what is different between my code and the offical code on setPositionSIze page but i failed.
Thanks in advance,