Long only system. When I scale-out of a position, I want to exit the same amount of shares as my last buy entry or buy scale-in. I am trying to use the the high level CBT to adjust the scale-out shares to appropriate quantity, so first I am capturing the last entry quantity into a static var to be retrieved later and used on the scale-out. A snipit of my attempt is below.
for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
{
tmpPrc = sig.Price;
if (doTrace) { _TRACE(dn_tn_i + " tmpPrc: " + NumToStr(tmpPrc, 1.2)); }
//Long only system.
if (sig.IsLong() AND NOT sig.Type == 2 AND NOT sig.Type == 6 ) // if this is a buy or scale-in, and not a sell and not a scale-out
{
psize = sig.PosSize;
if (psize < 0)
psize = (-psize/100) * bo.Equity;
LastEntryShares = psize / tmpPrc;
StaticVarSet ( "LastEntryShares" + sig.Symbol, LastEntryShares);
if (doTrace) { _TRACE(dn_tn_i + " En ---LastEntryShares: " + NumToStr(LastEntryShares, 1.4) + " psize: " + NumToStr(psize, 1.2) + " sig.Price: " + NumToStr(sig.Price, 1.2) + " sig.Type: " + NumToStr(sig.Type, 1.0) ); }
}
if (sig.Type == 6) // If this signal is a scale-out exit
{
LastEntryShares = StaticVarGet ( "LastEntryShares" + sig.Symbol);
if (doTrace) { _TRACE(dn_tn_i + " Ex ---LastEntryShares: " + NumToStr(LastEntryShares, 1.4)); }
sig.PosSize = LastEntryShares * tmpPrc;
}
}
The problem is for the scale-ins, the CBT is returning a -1 for price. When I use a detailed report, I can see the correct scale-price, so why would the CBT give me a -1 here? According to the manual it is supposed to be able to return price for scales.
And obviously, you need to have BuyPrc already defined (_TRACE the loop to log the values or use an exploration to see what values you are actually assigning).
In my previous example when I get a buy signal I set Buy directly to SigScalIn because AB treats the first sigScaleIn as buy anyway (see Example 2 of Pyramiding (scaling in/out).
I did it exactly like this now and I am still getting -1 for sig.Price on just the sig.Type 5 (scale-in). I know the scale-in entry prices are correct because I draw arrows on the chart, and also study the detailed backtest report.
You need to show entire code, not part of it. Likely you are printing values after processing signals. ProcessTradeSignals may modify values and -1 price usually means that signal was already processed and/or should be skipped.