Highest Priority in Simulation Mode not honored
Author: novendeh
Creation Date: 5/18/2011 9:23 PM
profile picture

novendeh

#1
When I run the following strategy in simulation mode, using 90% Equity and 5 stocks, it doesn't seem to buy the highest priority stock.

CODE:
Please log in to see this code.
profile picture

Eugene

#2
Some obvious mistakes:

1. When you uncomment it, rename "MA Period" so that there's no two or more identic strategy parameters:
CODE:
Please log in to see this code.

2. Since you're using a 240-period ROC, it's incorrect to start the bar this way, well before all indicators start producing valid values. Use GetTradingLoopStartBar(roc12.FirstValidValue) or something like that to fix this:
CODE:
Please log in to see this code.


Fixed code:
CODE:
Please log in to see this code.


If you have doubts, please clarify how do you determine that "it doesn't seem to buy the highest priority stock" by example.
profile picture

novendeh

#3
Eugene - thanks - your code is cleaner but does not fix the problem. Let me explain more

I have five stocks that I can buy from BND DBC VEU VNQ VTI
I buy the top one, based on ranking.
I use portfolio simulation mode.I
Run simulation for last five or seven years.
In results, many of the more recent buys are BND, which does not have highest ranking - I believe it is lowest ranking
More confusing, is I change line to LastPosition.Priority = - Rank[bar];, I still get buying of BND in more recent buys.
I use Yahoo daily data.


This code seems to buy the lowest ranked strategy, but a similar one that buys on last day of month works fine.

Let me know if this makes sense to you or if you need more explanation. Thanka.
profile picture

Eugene

#4
There is no problem. The code works correctly, it does pick the highest priority stock.

The code is programmed so that there's very high likelihood of repurchasing the next day after exit. The signals in BND just happen to occur earlier than in the other stocks given the default numberOfBars. Reduce the numberOfBars to e.g. 5 and BND won't "dominate" the backtest.

Take March, April 2011 for example. Run in single symbol mode. Notice the entry signals in BND happening on the 2nd day of month. Now compare to the other 4 stocks: entry signals are coming much later. Hope this makes it clear.
profile picture

novendeh

#5
Eugene - I am still missing something. From your last response, if I change number of years the simulation is run from 7 to 3, results look good. This is because, I believe, that some of these ETFs did not exist 7 years ago, hence different buy/sell dates.

Yet it I just look at march through may 2011, the highest rank for BND is about 12. Yet, the lowest rank for DBC is about 35. Why doesn't simulation buy DBC in this time frame with numberOfBars the default value of 21. The same is true from most of the other ETFs during this period.

I'm sure I am missing something obvious, but I can't figure it out.

Thanks.
profile picture

Eugene

#6
You're wondering why the system would "stick" to a stock and "follow" it. There is no mistake but you should step through your own system's trades carefully. I'll try to explain again what happens.

I. With the default periods, add this line anywhere within the entry rule block for easier visualization:

CODE:
Please log in to see this code.


Now run a portfolio backtest and then switch symbols to step through the trades. Note the order of bars with colored background following the first exit signal. BND always comes first to re-enter regardless of priority, as other candidates have a different potential entry date! The longer is the exit parameter (e.g. 21 vs. 5) the more obvious it becomes: at some point in time, BND did have a higher priority than others and then it started to "dominate". Notice the signal order in May 2011. Which symbol gave the first entry signal? BND. Others simply haven't had the chance, and priority is not in play here!

II. For debugging, add this one on top of your entry conditions:

CODE:
Please log in to see this code.


This forces the system to buy on the 1st of each month (and only), equalizing the chances of making an entry (by fixing the date) so that you always know when it happens. With the default Period of 21, re-run the MSB. Does it make the backtest results appear more reasonable than they were before? I bet it does.

What happens here? Now each symbol has an equal chance of making an entry; no single stock would "dominate" the backtest because your rules tell to re-entry on the very next day (while the stock is still above the 200-day SMA... it can last for months).

P.S. To stress this once again, this by no means is a "priority problem". It's about the order of entry signals. The system is doing exactly how you programmed it. Hope this finally makes it clear, as I did my best to explain it to you. ;)
profile picture

novendeh

#7
Eugene - Thanks for above. The code for buying on the first of month works fine. However, I am still trying to understand how simulation works compared with how I think it should. I tried a very simple two stock strategy - BND and VNQ with buying the top ranked stock. Same results if I use data for most recent 7 years, dominated by BND in the last year or so. However, if I change simulation to 3 years, the last several trades are VNQ.

From my way of thinking (which probably is not how simulator works) is:

I should have choice of of buying VNQ or BND at each 21st day, since I've sold the day before a position and have the cash available. Yet different results for different start dates for simulation. Entry day of trade for each stock should not matter in "real life" at end of trading sequence. This doesn't make sense to me.

Sorry to keep this thread opened, but I'm tying to understand this.

Thanks for your patience on follow through.

profile picture

Eugene

#8
QUOTE:
I should have choice of of buying VNQ or BND at each 21st day, since I've sold the day before a position and have the cash available. Yet different results for different start dates for simulation. Entry day of trade for each stock should not matter in "real life" at end of trading sequence.

I might have some bad news for you. This is not how Wealth-Lab was designed to work.

Those green bar backgrounds in the code are there for a reason. With my "first day of month" rule removed and the period = 21 days, why do you think on the chart of each symbol Wealth-Lab highlights a bar in Green only each 21 days? And not every day while the stock is above its 200-day SMA?

Because the system still holds a paper position (Raw Profit mode trade) in all these stocks. Why?

Wealth-Lab first executes the system in Raw Profit mode to generate a list of trades to which it later applies your position sizing rules. This means that all entry signals are honored by portfolio simulation in a multi-symbol backtest. In other words, Wealth-Lab applies position sizing rules as an overlay, after creating the complete list of trades in Raw Profit mode (with 1 share/contract).

The capital is not enough to take all these trades at 90% sizing, and how Wealth-Lab works in this situation is explained in the WL User Guide, Strategy Window > Backtesting Strategies > How Trades Are Chosen. The position sizing logic can not close all "ghost" positions by magic behind the scenes. It's the next Raw Profit trade that occurs after the exit signal that matters. That's it.


...How you envision Wealth-Lab working under this circumstances, is called "date-sequenced backtesting" and is thoroughly explained by Dr. Bruce Vanstone in the following discussion thread:

Cone- Pulled this off the web re another back tester.....What does this mean and does Wealth Lab meet this same criteria as far has being just as robust, etc?

Please check out his replies on 11/18/2009 2:11 AM, 11/18/2009 2:06 PM and 11/18/2009 4:36 AM: it's the best read on subject. However, implementing this concept means a major redesign for Wealth-Lab, and currently this is highly unlikely.
profile picture

novendeh

#9
Eugene - this is not bad news - I now understand what and why is going on. Thanks for your time on this and patiently working through the issue with me. I really appreciate it.

I also have a few questions on C#, which were raised with some of your coding. I will start a separate thread since they are really not related to this topic.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).