Looping a boolean value?

Hi! I am looking to loop a boolean value for 5 bars if the original bar is true. Is there a simpler way to repeat this code and have it end if a different condition is true? Sometimes I want the value to continue true for close to 100 candles, and this code is quite inefficient to type out. Here is the code...

##WOODIES EXTREMES
def extreme_High = WoodiesCCI()."CCI" >= 195;
def extreme_Low = WoodiesCCI()."CCI" <= -195;

def eh1 = extreme_High[1] is true;
def eh2 = eh1[1] is true;
def eh3 = eh2[1] is true;
def eh4 = eh3[1] is true;
def eh5 = eh4[1] is true;

def el1 = extreme_Low[1] is true;
def el2 = el1[1] is true;
def el3 = el2[1] is true;
def el4 = el3[1] is true;
def el5 = el4[1] is true;

def extremeHigh = extreme_High is true or eh1[1] is true or eh2[1] is true or eh3[1] is true or eh4[1] is true or eh5[1];
def extremeLow = extreme_Low is true or el1[1] is true or el2[1] is true or el3[1] is true or el4[1] is true or el5[1];
 
Solution
Ruby:
def Direction; def Count;
def Top_Break_Down =
    FW_MOBO_Basic()."BreakDownArrow"
;
def Top_Break_Out =
    FW_MOBO_Basic()."BreakOutArrow"
;
if Top_Break_Out {
    Direction = yes;
    Count =
        if !Direction[1]
        then 1
        else Count[1] + 1;
} else if Top_Break_Down {
    Direction = no;
    Count =
        if Direction[1]
        then 1
        else Count[1] + 1;
} else {
    Direction = Direction[1];
    Count = Count[1] + 1;
}
plot TB_Buy =
    Direction and Count <= 5
;
plot TB_Sell =
    !Direction and Count <= 5
;
TB_Buy.setpaintingStrategy(
    paintingStrategy.BOOLEAN_ARROW_UP
);
TB_Sell.setpaintingStrategy(
    paintingStrategy.BOOLEAN_ARROW_DOWN
);
Try this, and tell me if the line makes a bump up where you want it to.

Ruby:
declare lower;
def extreme_High = WoodiesCCI()."CCI" >= 195;
Plot x = Sum(extreme_High,5) >= Yes;
 
Ruby:
declare Lower;
def Extreme_High = WoodiesCCI()."CCI" >= 195;
def AnotherFunction = something;
Plot x = Sum(Extreme_High,5) >= Yes AND !AnotherFunction;

Which is basically saying AND NOT-AnotherFunction.

Any of these would work also:

AND AnotherFunction == No;
AND AnotherFunction == 0;
AND AnotherFunction != Yes;
 
Ruby:
declare Lower;
def Extreme_High = WoodiesCCI()."CCI" >= 195;
def AnotherFunction = something;
Plot x = Sum(Extreme_High,5) >= Yes AND !AnotherFunction;

Which is basically saying AND NOT-AnotherFunction.

Any of these would work also:

AND AnotherFunction == No;
AND AnotherFunction == 0;
AND AnotherFunction != Yes;
Really appreciate your help - is it able to stop plotting future plots once another function becomes true? That only made it stop plotting for the single bar that the condition is true.

#declare lower;
def extreme_High = WoodiesCCI()."CCI" >= 195;
def extreme_Low = WoodiesCCI()."CCI" <= -195;
Plot x = Sum(extreme_High, 100) >= Yes AND !extreme_Low;
Plot y = Sum(extreme_Low, 100) >= Yes AND !extreme_High;

** I want to plot x until y becomes true, then stop plotting x. Then continue to plot y until x becomes true, and so on...
 
I want to plot x until y becomes true, then stop plotting x. Then continue to plot y until x becomes true, and so on...
Ruby:
declare lower;
def Extreme;
def Woody = WoodiesCCI()."CCI";
if Woody >= 195 {
    Extreme = Yes;
} else if Woody <= -195 {
    Extreme = No;
} else {
    Extreme = Extreme[1];
}
plot Result = Extreme;
 
Thanks for helping on this. Wanted to re-visit with one more question. The code above works great for plotting x until y, and y until x. Is it possible to plot x for 5 bars but have it be cut off if y becomes true during those 5 bars?

For example ... Plot x = Sum(Extreme_High,5) >= Yes AND !AnotherFunction; ... ends up returning 5 consecutive plots if AnotherFunction is false on the first bar, but does not stop of Another Function is true on the 2nd bar/so on. I would like it to stop plotting if AnotherFunction becomes true in any of the consecutive bars. Is this possible?

