Fixed (absolute) TakeProfit and StopLoss levels

Hi all,

I'm new to programming so I apologize on forehand if it's a stupid question due to my inexperience...

I've been trying to work out a way to place fixed (absolute) TakeProfit and StopLoss levels into a trading system.

I've tried to use ApplyStop which works well in general but the trades will repeat daily because I cannot use ExRem with ApplyStop because it doesn't generate a Sell or Cover (please correct me if I'm wrong and there is a way to make ApplyStop generate Sell/Cover signals).
So I'm now trying to use the ValueWhen function instead.

Of course I have read the (many) other topics on ValueWhen in this forum, I know the function reference and I've studied many codes that use the ValueWhen function, but I still cannot figure out what goes wrong in my example.

I've made an easy to read example of the problem I have below.

Full code:

_SECTION_BEGIN("Donchian breakout with fixed TakeProfit and StopLoss levels");

		Basic Chart

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

		Donchian Channel

DonchianUpper = HHV(Ref(H+0.02,-1),periods);
DonchianLower = LLV(Ref(L-0.02,-1),periods);

		Trade rules



stopLevelLong = ValueWhen(Buy, DonchianUpper - ATRMultiplier*ATR(ATRPeriods), 1);
stopLevelLongDelta = BuyPrice - stopLevelLong;

TakeProfitFactor = 2.0;

takeProfitLevelLong = ValueWhen(Buy, BuyPrice + TakeProfitFactor*stopLevelLongDelta, 1);

SellPrice = stopLevelLong OR takeProfitLevelLong;

Sell = High >= takeProfitLevelLong OR Low <= stopLevelLong;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

Plot trade levels and entry/exits

Plot (takeProfitLevelLong,"TP",colorGreen,styleDashed);
Plot (stopLevelLong,"SL",colorRed,styleDashed);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-50);
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorWhite, 0,L, Offset=-50);


I think the code is easy to read for everybody to understand what I'm trying to do, but in short the system is the following:
-I buy when the price breaks the upper Donchian Channel
-The StopLoss level is BuyPrice - 2ATR
-The TakeProfit Level is BuyPrice + 4
-Sell when either StopLoss OR TakeProfit Level is hit.

Using ValueWhen for example like this (simplyfied line from the full code above):

stopLevelLong = ValueWhen(Buy, DonchianUpper, 1);

I would expect the StopLevelLong to stay at a fixed absolute level (at the level of DonchianUpper at the time of the most recent Buy) until the next Buy signal (when it changes to the level of DonchianUpper at the time of that Buy).

However, when I plot the trades I can see it works on some of the trades but not on all.

I've found a part of a graph with 3 trade examples of the code above. On two of the trades (I've named them trades 1 and 3) it works like I think it should work. But on one of the trades (trade 2) it doesn't.

I would expect the StopLoss and TakeProfit levels to be straight horizontal lines until the next Buy signal (like in trades 1 and 3). But for some reason on Trade 2 the levels change even though there are no Buy signals.

I hope the screenshots are clear. The Donchian channel boundaries are the blue lines, the StopLoss level is the red dotted line and the TakeProfit level is the green dotted line:

Base picture

Trade 1 and 3

Trade 2

I hope somebody can point me in the right direction or tell me what I'm doing wrong. Maybe I'm not using the ValueWhen function correctly, or maybe there is a way to make ApplyStop generate a Sell or Cover signal so I can use ExRem?

Any advice is highly appreciated. Thanks !

1 Like

You should not re-invent the wheel. Use ApplyStop for your stops. You can use absolute price levels with ApplyStop. It is explained in the Knowledge Base:

Thanks for your quick answer Tomasz !

I have tried to use ApplyStop before I made the post. But with ApplyStop I cannot prevent repeat trades because I couldn't find a way to use ExRem (because ApplyStop is just an instruction for analysis, it returns nothing in the code, so it does not generate Sell/Cover signals).
Do you know a way to fix that?

In addition, I would still like to know why the ValueWhen function does not work in the "trade 2" example, for learning purposes. Is there an explanation for this? I would just like to understand what I'm doing wrong.

Again, I'm new to programming so I apologize on forehand if it's a stupid question due to my inexperience.

Thanks in advance.

You did not read the documentation of Applystop carefully or completely.
You do not need ExRem.

Please read doc page from top to bottom including comments:

So call Buy Sell after Equity call in chart.

Also knowledge base has example for plotting lines after AppyStop.

Besides forum has examples of plotting profit and loss target lines if using ApplyStop already.
Please use forum search feature.


This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.