NR4 Inside Bar for ThinkorSwim

@om4 Try this

Code:
# Generation time: 2020-09-26T16:01:42.946Z

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot PatternPlot =
    ((Sum(IsUp, 1)[2] >= 0)) and
    ((Sum(IsUp, 1)[1] >= 0)) and
    ((Sum(IsUp, 1)[0] >= 0)) and
    Lowest(low[2], 1) < Lowest(low[1], 1) and
    Highest(high[2], 1) > Highest(high[1], 1) and
    Highest(high[1], 1) > Highest(high[0], 1) and
    Lowest(low[1], 1) < Lowest(low[0], 1);
AssignbackgroundColor(if PatternPlot then color.yellow else color.gray);
 
I found an indicator on Hahn's site but when i find inside days from my scans, it's not showing an entry for all of them like it should. I use wicks, so what im trying to do is:

If it had an inside day yesterday, i would like to draw a SOLID line at the top of the candle and bottom of that candle, long and short entries. I wouls also like to draw a DASHED line for the target, which would be the high/low of the bar prior to the inside day

485545255064_Screen-Shot-2017-01-27-at-11.54.16-AM.png


Code:
input offset = 1;
#def count = 2;
def range = high – low;
#def priorrange = high[1] – low[1];
def priorrange = range[1];
def nibsize = absvalue(((open – close) / (open[1] – close[1])-1));
def sizeConstraint = absvalue((range / priorrange)-1) >= .5 and nibsize >= .5;
def hiLowConstraint = high <=high[1] and low >= low[1];
def inside_bar = sizeConstraint and hiLowConstraint;
rec insideBarRange = CompoundValue(1, if inside_bar then range else insideBarRange[1], Double.NaN);
#plot s = inside_bar && lowest(range,count);
def plotRightExpansionArea = !IsNaN(close[1]) and IsNaN(close[0]);
rec longEntry = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then high[offset] + 0.01 else Double.NaN else longEntry[1], Double.NaN);
rec longExit = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then longEntry + insideBarRange else Double.NaN else longExit[1], Double.NaN);
rec shortEntry = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then low[offset] + 0.01 else Double.NaN else shortEntry[1], Double.NaN);
rec shortExit = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then shortEntry - insideBarRange else Double.NaN else shortExit[1], Double.NaN);
plot longEntryLevel = longEntry;
longEntryLevel.SetDefaultColor(Color.GREEN);
plot longExitLevel = longExit;
longExitLevel.SetDefaultColor(Color.DARK_GREEN);
longExitLevel.SetPaintingStrategy(PaintingStrategy.DASHES);
longExitLevel.SetLineWeight(3);
plot shortEntryLevel = shortEntry;
shortEntryLevel.SetDefaultColor(Color.RED);
plot shortExitLevel = shortExit;
shortExitLevel.SetDefaultColor(Color.RED);
shortExitLevel.SetPaintingStrategy(PaintingStrategy.DASHES);
shortExitLevel.SetLineWeight(3);
AssignPriceColor(if inside_bar then Color.WHITE else Color.CURRENT);
 
I have the below script which looks for an inside bar that is 59% of the previous bar with a current (3rd bar) still within the 2nd bar.

I want to add a script so that the 2nd bar must be touching vwap for it to be true.

Code:
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def Candle0Length = high[0] - low[0];
def Candle1Length = high[1] - low[1];
def Candle2Length = high[2] - low[2];
def avgRange = 0.05 * Average(high - low, 20);

plot PatternPlot =
    ((Sum(IsUp, 1)[2] >= 0)) and
    ((Sum(IsUp, 1)[1] >= 0)) and
    ((Sum(IsUp, 1)[0] >= 0)) and
    Highest(high[1], 1) > Highest(high[0], 1) and
    Lowest(low[2], 1) < Lowest(low[1], 1) and   
    Highest(high[2], 1) > Highest(high[1], 1) and
    Lowest(low[1], 1) < Lowest(low[0], 1) and
    (Candle1Length <= (0.59 * Candle2Length));
  
AssignbackgroundColor(if PatternPlot then color.plum else color.black);
 
thanks, used that code and modified it to paint all inside bars


declare upper;
declare once_per_bar;

def vHigh = high;
def vLow = low;
def nan = Double.NaN;

def range = vHigh - vLow;

AssignPriceColor(if range < range[1] and vHigh < vHigh[1] and vLow > vLow[1] then Color.wHITE else Color.CURRENT);
 
@om4 Try this

Code:
# Generation time: 2020-09-26T16:01:42.946Z

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot PatternPlot =
    ((Sum(IsUp, 1)[2] >= 0)) and
    ((Sum(IsUp, 1)[1] >= 0)) and
    ((Sum(IsUp, 1)[0] >= 0)) and
    Lowest(low[2], 1) < Lowest(low[1], 1) and
    Highest(high[2], 1) > Highest(high[1], 1) and
    Highest(high[1], 1) > Highest(high[0], 1) and
    Lowest(low[1], 1) < Lowest(low[0], 1);
