# Request: A formula that trails a stop/level when a certain price is breached.

#### MrBackwardation

##### New member
Hello, I had a request for a formula that trails a stop based on the cross of a price level. It would also preferably detect whether an entry is long or short, but I have a sloppy workaround if that's not possible (I set a parameter for if entry price is between two levels).
For example, if stock X has levels of A, B, and C, I want to have a stop at A when price is entered at D. If price closes above B, I want to move my stop to E. And if price closes above C, I want to move my stop to F.
I have tried creating an "if A then D else if B then E else if..." chain for my parameters, but it does not plot. I've tried utilizing the "close crosses over A" terminology, but for some reason it does not work. Here is my failed attempt:

plot R4_trail = if close > h4 then stopLevel_h3m_L

else if close crosses above h4m then stopLevel_h4_L

else if close crosses above h5 then stopLevel_h5_L

else if close crosses above (h5 + 5.00) then (stopLevel_h5_L + 5.00)

else if close crosses above (h5 + 10.00) then (stopLevel_h5_L +10.00)

else if close crosses above (h5 + 15.00) then (stopLevel_h5_L +15.00)

else if close crosses above (h5 + 20.00) then (stopLevel_h5_L +20.00)

else if close crosses above (h5 + 25.00) then (stopLevel_h5_L +25.00);
Then I would have created a condition to detect if the trade was long, then simply sell to close when low crossed below the R4_trail.

The notable levels are h4, h4m, h5, then h5+5,10,15...

Hello, I had a request for a formula that trails a stop based on the cross of a price level. It would also preferably detect whether an entry is long or short, but I have a sloppy workaround if that's not possible (I set a parameter for if entry price is between two levels).
For example, if stock X has levels of A, B, and C, I want to have a stop at A when price is entered at D. If price closes above B, I want to move my stop to E. And if price closes above C, I want to move my stop to F.
I have tried creating an "if A then D else if B then E else if..." chain for my parameters, but it does not plot. I've tried utilizing the "close crosses over A" terminology, but for some reason it does not work. Here is my failed attempt:

Then I would have created a condition to detect if the trade was long, then simply sell to close when low crossed below the R4_trail.

The notable levels are h4, h4m, h5, then h5+5,10,15...

maybe this will help ?
this draws horizontal lines for a target and a stop.

when close moves above the target line, both lines move up, by the target step amount.
when close moves below the stop line, both lines move down, by the stop step amount.

the default target step level is \$0.80
the default stop step level is \$0.40

Code:
``````# stop_level_ver2_00a

#Request: A formula that trails a stop/level when a certain price is breached.
#MrBackwardation   #2/26  #1

#--------------------------------

def na = double.nan;
def bn = barnumber();

#input stop_levels_type = { default percent , dollar };
input gain_step = 0.8;
input loss_step = 0.4;

def target;
def stop;
if bn == 1 then {
target = open + gain_step;
stop = open - loss_step;
} else if close > target[1] then {
target = target[1] + gain_step;
stop = stop[1] + gain_step;
} else if close < stop[1] then {
target = target[1] - loss_step;
stop = stop[1] - loss_step;
} else {
target = target[1];
stop = stop[1];
}

plot ztarget = if isnan(close) then na else target;
ztarget.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ztarget.SetDefaultColor(Color.white);
ztarget.setlineweight(1);
ztarget.hidebubble();

plot zstop = if isnan(close) then na else stop;
zstop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zstop.SetStyle(Curve.MEDIUM_DASH);
zstop.SetDefaultColor(Color.white);
zstop.setlineweight(1);
zstop.hidebubble();

#``````

Last edited:
maybe this will help ?
this draws horizontal lines for a target and a stop.

when close moves above the target line, both lines move up, by the target step amount.
when close moves below the stop line, both lines move down, by the stop step amount.

the default target step level is \$0.80
the default stop step level is \$0.40

Code:
``````# stop_level_ver2_00a

#Request: A formula that trails a stop/level when a certain price is breached.
#MrBackwardation   #2/26  #1

#--------------------------------

def na = double.nan;
def bn = barnumber();

#input stop_levels_type = { default percent , dollar };
input gain_step = 0.8;
input loss_step = 0.4;

def target;
def stop;
if bn == 1 then {
target = open + gain_step;
stop = open - loss_step;
} else if close > target[1] then {
target = target[1] + gain_step;
stop = stop[1] + gain_step;
} else if close < stop[1] then {
target = target[1] - loss_step;
stop = stop[1] - loss_step;
} else {
target = target[1];
stop = stop[1];
}

plot ztarget = if isnan(close) then na else target;
ztarget.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ztarget.SetDefaultColor(Color.white);
ztarget.setlineweight(1);
ztarget.hidebubble();

plot zstop = if isnan(close) then na else stop;
zstop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zstop.SetStyle(Curve.MEDIUM_DASH);
zstop.SetDefaultColor(Color.white);
zstop.setlineweight(1);
zstop.hidebubble();

#``````

Thanks for digging this up. Is there anyway to add a formula that tells you the risk/reward with the plotted stop and target?

maybe this will help ?
this draws horizontal lines for a target and a stop.

when close moves above the target line, both lines move up, by the target step amount.
when close moves below the stop line, both lines move down, by the stop step amount.

the default target step level is \$0.80
the default stop step level is \$0.40

Code:
``````# stop_level_ver2_00a

#Request: A formula that trails a stop/level when a certain price is breached.
#MrBackwardation   #2/26  #1

#--------------------------------

def na = double.nan;
def bn = barnumber();

#input stop_levels_type = { default percent , dollar };
input gain_step = 0.8;
input loss_step = 0.4;

def target;
def stop;
if bn == 1 then {
target = open + gain_step;
stop = open - loss_step;
} else if close > target[1] then {
target = target[1] + gain_step;
stop = stop[1] + gain_step;
} else if close < stop[1] then {
target = target[1] - loss_step;
stop = stop[1] - loss_step;
} else {
target = target[1];
stop = stop[1];
}

plot ztarget = if isnan(close) then na else target;
ztarget.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ztarget.SetDefaultColor(Color.white);
ztarget.setlineweight(1);
ztarget.hidebubble();

plot zstop = if isnan(close) then na else stop;
zstop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#zstop.SetStyle(Curve.MEDIUM_DASH);
zstop.SetDefaultColor(Color.white);
zstop.setlineweight(1);
zstop.hidebubble();

#``````

Hal, Any way to make this work for both directions? When I test this, It works as planned in an uptrend but, when in a down trend the stops and profits are backwards. Seems like it would be tough to accomplish as a pull back would put stops and profits as the opposite. Am I seeing this correctly?

Hal, Any way to make this work for both directions? When I test this, It works as planned in an uptrend but, when in a down trend the stops and profits are backwards. Seems like it would be tough to accomplish as a pull back would put stops and profits as the opposite. Am I seeing this correctly?
should be able to.
i'm away.. will look at tonight or tomorrow

Last edited:

87k+ Posts
173 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?