Random Entry System
Author: Net4walk
Creation Date: 5/14/2009 9:30 PM
profile picture

Net4walk

#1
I hope somebody can help me here.
I am trying to test a random entry system on a portfolio of 20 stocks. System will use a random generator to decide whether go long or short (50% chance for each). The stop loss will be a volatility trailing stop say 8 times of 20 day ATR. System will always be in the market, that means as soon as a position is stopped out, it will enter again randomly long or short and set the new trailing stop to 8 times 20 day ATR based on the entry price. Every position will only risk 1% of the total equity, all the positions addup have a maximum risk of 15% of the total equity. That means the system will only take maximum 15 positions on 15 stocks, one position for each stock.
I am able to do everything except for the 1% risk for the each position, because the script won’t let me choose how many shares to buy for each position. I have to choose different amount of shares for each position to achieve 1% risk ‘cause the stop loss amount for each position is different. How can I implement this system in WealthLab. Please help me.

Thanks
profile picture

Eugene

#2
To assign the initial risk of 1% per each position, you'll need to: 1) set the risk stop level in your strategy and 2) use the Max % Risk position sizing option.

See Portfolio Simulation Mode > Max Percent Risk in the User Guide for a detailed explanation of the position size option, RiskStopLevel in the QuickRef for a code example, and I'd also recommend the following illustrative forum thread:

Risk Based Position sizing
profile picture

Net4walk

#3
Hi Eugene,

Thanks for the reply. I made some codes to implement this system, but I got a runtime error. Could you please help check where is the problem.

Thanks


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

Eugene

#4
1. No wonder that you've got that runtime error, as your index is going out of range when using Open[bar+1] at the last bar in your data.

2. In addition, your strategy is peeking into the future by looking to cover the position on a stop at bar (should be: bar+1). More information available in the WealthScript Programming Guide, Programming Trading Strategies > Peeking and Wealth-Lab 5 Wiki:

Bars, Loops, and Bar + 1
Peeking | The Dangers of Looking Ahead in Trading System Development
profile picture

Net4walk

#5
Thanks Eugene!
I just thought that Open[bar+1] would be the actual fill price and using that to calculate risk level would be more precise. But anyway, I fixed it and it's working under Max Percent Risk 1% position sizing model.

But I have a question....How does WL calculate the equity which is the base for calculating the 1% risk for each trade? Could you give me the algorism, please.
I tried to save all the Positions to a cvs file. the 'Shares' field is all 1.

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

Eugene

#6
QUOTE:
I tried to save all the Positions to a cvs file. the 'Shares' field is all 1.

Let me just quote the QuickRef for Position.Shares:
QUOTE:
In portfolio simulation mode, all trades are pre-executed using 1 share per Position, and then position sizing is applied after the fact. So the Shares property will always return 1 while the Strategy is executing.

The same applies to Alerts while the Strategy is executing.
profile picture

Net4walk

#7
Wow, you are working late and hard!

Yes, indeed, now I understand the logic on the approach how WL is using to apply the position sizing.

I just want to know how WL is calculating the current total equity when applying the position sizing. I hope that's not a black-box to be kept in secret.

Say, I am using portfolio simulation mode with 1% of Max percent Risk for every position, with a start equity $100,000. I have all the trades lined up.

1. The first one is simple, I'd risk 1% of 100,000(current equity) which is $1000, I calculate the shares by 1000/(risk per share). Say on this trade the total cost on all the shares is $15,000.
2. But how to determine the second one on current equity? Does WL use $90,000 or $100,000 or ($100,000 - $15,000)--the buying power left?
3. How about the third one? And this time the first trade stopped out with a profit of $500. So how does WL Calculate the current equity for the 1% to risk?

And there are many ways we can choose on how to calculate the current equity for how much percentage to risk. Do we have the choice in WL?

For the ways how to calculate current equity, Van Tharp's "Definitive guide to position sizing" has more details

Thanks~

profile picture

Cone

#8
There's only one value of equity - it's the cash plus the value of the equity positions at the close of the bar. Do we really need Van Tharp for this one?
profile picture

Net4walk

#9
My belief is that the risk control is the 'life or death' part of trading. So every time I take a trade, I will calculate very carefully how much I will risk on this trade. I alway calculate my risk using the percentage of my equity, that makes the equity calculation very important to me.

The way WL calculates equity is risky, because it doesn't consider current stop loss level for each active position. And another question is how to calculate the value of a short position?

