Most trades not entered after adding RiskStopLevel
Author: CharlieChalk
Creation Date: 8/20/2014 11:45 AM
profile picture

CharlieChalk

#1
HI guys i'm new here and struggling to get some very basic coding done. While in time i need to go back and learn C in the meantime i'm in the middle of something and would like to complete this first before going back to basics.

I have this code of a basic breakout system which i have added the RSIKStopLevel function but now when i apply the "max precent risk" to the Position SIzer most trades are not eventered. I'm basically trying to get to the point so that i can have a ATR adjusted stop level and position size adjusted off ATR too!



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

Eugene

#2
Hi,

The way you set RiskStopLevel is like it's expressed as a distance whereas Wealth-Lab expects a price level:
CODE:
Please log in to see this code.


Is this closer to what you intended?

CODE:
Please log in to see this code.


P.S. The slider parameter names should not be identical or optimizing the strategy would fail.
profile picture

CharlieChalk

#3
Hi Eugene,

thanks for the quick reply earlier today.

I have changed the code and added as you suggest and while teh code works and trades are not being missed, when i change the RSIKStopLevel used from 1% to 2% or 5% to 10% the results are the exact same.

Within position sizing i have one option selected which is 1 Share\Contract

Regards,

Charles
profile picture

Eugene

#4
That's right. When you choose "Fixed Share/Contract" (or any other option but "Max % Risk"), changing RiskStopLevel value won't affect your position sizing. You have to select "Max % Risk" for that option to apply.
profile picture

CharlieChalk

#5
Damn, first post was just lost but i guess this post will now be shorter and more concise.

I have added the RiskStopLevel and am still having issues. If i remove the code i get some coherent results with basic "Shares/Contracts = 1" but as soon as i add the code you help with above it doesn't work. What also confuses me is the code basically states that a stop should be placed X percent below the entry price, yet this does not occur?!

Also, i have been trying to use Percent Volatility for position sizing and when this is applied to my futures data 2/3 of the trades are not taken and the equity curve looks like that of the Himalayan foothills, i have no options selected within this position technique.

Also, if i use Max % Risk 3/4 of the trades are not taken and the equity curve looks like a Bell Curve gone wrong.

For whatever reason these Position Sizing techniques aren't working as intended. Any obvious reason why?

Regards,

Charles
profile picture

Eugene

#6
QUOTE:
What also confuses me is the code basically states that a stop should be placed X percent below the entry price, yet this does not occur?!

The code states that the risk stop level is placed X% below the entry price (for long trades; vice versa for shorts), but you do not follow your own convention for exits. Actually, you're exiting a position with a channel exit. According to the RiskStopLevel entry in the QuickRef:
QUOTE:
You must also be diligent in your Strategy to actually use the established stop level as an exit. If you do not, the Strategy could lose considerably more than the Maximum Risk that you established in the Position Size setting.

To force exit at RiskStopLevel, you might want to replace this...
CODE:
Please log in to see this code.

with this line:
CODE:
Please log in to see this code.



QUOTE:
For whatever reason these Position Sizing techniques aren't working as intended. Any obvious reason why?

For Percent Volatility, the percentage might be too big with regard to your starting or current equity.
profile picture

CharlieChalk

#7
The change in code predictably forces the stop but then it has no trailing stop. I included both so one was to cut your losses short and the other to let your profits run. At the moment with the changes there is no profit exit which was the trailing channel breakout.

I've had a hack at the code myself and posted below

What this means to me is as follows -

If my last position is Short then Cover at the RiskStopLevel else Cover at the N Bar High
If my last position is Long then Sell at the RiskStopLevel else Sell at the N Bar Low

While the code does verify, only 1/100 of the trades are entered.

Thanks, i am trying slowly to get my head round this…

CODE:
Please log in to see this code.

profile picture

Eugene

#8
QUOTE:
I've had a hack at the code myself and posted below

Unfortunately, this is not going to work. Please see the WealthScript Programming Guide on how to handle multiple exits: Programming Trading Strategies > Peeking > Order of Trading Signals. RiskStopLevel can not be used like that, like a method (void) - it's a property. If you correctly assigned RiskStopLevel to a Position before creating it - as Wealth-Lab expects - then you'll be able to access it via the Position.RiskStopLevel property.

Here's an on-the-fly correction:

CODE:
Please log in to see this code.


QUOTE:
While the code does verify, only 1/100 of the trades are entered.