AssignbackgroundColor(if PatternPlot then color.yellow else color.gray);
is there a way to use as a watchlist column ?
 
An inside day occurs when the stock trades within the range of the previous day high and low. Combining with Narrow Range 4 (NR4) will provide a high probability of trading the breakout or breakdown of the inside day bar.

Identify Inside Day:
  • When the bar is within the high and low of its previous day.

Identify NR4:
  • Made up of 4 bars
  • Most recent candlestick will have a range that is smaller than the 3 previous bars.
Below is the indicator that applies the rules on how to identify Inside Day and NR4. This way you can easily figure out when we have an Inside Day with "narrow-range-4-bars".

So instead of just looking for inside day candlestick, we're now looking for an inside day with a range that is smaller than the 3 previous bars.

thinkScript Code

Rich (BB code):
def lowVol = (VolatilityStdDev(6) / VolatilityStdDev(100)) < 0.5;
def insideDay = high < high[1] and low > low[1];
def range = high - low;
def NR4 = range < Lowest(range[1], 3);
plot signal = lowVol and insideDay and NR4;
plot signal2 = lowVol and insideDay and NR4;
     signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
     signal.SetDefaultColor(Color.MAGENTA);
     signal.SetLineWeight(1);
signal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
     signal2.SetDefaultColor(Color.MAGENTA);
     signal2.SetLineWeight(1);

Inside Day + NR4 Scanner

Rich (BB code):
def lowVol = (VolatilityStdDev(6) / VolatilityStdDev(100)) < 0.5;
def insideDay = high < high[1] and low > low[1];
def range = high - low;
def NR4 = range < Lowest(range[1], 3);
def s = lowVol and insideDay and NR4;
plot signal = s within 1 bars;

Now that you have the indicator and scanner on your ThinkorSwim, here is the guide on how to trade this particular inside day candlestick that includes the NR4 rules.

Inside Day and Narrow Range 4 Strategy

After you add the indicator, it will plot arrows pointing at any inside days. Here is an example.

nGeRh1i.png


The above screenshot is $BABA 6m 1D timeframe with 2 inside days.

The strategy for trading this is fairly simple. You plot the high and low of the Inside Day bar. Something like this:

e7SP4HZ.png


You can initiate a long position if the candlestick breaks above the high of the Inside Day bar or short the stock if the candlestick breaks below the low of the Inside Day bar.



That is how I would personally trade Inside Day + NR4.

A few more examples:

q6p46fA.png


V8oSk9k.png


e2nwdi7.png


6SHdhfJ.png


DTcT7aN.png


Good luck with trading :)

Credits:
Hello Ben,

We have Inside, Double, Triple, NR4, out of all which one is best?
 
@ukd1506 It depends on your trading style, load them on your charts and see which ones compliment how you trade.
 
I found an indicator on Hahn's site but when i find inside days from my scans, it's not showing an entry for all of them like it should. I use wicks, so what im trying to do is:

If it had an inside day yesterday, i would like to draw a SOLID line at the top of the candle and bottom of that candle, long and short entries. I wouls also like to draw a DASHED line for the target, which would be the high/low of the bar prior to the inside day

485545255064_Screen-Shot-2017-01-27-at-11.54.16-AM.png


Code:
input offset = 1;
#def count = 2;
def range = high – low;
#def priorrange = high[1] – low[1];
def priorrange = range[1];
def nibsize = absvalue(((open – close) / (open[1] – close[1])-1));
def sizeConstraint = absvalue((range / priorrange)-1) >= .5 and nibsize >= .5;
def hiLowConstraint = high <=high[1] and low >= low[1];
def inside_bar = sizeConstraint and hiLowConstraint;
rec insideBarRange = CompoundValue(1, if inside_bar then range else insideBarRange[1], Double.NaN);
#plot s = inside_bar && lowest(range,count);
def plotRightExpansionArea = !IsNaN(close[1]) and IsNaN(close[0]);
rec longEntry = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then high[offset] + 0.01 else Double.NaN else longEntry[1], Double.NaN);
rec longExit = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then longEntry + insideBarRange else Double.NaN else longExit[1], Double.NaN);
rec shortEntry = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then low[offset] + 0.01 else Double.NaN else shortEntry[1], Double.NaN);
rec shortExit = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then shortEntry - insideBarRange else Double.NaN else shortExit[1], Double.NaN);
plot longEntryLevel = longEntry;
longEntryLevel.SetDefaultColor(Color.GREEN);
plot longExitLevel = longExit;
longExitLevel.SetDefaultColor(Color.DARK_GREEN);
longExitLevel.SetPaintingStrategy(PaintingStrategy.DASHES);
longExitLevel.SetLineWeight(3);
plot shortEntryLevel = shortEntry;
shortEntryLevel.SetDefaultColor(Color.RED);
plot shortExitLevel = shortExit;
shortExitLevel.SetDefaultColor(Color.RED);
shortExitLevel.SetPaintingStrategy(PaintingStrategy.DASHES);
shortExitLevel.SetLineWeight(3);
AssignPriceColor(if inside_bar then Color.WHITE else Color.CURRENT);
Mayhaps you can try this.
note: i tried to keep as much of the original code as possible
note: removed the constraints, so it picks up all inside bars [some were missing with the constraints in there]
the main change: modified longExit and shortExit to refer to the high and low of the bar prior to your inside_bar

