Looking for indicator for consecutive bars up or down.

Lone Wolf

New member
Plus
Does anybody know how to code a simple indicator that would count a certain # of consecutive up or down bars, and then point an arrow at that candle or bar?

For example - Let's say I want the indicator to point an arrow at the candle if the market closed negative for 5 consecutive bars. Same for up bars as well.

Is this possible to do?

Thanks!
 
Does anybody know how to code a simple indicator that would count a certain # of consecutive up or down bars, and then point an arrow at that candle or bar?

For example - Let's say I want the indicator to point an arrow at the candle if the market closed negative for 5 consecutive bars. Same for up bars as well.

Is this possible to do?

Thanks!

Code:
# count_bars_updwn

input count_x_bars = 5;
def up = close > close[1];
def dwn = close < close[1];

def upcnt = sum(up, count_x_bars);
def dwncnt = sum(dwn, count_x_bars);

plot zup = if upcnt == count_x_bars then 1 else 0;
plot zdwn = if dwncnt == count_x_bars then 1 else 0;

zup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zup.SetDefaultColor(color.cyan);
zup.SetLineWeight(2);

zdwn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
zdwn.SetDefaultColor(color.cyan);
zdwn.SetLineWeight(2);
#
#
 

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

Code:
# count_bars_updwn

input count_x_bars = 5;
def up = close > close[1];
def dwn = close < close[1];

def upcnt = sum(up, count_x_bars);
def dwncnt = sum(dwn, count_x_bars);

plot zup = if upcnt == count_x_bars then 1 else 0;
plot zdwn = if dwncnt == count_x_bars then 1 else 0;

zup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zup.SetDefaultColor(color.cyan);
zup.SetLineWeight(2);

zdwn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
zdwn.SetDefaultColor(color.cyan);
zdwn.SetLineWeight(2);
#
#
Dude, thank you so much!

Sort of on the same topic, do you know why the count is off on this indicator?

It looks like sometimes it is correct and other times its off by a bar or two. For example, I will count 6 down bars, but the count will show 5 sometimes.

Code:
#
# ConsecutiveBarCount
#
# Study to indicate count consecutive up/down bars.
#
# Author: Kory Gill, @korygill
#
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20190709-1200-KG    - created
# ...
# ...
#
# Comment out unnecessary portions to preserve TOS memory and enhance speed
#

declare lower;
declare once_per_bar;

#
# Common variables. Using variables reduces calls to TOS iData server.
#

# iData Definitions
#def vHigh = high;
#def initHigh =  CompoundValue(1, high, high);  # creates an initialized variable for high
#def vLow = low;
#def initLow = CompoundValue(1, low, low);
#def vOpen = open;
#def initOpen = CompoundValue(1, open, open);
def vClose = close;
#def initClose = CompoundValue(1, close, close);
#def vVolume = volume;
#def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;

# Bar Time & Date
#def bn = BarNumber();
#def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
#def Today = GetDay() ==GetLastDay();
#def time = GetTime();
#def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
#def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX[1];
#def RTS  = RegularTradingStart(GetYYYYMMDD());
#def RTE  = RegularTradingEnd(GetYYYYMMDD());
#def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
#def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH[1];

# Bars that start and end the sessions
#def rthStartBar    = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingStart(GetYYYYMMDD())
#                         then bn
#                         else rthStartBar[1], 0);
#def rthEndBar      = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else rthEndBar[1], 1);
#def globexStartBar = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses below RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else globexStartBar[1], 1);
#def rthSession = if bn crosses above rthStartBar #+ barsExtendedBeyondSession
#                    then 1
#                    else if   bn crosses above rthEndBar #+ barsExtendedBeyondSession
#                         then 0
#                    else rthSession[1];

def zeroLine = 0;
plot pZeroLine = zeroLine;
pZeroLine.SetDefaultColor(Color.WHITE);
def upLine = 5;
plot pUpLine = upLine;
pUpLine.SetDefaultColor(Color.GREEN);
def downLine = -5;
plot pDownLine = downLine;
pDownLine.SetDefaultColor(Color.RED);

def barUp = vClose > vClose[1];
def barDown = vClose < vClose[1];

def barUpCount = CompoundValue(1, if barUp then barUpCount[1] + 1 else 0, 0);
plot pBarUpCount = if barUpCount > 0 then barUpCount else nan;
pBarUpCount.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
pBarUpCount.AssignValueColor(Color.GREEN);

def barDownCount = CompoundValue(1, if barDown then barDownCount[1] - 1 else 0, 0);
plot pBarDownCount = if barDownCount < 0 then barDownCount else nan;
pBarDownCount.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
pBarDownCount.AssignValueColor(Color.RED);

AddLabel(yes, "Consecutive Bar Count", Color.GRAY);
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
476 Online
Create Post

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