You're probably running the test in multi-symbol backtest (MSB) mode? Selected Max % Risk is probably too big with regard to the current equity. Wealth-Lab is unable to take as much positions at the given % risk. Try a smaller % risk in MSB as opposed to SSB (single symbol backtest) mode. Please see the Wealth-Lab User Guide > Reference > Data Panel > Position Size Control > Portfolio Simulation Mode > Max Percent Risk on how it's determined.
profile picture

CharlieChalk

#9
Hello Eugene,

I'm affraid i'm still stuck on this RiskStopLevel issue. While i have made the changes you suggested, and tried some alternatives after read the manual, the code still does not work as intended.

Firstly, i still get warning message saying something like "must use the RiskStopLevel to apply Max Pct Risk" which as you can see below i have but secondly the short side is entering and exiting trades almost immediatly, which as you can see from the code is in no way implied. The code verifies, the stops are in correct order and i can't see any obvious errors with the code!

CODE:
Please log in to see this code.

profile picture

Eugene

#10
QUOTE:
Firstly, i still get warning message saying something like "must use the RiskStopLevel to apply Max Pct Risk"

You are not assigning RiskStopLevel before establishing a Position in this code as instructed in the QuickRef.
profile picture

CharlieChalk

#11
Hi Eugene,

the butchering continues with some small progressive hacks.

I'm getting error messages saying
"p is inaccessible due to its protected level"

and

"Wealth-Lab indicator.ATR is a 'type' but is used like a 'variable'"

I have added that RiskStopLevel exit code to the entry to prevent peeking and also added "double riskstoplevel"

Thanks Charles

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

Eugene

#12
1. Do not declare "double RiskStopLevel;" It's absolutely not required:
CODE:
Please log in to see this code.


2.1. Logical error:
CODE:
Please log in to see this code.

One can not access a Position's entry price prior to having created it. Like in real life, you don't know entry price until your order is executed. To assign initial stop risk level, pick a different value not tied up to EntryPrice. What you had in the beginning was OK (after my correction), for example:
CODE:
Please log in to see this code.


2.2. Besides, there's a syntax error: the property name is EntryPrice, not entryprice.
profile picture

CharlieChalk

#13
Thanks Eugene,

don't worry i'm doing a course over Sept to Dec in C Sharp but i have another question! I get the error message:

"'WealthLab.Indicators.ATR' is a 'type' but is used like a 'variable'"

As far as i'm concerned it is an indicator and should be placed and coded like the other indicators? I've used the ATR as it is presented in the RiskStopLevel within the QuickRef

Charles
profile picture

Eugene

#14
QUOTE:
"'WealthLab.Indicators.ATR' is a 'type' but is used like a 'variable'"


Like any indicator, ATR is a series so it should be accessed like a DataSeries i.e. ATR.Series (....). For more details please refer to the WealthScript Programming Guide > DataSeries > Series Operators, and to the Wiki link in this post.
profile picture

CharlieChalk

#15
Hello Eugene,


i solved the problem of ATR thanks to a previous post of yours regarding EntryPrice rather than 'entryprice', i had used higher case ATR rather than atr!

Sadly i have another problem here and i'm getting very confused . I thought that i'd be able to program my own position sizing formula into Welathlab?

Contracts =( Risk% * AC Size) / ((ATR14 * PointValue) * ATRStopMult)

I'm finding these gray box PosSizers confusing and how they work with my RiskStopLevels within the code and am just testing these PosSizers in an iterative manner hoping that i can see what they are actually doing and how they are interacting with the RiskStopLevel code

Also the basic riskstoplevel isn't working as intended. As you can see in the code, the stop levels are 2% from the entry price which is the Highest High N bars or Lowest Low of N bars yet many stops are being triggered at much lower prices even though no gaps in the market have occured.

I've also applied PercentVolatility and the results are crazy and the system ends up taking as many as 200k lots though the stop levels appea rthe same. SHoud i not be using an ATR defined stop in RiskStopLevel to match up with theis Percent Volatility posSIzer and is there any formula so that i can see what it does exactly?

Regards,

Charles

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

Eugene

#16
QUOTE:
I thought that i'd be able to program my own position sizing formula into Welathlab?

Contracts =( Risk% * AC Size) / ((ATR14 * PointValue) * ATRStopMult)

At first glance, this doesn't seem different from Percent Volatility position sizing.

QUOTE:
SHoud i not be using an ATR defined stop in RiskStopLevel to match up with theis Percent Volatility posSIzer and is there any formula so that i can see what it does exactly?