Code:
input offset = 1;
input exceed = 0.00;
def inside_bar =   high <=high[1] and low >= low[1];
def plotRightExpansionArea = !IsNaN(close[1]) and IsNaN(close[0]);
rec longEntry = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then high[offset] + exceed else Double.NaN else longEntry[1], Double.NaN);
rec longExit = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then high[offset + 1] else Double.NaN else longExit[1], Double.NaN);
rec shortEntry = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then low[offset] - exceed else Double.NaN else shortEntry[1], Double.NaN);
rec shortExit = CompoundValue(1, if plotRightExpansionArea[-1] then if inside_bar[offset] then low[offset + 1] else Double.NaN else shortExit[1], Double.NaN);
plot longEntryLevel = longEntry;
longEntryLevel.SetDefaultColor(Color.GREEN);
plot longExitLevel = longExit;
longExitLevel.SetDefaultColor(Color.DARK_GREEN);
longExitLevel.SetPaintingStrategy(PaintingStrategy.DASHES);
longExitLevel.SetLineWeight(3);
plot shortEntryLevel = shortEntry;
shortEntryLevel.SetDefaultColor(Color.RED);
plot shortExitLevel = shortExit;
shortExitLevel.SetDefaultColor(Color.RED);
shortExitLevel.SetPaintingStrategy(PaintingStrategy.DASHES);
shortExitLevel.SetLineWeight(3);
AssignPriceColor(if inside_bar then Color.WHITE else Color.CURRENT);

 
An inside day occurs when the stock trades within the range of the previous day high and low. Combining with Narrow Range 4 (NR4) will provide a high probability of trading the breakout or breakdown of the inside day bar.

Identify Inside Day:
  • When the bar is within the high and low of its previous day.

Identify NR4:
  • Made up of 4 bars
  • Most recent candlestick will have a range that is smaller than the 3 previous bars.
Below is the indicator that applies the rules on how to identify Inside Day and NR4. This way you can easily figure out when we have an Inside Day with "narrow-range-4-bars".

So instead of just looking for inside day candlestick, we're now looking for an inside day with a range that is smaller than the 3 previous bars.

thinkScript Code

Rich (BB code):
def lowVol = (VolatilityStdDev(6) / VolatilityStdDev(100)) < 0.5;
def insideDay = high < high[1] and low > low[1];
def range = high - low;
def NR4 = range < Lowest(range[1], 3);
plot signal = lowVol and insideDay and NR4;
plot signal2 = lowVol and insideDay and NR4;
     signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
     signal.SetDefaultColor(Color.MAGENTA);
     signal.SetLineWeight(1);
signal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
     signal2.SetDefaultColor(Color.MAGENTA);
     signal2.SetLineWeight(1);

Inside Day + NR4 Scanner

Rich (BB code):
def lowVol = (VolatilityStdDev(6) / VolatilityStdDev(100)) < 0.5;
def insideDay = high < high[1] and low > low[1];
def range = high - low;
def NR4 = range < Lowest(range[1], 3);
def s = lowVol and insideDay and NR4;
plot signal = s within 1 bars;

Now that you have the indicator and scanner on your ThinkorSwim, here is the guide on how to trade this particular inside day candlestick that includes the NR4 rules.

Inside Day and Narrow Range 4 Strategy

Thanks @BenTen! So if I were to look for stocks whose yesterday's range is well contained with day-before-yesterday(ie 2 bars ago on daily) AND yesterday's range is smaller than 3 previous bars, would I do something like this?

def lowVol = (VolatilityStdDev(6) / VolatilityStdDev(100)) < 0.5;
def insideDay = high[1] < high[2] and low[1] > low[2];
def range = high[1] - low[1];
def NR4 = range < Lowest(range[1], 3);
def s = lowVol and insideDay and NR4;
plot signal = s within 1 bars;

 
Thanks @BenTen! So if I were to look for stocks whose yesterday's range is well contained with day-before-yesterday(ie 2 bars ago on daily) AND yesterday's range is smaller than 3 previous bars, would I do something like this?

def lowVol = (VolatilityStdDev(6) / VolatilityStdDev(100)) < 0.5;
def insideDay = high[1] < high[2] and low[1] > low[2];
def range = high[1] - low[1];
def NR4 = range < Lowest(range[1], 3);
def s = lowVol and insideDay and NR4;
plot signal = s within 1 bars;
I didn't test this, but the logic and syntax is correct.
Recommend you put it on your chart. Nothing beats personal experience ;) The only way you will know what works best for you is to play with the settings and see how the indicator line up w/ your strategy and with your other indicators. To determine if this indicator brings value, analyze it over different timeframes, across history and with multiple instruments.
 

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