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.
Size:
Color:
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.
Size:
Color:
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
Size:
Color:
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.
Size:
Color:
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
Size:
Color:
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
Size:
Color:
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
Size:
Color:
fyi for anyone using the algorithm, here is a correction
CODE:
Please log in to see this code.
not +1
Size:
Color:
CODE:
Please log in to see this code.
Corrected both posts above.
Size:
Color: