Else if conditions question

Hello folks,

Just a quick question, as per below scripts:

As below "Test" scripts, my intention is when condition one (1) both BB_signal and RSI_signal is toggle "Yes",
condition two (2) is BB_signal toggle "Yes" and RSI_signal toggle "NO"
and condition three (3) is BB_signal toggle "NO" and RSI_signal toggle "Yes".
But when I toggle either one of BB_signal or RSI_signal to "NO", the whole signal disappear. Does anyone know what have I done wrongly? Much appreciated your advices.

Hope I have explained clearly. Cheers

### Test

def bear_signal = if (BB_downArrow and RSI_downArrow) then high else if BB_downArrow then high else if RSI_downArrow then high else double.nan;
plot Bearish_signal = bear_signal;
Bearish_signal.SetPaintingStrategy(PaintingStrategy.arrow_down);
Bearish_signal.SetDefaultColor(Color.orange);



Code:
### One (1) BollingerBands
input BB_signal = yes;
def price = close;
input displace = 0;
input BB_length = 20;
input Num_Dev_Num = 2;
def Num_Dev_Dn = -Num_Dev_Num;
def Num_Dev_up = Num_Dev_Num;
def BB_averageType = AverageType.Simple;
def offset = sqrt ((high - low)) * 0.2;

def sDev = stdev(data = price[-displace], length = BB_length);

