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...

Thanks in advance!
 
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...

Thanks in advance! hal_target_step


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

#https://usethinkscript.com/threads/request-a-formula-that-trails-a-stop-level-when-a-certain-price-is-breached.14635/
#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);
#zbuy_level.SetStyle(Curve.MEDIUM_DASH);
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();

#

1MXGnGG.jpg
 
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

#https://usethinkscript.com/threads/request-a-formula-that-trails-a-stop-level-when-a-certain-price-is-breached.14635/
#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);
#zbuy_level.SetStyle(Curve.MEDIUM_DASH);
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();

#

1MXGnGG.jpg
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

#https://usethinkscript.com/threads/request-a-formula-that-trails-a-stop-level-when-a-certain-price-is-breached.14635/
#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);
#zbuy_level.SetStyle(Curve.MEDIUM_DASH);
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();

#

1MXGnGG.jpg
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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
476 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top