CBT mixing Mid-Level with Low-level methods?

@Tomasz or any other advanced user, as I study the Custom Backtest Interface (which we seem to commonly refer to as the 'CBT') I am trying to make sure I do not mix methods from High/Mid/Low levels. There are several very explicit instructions in the user guide (for example not to use UpdateStats nor HandleStops in high-level and mid-level approaches), but there is an example of using the Mid-Level ProcessTradeSignals with the Low-Level ScaleTrade

The Official Knowledge Base article that has served as a template for coding re-balancing CBT's for many years has them together.

http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

My long winded question is, are there other Mid-Level
(or High-Level) CBT methods that can be used within a Low-Level CBT? And doesn't ProcessTradeSignals handle scale trades?

I am assuming that ProcessTradeSignals works in the example because none of the Low-level processes that it replaces are in the code ( i.e. EnterTrade()/ExitTrade()/UpdateStats()/HandleStops() )

Secondary question, I believe that ProcessTradeSignals also updates the equity value for the bar. If you are re-balancing, you need to call this before the re-balancing procedure so position values are calculated based on the current equity value. Otherwise you would be using previous bar equity value. Is that correct? (I ask this because in Mid-Level examples, for example from the "Houston" presentation, it is called at the end of the loop after looping through the signals and not before).

1 Like

Hi Larry

ProcessTradeSignals processes the signals in the signal list, ie the bar-by-bar signals that are created by the Buy, Sell, Short & Cover arrays (or PositionScore for rotational) in your regular code. If you make changes to the signals in the CBT (such as with sig.PosSize) by looping through the signal list, you need to process the signals after that using ProcessTradeSignals for the changes to take effect.

Low-level commands such as ScaleTrade, EnterTrade and ExitTrade, all have immediate effect, so don't need to be "processed" afterwards with ProcessTradeSignals. They don't form signals in the regular signal list.

In your linked example, the signals are processed first on the bar, then scaling is applied to the positions that result from that.

As mentioned, if you were instead looping through your signals to apply changes, the ProcessTradeSignals command would come after.

And if you are doing all the entries and exits in the CBT using EnterTrade and ExitTrade and ignoring any signal list, you would be totally in low-level and then use UpdateStats and HandleStops.

3 Likes

@HelixTrader Thanks Alan. I guess in some ways it would seem more straightforward to stay within the Low-Level CBT instead of mixing with aspects of a Mid-Level (for each bar, go through Exit Signals and raise cash, then re-balance with scaling, then go through new fresh Entry Signals).

But it does lead to a curiosity about the scale trading.

 bo.ScaleTrade( bar, pos.Symbol, diff < 0, price, abs( diff ) );

In a real time portfolio there will likely be some scaling up and some scaling down of positions to meet the desired "re-balance". If you are not using Margin or have already "maxed out" your margin then you would need to scale down your over-weighted positions to raise cash before you could scale up in the under-weighted positions. There is no indication that I have seen in the User Guide that the above line takes that into consideration, though perhaps I should assume that it does.

No suggestion that we would need to code a two step process (scale down first followed by the scale ups).

Have you seen anything specific about that?

Thanks again and great to hear from you!

1 Like

On the point of using Mid-Level v Low-Level, I'd always make use of Mid-Level if you can. It just takes out a lot of the coding effort you would otherwise have to write in. You can always mix in the elements of Low-Level you need, as discussed above.

When I've had to do scaling in the CBT in the past I've coded it in two steps for that very reason. First loop for Scale-Outs, then second loop for Scale-Ins. As with all things Amibroker though, there may be an alternative approach.

2 Likes