plot MidLine = MovingAverage(BB_averageType, data = price[-displace], length = BB_length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

def BB_bear_signal = if BB_signal and high > upperBand then 1 else double.nan;
def BB_downArrow = BB_bear_signal;
#BB_downArrow.SetPaintingStrategy(PaintingStrategy.arrow_down);
#BB_downArrow.SetDefaultColor(Color.orange);

def BB_bull_signal = if BB_signal and low < LowerBand then 1 else double.nan;
def BB_upArrow = BB_bull_signal;
#BB_upArrow.SetPaintingStrategy(PaintingStrategy.arrow_up);
#BB_upArrow.SetDefaultColor(Color.light_green);

### Two (2) RSI
input RSI_signal = yes;
input RSI_length = 14;
input over_Bought = 80;
input over_Sold = 20;
#input price = close;
def RSI_averageType = AverageType.WILDERS;
#input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(RSI_averageType, price - price[1], RSI_length);
def TotChgAvg = MovingAverage(RSI_averageType, AbsValue(price - price[1]), RSI_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);
def RSI_bear_signal = if RSI_signal and RSI > Over_Bought then 1  else Double.NaN;
plot RSI_downArrow = RSI_bear_signal;
#RSI_downArrow.SetPaintingStrategy(PaintingStrategy.arrow_down);
#RSI_downArrow.SetDefaultColor(Color.orange);
def RSI_bull_signal = if RSI_signal and RSI < Over_Sold then 1 else Double.NaN;
plot RSI_upArrow = RSI_bull_signal;
#RSI_upArrow.SetPaintingStrategy(PaintingStrategy.arrow_up);
#RSI_upArrow.SetDefaultColor(Color.light_green);

### Test

def bear_signal = if (BB_downArrow and RSI_downArrow) then high else if BB_downArrow then high else if RSI_downArrow then high else double.nan;
plot Bearish_signal = bear_signal;
Bearish_signal.SetPaintingStrategy(PaintingStrategy.arrow_down);
Bearish_signal.SetDefaultColor(Color.orange);
 
Last edited:
Hello folks,

Just a quick question, as per below scripts:

As below "Test" scripts, my intention is when condition one (1) both BB_signal and RSI_signal is toggle "Yes",
condition two (2) is BB_signal toggle "Yes" and RSI_signal toggle "NO"
and condition three (3) is BB_signal toggle "NO" and RSI_signal toggle "Yes".
But when I toggle either one of BB_signal or RSI_signal to "NO", the whole signal disappear. Does anyone know what have I done wrongly? Much appreciated your advices.

Hope I have explained clearly. Cheers

### Test

def bear_signal = if (BB_downArrow and RSI_downArrow) then high else if BB_downArrow then high else if RSI_downArrow then high else double.nan;
plot Bearish_signal = bear_signal;
Bearish_signal.SetPaintingStrategy(PaintingStrategy.arrow_down);
Bearish_signal.SetDefaultColor(Color.orange);



Code:
### One (1) BollingerBands
input BB_signal = yes;
def price = close;
input displace = 0;
input BB_length = 20;
input Num_Dev_Num = 2;
def Num_Dev_Dn = -Num_Dev_Num;
def Num_Dev_up = Num_Dev_Num;
def BB_averageType = AverageType.Simple;
def offset = sqrt ((high - low)) * 0.2;

def sDev = stdev(data = price[-displace], length = BB_length);

plot MidLine = MovingAverage(BB_averageType, data = price[-displace], length = BB_length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

def BB_bear_signal = if BB_signal and high > upperBand then 1 else double.nan;
def BB_downArrow = BB_bear_signal;
#BB_downArrow.SetPaintingStrategy(PaintingStrategy.arrow_down);
#BB_downArrow.SetDefaultColor(Color.orange);

def BB_bull_signal = if BB_signal and low < LowerBand then 1 else double.nan;
def BB_upArrow = BB_bull_signal;
#BB_upArrow.SetPaintingStrategy(PaintingStrategy.arrow_up);
#BB_upArrow.SetDefaultColor(Color.light_green);

### Two (2) RSI
input RSI_signal = yes;
input RSI_length = 14;
input over_Bought = 80;
input over_Sold = 20;
#input price = close;
def RSI_averageType = AverageType.WILDERS;
#input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(RSI_averageType, price - price[1], RSI_length);
def TotChgAvg = MovingAverage(RSI_averageType, AbsValue(price - price[1]), RSI_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);
def RSI_bear_signal = if RSI_signal and RSI > Over_Bought then 1  else Double.NaN;
plot RSI_downArrow = RSI_bear_signal;
#RSI_downArrow.SetPaintingStrategy(PaintingStrategy.arrow_down);
#RSI_downArrow.SetDefaultColor(Color.orange);
def RSI_bull_signal = if RSI_signal and RSI < Over_Sold then 1 else Double.NaN;
plot RSI_upArrow = RSI_bull_signal;
#RSI_upArrow.SetPaintingStrategy(PaintingStrategy.arrow_up);
#RSI_upArrow.SetDefaultColor(Color.light_green);

### Test

def bear_signal = if (BB_downArrow and RSI_downArrow) then high else if BB_downArrow then high else if RSI_downArrow then high else double.nan;
plot Bearish_signal = bear_signal;
Bearish_signal.SetPaintingStrategy(PaintingStrategy.arrow_down);
Bearish_signal.SetDefaultColor(Color.orange);

explained clearly? sorry, no. i am confused by this post.

generally it is helpful to include some descriptions or examples. but in this case it just makes me confused. your words don't match the test code, or the other code, the logic is all different.
so i have no idea which one i am supposed to look at.

i am not sure what this means? your choice of words is very confusing.
...
As below "Test" scripts, my intention is when condition one (1) both BB_signal and RSI_signal is toggle "Yes",

conditions are true or false. (or na).
when describing logic, leave out all the extra words,
intention, is toggle,...

if you use if and true or false, and write out each comparison on a new line, it becomes easier to read,

cond1 is true ,
if BB_signal is true
and RSI_signal is true.

cond2 is true,
if BB_signal is true
and RSI_signal is false

cond3 is true,
if BB_signal toggle is false
and RSI_signal toggle is true.

then you can make minor changes, to make them become formulas.

def cond1 = if (BB_signal and RSI_signal) then 1 else 0;

def cond2 = if (BB_signal toggle and RSI_signal) then 1 else 0;

def cond3 = if ( !BB_signal toggle and RSI_signal toggle) then 1 else 0;


in general, whenever a question includes,
quick , it isn't.
simple, it isn't.
 
explained clearly? sorry, no. i am confused by this post.

generally it is helpful to include some descriptions or examples. but in this case it just makes me confused. your words don't match the test code, or the other code, the logic is all different.
so i have no idea which one i am supposed to look at.

i am not sure what this means? your choice of words is very confusing.
...
As below "Test" scripts, my intention is when condition one (1) both BB_signal and RSI_signal is toggle "Yes",

conditions are true or false. (or na).
when describing logic, leave out all the extra words,
intention, is toggle,...

if you use if and true or false, and write out each comparison on a new line, it becomes easier to read,

cond1 is true ,
if BB_signal is true
and RSI_signal is true.

cond2 is true,
if BB_signal is true
and RSI_signal is false

cond3 is true,
if BB_signal toggle is false
and RSI_signal toggle is true.

then you can make minor changes, to make them become formulas.

def cond1 = if (BB_signal and RSI_signal) then 1 else 0;

def cond2 = if (BB_signal toggle and RSI_signal) then 1 else 0;

def cond3 = if ( !BB_signal toggle and RSI_signal toggle) then 1 else 0;


in general, whenever a question includes,
quick , it isn't.
simple, it isn't.
@halcyonguy Hi bro, thank you for your reply!

First, I apologies for my earlier explanations. So sorry..

I manage to solve the logic as below conditions:

def bear_signal = If (BB_signal and !RSI_signal) then BB_downArrow else if (!BB_signal and RSI_signal) then RSI_downArrow else if (BB_signal and RSI_signal) then (BB_downArrow and RSI_downArrow) else double.nan ;
plot Bearish_signal = bear_signal;
Bearish_signal.SetPaintingStrategy(PaintingStrategy.boolean_WEDGE_DOWN);
Bearish_signal.SetDefaultColor(Color.white);

Actually, I am working on a filter of indicators. For example, as above there are three (3) conditions:

The image shown the second condition.


1) First condition is BB_signal and !RSI_signal then BB_downArrow

2) Second condition is !BB_signal and RSI_signal then RSI_downArrow

