Is there anyway either in the main code or via the CBT to detect when a trade was blocked due to insufficient funds?
I do not think you can detect it without use of CBT.
In CBT you may compare position value with bo.Cash.
https://www.amibroker.com/guide/a_custombacktest.html
Here is a trace from the CBT. If bo.Cash is not decrementing upon each new entry, how am I supposed to be able to tell if a trade is taken or skipped?
And here is the code within the CBT that produced that trace:
if ( i > 0 )
{
dn_tn = NumToStr(dn[i], 1.0, False, False) + "_" + NumToStr(tn[i], 1.0, False, False);
dn_tn_i = "DT: " + dn_tn + " i: " + NumToStr(i,1.0, True, False) + " ||||| " ;
for ( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() )
{
OpenPosShares = openpos.Shares;
StaticVarSet ( "OpenPosShares" + openpos.Symbol, OpenPosShares);
//_TRACE(Dtf + " ##### OpenPosShares: " + NumToStr(OpenPosShares, 1.0) );
}
for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
{
tmpPrc = sig.Price;
//if (doTrace) { _TRACE(dn_tn_i + " tmpPrc: " + NumToStr(tmpPrc, 1.2)); }
if (sig.IsLong() AND NOT sig.Type == 2 AND NOT sig.Type == 6 )
{
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 --- " + sig.Symbol + " LastEntryShares: " + NumToStr(LastEntryShares, 1.4) + " psize: " + NumToStr(psize, 1.2) + " sig.Price: " + NumToStr(tmpPrc, 1.2) + " sig.Type: " + NumToStr(sig.Type, 1.0) + " bo.Cash: " + NumToStr(bo.Cash, 1.2)); }
}
if (sig.Type == 6) // If this signal is a scale-out exit
{
LastEntryShares = StaticVarGet ( "LastEntryShares" + sig.Symbol);
OpenPosShares = StaticVarGet ( "OpenPosShares" + sig.Symbol);
if (doTrace) { _TRACE(dn_tn_i + " Ex --- " + sig.Symbol + " LastEntryShares: " + NumToStr(LastEntryShares, 1.4) + " OpenPosShares: " + NumToStr(OpenPosShares, 1.4) + " bo.Cash: " + NumToStr(bo.Cash, 1.2)); }
sig.PosSize = LastEntryShares * tmpPrc; // Set modified position size back into object
}
}
}
I know I can see it in the detailed trade log, but I need to detect it from code.
I also know how to extensively debug AB code.
It doesn't look like you're actually processing the entry signals with bo.ProcessTrades() or bo.EnterTrade(), so of course your cash wouldn't be changing.
at the end of the CBT loop I am using:
bo.ProcessTradeSignals( i );
But this seems to only process once per bar, and not trade-by-trade within the bar. So if you look at my trace in the graphic above the cash is moving, but just not within the bar. I need to know trade by trade which ones are being skipped or not. Not bar-by-bar.
If you need to know how cash is affected by each trade, then you will need to enter and exit the trades yourself, i.e. use the low-level CBT instead of the mid-level CBT.