Thanks for your help!
 
####### TOP BAND ###########
#GREEN
def TopBandUp;
def Top_Break_Down = FW_MOBO_Basic()."BreakDownArrow";
def Top_Break_Out = FW_MOBO_Basic()."BreakOutArrow";
if Top_Break_Out is true {
TopBandUp = yes;
} else if Top_Break_Down is true {
TopBandUp = no;
} else {
TopBandUp = TopBandUp[1];
}
plot TB_Buy = TopBandUp;

#RED
def TopBandDown;
def TopBreakDown = FW_MOBO_Basic()."BreakDownArrow";
def TopBreakOut = FW_MOBO_Basic()."BreakOutArrow";
if TopBreakDown is true {
TopBandDown = yes;
} else if TopBreakOut is true {
TopBandDown = no;
} else {
TopBandDown = TopBandDown[1];
}
plot TB_Sell = TopBandDown;

TB_Buy.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
TB_Sell.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);

I want TB_Buy to be true only for 5 bars IF TB_Sell continues to remain false...
 
Is your goal to just stop the similar arrows from repeating?

Like instead of; Up, Up, Up, Down...

It would be Up, Nothing, Nothing, Down...

?
 
Yes my goal is to stop similar arrows from repeating IF the opposite becomes true

(Up, Up, Up, Up, Up, nothing, nothing, Down, Down, Down, Down, Down)

If Down becomes true during the 5 ups being plotted, I want to stop Up from plotting and then plot Down for 5 bars or until Up becomes true

If you pull up the FW_MOBO_BASIC study, you will see that it has a green cloud and a red cloud. My goal is to simply capture the first 5 bars of a green move, and the first 5 moves of a red move.

FW_MOBO_BASIC has an up arrow for a breakout and a down arrow for breakdown. My goal is to:
1. Plot an up arrow for 5 bars if breakout is true AND breakdown remains false for 5 bars
2. Plot a down arrow for 5 bars if breakdown is true AND breakout remains false for 5 bars.

If the opposite arrow occurs within 5 bars being plotted, I want to discontinue the remaining of the 5 bars from being plotting.
 
Ruby:
def Direction; def Count;
def Top_Break_Down =
    FW_MOBO_Basic()."BreakDownArrow"
;
def Top_Break_Out =
    FW_MOBO_Basic()."BreakOutArrow"
;
if Top_Break_Out {
    Direction = yes;
    Count =
        if !Direction[1]
        then 1
        else Count[1] + 1;
} else if Top_Break_Down {
    Direction = no;
    Count =
        if Direction[1]
        then 1
        else Count[1] + 1;
} else {
    Direction = Direction[1];
    Count = Count[1] + 1;
}
plot TB_Buy =
    Direction and Count <= 5
;
plot TB_Sell =
    !Direction and Count <= 5
;
TB_Buy.setpaintingStrategy(
    paintingStrategy.BOOLEAN_ARROW_UP
);
TB_Sell.setpaintingStrategy(
    paintingStrategy.BOOLEAN_ARROW_DOWN
);
 
Solution
HI @Joshua! Thank you so much for your help on the previous posts. I've run into one more issue and had a question.

The code below works great and produces 5 consecutive outputs for TB_Buy_Entry and BB_Buy Entry. Is there a way to only produce a TB_Buy_Entry if BB_Buy is true prior to this?

My goal is to produce 5 consecutive arrows if BB_Buy is true prior to, or at the same time, as TB_Buy_Entry. If BB_Buy occurs after TB_Buy_Entry, I do not want TB_Buy_Entry to return a value.

######### BOTTOM BAND ##########
#GREEN
def BottomBandUp;
def Break_Down = FW_DPO_MOBO().BreakDownArrow;
def Break_Out = FW_DPO_MOBO().BreakOutArrow;
if Break_Out is true {
BottomBandUp = yes;
} else if Break_Down is true {
BottomBandUp = no;
} else {
BottomBandUp = BottomBandUp[1];
}
plot BB_Buy = BottomBandUp;

#RED
def BottomBandDown;
def Breakdown = FW_DPO_MOBO()."breakdownArrow";
def Breakout = FW_DPO_MOBO().BreakOutArrow;
if Breakdown is true {
BottomBandDown = yes;
} else if Breakout is true {
BottomBandDown = no;
} else {
BottomBandDown = BottomBandDown[1];
}
plot BB_Sell = BottomBandDown;


