Double Trailing ATR indicator

TMac

New member
I would like to create a strategy using two ATR straining stops and a moving average. Essentially, there would be and ATR trailing stop plotted with one period and some multiplier and then a second one that has a different period and multiplier. Finally, a moving average would be added to determine trend.

The idea is to only take trades when all three agree.
Example: Moving average slope is positive = go long
14 period 3.5 ATR trailing stop state == state.long
7 period 1.75 ATR trailing stop state == state.long
exit when price hits fast period ATR trail. etc


Ideally I would like this to be a strategy that closes positions by the end of day and does not allow positions to be opened during the first or last 30 min of trading session.

Any help is greatly appreciated!
 
Solution
I would like to create a strategy using two ATR straining stops and a moving average. Essentially, there would be and ATR trailing stop plotted with one period and some multiplier and then a second one that has a different period and multiplier. Finally, a moving average would be added to determine trend.

The idea is to only take trades when all three agree.
Example: Moving average slope is positive = go long
14 period 3.5 ATR trailing stop state == state.long
7 period 1.75 ATR trailing stop state == state.long
exit when price hits fast period ATR trail. etc

this calculates 2 atr trailing stops and an average, and draws them.
this calls the function. i didn't copy all of the...
I would like to create a strategy using two ATR straining stops and a moving average. Essentially, there would be and ATR trailing stop plotted with one period and some multiplier and then a second one that has a different period and multiplier. Finally, a moving average would be added to determine trend.

The idea is to only take trades when all three agree.
Example: Moving average slope is positive = go long
14 period 3.5 ATR trailing stop state == state.long
7 period 1.75 ATR trailing stop state == state.long
exit when price hits fast period ATR trail. etc

this calculates 2 atr trailing stops and an average, and draws them.
this calls the function. i didn't copy all of the original code. so there is no state variable.
it determines 'long' when close > an atr line.
it draws green arrows when both ATRs are < close and the average is moving up.


Code:
# double_atr_trailstop_02

# https://usethinkscript.com/threads/double-trailing-atr-indicator.12237/
# The idea is to only take trades when all three agree.

# buy
# Example: Moving average slope is positive = go long
# 14 period 3.5 ATR trailing stop state == state.long
#  7 period 1.75 ATR trailing stop state == state.long

# sell
# exit when price hits fast period ATR trail. etc


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

# atr1
# 14 period 3.5 ATR trailing stop state == state.long
input trailType1 = {default modified, unmodified};
input ATRPeriod1 = 14;
input ATRFactor1 = 3.5;
input firstTrade1 = {default long, short};
input averageType1 = AverageType.WILDERS;

def trail1 = ATRTrailingStop(
trail_type =  trailType1,
atr_period = ATRPeriod1,
atr_factor = ATRFactor1,
first_trade  = firstTrade1,
average_type = averageType1
);

def istrail1_long = (trail1 < close);

plot z1 = trail1;
z1.setdefaultcolor(color.yellow);

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

# atr2
#  7 period 1.75 ATR trailing stop state == state.long
input trailType2 = {default modified, unmodified};
input ATRPeriod2 = 7;
input ATRFactor2 = 1.75;
input firstTrade2 = {default long, short};
input averageType2 = AverageType.WILDERS;

def trail2 = ATRTrailingStop(
trail_type =  trailType2,
atr_period = ATRPeriod2,
atr_factor = ATRFactor2,
first_trade  = firstTrade2,
average_type = averageType2
);

def istrail2_long = (trail2 < close);

plot z2 = trail2;
z2.setdefaultcolor(color.cyan);

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

# avg
input MA1_len = 9;
input MA1_type =  AverageType.EXPONENTIAL;
def price = close;

def ma1 = MovingAverage(ma1_type, price, ma1_len);

def ismaup = ( ma1 > ma1[1] );

plot zma = ma1;
z2.setdefaultcolor(color.magenta);
z2.SetStyle(Curve.MEDIUM_DASH);

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

def xlong = istrail1_long and istrail2_long and ismaup;
def xshort = close crosses trail2;

plot buy1 = xlong;
buy1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy1.SetDefaultColor(Color.green);
buy1.setlineweight(3);
buy1.hidebubble();

plot sell1 = xshort;
sell1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell1.SetDefaultColor(Color.red);
sell1.setlineweight(3);
sell1.hidebubble();
#
#------------------------------
addchartbubble(0, low*0.99,
 istrail1_long + "\n" +
 istrail2_long + "\n" +
 ismaup
, color.yellow, no);
#
#

H4TAbbA.jpg
 
Solution

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

this calculates 2 atr trailing stops and an average, and draws them.
this calls the function. i didn't copy all of the original code. so there is no state variable.
it determines 'long' when close > an atr line.
it draws green arrows when both ATRs are < close and the average is moving up.


Code:
# double_atr_trailstop_02

# https://usethinkscript.com/threads/double-trailing-atr-indicator.12237/
# The idea is to only take trades when all three agree.

# buy
# Example: Moving average slope is positive = go long
# 14 period 3.5 ATR trailing stop state == state.long
#  7 period 1.75 ATR trailing stop state == state.long

# sell
# exit when price hits fast period ATR trail. etc


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

# atr1
# 14 period 3.5 ATR trailing stop state == state.long
input trailType1 = {default modified, unmodified};
input ATRPeriod1 = 14;
input ATRFactor1 = 3.5;
input firstTrade1 = {default long, short};
input averageType1 = AverageType.WILDERS;

def trail1 = ATRTrailingStop(
trail_type =  trailType1,
atr_period = ATRPeriod1,
atr_factor = ATRFactor1,
first_trade  = firstTrade1,
average_type = averageType1
);

def istrail1_long = (trail1 < close);

plot z1 = trail1;
z1.setdefaultcolor(color.yellow);

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

# atr2
#  7 period 1.75 ATR trailing stop state == state.long
input trailType2 = {default modified, unmodified};
input ATRPeriod2 = 7;
input ATRFactor2 = 1.75;
input firstTrade2 = {default long, short};
input averageType2 = AverageType.WILDERS;

def trail2 = ATRTrailingStop(
trail_type =  trailType2,
atr_period = ATRPeriod2,
atr_factor = ATRFactor2,
first_trade  = firstTrade2,
average_type = averageType2
);

def istrail2_long = (trail2 < close);

plot z2 = trail2;
z2.setdefaultcolor(color.cyan);

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

# avg
input MA1_len = 9;
input MA1_type =  AverageType.EXPONENTIAL;
def price = close;

def ma1 = MovingAverage(ma1_type, price, ma1_len);

def ismaup = ( ma1 > ma1[1] );

plot zma = ma1;
z2.setdefaultcolor(color.magenta);
z2.SetStyle(Curve.MEDIUM_DASH);

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

def xlong = istrail1_long and istrail2_long and ismaup;
def xshort = close crosses trail2;

plot buy1 = xlong;
buy1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy1.SetDefaultColor(Color.green);
buy1.setlineweight(3);
buy1.hidebubble();

plot sell1 = xshort;
sell1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell1.SetDefaultColor(Color.red);
sell1.setlineweight(3);
sell1.hidebubble();
#
#------------------------------
addchartbubble(0, low*0.99,
 istrail1_long + "\n" +
 istrail2_long + "\n" +
 ismaup
, color.yellow, no);
#
#

H4TAbbA.jpg
This is awesome! Thank you.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
426 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