- ago
I am attempting to exit a position when the current value of an smoothed Close drops a certain amount. Here is what I tried :
CODE:
         int totalbars = idx - LastPosition.EntryBar + 1;             double max = smoothedPrices.GetHighest(LastPosition.EntryBar, totalbars);             if (smoothedPrices[idx] < exitMultiplier * max)             PlaceTrade(bars, TransactionType.Sell, OrderType.Market);

What Am I doing wrong? Thanks!
0
264
19 Replies

Reply

Bookmark

Sort
- ago
#1
I think the idx index on Highest is "inclusive" such that you don't need the +1 when calculating max. If it was exclusive, then you would need the +1 because you would be one bar short.

CODE:
double max = Highest.Value(idx,smoothedPrices,idx-LastPosition.EntryBar);

CODE:
public override void Initialize(BarHistory bars) {    const int idx = 10;    double highest = Highest.Value(idx, bars.Close, 22); //22 bars includes the idx bar }
0
- ago
#2
Carova,
I'd suggest to think in terms of indicators (i.e. a series of values) whenever possible.

In your request I'd create an indicator "smoothedCloses".
If it comes to check for an exit you work with smoothedCloses values at the relevant points in time:
entryBar - entry time of position
idx - current bar.

here is the complete picture:
CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript4 { public class MyStrategy : UserStrategyBase { public override void Initialize(BarHistory bars) { smoothedCloses = new SMA(bars.Close, 10); // create indicator series } public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { // code your entry here } else { double multiplier = 0.97; // 3% int entryBar = LastPosition.EntryBar; // current bar is idx if(smoothedCloses[idx] < smoothedCloses[entryBar] * multiplier) { // Exit ClosePosition(LastPosition, OrderType.Market); } } }       SMA smoothedCloses; } }


It is not very clear to me what you try to accomplish with "Highest". Could you please explain in plain english?
0
- ago
#3
superticker,

Thanks for the solution!

DrKoch,

The indicator, smoothedPrices, is my approach to deal with impulsive noise in the data stream. It is a robust filter that help prevent me from being shaken out of a trade because a 1-2 bar negative excursion while the trend is still very positive.
0
- ago
#4
QUOTE:
deal with impulsive noise in the data stream


Have a look at the ASEMA indictor (asymetric moving average).
(Part of finnatic.Indicators extension)

You could use it to react faster to downwards movements while still smoothing with a larger period.
Should improve your overall results.
0
- ago
#5
DrKoch,

I do use a highly modified version of your ASEMA concept. You came up with that great idea quite a while ago and I ran with it! I haven't stopped since. 😁
0
- ago
#6
QUOTE:
I do use a highly modified version of your ASEMA concept


Would you mind to elaborate?

I am curious to see how that idea evolved.

If you're reluctant to post publicly you might send a mail to rene dot koch at finantic dot de.
0
- ago
#7
Your original ASEMA is "static", i.e. uses fixed lengths. I have been using adaptive "indicators" for at least 30 years to make them tune themselves to the Market. It is no different in the case of your ASEMA concept. My version, which I call an Adaptive Asymmetric Smoother, adjusts the alphas for both up and down moves based on long and short term momentum and volatility. The concept is straightforward and easy to develop once you get a sense of how the smoother should react in fast, slow, and high and low volatility situations, with varying degrees of correlation. One of the real keys to think about is that not all "noise" in the Market is the same and you need to address each type of "noise" in a very different manner.

EDIT: Just to be clear, your ASEMA is a lot more streamlined (taking only 2 adjustable variable and 30 lines of code) compared to my version. 😉
0
- ago
#8
QUOTE:
adjusts the alphas ... based on long and short term momentum and volatility.

This is the concept of Kalman filters. Did you experiment with those?

(available from finantic.Kalman extension)
0
- ago
#9
Yes. I use a 3-D heavy-tail Kalman variant quite a bit also. As you probably know, you need a lot of different tools in the toolbox! 😁
0
- ago
#10
QUOTE:
... been using adaptive "indicators" ... to make them tune themselves.... It is no different in the case of your ASEMA concept. My version, which I call an Adaptive Asymmetric Smoother, adjusts the alphas for both up and down moves based on long and short term momentum and volatility.

I've been using John Ehlers' DSMA indicator with very good success, which is also adaptive. The idea is to give it a fast response time while still smoothing the input time series. I never thought about making it asymmetric. I suppose I could put two DSMAs together to make a hybrid indicator to do that. I'm just wondering if having a hybrid asymmetric indicator is better than having two independent DSMAs in my strategy? (I'll have to think about that.)

Concerning the Kalman filter, recall it has a front-end covariant matrix to weight its inputs. That works great for "physical systems" like we see in engineering and the real world where the noise is normally distributed, but in non-physical systems (stocks), an outlier can tilt that matrix. One needs to have some kind of nonlinear front-end filter prior to the Kalman to trim the outliers first to protect the integrity of that covariant matrix.
0
- ago
#11
superticker,

I have a modified version of DSMA also. DSMA only adapts to volatility which I think is a significant limitation.

Kalman Filters - you are so right about most of the Kalman variants since they were designed for linear physical systems with the expectation of Gaussian noise. We aren't in Kansas anymore! 😁 (this last statement will probably be lost on the non-US members).

A proper Kalman for financial series must account for the observed realities of a mass-less, non-linear system with fat-tailed noise. It takes some real work to get there.
1
- ago
#12
QUOTE:
I use a 3-D heavy-tail Kalman variant

this sounds very interesting.

Do you have any links/pointers/references/papers or is it a secret invention?
0
- ago
#13
Closer to a "secret invention". 😉 I was tipped off to the underlying concept over two decades ago by a friend who used to work for Renaissance. I had been on his Dissertation Committee before he went there. 😁 I just recently finished converting it to C# since I am now forced to move to WL8 with the sunsetting of WL6 next month. A LOT of my tools need to be moved! 😣
0
Glitch8
 ( 8.89% )
- ago
#14
Don't forget about our Concierge service if you ever feel you need an extra hand!
0
- ago
#15
👍
0
- ago
#16
QUOTE:
... the underlying concept (i.e. a robust nonlinear Kalman filter) over two decades ago by a friend who used to work for Renaissance. I had been on his Dissertation Committee before he went there. 😁 I just recently finished converting it to C# since I am now forced to move to WL8

I might be willing to help you convert your robust Kalman filter if I'm allowed to use the code. I might even be able to improve the code.

Did your friend include an implementation of the "robust" Kalman filter in his dissertation? That would make a great dissertation topic! If so, what's his name and the campus he did his dissertation at? I would like to read about it.

QUOTE:
DSMA only adapts to volatility which I think is a significant limitation.

If there's a paper indicating how you included the adaptation of momentum (i.e. first derivative behavior), I would be interested. And again, I would be willing to help you convert it to C# if I could use the code. And you're welcome to any improvements I would make.
0
- ago
#17
Damn! Suprticker was quicker!

I had a similar idea, but due to weekend and timezone I'm late...
I'd be happy to help you converting your stuff to C#/WL8.
Probably we could even colaborate on creating a super-clever extension (I have some experience with extensions for WL ;) ) and share income.

0
- ago
#18
Guys,

Thanks for your offers! I have a little local help since most of what I am doing is translating directly to C# without any WL interface.

DrKoch,

Question for you... sophisticated tools require a sophisticated user. Proper setup of the Qs and Rs for even a simple Kalman require some understanding. How have you worked through the process for the users of the tools you provide? You can't really hard-code everything or else you diminish the value of the capability. I am interested in your approach.
0
- ago
#19
QUOTE:
... sophisticated tools require a sophisticated user.... You can't really hard-code everything or else you diminish the value of the capability.

I certainly agree with that. It's great to set up a linear relationship and have Math.NET solve for the coefficients (particular solution), but you can only take adaptive solutions so far.
0

Reply

Bookmark

Sort