RiskStopLevel & Max % Risk: unexpected trade size
Author: akardar1
Creation Date: 4/9/2015 2:28 PM
profile picture

akardar1

#1
Hi,

I am testing an idea on a basket of futures, but drilling down on 1 contract to troubleshoot.

SCALE: daily
Date Range: 01/01/2001 to 31/12/2004
Starting equity: 100,000
Max percent risk: 2%
Margin Factor 1:1

I run the test and get a list of 30 trades > I copy to excel to verify the trades.
The sums of gains and losses in Excel = the Net Profit on the front end. Good.
Because i am only testing with 1 contract (WTI), it follow each trade through and the equity and can work out the size for the trades.

In the list of trades, the entry price, exit price, and riskstoplevel @ the point of entry are as expected.
However, for some reason, a manual calculation of the signals shows that some trades are taken with "wrong" size. (as if the system is choosing another logic by which to calculate size)

For instance, i a trade where difference between entry & stop is 824 usd, and risk unit (2% of equity is) is 1933, it should trade 2 lots, however, trades tab shows 4 lots. Screenshots to follow
profile picture

akardar1

#2
I have attached some screenshots, but the trades tab shows this well.
profile picture

Eugene

#3
Hi,

Looks like both your trades are done at open i.e. BuyAtMarket. However, your screenshots create impression of not using the signal bar's close price. Please refer to the Wealth-Lab User Guide > Strategy Window > Backtesting Strategies > 100% of Equity Sizing > Basis Price.

I couldn't find what position size did you expect in #1 (i.e. what's the discrepancy?) but on screenshot #2 it's clear that since the basis price is much closer, the risk is smaller. Hence 4 lots not 2.

profile picture

Cone

#4
Eugene's right. On CL TEST.2 the basis price must be the close of the previous bar: about 86.65, which is 0.41 from the 86.24 stop. 1933 / 410 = 4.7, or 4 contracts.
profile picture

akardar1

#5
Thanks Eugene, Cone,

Example # 1 was only to show a working example (let's ignore that screenshot).

Yes, you are correct, I am trading at the open and using BuyAtMarket. (This made sense, I have a signal bar based on the closing price, and I execute at the next bar's open). I believe this is the correct way of doing things to avoid the peek forward syndrome.
However, after having now refreshed my memory on the Basis Price, this raises some concerns.

In this case the signal bar's close is the basis price > this is the price which WL (PosSizer: max% risk) uses to calculate the number of lots to trade. Therefore this is the price I should be execute at (not the next bar's open). In reality, a MOC order.
Aren't I peeking forward If I am executing on the signal bar's close?

With the current setup, it makes no sense to size a position based on the close, and trade at the open (which could be X ticks away).

QUOTE:
However, your screenshots create impression of not using the signal bar's close price


Using it for what ? Entry ?
profile picture

Eugene

#6
QUOTE:
Aren't I peeking forward If I am executing on the signal bar's close?

No but AtClose orders can't produce alerts. (An exception that works with few providers capable of delivering partial bar data like Yahoo! can be found in the WealthScript Programming Guide > Programming Trading Strategies > Alerts > How to: Alert for AtClose Signals).

QUOTE:
With the current setup, it makes no sense to size a position based on the close, and trade at the open (which could be X ticks away).

But Wealth-Lab must determine the position size before making entry. In other words, it uses today's close price to tell you after today's close how many shares/contracts to buy/whatever tomorrow. You can not do it at the open tomorrow as the price will be moving away!

Quoting the FAQ on how the process works in Wealth-Lab:

1. A complete bar updates (can be 1-minute, 1-day, etc.)
2. Your strategy executes over all the bars in the chart and determines if it wants to trade on the next bar.
* If it doesn't, you don't do anything.
* If it does, you place orders for the next bar and they're worked by your broker. (Process complete).

The good news that it is possible to use tomorrow's open price for Alert sizing. For AtMarket orders only and for data providers that support GetSessionOpen (i.e. can return the opening price of "today" shortly after market open), the Position Options PosSizer contains the "Skipped trade solution". For real-time alerts, it peeks at the Open.PartialValue (if available) of the incomplete bar ("Ghost bar") to determine the new basis price. Unfortunately, it should NOT work with ASCII data provider (which you're using on screenshots) since it's incompatible by design.

QUOTE:
Using it for what ? Entry ?

For making your position size calculations. You were under incorrect impression that the position sizing for AtMarket orders is calculated based using the entry price.
profile picture

akardar1

