# Volatility Stop Strategy with non repainting entries

#### Bobbydigital83

##### Member
I enjoy this community. Have you ever been on the futures IO and/or TOS chat room? You cannot ask for anything without getting ripped to pieces or mocked because you forgot to cross your "t's." So thank you all for be a whole lot kinder than other forums.

First let me begin by saying, as a suggestion that if someone posts a successful strategy to this community then free lifetime VIP membership should be granted to the person who post/shares it. Include a special icon/badge too and hopefully others would want to contribute as well. Many strategies posted appear successful all over the internet but often end having terrible repaint issues. People posting a successful strategy is very very rare.

So what I have here is simple Volatility Stop Strategy where the entry will not repaint on you. I grabbed the Volatility Stop code from here and played around with it and I am making my contribution. This is for a 100 tick chart on /EMD with the afterhours turned OFF. It can also work on the 1 min but it will whipsaw a little more.

Attached is the view of the chart along with the settings. P&L is set to 1 contract of /EMD.

It has been proving successful during these volatile times so hopefully it plays out nicely for the time being. Thank you!

Code:
#
input mult = 3.0; #Multiple of Average True Range to use
input length = 10; #Average True Range period
#
# Input to select Close or High/Low for switching:
#
input Style={default Close, HighLow};
def type;
switch(Style){
case Close:
type=1;
case HighLow:
type=0;
}
#
# Set parameters based on switch selection:
#
def uu=if type then close else high;
def ll=if type then close else low;
def na=double.nan;
#
# Calculate average range for volatility:
#
def atr = expaverage(high-low,length);
#
# Calculate initial short and long volatility stop values:
#
def svs =low+ceil(mult*atr/ticksize())*ticksize();
def lvs =high-ceil(mult*atr/ticksize())*ticksize();
#
# Set up the trailing stop logic:
#
rec shortvs=if isnan(shortvs[1]) then svs else if uu>shortvs[1] then svs else min(svs,shortvs[1]);
rec longvs=if isnan(longvs[1]) then lvs else if ll<longvs[1] then lvs else max(lvs,longvs[1]);
#
# Detect if we breached the trailing stop (close or high/low):
#
def longswitch=if uu>=shortvs[1] and uu[1]<shortvs[1] then 1 else 0;
def shortswitch=if ll<=longvs[1] and ll[1]>longvs[1] then 1 else 0;
#
# This logic remembers the direction and changes when needed:
#
rec direction= compoundvalue(1,if isnan(direction[1]) then 0 else
if direction[1]<=0 and longswitch then 1
else if direction[1]>=0 and shortswitch then -1
else direction[1],0);
#
# Set up the two plots. "na" makes the current value disappear
# if we?re trading in the opposite direction:
#
plot short=if direction>0 then na else shortvs;
plot long=if direction<0 then na else longvs;

#addlabel(yes,"Short", if short then color.green else color.red);
#
# Formatting:
#
long.SetDefaultColor(color.magenta);
long.SetStyle(curve.points);
long.SetLineWeight(2);
short.SetDefaultColor(color.red);
short.SetStyle(curve.points);
short.SetLineWeight(2);
#
#
def alerttrigger= if direction[1]<=0 and longswitch then 1
else if direction[1]>=0 and shortswitch then 1 else 0;
# BLOCK CODE BELOW
#input UseAlerts = {false, default true};
#input AlertType = {default "BAR", "ONCE", "TICK"};
#input AlertSound = {"Bell", "Chimes", default "Ding", #"NoSound", "Ring"};

AddOrder(OrderType.BUY_AUTO,longswitch is true, close[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "LONG");

AddOrder(OrderType.SELL_AUTO, shortswitch is true, close [-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "SHORT");

Last edited:

The 1 min chart on /EMD with the afterhours set to OFF. P&L is set to 1 contract of /EMD. It still will NOT repaint on you.

Code:
#
input mult = 3.0; #Multiple of Average True Range to use
input length = 10; #Average True Range period
#
# Input to select Close or High/Low for switching:
#
input Style={default Close, HighLow};
def type;
switch(Style){
case Close:
type=1;
case HighLow:
type=0;
}
#
# Set parameters based on switch selection:
#
def uu=if type then close else high;
def ll=if type then close else low;
def na=double.nan;
#
# Calculate average range for volatility:
#
def atr = expaverage(high-low,length);
#
# Calculate initial short and long volatility stop values:
#
def svs =low+ceil(mult*atr/ticksize())*ticksize();
def lvs =high-ceil(mult*atr/ticksize())*ticksize();
#
# Set up the trailing stop logic:
#
rec shortvs=if isnan(shortvs[1]) then svs else if uu>shortvs[1] then svs else min(svs,shortvs[1]);
rec longvs=if isnan(longvs[1]) then lvs else if ll<longvs[1] then lvs else max(lvs,longvs[1]);
#
# Detect if we breached the trailing stop (close or high/low):
#
def longswitch=if uu>=shortvs[1] and uu[1]<shortvs[1] then 1 else 0;
def shortswitch=if ll<=longvs[1] and ll[1]>longvs[1] then 1 else 0;
#
# This logic remembers the direction and changes when needed:
#
rec direction= compoundvalue(1,if isnan(direction[1]) then 0 else
if direction[1]<=0 and longswitch then 1
else if direction[1]>=0 and shortswitch then -1
else direction[1],0);
#
# Set up the two plots. "na" makes the current value disappear
# if we?re trading in the opposite direction:
#
plot short=if direction>0 then na else shortvs;
plot long=if direction<0 then na else longvs;

#addlabel(yes,"Short", if short then color.green else color.red);
#
# Formatting:
#
long.SetDefaultColor(color.magenta);
long.SetStyle(curve.points);
long.SetLineWeight(2);
short.SetDefaultColor(color.red);
short.SetStyle(curve.points);
short.SetLineWeight(2);
#
#
def alerttrigger= if direction[1]<=0 and longswitch then 1
else if direction[1]>=0 and shortswitch then 1 else 0;
# BLOCK CODE BELOW
#input UseAlerts = {false, default true};
#input AlertType = {default "BAR", "ONCE", "TICK"};
#input AlertSound = {"Bell", "Chimes", default "Ding", #"NoSound", "Ring"};

AddOrder(OrderType.BUY_AUTO,longswitch is true, open[-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "LONG");

AddOrder(OrderType.SELL_AUTO, shortswitch is true, open [-1], tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "SHORT");

Last edited:

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
344 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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.