3) Third condition is BB_signal and RSI_signal then BB_downArrow and RSI_downArrow

As I am only using the combination of two (2) indicators and therefore, there are three (3) permutations and combination.

If I am using three (3) indicators, the filter combination and permutations will be six (6).

For example, I am adding CCI into the combination and the permutation will be as below:

1) BB_signal + RSI_signal + CCI_signal
2) !BB_signal + RSI_signal + CCI_signal
3) BB_signal + !RSI_signal + CCI_signal
4) BB_signal + RSI_signal + !CCI_signal
5) !BB_signal + RSI_signal + !CCI_signal
6) BB_signal + !RSI_signal + !CCI_signal

Therefore, if I am using total of six (6) indicators the filter combination and permutation will be forty-eight (48).

I am checking if there is any other ways to write the scripts easier?

For example,

Input BB_signal = yes;
Input RSI_signal = yes;
Input CCI_signal = yes;

def signal = BB_signal + RSI_signal + CCI_signal;

If I toggle the BB_signal to "No",

then "def signal script will return only RSI_signal + CCI_signal.

Politely request if you could advise. Much appreciated. Cheers
 
@halcyonguy Hi bro, thank you for your reply!

First, I apologies for my earlier explanations. So sorry..

I manage to solve the logic as below conditions:

def bear_signal = If (BB_signal and !RSI_signal) then BB_downArrow else if (!BB_signal and RSI_signal) then RSI_downArrow else if (BB_signal and RSI_signal) then (BB_downArrow and RSI_downArrow) else double.nan ;
plot Bearish_signal = bear_signal;
Bearish_signal.SetPaintingStrategy(PaintingStrategy.boolean_WEDGE_DOWN);
Bearish_signal.SetDefaultColor(Color.white);

Actually, I am working on a filter of indicators. For example, as above there are three (3) conditions:

The image shown the second condition.


1) First condition is BB_signal and !RSI_signal then BB_downArrow

2) Second condition is !BB_signal and RSI_signal then RSI_downArrow

3) Third condition is BB_signal and RSI_signal then BB_downArrow and RSI_downArrow

As I am only using the combination of two (2) indicators and therefore, there are three (3) permutations and combination.

If I am using three (3) indicators, the filter combination and permutations will be six (6).

For example, I am adding CCI into the combination and the permutation will be as below:

1) BB_signal + RSI_signal + CCI_signal
2) !BB_signal + RSI_signal + CCI_signal
3) BB_signal + !RSI_signal + CCI_signal
4) BB_signal + RSI_signal + !CCI_signal
5) !BB_signal + RSI_signal + !CCI_signal
6) BB_signal + !RSI_signal + !CCI_signal

Therefore, if I am using total of six (6) indicators the filter combination and permutation will be forty-eight (48).

I am checking if there is any other ways to write the scripts easier?

For example,

Input BB_signal = yes;
Input RSI_signal = yes;
Input CCI_signal = yes;

def signal = BB_signal + RSI_signal + CCI_signal;

If I toggle the BB_signal to "No",

then "def signal script will return only RSI_signal + CCI_signal.

Politely request if you could advise. Much appreciated. Cheers
@halcyonguy, Hi bro, maybe you might have missed my question?!

May I check with you if you know of any ways to write the scripts much more straightforward as per my previous post?

Thanks again.
 

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