I am testing a strategy on a basket of stocks. The strategy always exits n bars
after entry. I
found one interesting thing. When a buy signal is generated on a particular day
on which
previous other stock trades should be exited, the following two difference
pieces of code
will behave differently:
1. When cash is maxed out and Buy and Sell happen on the same bar,
this code will sell positions and use the proceeds to enter new positions:
BuyPrice = O;
SellPrice = O;
Buy = Ref(Condition, -1);
Buy = ExRemSpan(Buy, Delay);
Sell = Ref(Buy, -Delay);
2. When cash is maxed out and Buy and Sell happen on the same bar,
this code will sell positions and skip the new positions:
BuyPrice = O;
SellPrice = O;
Buy = Ref(Condition, -1);
Buy = ExRemSpan(Buy, Delay);
Sell = 0;
priceatbuy = 0;
for(i = 0; i < BarCount; i++) {
if (priceatbuy == 0 AND Buy[i]) {
priceatbuy = BuyPrice[i];
}
if (priceatbuy > 0 AND Buy[i - Delay]) {
SellPrice[i] = O[i];
Sell[i] = 5; // n-bar exit code
priceatbuy = 0;
}
}
Changing the BuyPrice and/or SellPrice to Open or Close does not change the
behavior.
Does anyone know how to make code 2 behave the same as code 1? I have to use
the for
loop approach for scaling out.
Thanks in advanced,