trailing stop strategy?

Gdragon28

New member
VIP
Hi everybody,
I have a favor to ask and hope someone can help with the coding for my trailing stop strategy.
I am looking for the trailing stop code that can do the following:
1. Assuming 1st long entry is at the market 1st bar close.
2. The long trade will close out with 0.5% trailing stop from the 1st entry and then switch it to a short trade.
3. The short trade will cover with 0.5% trailing stop as well, and then switch it back for a long trade.
Any help will be grateful appreciate!
Thank you in advance!
Bryan
 
Solution
Hi everybody,
I have a favor to ask and hope someone can help with the coding for my trailing stop strategy.
I am looking for the trailing stop code that can do the following:
1. Assuming 1st long entry is at the market 1st bar close.
2. The long trade will close out with 0.5% trailing stop from the 1st entry and then switch it to a short trade.
3. The short trade will cover with 0.5% trailing stop as well, and then switch it back for a long trade.
Any help will be grateful appreciate!
Thank you in advance!
Bryan hal_trail

this checks if bar #1 is up or down, then chooses to go long or short.
the trade variable is set based on a long or short trade.
when a high or low crosses the trail line, it flips to the opposite trade.

i...
Hi everybody,
I have a favor to ask and hope someone can help with the coding for my trailing stop strategy.
I am looking for the trailing stop code that can do the following:
1. Assuming 1st long entry is at the market 1st bar close.
2. The long trade will close out with 0.5% trailing stop from the 1st entry and then switch it to a short trade.
3. The short trade will cover with 0.5% trailing stop as well, and then switch it back for a long trade.
Any help will be grateful appreciate!
Thank you in advance!
Bryan hal_trail

this checks if bar #1 is up or down, then chooses to go long or short.
the trade variable is set based on a long or short trade.
when a high or low crosses the trail line, it flips to the opposite trade.

i changed the % to be 0.15 , not 0.5, so the trail line would be closer to the candles. it is used to find the % number of the close, then added or subtracted to a price. you didn't say a % of what.


Code:
#trailing_stop_strat

#https://usethinkscript.com/threads/trailing-stop-strategy.18440/
#trailing stop strategy?
#1. Assuming 1st long entry is at the market 1st bar close.
#2. The long trade will close out with 0.5% trailing stop from the 1st entry and then switch it to a short trade.
#3. The short trade will cover with 0.5% trailing stop as well, and then switch it back for a long trade.



def na = Double.NaN;
def bn = BarNumber();

def first = if bn == 1 then 1 else 0;
def isup = close > open;
def isdwn = close < open;

input trail_per = 0.15;
def trailamt = close * (trail_per / 100);


def trail;
def trade;
if bn == 1 then {
  trade = if isup then 1 else -1;
  trail = if trade == 1 then (low[0] - trailamt) else (high[0] + trailamt);
} else if low < trail[1] and trade[1] == 1 then {
# chg long to short trade
  trade = -1;
  trail = (high[1] + trailamt);
} else if high > trail[1] and trade[1] == -1 then {
# chg short to long trade
  trade = 1;
  trail = (low[1] - trailamt);
} else {
  trade = trade[1];
  trail = if trade == 1 then max(trail[1], (low[1] - trailamt)) else min(trail[1], (high[1] + trailamt));
}


plot zlong = if trade > 0 then trail else if trade[1] > 0 and trade < 0 then trail[1] else na;
plot zshort = if trade < 0 then trail else if trade[1] < 0 and trade > 0 then trail[1] else na;
zlong.setdefaultcolor(color.cyan);
zshort.setdefaultcolor(color.cyan);


plot zdwn = if trade[1] == 1 and trade[0] == -1 then high else na;
zdwn.SetPaintingStrategy(PaintingStrategy.ARROW_down);
zdwn.SetDefaultColor(Color.red);
zdwn.setlineweight(4);
zdwn.hidebubble();


plot zup = if trade[1] == -1 and trade[0] == 1 then low else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_up);
zup.SetDefaultColor(Color.green);
zup.setlineweight(4);
zup.hidebubble();


#-----------------------------
addchartbubble(0, low*0.996,
trailamt + "\n" +
trade + "\n" +
trail
, color.yellow, no);
#
 

Attachments

  • 00b-pic.JPG
    00b-pic.JPG
    102.9 KB · Views: 55
Last edited:
Solution
Hi Halcyponguy,
Thank you very much for the code with such a fast response! This looks very promising and I will give it a try and provide you an update afterwards.
Greatly appreciate your help on this!

 

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