WFA system reprocessing when multiple position are contemporary held

Hello guys, I would like to reprocess the equity line resulting from a WFA with three parameters: Par1, Par2, Par3.

The system I'm testing opens up to 4 positions simultaneously, (backtestRegularRawMulti) with a logic like: buy = indicator < threshold;

For each entry signal, the corresponding exit signal is determined by the first to occur among the following 3 conditions:

  • time exit (applyStop(stopTypeExit, StopModeBars, Par1)
  • take profit (applyStop(stopTypeProfit, StopModePercent, Par2)
  • logic Exit (Cross(C, MA(C, Par3))


When I perform a WFA, trades eventually opened at the end of the period are closed. If I use the Flip(Buy, Sell) function to convert buy signals from pulse to state (as described in another post), since the system is backtest raw multi, more trades are opened than necessary, which is not desirable.

One possible solution I'm considering might be to derive the buy signals from the report:

  1. report explore -> report -> trades -> copy table
  2. save the table as CSV
  3. load the CSV into a matrix
  4. extract the "date" column and generate buy signals from that

and calculate exit signals normally, but this is also not ideal because the parameters that determine the exit rules change during the WFA process.

So, it would be necessary to export the WFA report to a CSV file (using the batch function) and then derive the dates and parameter values from that file and then associate them in some vectors to use as parameters for calculating exit conditions.

But I still encounter a problem that I'm not sure if I can solve with CBT:

When a trade goes to market, I want the set of parameters (Par1, Par2, and Par3) associated with that trade to remain valid until its closure, regardless of whether in the meantime the WFA has found a new set. The new set will only be valid for new trades.

For example,
if when the market trade has a closing condition: cross with MA(C, 10) and holding period 7 days, and it remains open after a WFA update whose new parameters are MAperiod = 5 and holding = 3, I don't want it to be closed after 3 days or at the crossing of the MA 5.

I have studied the CBT documentation and understood that the signal generation phase occurs in phase 1, before the part where I can interact with the CBT. I would need to be able to assign extra attributes to the trade so that I can recall them somehow later during the backtest. Is it possible in any way with CBT?

Do you have any advice on how I could simulate this system correctly?

1 Like

I will admit that I do not fully understand what you are trying to accomplish, nor what specific problem you're running into. However, the simple answer to this question:

Is that you should just use static variables that are tied back to the trade. Normally, you can achieve this just by using the symbol as part of your static variable name, for example, something like this would work just before or after calling bo.EnterTrade() in the CBT:

StaticVarSet(sig.Symbol+"par1", par1);

However, if you really have multiple open trades for the same symbol, then you'll need an additional component in the static variable name to be able to distinguish between two different trades for symbol ABC. For example you could include the datenum for the entry date of the trade.

When it's time to exit the trade (which has it's own set of challenges, especially if you have multiple open trades for the same symbol), then you can use StaticVarGet() using the same naming convention as you used for the StaticVarSet() call.

Thank you mradtke for your reply and for the suggestion. I really have multiple open trades for the same symbol (up to 4).
You're right! I could just save the extra information I need on some static vars. The solution was under my nose... :slight_smile:

BTW I'm still learning the CBT and I'm not yet comfortable with the methods, maybe part of my difficulties came from that. So I find your smart reply very useful


1 Like