Trailing Stop Loss - Help needed

beginner_in_need

New member
Hello,


I'm trying to do the following with no luck thus far:

A trailing stop loss that works in the following way -

Once the price is $2 above entry price, activate the trailing stop loss, and from therein it will increment by .01 as price increases accordingly. So if I buy at $10, the trailing SL will activate when price is at $12 and in theory, if the price continues up to $12.50, the trailing SL would be at a high of $10.50 due to the increments.

Any help with this would be greatly appreciated. Will come a long way to backtesting my strategy. Thanks.
 
Solution
What you have described cannot be done using a standard TrailingStopLoss... The only way to come close would be by using a Conditional Order where you have more granular control over the criteria you want to utilize...

That being said, for a simple Strategy you can code this into your AddOrder() parameters but would require multiple lines of code to meet conditions - it's just with real orders that it wouldn't be possible without CO's... There would be little benefit in working up a Strategy if you aren't planning on using Conditional Orders for live trades...
What you have described cannot be done using a standard TrailingStopLoss... The only way to come close would be by using a Conditional Order where you have more granular control over the criteria you want to utilize...

That being said, for a simple Strategy you can code this into your AddOrder() parameters but would require multiple lines of code to meet conditions - it's just with real orders that it wouldn't be possible without CO's... There would be little benefit in working up a Strategy if you aren't planning on using Conditional Orders for live trades...
 
Last edited:
Solution
What you have described cannot be done using a standard StopLoss... The only way to come close would be by using a Conditional Order where you have more granular control over the criteria you want to utilize...

That being said, for a simple Strategy you can code this into your AddOrder() parameters but would require multiple lines of code to meet conditions - it's just with real orders that it wouldn't be possible without CO's... There would be little benefit in working up a Strategy if you aren't planning on using Conditional Orders for live trades...
Hi, thanks for your message. My intention is to do it solely for testing within a strategy for backtesting purposes and not the real orders. In that case, what would you suggest?
 
Hello,


I'm trying to do the following with no luck thus far:

A trailing stop loss that works in the following way -

Once the price is $2 above entry price, activate the trailing stop loss, and from therein it will increment by .01 as price increases accordingly. So if I buy at $10, the trailing SL will activate when price is at $12 and in theory, if the price continues up to $12.50, the trailing SL would be at a high of $10.50 due to the increments.

Any help with this would be greatly appreciated. Will come a long way to backtesting my strategy. Thanks.
just another approach would be to use parabolic SAR, maybe changing parameters from (0.02,0.2) => (0.036,0.2) if you are more aggressive or keep it as default for a more conservative style. what I like with pSAR is that it considers not just price movements but also time (time is $)
 
Hello,


I'm trying to do the following with no luck thus far:

A trailing stop loss that works in the following way -

Once the price is $2 above entry price, activate the trailing stop loss, and from therein it will increment by .01 as price increases accordingly. So if I buy at $10, the trailing SL will activate when price is at $12 and in theory, if the price continues up to $12.50, the trailing SL would be at a high of $10.50 due to the increments.

Any help with this would be greatly appreciated. Will come a long way to backtesting my strategy. Thanks. hal_trail


this is a strategy, not a study. it has addorder() functions. Must be saved in the strategy tab to the right of the study tab.


this creates a trailing stop line

the stop line starts out at $2 below the buy price
input stop_price_offset = -2.00;

as price moves up, the stop line moves up. if price drops, the stop line stays the same.
the stop line will stay within stop_price_offset of price (close).
if close goes below the stop line, then the trade ends.

stop line is cyan
buy price is green
test signals, 2 averages, yellow and gray


Code:
# trail_stop_offset_inc_00c

#https://usethinkscript.com/threads/trailing-stop-loss-help-needed.16492/
#Trailing Stop Loss - Help needed
#Once the price is $2 above entry price, activate the trailing stop loss, and from therein it will increment by .01 as #price increases accordingly.

#------------------------------
# test data
# ma_cross

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

def price = close;

input ma1_len = 21;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input ma2_len = 55;
input ma2_type =  AverageType.EXPONENTIAL;
def ma2 = Movingaverage(ma2_type, price, ma2_len);

input show_test_data_lines = yes;
plot z1 = if show_test_data_lines then ma1 else na;
#z1.setdefaultcolor(color.light_gray);
z1.setdefaultcolor(color.yellow);
#z1.setlineweight(1);
z1.hidebubble();

plot z2 = if show_test_data_lines then ma2 else na;
z2.setdefaultcolor(color.light_gray);
#z2.setlineweight(1);
z2.hidebubble();

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

# test signals
def buy1 = ma1 crosses above ma2;
def sell1 = ma1 crosses below ma2;

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

input stop_price_offset = -2.00;
#input cross_stop_amt = 0.3;

def big = 99999;

# horz buy level
def buy_pr = if bn == 1 or sell1 then big
 else if buy1 then close
 else buy_pr[1];

def y = (buy_pr + stop_price_offset);
def cls_diff1 = close - close[1];
def cls_diff2 = close - buy_pr;

def stop_lvl = if bn == 1 then 0
 else if sell1 then 0
 else if close < stop_lvl[1] then 0
 else if buy1 then y
 else if (stop_lvl[1] > 0 and cls_diff1 > 0) then max(stop_lvl[1], close + stop_price_offset)
 else stop_lvl[1];

# horz buy level
plot z3 = if buy_pr < big then buy_pr else na;
z3.setdefaultcolor(color.green);
z3.setlineweight(1);
#z3.hidebubble();

plot z4 = if stop_lvl > 0 and stop_lvl < big then stop_lvl else na;
z4.setdefaultcolor(color.cyan);
z4.setlineweight(2);
#z4.hidebubble();

def sell2 = if sell1 or (stop_lvl[1] > 0 and stop_lvl == 0) then 1 else 0;
def buy2 = if stop_lvl[1] == 0 then buy1 else 0;

input trade_size = 1;

AddOrder(OrderType.BUY_TO_OPEN, buy2, open[-1], tradeSize = trade_size, tickColor = Color.green, arrowColor = Color.green, name = "buy");
AddOrder(OrderType.SELL_TO_CLOSE, sell2, open[-1] , tradeSize = trade_size, tickcolor = Color.red, arrowcolor = Color.red, name = "sell");

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

input test1_bubble = no;
addchartbubble(test1_bubble, low*0.98,
buy1 + "\n" +
stop_lvl
, color.yellow, no);

#

MRK 15min
NPGbXmI.jpg
 
Last edited by a moderator:

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