def BB_Direction;
def BB_Count;
def Bottom_Break_Down_Entry =
FW_DPO_MOBO()."BreakDownArrow"
;
def Bottom_Break_Out_Entry =
FW_DPO_MOBO()."BreakOutArrow"
;
if Bottom_Break_Out_Entry {
BB_Direction = yes;
BB_Count =
if !BB_Direction[1]
then 1
else BB_Count[1] + 1;
} else if Bottom_Break_Down_Entry {
BB_Direction = no;
BB_Count =
if BB_Direction[1]
then 1
else BB_Count[1] + 1;
} else {
BB_Direction = BB_Direction[1];
BB_Count = BB_Count[1] + 1;
}
plot BB_Buy_Entry =
BB_Direction and BB_Count <= 5
;
plot BB_Sell_Entry =
!BB_Direction and BB_Count <= 5
;

BB_Buy_Entry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BB_Sell_Entry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);



######### TOP BAND ###########
#GREEN
def TopBandUp;
def Top_Break_Down = FW_MOBO_Basic()."BreakDownArrow";
def Top_Break_Out = FW_MOBO_Basic()."BreakOutArrow";
if Top_Break_Out is true {
TopBandUp = yes;
} else if Top_Break_Down is true {
TopBandUp = no;
} else {
TopBandUp = TopBandUp[1];
}
plot TB_Buy = TopBandUp;

#RED
def TopBandDown;
def TopBreakDown = FW_MOBO_Basic()."BreakDownArrow";
def TopBreakOut = FW_MOBO_Basic()."BreakOutArrow";
if TopBreakDown is true {
TopBandDown = yes;
} else if TopBreakOut is true {
TopBandDown = no;
} else {
TopBandDown = TopBandDown[1];
}
plot TB_Sell = TopBandDown;


def TB_Direction;
def TB_Count;
def Top_Break_Down_Entry =
FW_MOBO_Basic()."BreakDownArrow"
;
def Top_Break_Out_Entry =
FW_MOBO_Basic()."BreakOutArrow"
;
if Top_Break_Out_Entry {
TB_Direction = yes;
TB_Count =
if !TB_Direction[1]
then 1
else TB_Count[1] + 1;
} else if Top_Break_Down_Entry {
TB_Direction = no;
TB_Count =
if TB_Direction[1]
then 1
else TB_Count[1] + 1;
} else {
TB_Direction = TB_Direction[1];
TB_Count = TB_Count[1] + 1;
}
plot TB_Buy_Entry =
TB_Direction and TB_Count <= 5
;
plot TB_Sell_Entry =
!TB_Direction and TB_Count <= 5
;
 
Last edited by a moderator:
Hello! Quick question...The current code for TB_BUY_ENTRY plots an up arrow for the first 5 candlesticks that there is a breakout. I am trying to only plot TB_Buy_Entry IF BB_BUY is also true prior to this. If BB_Sell is true (or BB_Buy is false), I want TB_Buy_Entry to return void. Is anyone able to program this?

## BANDZZZ ##
#BB BUY
def BottomBandUp;
def Break_Down = FW_DPO_MOBO().BreakDownArrow;
def Break_Out = FW_DPO_MOBO().BreakOutArrow;
if Break_Out is true {
BottomBandUp = yes;
} else if Break_Down is true {
BottomBandUp = no;
} else {
BottomBandUp = BottomBandUp[1];
}
plot BB_Buy = BottomBandUp;

#BB SELL
def BottomBandDown;
def Breakdown = FW_DPO_MOBO()."breakdownArrow";
def Breakout = FW_DPO_MOBO().BreakOutArrow;
if Breakdown is true {
BottomBandDown = yes;
} else if Breakout is true {
BottomBandDown = no;
} else {
BottomBandDown = BottomBandDown[1];
}
plot BB_Sell = BottomBandDown;

def TB_Direction;
def TB_Count;
def Top_Break_Down_Entry =
FW_MOBO_Basic()."BreakDownArrow"
;
def Top_Break_Out_Entry =
FW_MOBO_Basic()."BreakOutArrow"
;
if Top_Break_Out_Entry {
TB_Direction = yes;
TB_Count =
if !TB_Direction[1]
then 1
else TB_Count[1] + 1;
} else if Top_Break_Down_Entry {
TB_Direction = no;
TB_Count =
if TB_Direction[1]
then 1
else TB_Count[1] + 1;
} else {
TB_Direction = TB_Direction[1];
TB_Count = TB_Count[1] + 1;
}
plot TB_Buy_Entry =
TB_Direction and TB_Count <= 5
;

