I am trying to write a code extensively without using the built-in AB
features such as trailing stops and position sizes. So far I was able
to put in the entries and exits but am having some trouble with the
trade size which uses the current equity curve. This is the current code
(ignore the strategy itself, as it is just random):
priceatbuy = 0;
for(i=0; i<20; i++)
{
Buy[i] = 0;
Sell[i] = 0;
}
for(i=20; i<BarCount; i++)
{
if( priceatbuy == 0 & BuyPrice[i] > entryLevel[i] )
{
Buy[i] = 1;
BuyPrice[i] = Max(entryLevel[i], Low[i]);
priceatbuy = BuyPrice[i];
}
else
Buy[i] = 0;
if( priceatbuy > 0 & SellPrice[i] < exitLevel[i] )
{
Sell[i] = 1;
SellPrice[i] = Min(exitLevel[i], High[i]);
priceatbuy = 0;
}
else
Sell[i] = 0;
}
Now I want to buy shares equal to 50% of current equity (updated daily,
not initial equity). I tried adding the following line into the FOR
statement but that didn't work:
SetPositionSize( equity()*0.5/priceatbuy, spsShares );
Any suggestions? I know that SetPositionSize(-50, spsPercentofEquity )
would do the job, but I would like to be able to tweak the number of
shares manually. Thanks!