Adjust Stop Loss if Target Threshold is Reached

hammerschloggen

New member
I am working on a strategy that is functioning properly up to a certain point, but I want to be able to adjust my Stop Loss if the High ever breaks a Target Threshold.
The primary avenue I have explored is to use the GetValue() and GetMaxValueOffset() to check if the High has ever been greater than the Target since the trade was made. Unfortunately, I have to use a counter to do so and since the count number is dynamic it does not work with the GetMaxValueOffset() function.
Any suggestions would be great. Assume ExitCondition has some pre-defined criteria.

Code:
def Target = EntryPrice() + EntryPrice() * 0.05;
def StopLoss = EntryPrice() - EntryPrice() * 0.05;

def Count = if !isNaN(EntryPrice()) then Count[1] + 1 else 0;
def OverTarget = GetValue(high, GetMaxValueOffset(high, Count), Count) >= Target;

def ExitPrice = if OverTarget then EntryPrice() else StopLoss;
AddOrder(OrderType.SELL_TO_CLOSE, ExitCondition, ExitPrice, tickcolor = Color.RED, arrowcolor = Color.RED, name = "Exit" + BarNumber());
 
I am working on a strategy that is functioning properly up to a certain point, but I want to be able to adjust my Stop Loss if the High ever breaks a Target Threshold.
The primary avenue I have explored is to use the GetValue() and GetMaxValueOffset() to check if the High has ever been greater than the Target since the trade was made. Unfortunately, I have to use a counter to do so and since the count number is dynamic it does not work with the GetMaxValueOffset() function.
Any suggestions would be great. Assume ExitCondition has some pre-defined criteria.

Code:
def Target = EntryPrice() + EntryPrice() * 0.05;
def StopLoss = EntryPrice() - EntryPrice() * 0.05;

def Count = if !isNaN(EntryPrice()) then Count[1] + 1 else 0;
def OverTarget = GetValue(high, GetMaxValueOffset(high, Count), Count) >= Target;

def ExitPrice = if OverTarget then EntryPrice() else StopLoss;
AddOrder(OrderType.SELL_TO_CLOSE, ExitCondition, ExitPrice, tickcolor = Color.RED, arrowcolor = Color.RED, name = "Exit" + BarNumber());
See if this helps: First image is before Target is met. Second image is moving stop to entry when Target is met.

Capture.jpg
Capture1.jpg


Code:
#Example_AutoAdjust_StopLoss
#Sleepz
def cond        = ExpAverage(close, 3) crosses above ExpAverage(close, 8);
def entryprice  = if cond then close else entryprice[1];
def entrybar    = if cond then BarNumber() else Double.NaN;
def targetprice = entryprice + .05;

plot targetmet   = HighestAll(if BarNumber() > HighestAll(entrybar) and high crosses above targetprice then targetprice else Double.NaN);
targetmet.SetDefaultColor(Color.GREEN);

plot ep = HighestAll(if BarNumber() == HighestAll(entrybar) then entryprice else Double.NaN);
ep.SetDefaultColor(Color.WHITE);

plot stoploss = HighestAll(if BarNumber() < HighestAll(entrybar) then Double.NaN else if !IsNaN(targetmet) then entryprice else entryprice - .04);
stoploss.SetDefaultColor(Color.RED);

input bubblemover = 5;
def b  = bubblemover;
def b1 = b + 1;
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), ep[b1], "Entry", Color.WHITE, yes);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), stoploss[b1], "Stop", Color.RED, no);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), targetprice[b1], "Target", if !IsNaN(targetmet[b1]) then Color.GREEN else Color.GRAY, yes);
 

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