Outside Bars and/or Inside Bars (Candle) Combinations For ThinkOrSwim

Thepremier

New member
Can someone help me with a script for an outside day and and an inside day? I would like to create a scan for them.

 
Last edited:

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

Are you sure you're not using it in conjunction with any other code? I can't get it to paint volume in the first place, let alone know how to stop it.
 
Thank you Joshua. It works great on the price bars but it is also painting the volume histogram. Is there a way to block it from painting the volume?

It appears that you have selected in setting's appearance to have volume bars color as symbol ticks. As assignpricecolor colors a bar, it then is picked up by the color settings. To workaround this, you could use the following addchart coloring of those bars.

Code:
def outsideBarID = high > high[1] and low < low[1];
def nan = Double.NaN;
def h = if outsideBarID then high else nan;
def l = if outsideBarID then low else nan;
def o = if outsideBarID then if open > close then open else close else nan;
def c = if outsideBarID then if open > close then close else open else nan;
AddChart(h, l, o, c, ChartType.CANDLE, growColor = Color.WHITE);
 
It appears that you have selected in setting's appearance to have volume bars color as symbol ticks. As assignpricecolor colors a bar, it then is picked up by the color settings. To workaround this, you could use the following addchart coloring of those bars.
Thank you so much - I could not figure it out but this makes sense.
 
The below code adds an arrow on top of the inside candle. I was hoping someone could alter to code to instead just paint the inside bar candle white instead of adding an arrow on top of it.

Existing Code Ben wrote, which adds a arrow on top of the inside bar candle:
Code:
# Inside Bar
# Mobius
# 8.7.2017

def inside = high < high[1] and low > low[1];
plot inside_bar = inside;
inside_bar.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
inside_bar.SetDefaultColor(Color.MAGENTA);
inside_bar.SetLineWeight(1);
Here ya go:
Ruby:
# Inside Bar
# Mobius
# 8.7.2017

def inside = high < high[1] and low > low[1];

AssignPriceColor(
if inside then color.white else color.current);
 
@Thepremier Here you go:

Rich (BB code):
# Inside and Outside Bar
# Mobius
# 8.7.2017

def inside = high < high[1] and low > low[1];
def outside = high > high[1] or low < low[1];
AssignPriceColor(if inside
                then color.cyan
                else if outside
                    then color.yellow
                else color.current);

That indicator will plot outside and inside bars on your chart. Switch over to the Scan tab and use it as a scanner if you like. We also have Double Inside Day and Narrow Range Day as well.
Hi Ben its telling me a plot is expected. ?
 
Hi Ben its telling me a plot is expected. ?
By default, charts expect studies to plot something. Because this indicator only paints candles. It vomits an error.
So even though we don't want to plot anything, there has to be a plot statement. So we are going to make a silly plot and then hide the silly plot.
Here ya go:
Ruby:
# Inside and Outside Bar
# Mobius
# 8.7.2017

def inside = high < high[1] and low > low[1];
def outside = high > high[1] or low < low[1];
AssignPriceColor(if inside
                then color.cyan
                else if outside
                    then color.yellow
                else color.current);
                    
plot data = close;
data.hide();
 
@maddy91 a very simple watchlist column can be -
Code:
plot inside = if high < high[1] and low > low[1] then 1 else 0;
inside.assignvaluecolor(if inside then color.white else color.black);
assignbackgroundcolor(if inside then color.magenta else color.black);
 
add condition to Inside Bar script
Below script looks for Inside bar crossing/touching VWAP

i need a condition added so that the script is only true when that inside bar that it found be 40% of the first bar of the day.

the 40% is the difference between high and low of each bar being compared. if first bar is $1 high .50 low difference is .50...if the inside bar that triggered high is .75 and low .50 difference is .25...difference between inside bar and 1st bar of day = .25 / .50 = .25 *100 = meaning inside bar is 50% of first bar.

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


input timeFrame = {default DAY, WEEK, MONTH};

def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;

def ema=movAvgExponential(close,9,0,no);
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def 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) ;
#patternPlot.setPaintingStrategy(paintingStrategy.ARROW_UP);
def inside=high<high[1] and low >low[1];
def cross=high>price and low<price;
def con=inside [1] and cross [1];#PatternPlot and close crosses ema or patternPlot and close crosses price;
plot CONDITION=if con then low else double.NaN;
condition.setPaintingStrategy(paintingStrategy.POINTS);
conditiON.setLineWeight(3);
alert(con,"ALERT",alert.ONCE,sound.Ding);
AssignbackgroundColor(if condition then color.plum else color.black);
 