The equity model I would like to test this random entry system on is reduced equity model. In this model, if I take a position risking $1000, I would take this $1000 off my equity to calculate next position risk. I would then put the $1000 back to my equity when this position shows a profit and I raised my stop to break even. If later I move the stop to lock in some profit, i would add this locked in profit to my equity.

I noticed the current WL couldn't do this equity model, because this model has to know the stop levels for a position from EntryBar to ExitBar. The Position type doesn't have a list of stop levels for each bar between entrybar and exitbar.
profile picture

Eugene

#10
I remember some discussion on external forums around 2003-2004, maybe it was at Chuck LeBeau's forum or at TurtleTrading, regarding testing those Van Tharp's equity models (total, core and reduced, you know). The impact on the result wasn't groundbreaking.
profile picture

Net4walk

#11
It's still nice to have this option to use different equity models. For the same strategy, how you position sizing will determine the draw down, the chance of ruin, the probability of getting a nice return etc.

Anyway, I have an ambitious plan on this, please tell me if it's operatable:

1. I will use the Tag property in Position type(thank WL that it's not readonly) to log the stop level on each bar with an array StopLevel[0 to ExitBar-EntryBar], I will make sure to log it on every bar for any open positions.

2. I just noticed and tried that we can design a windows form in Extension DLL, and actully load that form from inside the script. This gives me a lot of imagination. I will transfer all the useful data from inside the script to the form, then I can apply my own position sizing method, and show the analysis on the form.

That's a lot of work, but that's fun!
profile picture

Eugene

#12
1 - Although I haven't experimented with this, on the surface it can work out - as you said, the property isn't read-only.
2 - I'm not sure if I got your logic right, but alternatively you could implement the IPerformanceVisualizer interface and create a performance visualizer. After the Strategy completes execution, you can get the complete Performance results and massage that data.

Here are lots of examples to work with:
Community.Visualizers source code

Good luck.
profile picture

Net4walk

#13
Tell me if I am wrong here:
I don't think I can implement the IPerformanceVisualizer to get what I want. I want to change the way WL calculates the equity, and base on the new equity calculation to position size each 1 share trade. It's not possible unless I have WL source code. I can't even instantiate type SystemPerformance and SystemResults for the visualizer, 'cause I don't have access to those internal obfucated methods.
So, I am pretty much on my own here.

profile picture

Cone

#14
QUOTE:
The way WL calculates equity is risky, because it doesn't consider current stop loss level for each active position.
That's apples and oranges. WL is "marked to market" equity. The value is the last price paid. It is not "risky", it is real.

You can calculate your "EquityAfterStop" values however you like, but it's not going to change the actual value of your account, i.e., the equity.

QUOTE:
If later I move the stop to lock in some profit, i would add this locked in profit to my equity
Sounds pretty risky to me. What once was a profit can easily be turned into a loss (even a large one) overnight. Since you're trying to control risk, how do you account for this eventuality? It will happen.


No, Visualizers won't help, but PosSizers will - when they become available (I think late this year). In PosSizers, you can take the Equity value, throw it away, and invent anything that you want to use as equity for your sizing calculations.
profile picture

Net4walk

#15
Over night gap would happen to any equity model, I guess I would account for this by not risking too much percentage on my positon.

Anyway, people have different opinions on different equity models, just like yours and mine. That makes it even more important for WL to give users an option to choose different equity model to test stategies. Do you agree me on this?

I don't know how are you gonna implement PosSizer, but hopefully it lets us to calculate the equity based on current stop on each active positon.
profile picture

Cone

#16
Does your broker show you different values for your account based on your GTC stops?

While we respect everyone's opinion, I don't want to give you any false hopes. MTM Equity isn't going to change and there won't be an option for different models, sorry.

PosSizers (called SimuScripts in Version 4) allow you to access Porfolio Equity (MTM). From that value, you can add or subtract any number you want. You can loop through active positions, determine their shares and stops, and calculate an equity basis for sizing. It's very straightforward and everyone can have their own solution.
profile picture

Net4walk

#17
Thanks Cone. Like I said I was planning to do the position sizing based on all positions by myself anyway. I plan to load a new form designed in extention DLL from the script, then I can do a lot of interest things in that form. Thank WL to let me do this.
profile picture

Cone

#18
Consider using Strategy Parameters as an input for any numeric value. Depending on what you need to accomplish, using "Straps" is a lot easier than integrating a whole form just to enter values!
profile picture

Net4walk

#19
Thanks
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).