The Percent Volatility PosSizer doesn't take your RiskStopLevel into consideration. See below:

QUOTE:
I'm finding these gray box PosSizers confusing and how they work with my RiskStopLevels within the code and am just testing these PosSizers in an iterative manner hoping that i can see what they are actually doing and how they are interacting with the RiskStopLevel code

FYI, PosSizers usually implements their own sizing logic, but over a dozen of them work with RiskStopLevel by virtue of supporting the "basic" position sizing options (fixed dollar, percent equity, max risk %). Only one PosSizer requires RiskStopLevel - Increasing risk % with growing equity.

QUOTE:
Also the basic riskstoplevel isn't working as intended

Sure it isn't. Take a careful look at your definitions:

CODE:
Please log in to see this code.


Do you already see a logic error? (Hint: you'd want short trades to be stopped out at hi[bar] * 1.02, and not at lo[bar] * 1.02 !
profile picture

CharlieChalk

#17
Ha! yes a basic problem. However; the trades i was looking at were long trades and changing the code has made no difference and many trades with Fixed Dollar mode have much large stops than 2%.

Also, the PosSizer Percent Volatility compounds so i obviously get an exponential equity curve which is handy for rubbing your hands like Gollum but is there a way of turning this off so i can see the progression of the equity curve and how smooth or volatilie it is over time. Obviously the stats can tell me much of this but a picture tells a thousand words!

there has been a big communication error from the start, i only ever wanted to work with RiskStopLevel because i thought this needed to be in the code on some level to use volatility adjusted stops and position sizing.

Is there any way for me to directly program the formula for volatility adjusted positioning myself, i can just visualise in my head what i program and see in a forumula more easily. This PosSizer is becoming a head banger and after a week i'm just not moving forward. Having searched all over the place on the web, site, docs etc i finally stumbled in to Position Option buried in the drop down list for Position Sizers. The Position Options allows me to change wehther to reinvest or not but then when applied to Max Pct Risk, it still reinvests, i go back and check the options and it still is checked not to reinvest. I then apply the PercentVolatility to teh system and check not to reinvest except it still reinvests, but this time, everytime i go back to Position Options it has become unchecked!

My head is in a spin and i'm finding it difficult to work with a gray box of position sizers that i don't know how they work made harder to understand because of the compounding of all the trading results.

I used to think that PercentVolatility and other PosSizers looked at the RiskStopLevel and then would position size off this, now i beleive this isn't the case. I'm guessing that "Percent Volatility is a percentage of the ATR * PointValue which is then divided into (AccountSize * Cap Positions at X% of Account Equity)?

Cheers Charles
profile picture

Eugene

#18
QUOTE:
Ha! yes a basic problem. However; the trades i was looking at were long trades and changing the code has made no difference and many trades with Fixed Dollar mode have much large stops than 2%.

RiskStopLevel does not apply in Fixed Dollar mode - only when Max % Risk is engaged.
QUOTE:
is there a way of turning this off so i can see the progression of the equity curve

Are you talking about effects of profit reinvestement? Position Options has the option to turn off reinvesting profits, plus, it supports Max % Risk to be used with a RiskStopLevel.
QUOTE:
Is there any way for me to directly program the formula for volatility adjusted positioning myself,

I believe this has been discussed on the forum already. Programming your custom PosSizer in Visual Studio is the way to program an own formula. Percent volatility can also be implemented as Max Percent Risk, but you already knew that: How do I implement volatility position sizing?
QUOTE:
The Position Options allows me to change wehther to reinvest or not but then when applied to Max Pct Risk, it still reinvests,

Answered here.
profile picture

CharlieChalk

#19
Hi Eugene,

i'm back on this RiskStopLevel stuff again!

I have added RiskStopLevel to a buy the dips system to prevent worse case losses in a 1987 / flash crash scenario. When the system is applied to SPY it just plots many trades on every bar even though it verifies. I have looked a Peaking and think that the application is fine and the risk stop is applied before entry and reentry. I have played with forcing stop on {bar +1] but this makes no difference.

Charles

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

Eugene

#20
Please please read some basic C# 101 book as you're making obvious syntax errors:

How do I start with C# ?

It doesn't make sense to continue until you have a thorough understanding of the absolute basics such as the use of curly braces:

CODE:
Please log in to see this code.

etc etc.

This is the same error (missing curly brackets) repeated four times in this snippet.