Last edited by a moderator:
Below script looks for Inside bar crossing/touching VWAP

i need a condition added so that the script is only true when that inside bar that it found be 40% of the first bar of the day.

the 40% is the difference between high and low of each bar being compared. if first bar is $1 high .50 low difference is .50...if the inside bar that triggered high is .75 and low .50 difference is .25...difference between inside bar and 1st bar of day = .25 / .50 = .25 *100 = meaning inside bar is 50% of first bar.

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


input timeFrame = {default DAY, WEEK, MONTH};

def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;

def ema=movAvgExponential(close,9,0,no);
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def 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) ;
#patternPlot.setPaintingStrategy(paintingStrategy.ARROW_UP);
def inside=high<high[1] and low >low[1];
def cross=high>price and low<price;
def con=inside [1] and cross [1];#PatternPlot and close crosses ema or patternPlot and close crosses price;
plot CONDITION=if con then low else double.NaN;
condition.setPaintingStrategy(paintingStrategy.POINTS);
conditiON.setLineWeight(3);
alert(con,"ALERT",alert.ONCE,sound.Ding);
AssignbackgroundColor(if condition then color.plum else color.black);
@Svanoy could you look at above and possibly help?
 
@om4
I assume you meant 40% or more.
Code:
# Generation time: 2020-09-26T16:01:42.946Z

input timeFrame = {default DAY, WEEK, MONTH};

def cap = GetAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;

def ema = MovAvgExponential(close, 9, 0, no);
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def 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) ;
#patternPlot.setPaintingStrategy(paintingStrategy.ARROW_UP);
def inside = high < high[1] and low > low[1];
def cross = high > price and low < price;
def con = inside [1] and cross [1];#PatternPlot and close crosses ema or patternPlot and close crosses price;
###########################################################################################################################
def Con_High = if con then high else Con_High[1];
def Con_Low = if con then low else Con_Low[1];
def Con_High_Low_Range = AbsValue(Con_High - Con_Low);

input Start_Bar_Time = 0930;
def Start_Bar_High = If secondsFromTime(Start_Bar_Time)==(GetAggregationPeriod()/1000) then high[1] else Start_Bar_High[1];
def Start_Bar_Low = If secondsFromTime(Start_Bar_Time)==(GetAggregationPeriod()/1000) then low[1] else Start_Bar_Low[1];
def Start_Bar_High_Low_Range = AbsValue(Start_Bar_High - Start_Bar_Low);

def Percent_Check = RoundDown((Con_High_Low_Range/Start_Bar_High_Low_Range) * 100,1) >= 40;
AddChartBubble(Con and Percent_Check,high,RoundDown((Con_High_Low_Range/Start_Bar_High_Low_Range) * 100,1),color.white);
###########################################################################################################################
plot CONDITION = if con and Percent_Check then low else Double.NaN;
CONDITION.SetPaintingStrategy(PaintingStrategy.POINTS);
CONDITION.SetLineWeight(3);
Alert(con, "ALERT", Alert.ONCE, Sound.Ding);
AssignBackgroundColor(if CONDITION then Color.PLUM else Color.BLACK);
 
Last edited:
  • Like
Reactions: om4
@om4
I assume you meant 40% or more.
Code:
# Generation time: 2020-09-26T16:01:42.946Z

input timeFrame = {default DAY, WEEK, MONTH};

def cap = GetAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;

def ema = MovAvgExponential(close, 9, 0, no);
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def 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) ;
#patternPlot.setPaintingStrategy(paintingStrategy.ARROW_UP);
def inside = high < high[1] and low > low[1];
def cross = high > price and low < price;
def con = inside [1] and cross [1];#PatternPlot and close crosses ema or patternPlot and close crosses price;
###########################################################################################################################
def Con_High = if con then high else Con_High[1];
def Con_Low = if con then low else Con_Low[1];
def Con_High_Low_Range = AbsValue(Con_High - Con_Low);

input Start_Bar_Time = 0930;
def Start_Bar_High = If secondsFromTime(Start_Bar_Time)==(GetAggregationPeriod()/1000) then high[1] else Start_Bar_High[1];
def Start_Bar_Low = If secondsFromTime(Start_Bar_Time)==(GetAggregationPeriod()/1000) then low[1] else Start_Bar_Low[1];
def Start_Bar_High_Low_Range = AbsValue(Start_Bar_High - Start_Bar_Low);