#7
Thanks Eugene,

- If am not peeking forward (using AtClose), then why can't I execute at the close ? Theoretically, why am I not peeking forward ?
- In practice, if the bar closes at 10PM for instance, I should know whether my condition is true a few minutes before hand, enough time to enter.

QUOTE:
No but AtClose orders can't produce alerts. (An exception that works with few providers capable...


Do you know of hand whether either IQFeed and/or CSI support AtClose Signals ?

QUOTE:
But Wealth-Lab must determine the position size before making entry.


I agree and understand. This is why I am using AtMarket (the next bar) to execute, but I can see that it makes no sense if I am using the prior bar's closes to determine my size. Having said that, you cant realistically use the open, as that price move straight off the open, its it seems like a catch 22.
profile picture

Eugene

#8
QUOTE:
- If am not peeking forward (using AtClose), then why can't I execute at the close ?

Wealth-Lab does not generate alerts for AtClose orders. It's all in the WealthScript Programming Guide.

QUOTE:
Do you know of hand whether either IQFeed and/or CSI support AtClose Signals ?

Don't know what "CSI" is but IQFeed does not. If by "CSI" you mean ASCII-exported data, then see my answer above.
profile picture

Shaaker

#9
In order not to create a new post I am using this thread as I fount its title relevant to my question:

I cannot figure out why position sizes calculated by WLD are not consistent with manual calculation. Here are the details:

Capital: 50,000
Position sizing method: Max Pct Risk = 2%

position sizing section of the code:
CODE:
Please log in to see this code.


which generates trades shown in the screenshot (capture 1)

However, the Excel screenshot compares the same trades sizes with those calculated manually. (capture 2)

As you can see some numbers are not even close, and I am wondering why.


profile picture

Eugene

#10
Hi,

Indeed this topic is relevant to your question. Please review posts #3 and #6 above as they apply to your case. The mistake is to disregard the concept of basis price and the idea of determining the position size before "bar+1". Wealth-Lab uses the correct position size at "bar" according to its model.
profile picture

KGo

#11
Also total equity is the max position size when calculated shares exceed Equity.

See User Guide: Reference > Data Panel > Position Size Control > Portfolio Simulation Mode > Max Percent Risk
profile picture

Eugene

#12
@Shaaker

P.S. With Position Options PosSizer from MS123 PosSizer library it's possible to make Wealth-Lab's position sizing "peek" at the opening price of the next bar (bar+1) to determine the new basis price for the order. Try to activate the "Skipped trade solution" and see if it makes a difference.
profile picture

Shaaker

#13
Hi Eugene,

I had already studied all the documents and posts and tried all the suggestions and solutions therein unsuccessfully. However, I think I've just found what is causing this problem:

I am using a Margin Factor of 4 (total buying power of $200k). But it seems that the position sizing does not size any position that costs more than the current capital ($50k), and consequently reduces all the trade sizes to just below 50k.

I was aware of this point as explained in:
User Guide: Reference > Data Panel > Position Size Control > Portfolio Simulation Mode > Max Percent Risk

But my understanding was that using Margin Factor is the solution as suggested in:
User Guide > Strategy Window > Backtesting Strategies > 100% of Equity Sizing > Solution: Use Margin

How can this be explained and ultimately resolved?
------------------------------------------------------------

On a different note I have a questions about the difference between using "Skipped trade solution" vs. "RiskStopLevel = Bars.Open[bar+1] - atr[bar]". Are they not supposed to be the same peeking method (seeing the Open of next bar)? But each method generates totally different position sizing results.


Thank you.
profile picture

Eugene

#14
Hi,
QUOTE:
Are they not supposed to be the same peeking method (seeing the Open of next bar)?

No they are not. The PosSizer can peek and actually size your Alert while you can't easily reach the same result in WealthScript. It does not let you adjust the basis price for the last bar as PosSizers can. There's a special remark in the UG > Strategy Window > Backtesting Strategies > 100% of Equity Sizing:

On the last bar of the chart the next bar's open is not available, so the "AtMarket2" signals generate the standard "AtMarket" Alert using the Close as the basis price.

That's the key limitation resolved by using the "Skipped trade solution".

P.S. Selecting an even Margin Factor (3:1, 4:1 etc) as opposed to a value slightly above (e.g. 4.05:1) is a workaround to avoid dropped trades due to opening gaps.
profile picture

Shaaker

