Setting a stop using Maximum Excursion
Author: sedelstein
Creation Date: 8/28/2013 2:23 PM
profile picture

sedelstein

#1
Two reasons for the post

1) I hope this idea for setting a stop might be useful to others out there
2) I can use help as my C# is rusty and was hoping to see if there was a better way to code this


I was thinking that it would be a good idea to examine a stategy's trades and look at the worst maximum excursion for each one. Typically I find that the distribution of these has a long left tail (negative skew). I want to hold on to trades if the MAE is not too bad (most of the trades) but exit / stop out before I get into the very left tail. One of the ways is to stop out at the 25th (or some other) percentile of the distribution.

Here is some code that I wrote. I'm not sure it's the best way to get the job done. I'm hoping the C# gurus out there might offer some suggestions and it's helpful to you. Fwiw, this is how Microsoft Excel does the percentile calculation. Others might want to calculate it differently.

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

Cone

#2
If the idea is to use this information in the trading script, you shouldn't be accessing SystemResults, that would be peeking. Instead, you should look at already-closed positions.as of the current bar when you call the function.
profile picture

sedelstein

#3
No I wont be using in the trading script. I understand about peeking. When I take on new trades, having the history of these excursions will help me manage the positions and set some stops going forward. I realize it's untestable as a stand alone (at least in the current version of WL).
It would be interesting to have the ability to use results from one strategy (up to the preceding bar) to use in a different strategy.

Is the C# code ok? I wasn't sure if I should put the results from the List into an array, sort that and get the percentile or if there was a way to use the List directly
profile picture

Eugene

#4
Of course Lists can be sorted. Just create a List based on that unsortable IList<Position>. LINQ would make it appear better, but the old-fashioned manner using a delegate still works:
CODE:
Please log in to see this code.
profile picture

sedelstein

#5
Thank you Eugene. It will help make me a better C# programmer.

The other thing I am thinking about w/ regard to stops is relating MAE/MFE to different measures of historical vol just prior to trade entry as a guide to setting stops
profile picture

sedelstein

#6
Question

In my case, I passed SystemResults results to the methond

I noticed your version did not. How does your method "know" the positions?

Thanks for explaining it to me.

Steve
profile picture

sedelstein

#7
I got it to work when I passed
CODE:
Please log in to see this code.


and then
CODE:
Please log in to see this code.


Is this "cheaper" e.g. more parsimonious than using the array?

Thanks again
profile picture

sedelstein

#8
fyi for anyone using the algorithm, here is a correction

CODE:
Please log in to see this code.


not +1
profile picture

Eugene

#9
CODE:
Please log in to see this code.

Corrected both posts above.