def Percent_Check = RoundDown((Con_High_Low_Range/Start_Bar_High_Low_Range) * 100,1) >= 40;
AddChartBubble(Con and Percent_Check,high,RoundDown((Con_High_Low_Range/Start_Bar_High_Low_Range) * 100,1),color.white);
###########################################################################################################################
plot CONDITION = if con and Percent_Check then low else Double.NaN;
CONDITION.SetPaintingStrategy(PaintingStrategy.POINTS);
CONDITION.SetLineWeight(3);
Alert(con, "ALERT", Alert.ONCE, Sound.Ding);
AssignBackgroundColor(if CONDITION then Color.PLUM else Color.BLACK);
the script above is not respecting the inside bar requirement of the script. i used it today and every alert it provided was not an inside bar. as an example,

stock LC at 1:45 - 2pm 15min candle got triggered but it wasn't an inside bar.
 
@om4
I made no change to your original code for defining an inside bar.
I only looked at high/low ranges of bars your code triggered on and provided an additional filter for minimum percentage.
I am not seeing a triggered candle at 1:45 on LC with the code in my last post.
QIJrTgg.png
 
If I have an outside bar, how can I know if it breaks the previous day's high or low FIRST? Reversal direction depends upon which comes last WITHIN A SINGLE BAR. How can the script discern which occurs first? Thanks!
 
I'm trying to write a simple indicator based on insidebar criteria, the code is listed below, however, it doesn't work very well. I want it to only print the IBS value when it is an inside bar and the color should be green or red based on the IBS value.
You can see I also tried another way (commented out) but that didn't work either.

Any help is greatly appreciated.
input buyLevel = 0.20;
input sellLevel = 0.70;

def prevHigh = high[2];
def prevLow = low[2];
def prevClose = close [2];
def prevOpen = open [2];

def insideBar = high [1] < high [2] and low [1] > low [2];

def range1 = close [1] - low [1];
def range2 = high [1] - low [1];

def IBS = range1 / range2;

def longSignal = IBS < 0.20;
def shortSignal = IBS > 0.70;

plot signal = insideBar and longSignal;
AddChartBubble(signal, high, IBS, color.green, yes);

plot signal2 = insideBar and shortSignal;
AddChartBubble(signal2, high, IBS, color.red, yes);


#
#if IBS < buyLevel then {
#plot signal = insideBar;
#AddChartBubble(high,high, IBS, color.green,yes);
#} else {
#if IBS > sellLevel then {
#plot signal = insideBar;
#AddChartBubble(high,high, IBS, color.red,yes);
#} else {
#plot signal = insideBar;
#AddChartBubble(high,high, IBS, color.blue,yes);
#}
#}
 
@mosog

You put the conditions inside the addchartbubble statement as the first parameter to determine whether or not the bubble is displayed.

Code:
input buyLevel = 0.20;
input sellLevel = 0.70;

def prevHigh = high[2];
def prevLow = low[2];
def prevClose = close [2];
def prevOpen = open [2];

def insideBar = high [1] < high [2] and low [1] > low [2];

def range1 = close [1] - low [1];
def range2 = high [1] - low [1];

def IBS = range1 / range2;

def longSignal = IBS < 0.20;
def shortSignal = IBS > 0.70;

AddChartBubble(insideBar and longSignal, high, IBS, color.green, yes);
AddChartBubble(insideBar and shortSignal, high, IBS, color.red, yes);
 
@mosog

You put the conditions inside the addchartbubble statement as the first parameter to determine whether or not the bubble is displayed.

Code:
input buyLevel = 0.20;
input sellLevel = 0.70;

def prevHigh = high[2];
def prevLow = low[2];
def prevClose = close [2];
def prevOpen = open [2];

def insideBar = high [1] < high [2] and low [1] > low [2];

def range1 = close [1] - low [1];
def range2 = high [1] - low [1];

def IBS = range1 / range2;

def longSignal = IBS < 0.20;
def shortSignal = IBS > 0.70;

AddChartBubble(insideBar and longSignal, high, IBS, color.green, yes);
AddChartBubble(insideBar and shortSignal, high, IBS, color.red, yes);

Svanoy, thank you for your help, I'm checking it now.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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