#15
QUOTE:
No they are not. The PosSizer can peek and actually size your Alert while you can't easily reach the same result in WealthScript. It does not let you adjust the basis price for the last bar as PosSizers can. There's a special remark in the UG > Strategy Window > Backtesting Strategies > 100% of Equity Sizing:

On the last bar of the chart the next bar's open is not available, so the "AtMarket2" signals generate the standard "AtMarket" Alert using the Close as the basis price.

That's the key limitation resolved by using the "Skipped trade solution".

I understand their difference in terms of generating Alerts. However, my question was that why back testing Trades sizes are not the same.
When using "Skipped trade solution" I cannot duplicate and verify the trade sizes by manual calculation. Neither can I figure out how they are even calculated.

I am using this code with "Skipped trade solution"

CODE:
Please log in to see this code.



QUOTE:
P.S. Selecting an even Margin Factor (3:1, 4:1 etc) as opposed to a value slightly above (e.g. 4.05:1) is a workaround to avoid dropped trades due to opening gaps.


As mentioned in my previous post, I have been using Margin Factor of 4:1. But that does not resolve the problem of reduced sizes.
profile picture

Eugene

#16
QUOTE:
And I am not even able to figure how they are calculated.

They are calculated using the basis price which is Close[bar] by design.
profile picture

Shaaker

#17
And what about even Margin Factor not helping reduced size problem?
profile picture

KGo

#18
QUOTE:
And what about even Margin Factor not helping reduced size problem?

Expanding on my Post #11, specifically from UG:
QUOTE:
If the calculated position cash value is greater than the current equity and no other equity Positions exist in the Portfolio Simulation, position size will be based on 100% of current equity.

Margin Factor is not a multiplier of equity for the Max Risk sizing calculation. It only adds usable margin and the opportunity for more positions. Using 4.1 MF will generally allow 4 concurrent 100% equity positions which is what was shown in initiating post #9. Each of the 4 positions is sized at ~100% current equity.

Also right click equity curve chart and use "Copy Equity Curve Data to Clipboard" and paste to excel to calculate position size from the daily values.
profile picture

Shaaker

#19
Thanks for following up.

QUOTE:
Margin Factor is not a multiplier of equity for the Max Risk sizing calculation.


I agree. I understand the calculation mechanism and this was exactly my point in post #13. But in the same post I was asking why then the user guide is providing this solution as a workaround which is not working:

User Guide > Strategy Window > Backtesting Strategies > 100% of Equity Sizing > Solution: Use Margin

It seems that the User guide is misleading and needs to be corrected
profile picture

Eugene

#20
QUOTE:
It seems that the User guide is misleading and needs to be corrected

Incorrect. The User Guide is talking about the specific "100% Equity sizing" case where trades are dropped. You are not experiencing this issue as all trades are taken. The fact that they're sized at the Close[bar] is a different question. Wealth-Lab does this by design but you can affect it using PosSizer's "Skipped trade solution".
profile picture

Shaaker

#21
Let's look at this simple example:

CODE:
Please log in to see this code.


and run it on a data set with only two symbols: PG, CHX

with:
Starting Capital: $10,000
Max Percent Risk: 2%
Margin Factor: 4:1
Data Range: 1/15/2020 to 9/4/2020
Scale: Daily

See the results in capture1. And capture2 shows that trade sizes for CHX match manual calculation but those for PG are all reduced so that purchase cost would be less than the current capital. .

Please provide a workaround so that WLD would correctly size the positions for PG..




profile picture

Shaaker

#22
correct capture2
profile picture

KGo

#23
Truncate(Current Equity / Basis Price) = Max Shares

So for the first PG buy:
Truncate(10,000 / 135.77) = 73 Max shares allowed to buy
Where 135.77 is the CLOSE price of bar before the entry.(Close on 8/19/2020)

The 105 shares you calculated are not used because they exceed the max allowed at the BASIS price (Do not use the open purchase price).
105*135.77 = 14,255.85 exceeds 10,000 allowed so use the max of 73 shares as shown in the WL trade list.
profile picture

Shaaker

#24
Thanks KGo,

Totally correct. I was trying to show exactly the same thing (which you had also explained in #18). However, it would have been helpful if there was an option in WLD to skip the truncating action by allowing using Margin power as the available capital (instead of the current equity) similar to what you can choose to do in real world trading.


Also with this example, I waned to show that trades don't get dropped. Instead only their sizes get reduced: Just run the code on PG with 100% Equity and no Margin (1:1). As per User guide you are not supposed to see any trades as they all would be dropped, But it is not the case and that is why I was saying that the User guide is misleading...
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).