portfolio backtester - single pass priority logic  [SOLVED]

Questions about MultiCharts and user contributed studies.
mjm
Posts: 28
Joined: 22 Aug 2012
Has thanked: 9 times
Been thanked: 4 times

portfolio backtester - single pass priority logic

Postby mjm » 24 Aug 2012

I'd like to use the Portfolio Backtester to simulate a strategy where the decision whether to enter a new position depends on proposed trades this bar for other symbols. For example, I might want to limit the portfolio to holding no more than two stocks from each industry sector.

Suppose my Signal script generates Buy (to enter) signals for 30 symbols spanning 5 sectors. Within each of the 5 sectors, I have a way to rank the candidate trades. But I can't simply assign this ranking to the PortfolioEntriesPriority function because I want to make the top two trades from each sector, not the top 10 trades overall.

As a programmer, what I really want is a way to produce the list of candidate trades in one pass, and then sort and filter that list in a second pass before submitting the trades for execution. But that doesn't seem compatible with the single-pass design of the Portfolio Backtester.

Is there a way to do what I have in mind?

Thanks,
Mike

References:
http://www.multicharts.com/trading-software/index.php/Understanding_Portfolio_Backtesting
http://www.multicharts.com/trading-software/index.php/PortfolioEntriesPriority

mjm
Posts: 28
Joined: 22 Aug 2012
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtester - single pass priority logic

Postby mjm » 25 Aug 2012

what I really want is a way to produce the list of candidate trades in one pass, and then sort and filter that list in a second pass before submitting the trades for execution


It occurs to me that maybe I can do this by adding each symbol to the Instruments list twice, like {A, B, C, ..., A, B, C, ...}. On the first visit to symbol A it would calculate its candidate trades, and on the second visit it could decide whether to make the trade based on a review of all the other candidates.

Perhaps a dummy symbol XXX could occupy the middle of the list, like {A, B, C, ..., XXX, A, B, C, ...} and the script for XXX would make global decisions.

A little clumsy but is there a better approach?

Thanks,
Mike

mjm
Posts: 28
Joined: 22 Aug 2012
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtester - single pass priority logic

Postby mjm » 28 Aug 2012

It occurs to me that maybe I can do this by adding each symbol to the Instruments list twice, like {A, B, C, ..., A, B, C, ...}. On the first visit to symbol A it would calculate its candidate trades, and on the second visit it could decide whether to make the trade based on a review of all the other candidates.


I experimented with this idea. As far as I can tell, Portfolio Backtester treats the first and second instances of symbol A as distinct entities. That is, the list above is effectively {A1, B1, C1, ..., A2, B2, C2, ...}. So, for example, if A2 executes a trade, the new position is not visible in later bars to A1. As far as A1 is concerned, it never made a trade so it continues to have no position. This disrupts my plan to use A1 to make decisions on later bars.

Similarly, interposing a dummy symbol in between the two sets of symbols, as {A, B, C, ..., XXX, A, B, C, ...}, isn't much help. The idea was to use the first visit to A to get information about our current position and proposed trades, then make global trading decisions when we visit XXX, and finally execute certain trades on the second visit to A. But since the first A doesn't know about past trades that occurred in the second A, XXX doesn't have the information it needs.

This would be solved if while visiting XXX we could submit orders for A, B, C. Then we could use an instrument list with just one copy of each symbol {A, B, C, ..., XXX} with all orders submitted from XXX. Is that possible?

Or equivalently, if we could inspect the positions and trades of A,B,C when visiting symbol XXX. I see Portfolio_CurrentEntries and Portfolio_TotalTrades take a step in that direction but I don't see a way to get anything more detailed about positions and trades in other symbols.

I feel like I must be on the wrong track. Is there a way to use Portfolio Backtester to make trading decisions on the basis of your overall portfolio?

Thanks,
Mike

User avatar
Henry MultiСharts
Posts: 8521
Joined: 25 Aug 2011
Has thanked: 1213 times
Been thanked: 2741 times

Re: portfolio backtester - single pass priority logic

Postby Henry MultiСharts » 28 Aug 2012

Hello Mike,

I think that is possible to achieve your goal with the help of the Global Variables. On the last calculation during the first pass (on the last instrument of portfolio) you can write down to the variables all information you need, on the second pass you can read this info and do further execution.

mjm
Posts: 28
Joined: 22 Aug 2012
Has thanked: 9 times
Been thanked: 4 times

Re: portfolio backtester - single pass priority logic  [SOLVED]

Postby mjm » 19 Sep 2012

I ended up solving this using the two-pass approach with an intermediate dummy symbol where I make the global trading decisions. I used the "ELCollections" library for its more sophisticated global data structures. I highly recommend this package for programmers who are wondering why EasyLanguage is missing basic types like structures, lists, and maps.

So my symbol list looks like {A, B, C, ..., SPY, A, B, C, ..., SPY}. In the first pass across {A,B,C,...} I use the usual Multicharts functions like "Close" to get price history, but refer to global variables to know my trading history and current positions. I store trade recommendations in a global map. I don't do any real trading here.

In the first pass at SPY, I review the entire set of trading recommendations and choose which trades to do according to my strategy logic. I authorize these in the global map.

In the second pass across {A,B,C,...} I consult the global map to see what trades I should do. I use Buy/Sell/etc to execute the trades. I call Multicharts functions like "CurrentShares" and "PosTrade*" and store information about trading history and current position in the global map for use by pass 1 on the next bar.

Finally, in the second pass on SPY, I do some bookkeeping and write per-bar structured reports of trading activities.

I couldn't have done this without the ELCollections library. Using the simple built-in global variables would not be practical when backtesting over thousands of symbols.

-Mike
These users thanked the author mjm for the post (total 2):
Henry MultiСhartsmoses


Return to “MultiCharts”