Limit the number of Alerts and sort them by SignalName
Author: Panache
Creation Date: 1/26/2014 9:28 AM
profile picture

Panache

#1
How do you sort Alerts by SignalName?

If I backtest a strategy using 10% of equity, when I trade it, I only want 10 active positions at a time. If there are currently 9 active positions, I would like my strategy to only generate 1 alert (being the one with the highest priority, ie. SignalName)..

I apologize in advance for not being a C# programmer.
profile picture

Eugene

#2
On how to assign a Priority, look no further:

1. Wealth-Lab User Guide: Strategy Window > Backtesting Strategies > How Trades Are Chosen
2. QuickRef: Priority Property
profile picture

Panache

#3
Sorry, but my question was not how to assign a priority. My question is how to limit the number of alerts generated.

My strategy runs against the S&P 500 and is set at 10% of equity, ie 10 active positions.
At the close on Friday, it had 9 active position.
When I run it, WealthLab generates 25 Alerts.
I only want it to generate the 1 alert with the highest priority, so it will have 10 active positions at the close on Monday without me having to think about which alert to trade.

I think I have it. However, I can't think of an efficient way to combine this with the Lead Bar Solution.

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

Eugene

#4
If there are 9 active positions currently and you want just one more, Wealth-Lab won't take more than one if your Strategy's sizes positions at 10% of equity. And despite that your question is not about how to assign a priority, assigning a priority is what your strategy lacks in order to make it work:

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

Cone

#5
By using priority, you can stay in synch with the backtest and trade the signals in priority order without any special coding. Add the priority to the signal name too. So, if you have 9 positions at 10% equity, and the strategy generates 5 Alerts, you simply pick the one signal with the highest priority (signal name).

Caution, however.
SignalName is a string and therefore the sort is not numeric... e.g, 1, 10 through 19, 100 through 199, are all sorted before "2".

Incidentally, although your solution will work for backtest, it will give you unexpected results on a day on which there exists both exit and entry signals. Why? Because where you've placed a call to ActivePositions.Count (after the exit logic), the signals that you want to sell will still be "active" since they cannot be sold when the future bar does not already exist. But if you still want to do it this way (instead of the easy priority way) you can solve it by storing ActivePositions.Count in a variable, subtracting the number of sell alerts from it, and using the result in the deleteCount statement.
profile picture

Panache

#6
Thanks for the suggestions Cone.

Priority works great for back testing. What I'm trying to work around is the generation of excess Alerts when I run my strategies each day. While picking the Alerts with the highest priority isn't a big deal (I sort the Alerts by Signal Name), it does require me to go back into each strategy, sort the trades (by exit date), count the number of active positions and determine the number of orders to place for each strategy.

I'd like to be able to run all of my strategies through Strategy Monitor. However, when I last tried it, Strategy Monitor also showed the extra Alerts. This is especially problematical, because I may have several dozen alerts from multiple strategies which I need to remember to ignore. Unless I'm missing something, I haven't found a way to manually delete alerts in Strategy Monitor once they have been generated, so the excess Alerts stay there all day.

I know doing this via the strategy code is the hard way to do it, but I assume there is no way I can modify the way Wealth-Lab generates Alerts when running a strategy against a DataSet.

The reason I put the call to ActivePositions.Count where I did was that I assumed I would get inflated performance results if I placed it first. If I have $100,000 in capital, I don't believe I can sell $100,000 on bar + 1 at the same time I'm trying to buy $100,000 on bar + 1 without using margin. Therefore, if I'm limited to 10 positions of $10,000 each, I have to sell one first, before I can buy the next one, so between bars I will have less than 10 positions, even if my strategy wanted me to be fully invested all the time. However, as I think about it, my understanding must be wrong, since all of the standard coding in Wealth-Lab is sell on bar + 1 and buy on bar +1.

In case anyone looks at this thread in the future, the class AlertHolder keeps the priority as a double instead of having to convert it to a string, so the sort by priority returns a properly sorted list.
profile picture

Eugene

#7
QUOTE:
I know doing this via the strategy code is the hard way to do it, but I assume there is no way I can modify the way Wealth-Lab generates Alerts when running a strategy against a DataSet.


Actually, it might be pretty easy to filter out Alerts by Priority when using a PosSizer:

1. Priority Adjustment: don't let it take any positions below your chosen Priority threshold
2. Position Options: same as above (reject a Position if it's Priority is below some threshold) + reject a Position when the number of Alerts is above/below a cutoff value

Unfortunately, rejecting based on the number of Alerts won't work due to a live bug in WL (#62853 In PosSizers, the Candidates list is empty for Alert sizing) so you're left with the other option.
profile picture

Cone

#8
QUOTE:
I don't believe I can sell $100,000 on bar + 1 at the same time I'm trying to buy $100,000 on bar + 1 without using margin.
Wealth-Lab processes market sells first, then market buys, then stop/limit orders next. Consequently, margin isn't required in backtest for that trade. A broker would likely require a margin account for that trade.

The concept about accessing ActivePositions (or a Position's status) after you execute a trading signal bar + 1 may not be intuitive. Basically, that operation is a peeking error because of the way backtest engines work. When a signal on bar + 1 is executed, the engine actually "fills" the trade if the bar is actually available, which of course it is in backtesting. Therefore, you as a strategy developer must know not to do that.

QUOTE:
However, when I last tried it, Strategy Monitor also showed the extra Alerts.
Of course it will, for the reason I explained. In other words, on the last bar of the chart (when Alerts are created), ActivePositions.Count will still reflect the positions you intend to sell. (The engine cannot enter or exit Positions on bars that do not exist, so these signals are only alerts, i.e., cannot be filled.) So, if numberOfPositions is 10 and you have 10 ActivePositions, deleteCount will equal the alertCount on the last bar of the chart, when Alerts are created. You need to modify the code as I suggested previously.
profile picture

Eugene

#9
QUOTE:
Unfortunately, rejecting based on the number of Alerts won't work due to a live bug in WL (#62853 In PosSizers, the Candidates list is empty for Alert sizing) so you're left with the other option.


UPDATE:
This bug will be fixed in WLD 6.9.24. No ETA at the moment as we're still trying to fix an issue with this build.
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).