Moving target exit with ATR
Author: 07nioe
Creation Date: 9/1/2014 2:41 AM
profile picture

07nioe

#1
Hi all,

I use a static ATR Profit Taker to exit my positions. My idea is to adapt it to the market development, depending on where the trade moves after the position is filled.

Here are the rules:

If the position moves in the predicted direction leave the static ATR wher it is
If the position moves against me (1ATR), reduce the profit target by 1 ATR as well
Exit after 5 days by a time-based exit

Below you'll find the exit rules of my trading System with static ATR. I don't know how to implement the adapting ATR part of the exit rules.

It would be very kind, if someone could help me.

Best wishes
07nioe

CODE:
Please log in to see this code.





profile picture

Eugene

#2
Hi Niels,

I remember coding a somewhat similar logic called "Top Stop" (created by Adam White) when writing Active Trader Magazine articles:

QUOTE:
If the closing price is the highest of the past 10 days, raise the limit order by the absolute value of today's closing price minus yesterday's closing price. If it fails to make a 10-day high close, lower it by the same amount.


I hope you'll find them useful in coding your exit strategy:

ActiveTrader 2008-09 | Top Stop exit (Trend-following)
ActiveTrader 2008-09 | Top Stop exit (Counter-trend)

Let the forum know if you still need assistance with that.
profile picture

07nioe

#3
Hi Eugene,

Thank you for the links. Interesting. But I am not sure if the Top Stop strategy gives the right adapting ideas for my problem. In my opinion Top Stop sets two different Profit taker at the beginning - depending on the movement before the Position is filled. However, I need a code which corrects after the position is filled depending on its movement. My coding knowledge is not sufficient to develop this exit rule from scratch. Could you please help me?

Thank you very much.

Best wishes
Niels



.
profile picture

Eugene

#4
Niels,

Your impression is incorrect and you don't have to "develop from scratch". Likewise, the whole purpose of Top Stop's adjustment is to raise or lower after the Position is created depending on its movement i.e.

QUOTE:
If the closing price is the highest of the past 10 days, raise the limit order by the absolute value of today's closing price minus yesterday's closing price. If it fails to make a 10-day high close, lower it by the same amount.


It's the closest example I could find. As can be seen, substituting the highest high logic with your condition will get you there:

QUOTE:
If the position moves in the predicted direction leave the static ATR wher it is
If the position moves against me (1ATR), reduce the profit target by 1 ATR as well


To determine whether the position moves against you or in the predicted direction, you could take the Position.NetProfitAsOfBarPercent property for example. Please review the code examples I provided and let me know what's not clear.
profile picture

Eugene

#5
Here's my stab at coding your rules:

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

07nioe

#6
Hi Eugene,

thank you very much for your brilliant support. I am sure your are right. TopStop works well for my needs bu I am not able to completely understand the code. So I have some further questions regarding your code proposal. Your potential answers will help me to improve my knowledge:

What does AutoProfitLevel exactly do? I have only an idea what the following paragraph means:

CODE:
Please log in to see this code.


But it is just an idea. :-)

The next paragraphs remain a mystery. What does the code do?

CODE:
Please log in to see this code.


If you have time, mybe it would make sense to explain the lines step by step.

The help guide gives a first hint. But I have not fully understood the commands yet. The explanations there are not suffiecient for me.

Thank you very much again
Niels
profile picture

Eugene

#7
QUOTE:
What does AutoProfitLevel exactly do?

Please look it up in the QuickRef (F11 key, or highlight AutoProfitLevel and hit F1). The same for RiskStopLevel. These are properties of the Position object that I've chosen to use here for convenience. Undoubtedly there are other ways to code the same thing.

QUOTE:
I have only an idea what the following paragraph means:

I recommend taking a C# 101 book and reviewing the chapter on ternary operators. This paragraph is core to implementation of your idea:

CODE:
Please log in to see this code.


The key here is that I actually used these properties to exit from trades with...
CODE:
Please log in to see this code.



QUOTE:
The next paragraphs remain a mystery. What does the code do?

In particular, it assigns the following properties to a Position after its creation:

1) Priority (more details in the User Guide and WealthScript Programming Guide)
2) initial stop (entry price +/- 2 ATRs) via RiskStopLevel
3) profit target (entry price +/- 5 ATRs) using AutoProfitLevel

Hope that helps.
profile picture

07nioe

#8
Hi Eugene,

Thank you. I'm getting closer to what the code says or does. :-)

I have a further question regarding the plotting.

With the following Dataseries I'll calculate the ATR:

CODE:
Please log in to see this code.


I would like to plot it in the Chart window, but it does't work.

CODE:
Please log in to see this code.


you can see only the ds_TgtAtr_Lg and not Close + ds_TgtAtr_Lg.

Any idea?

Furthermore, this part io the TopStop Strategy is still a riddle:

CODE:
Please log in to see this code.

bar, High[bar], bar+1 and High[bar]+0.01; I don't understand These Parameters in this context.

Thank you very much.
Niels

profile picture

Eugene

#9
Niels,

QUOTE:
I would like to plot it in the Chart window, but it does't work.

"It doesn't work" is not a sufficient problem description, especially given that we never saw your complete code. These declarations should work.

QUOTE:
bar, High[bar], bar+1 and High[bar]+0.01; I don't understand These Parameters in this context.

It is documented in the QuickRef, so please highlight DrawPolygon and strike F1 key for explanations.
profile picture

07nioe

#10
Hi Eugene,

now it works. Problem resolved.

Is it possible to calculate a SMA of a stochastic indicator? Do you have an example?

Thank you
Niels
profile picture

Eugene

#11
Let's not put totally unrelated questions in one thread. Please start a new topic under the WealthScript forum.
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).