This article will describe how to build models that maintain multiple positions in Quantacula's Building Block Models. You can add more than one entry and exit to your model, but how do they relate to each other? The short answer is that each entry pairs with the exit (or exits) first encountered below in the list of building blocks.
In the example below, entry A will be paired with both exit B (a profit target) and exit C (a stop loss). Even if there are more exits beneath building block D, they will not be paired with entry A because block D is another entry, which begins a new sequence.
The model below uses two entry blocks to establish two possible open positions; a buy at stop at the highest high in 20 bars, and a buy at limit at the lowest low of 20 bars. It utilizes two exits, like in the previous example; a profit target and a stop loss. In this case, entry A will be paired with both exit C and exit D. Entry B will also be paired with both exit C and exit D.
You can use multiple entries to build models that scale into a position. For example, the model below buys if the CMO (Chande Momentum Oscillator) crosses above its oversold level. We duplicated the buy entry block so that the model takes two positions, each of which is paired with both the profit target and the stop loss exit.
The above setup is a valid model, but it might not behave exactly as you'd expect. When the CMO crosses above -50, both entry blocks will execute at the same time, so the model will take two positions on exactly the same bar. This could be the result you intended, but you might also want the model to hold off on the second position, waiting until the CMO crosses above the oversold level a second time.
You can accomplish this by adding another condition to the second entry, Open Position Count. By setting the parameter value to 1, the condition evaluates to true only if there is currently an open position being held by the backtester.
The Open Position Count condition has a parameter that controls whether you are testing for the number of positions in the symbol that is currently being backtested, or across the whole portfolio. In this case we set the parameter to count the open positions for the current symbol, so the condition will apply on a symbol-by-symbol basis.
Here is a chart from Quantacula Studio of this model in action. Note that the first entry occurred at a slightly lower price than the second. In this case, the 10% profit target was also slightly lower for the first position. Therefore, you see the first position exit prior to the second position.
If you examine the positions list, you might also wonder why the position's profit was reported as 9.80% and not 10%. This is because of the simulated commission that the Quantacula backtester deducts from the position profit.