TB_Buy_Entry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BB_BUY.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BB_SELL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
 
Perhaps this?

Code:
plot TB_Buy_Entry = BB_Buy and TB_Direction and TB_Count <= 5;

Although, if I remember correctly, Yes and No are more like surrogates for Up and Down in this case. Such as, if you just used something like BottomBand and !BottomBand, you wouldn't need separate BottomBandUp and ButtomBandDown. It might be best just to explain what you need using plain english, based on the original indicators you're referencing.
 
Last edited:
Hello! Quick question...The current code for TB_BUY_ENTRY plots an up arrow for the first 5 candlesticks that there is a breakout. I am trying to only plot TB_Buy_Entry IF BB_BUY is also true prior to this. If BB_Sell is true (or BB_Buy is false), I want TB_Buy_Entry to return void. Is anyone able to program this?

This has bubbles you can use to test and/or modify the following code. It will otherwise plot a cyan arrow when the count <=5 and tb_direction and the prior bar to the any bars count <=5 is a bb_buy. Otherwise if the prior bar is not a bb_buy then it will ploat a magenta arrow.

Here is a chart with some of the debug bubbles set to yes.
Capture.jpg
Ruby:
## BANDZZZ ##
#BB BUY
def BottomBandUp;
def Break_Down = FW_DPO_MOBO().BreakDownArrow;
def Break_Out = FW_DPO_MOBO().BreakOutArrow;
if Break_Out is true {
BottomBandUp = yes;
} else if Break_Down is true {
BottomBandUp = no;
} else {
BottomBandUp = BottomBandUp[1];
}
plot BB_Buy = BottomBandUp;

#BB SELL
def BottomBandDown;
def Breakdown = FW_DPO_MOBO()."breakdownArrow";
def Breakout = FW_DPO_MOBO().BreakOutArrow;
if Breakdown is true {
BottomBandDown = yes;
} else if Breakout is true {
BottomBandDown = no;
} else {
BottomBandDown = BottomBandDown[1];
}
plot BB_Sell = BottomBandDown;

def TB_Direction;
def TB_Count;
def Top_Break_Down_Entry =
FW_MOBO_Basic()."BreakDownArrow"
;
def Top_Break_Out_Entry =
FW_MOBO_Basic()."BreakOutArrow"
;
if Top_Break_Out_Entry {
TB_Direction = yes;
TB_Count =
if !TB_Direction[1]
then 1
else TB_Count[1] + 1;
} else if Top_Break_Down_Entry {
TB_Direction = no;
TB_Count =
if TB_Direction[1]
then 1
else TB_Count[1] + 1;
} else {
TB_Direction = TB_Direction[1];
TB_Count = TB_Count[1] + 1;
}
plot TB_Buy_Entry =
if TB_Direction and TB_Count <= 5 and bb_Buy[1] then 1 else if  TB_Direction and TB_Count <= 5 and !bb_Buy[1] then -1 else double.nan;
;
;
TB_Buy_Entry.setlineWeight(3);
TB_Buy_Entry.assignvalueColor(if TB_Buy_Entry == 1 then color.cyan else color.magenta);
TB_Buy_Entry.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BB_BUY.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BB_SELL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

input debug_buy   = no;
input debug_entry = no;
input debug_sell  = no;
input debug_count = no;
addchartBubble(debug_buy and bb_buy,high,"BBuy",color.green,yes);
addchartBubble(debug_sell and bb_sell,high,"BSell", color.red);
addchartBubble(debug_count and between(tb_count,1,5),low,tb_count,color.white,no);
addchartBubble(debug_entry and tb_buy_Entry==1,low,"BENTRY", color.cyan,no);
addchartBubble(debug_entry and tb_buy_entry==-1, low, "BVOID", color.magenta,no);
 
I will try to explain the best I can...I have two studies that I refer to: FW_BASIC_MOBO (I call this the Top Band) and FW_DPO_MOBO (I call this the Bottom Band)

Both of these studies will produce either a green band or a red band. If both bands are green and the Bottom Band was green FIRST, I want to produce 5 consecutive up arrows. If the bottom band becomes true after the top band becomes true, I do not want to return any arrows. Same thing goes with the bands becoming red.

I do not want the 5 consecutive arrows to continue forward if the Top Band switches.
 

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