regardless of setting the Backtest Mode as following
SetBacktestMode(backtestRegular); ,
I am still seeming to experience situations where Long positions are open at Buy signals, checked in the CBT as following:
for (bar=0; bar < BarCount; bar++) {
for (sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar)) {
if (sig.IsEntry()) {
// position sizing omitted as irrelevant
for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos()) {
if (sig.Symbol == openpos.Symbol) {
if (sig.IsLong AND openpos.IsLong) {
//sig.PosSize = 0;
_TRACE("LONG entry, pos exists: " + sig.Symbol + " " + NumToStr(openpos.Shares));
}
}
}
}
}
bo.ProcessTradeSignals(bar);
}
Furthermore, the outcome depends heavily on whether to accept that signal or not (whether setting the PosSize to zero in the check is commented out or not).
The code is incorrect, to omit position in CBT set the sig.Price to -1. Also you are interating thru open positions, AmiBroker won't open a new trade if position is already open in backtestRegular mode. Please read the manual Portfolio-level back testing carefully. It explains how excess signals are removed in backtestRegular mode.
I tried to change the value of the sig.posSize between -1 and -2 and the results are still different. The code:
for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos()) {
if (sig.Symbol == openpos.Symbol) {
if (sig.IsLong AND openpos.IsLong) {
sig.PosSize = -1; // or -2
_TRACE("LONG entry, pos exists: " + sig.Symbol + " " + NumToStr(openpos.Shares));
}
}
}
Shouldn't the result remain the same - that means, if Amibroker does not enter new trades in the backtestRegular mode if open positions exist, shouldn't the value of the sig.posSize have no effect on the outcome...?
Hint 1: In my original reply I did NOT say anything about position size. I wrote sig.Price = -1
Hint 2: changing position size (now I am talking about it since you asked) obviously changes the outcome as say 1% of gain from $100 is $1 while 1% gain from say $200 position is $2. So you get different outcome.
Sorry, but I did ask about changing the signal's position size in my first post. I erroneously thought that setting it to 0 would cancel the trade, but I still asked about changing the pos size.
I changed the pos size of an entry signal that had a matching open position. Because of that, the signal should not trigger a trade and therefore the pos size should not matter. It seems to, however.