Change exit rules if there are less than X signals
Author: nisaion2
Creation Date: 12/13/2008 4:46 PM
profile picture

nisaion2

#1
Hello,
Question regarding portfolio level backtesting. I have no experience in C# language. Is it possible for somebody to show how to write a script with portfolio level backtesting?

Simple Entry rules:
Go Long If RSI 6 crosses above 30.
Simple Exit rules:
Set a Profit Target of 4% or exit at Market after 5 bars.

Now is the hard part:
Looking on Portfolio Level:

Investing in each position 10% of equity.
If there are less than 3 signals to open new long positions in the entire portfolio, the exit should be: increase Profit Target by 2% or Hold Open Position 2 days more and exit at Market.

Thank you
profile picture

Cone

#2
Keep in mind that there are hundreds of examples on the Wealth-Lab Wiki, the Programming and User Guides, the QuickRef (F11), as well as the those that come preinstalled. Furthermore, you can gain a lot of insight into programming and WealthScript just by using the Strategy Rules Wizard (Shift+Ctrl+R) to program some simple Strategies.

Here's the Strategy -

CODE:
Please log in to see this code.

QUOTE:
Now is the hard part
The 10% equity sizing can currently be set in the Position Sizing control, but the other rules cannot currently be accomplished in Version 5 until PosSizers are ready next year. You can, however, do this using SimuScripts in Version 4.
profile picture

Eugene

#3
QUOTE:
but the other rules cannot currently be accomplished in Version 5 until PosSizers are ready next year.


Couldn't the global memory be used to count signals for the current bar as they build up?

(Using the Alerts.Count property could make it easier but unfortunately, it won't do inside the trading loop.)
profile picture

Cone

#4
Par for the course, I didn't read this too well. This kind of logic requires you to program the portfolio trading loop in the script since the trading logic depends on other symbols in the DataSet. Multi-Symbol Backtest (MSB) mode can't be used because trades are generated serially by symbol.

A simple list would be a better choice than global memory (if there's an option to global mem, it's probably best to use it). Generally, programming strategies like this is too complex for a novice, but for examples, see the "Rotation" scripts that come pre-installed.

----
We can use this as an example for the Knowledge Base if we can get more clarification of the specifications...
QUOTE:
If there are less than 3 signals to open new long positions in the entire portfolio, the exit should be: increase Profit Target by 2% or Hold Open Position 2 days more and exit at Market.

I don't fully understand when the Profit Target should be increased or when to hold the Position 2 days more. Does it mean that when only 3 Positions are open that the Profit tgt should be 6%? When should a Position be held for "2 days more and exited at Market"? Neither case is clear. You must specific because programming mandates specific requirements or you won't get what you want.
profile picture

nisaion2

#5
Let’s say our portfolio consist of 100 stocks. We want to invest in each position 10% of our equity. (10 max open positions at the same time). Every day(bar) we scan these 100 stocks for new signals.

Today is the first day (#1) and we do not have any open positions but scan generated 4 signals to buy on open (Market) tomorrow.

Day #2, we scan again and see that we already have 4 open positions and we get 3 new signals to open 3 new positions next day. We also see a limit to exit these 4 open positions at price of 4% profit. (Not 6% profit because there were 3 new signals).

Day #3, we scan these 100 stocks again and see that we already have 7 open positions but only one signal to open a new position. In this case we want the system to set a limit (for the next trading day) of 6% but not 4% for all these 7 open positions we already have.
It because number of signals to open new positions in the entire portfolio is less than 3.

Same idea with the increase in number of holding days of currently open positions when the entire portfolio generated less than 3 new signals.

The idea behind all this is that since we do not have many new signals to open new positions we prefer to keep our currently open positions longer and set a little bit higher profit target.

Thank you for your responses.
profile picture

Cone

#6
Okay, got it. I misinterpreted what you meant about 5 bars before.. I corrected the code above to reflect that time-based exit.
profile picture

Cone

#7
Here's a pretty good first cut at it.
Important! To run this script, click on one symbol only! Do not use MSB mode by selected the DataSet name.

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

wl64bit

#8
Just curious, is it possible to make a PosSizer that could implement the above logic without programming the portfolio-trading loop in the script?
profile picture

Eugene

#9
PosSizers only size positions. They're executed after the strategy has finished processing, so there's no way for a PosSizer to affect Strategy trading decisions (e.g. change the days and pftTgt while PosSizer is working.)
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).