Redundant Signals

stock_wiz

New member
Hello fellow coders. I am working on a script to alert a sell but am trying to get rid of the redundant "sell" signals that follow.

I have my conditions met to give me a "buy" signal, and my conditions met to give a "sell" signal. However, the issue is that when a sell signal is thrown, there are sometimes 2 or 3 more "sell" signals that follow. I am trying to code out the "sell" signals that follow the first. I am trying to figure out a way to only have one entry and one exit point plotted on my chart. I know i need it to be a conditional "If" statement, just unsure of the syntax that is involved. Essentially, when a condition to buy or sell is met, i want the script to look back to ensure that the same signal has not already been thrown as the last signal. For example, if the "sell" condition is met, but the last signal is also a "sell" signal, it would return false to avoid a redundant trip. If the last signal was a "buy" signal, the return would be true and would throw a "sell" signal. Script Below

input Length = 20;
input Price = close;
input displace = 0;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.EXPONENTIAL;

def EMA = MovAvgExponential("length" = 200);
def sDev = StDev(data = Price[-displace], length = Length);
def Buy = Price crosses above EMA within 1 bar;
def BBUpper = BollingerBands("length" = Length)."Upper_Band";
def Sell = close crosses below BBUpper within 1 bar;

plot BUYSIGNAL = Buy;
BUYSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BUYSIGNAL.SetDefaultColor(Color.GREEN);
BUYSIGNAL.SetLineWeight(5);

plot SELLSIGNAL = Sell;
SELLSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SELLSIGNAL.SetDefaultColor(Color.RED);
SELLSIGNAL.SetLineWeight(5);

plot MidLine = MovingAverage(averageType, data = Price[-displace], length = Length);
MidLine.SetDefaultColor(GetColor(1));
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
LowerBand.SetDefaultColor(GetColor(0));
plot UpperBand = MidLine + Num_Dev_up * sDev;
UpperBand.SetDefaultColor(GetColor(5));


I feel like I am overthinking this which is why i am unable to get the code to work.

Thanks for the help.
Corey
 
@stock_wiz change your Buy/Sell definitions and BuySignal/SellSignal plots to this.
The IF/THEN/ELSE statement carries both signals and alternates them, the plots only show when the signals change.
Ruby:
def Buy;
def Sell;

if Price crosses above EMA within 1 bar {
Buy = 1;
Sell = 0;
}else if close crosses below BBUpper within 1 bar {
Buy = 0;
Sell = 1;
}else{
Buy = Buy[1];
Sell = Sell[1];}

plot BUYSIGNAL = If Sell[1] and Buy then Buy else Double.NaN;
BUYSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BUYSIGNAL.SetDefaultColor(Color.GREEN);
BUYSIGNAL.SetLineWeight(5);

plot SELLSIGNAL = If Buy[1] and Sell then Sell else Double.NaN;
SELLSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SELLSIGNAL.SetDefaultColor(Color.RED);
SELLSIGNAL.SetLineWeight(5);
 
@stock_wiz change your Buy/Sell definitions and BuySignal/SellSignal plots to this.
The IF/THEN/ELSE statement carries both signals and alternates them, the plots only show when the signals change.
Ruby:
def Buy;
def Sell;

if Price crosses above EMA within 1 bar {
Buy = 1;
Sell = 0;
}else if close crosses below BBUpper within 1 bar {
Buy = 0;
Sell = 1;
}else{
Buy = Buy[1];
Sell = Sell[1];}

plot BUYSIGNAL = If Sell[1] and Buy then Buy else Double.NaN;
BUYSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BUYSIGNAL.SetDefaultColor(Color.GREEN);
BUYSIGNAL.SetLineWeight(5);

plot SELLSIGNAL = If Buy[1] and Sell then Sell else Double.NaN;
SELLSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SELLSIGNAL.SetDefaultColor(Color.RED);
SELLSIGNAL.SetLineWeight(5);
as I read your answer and notice your experience as I do so that only the last buy and sel are present and the previous bubbles disappear in order to keep my chart clean, thank you very much and forgive my ignorance and sorry my English
 
@ChristianS
In the case of the OP's code, the following alternates the buy/sell signal, records the bar number of the buy/sell signal, references the bar number of the last buy/sell signal at each bar on the chart and only displays the signal at signal change and if it is the last buy/sell signal.
Ruby:
def Buy;
def Sell;

if Price crosses above EMA within 1 bar {
Buy = 1;
Sell = 0;
}else if close crosses below BBUpper within 1 bar {
Buy = 0;
Sell = 1;
}else{
Buy = Buy[1];
Sell = Sell[1];}

def VBar = if !IsNaN(close) and BarNumber()>0 then BarNumber() else VBar[1];
###################################################################
#Do not remove#                                                   #
def VBarT = if IsNaN(close[-1000]) then VBar[-1000] else VBart[1];#
###################################################################

def BuySignalBN = If Sell[1] and Buy then VBar else BuySignalBN[1];
def SellSignalBN = If Buy[1] and Sell then VBar else SellSignalBN[1];

def LastBuySignalBN = fold c = 0 to VBar while !IsNaN(GetValue(close, -c)) do GetValue(BuySignalBN, -c);
def LastSellSignalBN = fold d = 0 to VBar while !IsNaN(GetValue(close, -d)) do GetValue(SellSignalBN, -d);

plot BUYSIGNAL = If Sell[1] and Buy and BuySignalBN == LastBuySignalBN then Buy else Double.NaN;
BUYSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BUYSIGNAL.SetDefaultColor(Color.GREEN);
BUYSIGNAL.SetLineWeight(5);

plot SELLSIGNAL = If Buy[1] and Sell and SellSignalBN == LastSellSignalBN then Sell else Double.NaN;
SELLSIGNAL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SELLSIGNAL.SetDefaultColor(Color.RED);
SELLSIGNAL.SetLineWeight(5);
 
Was having a similar issue tonight; I am looking to only get the first vertical bar of each Buy and Sell signal. How do I limit each color of vertical bars signal to only one bar?

Code:
#Plots Parabolic SARs and Super Trend combo as vertical bar.

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;

def SAR = ParabolicSAR(accelerationFactor=accelerationFactor, accelerationLimit=accelerationLimit);

def BullishPSAR = close > SAR;
def BearishPSAR = close < SAR;

# SuperTrend Yahoo Finance Replica - Modified from Modius SuperTrend
# Modified Modius ver. by RConner7
# Modified by Barbaros to replicate look from TradingView version
# v3.0

input AtrMult = 1.00;
input nATR = 6;
input AvgType = AverageType.HULL;

def ATR = ATR("length" = nATR, "average type" = AvgType);
def UP_Band_Basic = HL2 + (AtrMult * ATR);
def LW_Band_Basic = HL2 + (-AtrMult * ATR);
def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];
def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
else LW_Band;

def Long = if close > ST then ST else Double.NaN;
def Short = if close < ST then ST else Double.NaN;

# End Code SuperTrend Yahoo Finance Replica

AddVerticalLine(BullishPSAR and Long, "BUY", Color.GREEN, Curve.FIRM);
AddVerticalLine(BearishPSAR and Short, "SELL", Color.RED, Curve.FIRM);
 
Was having a similar issue tonight; I am looking to only get the first vertical bar of each Buy and Sell signal. How do I limit each color of vertical bars signal to only one bar?

Code:
#Plots Parabolic SARs and Super Trend combo as vertical bar.

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;

def SAR = ParabolicSAR(accelerationFactor=accelerationFactor, accelerationLimit=accelerationLimit);

def BullishPSAR = close > SAR;
def BearishPSAR = close < SAR;

# SuperTrend Yahoo Finance Replica - Modified from Modius SuperTrend
# Modified Modius ver. by RConner7
# Modified by Barbaros to replicate look from TradingView version
# v3.0

input AtrMult = 1.00;
input nATR = 6;
input AvgType = AverageType.HULL;

def ATR = ATR("length" = nATR, "average type" = AvgType);
def UP_Band_Basic = HL2 + (AtrMult * ATR);
def LW_Band_Basic = HL2 + (-AtrMult * ATR);
def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];
def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
else LW_Band;

def Long = if close > ST then ST else Double.NaN;
def Short = if close < ST then ST else Double.NaN;

# End Code SuperTrend Yahoo Finance Replica

AddVerticalLine(BullishPSAR and Long, "BUY", Color.GREEN, Curve.FIRM);
AddVerticalLine(BearishPSAR and Short, "SELL", Color.RED, Curve.FIRM);

This gets rid of most of the excess line by limiting the PSAR portion to when it first occurs through

Rich (BB code):
AddVerticalLine(BullishPSAR[1]==0 and BullishPSAR and Long, "BUY", Color.GREEN, Curve.FIRM);
AddVerticalLine(BearishPSAR[1]==0 and BearishPSAR and Short, "SELL", Color.RED, Curve.FIRM);

or you could have left the verticalline codes as is and changed these
Rich (BB code):
def BullishPSAR = close crosses above SAR;
def BearishPSAR = close crosses below SAR;

Capture.jpg
Ruby:
#Plots Parabolic SARs and Super Trend combo as vertical bar.

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;

def SAR = ParabolicSAR(accelerationFactor=accelerationFactor, accelerationLimit=accelerationLimit);

def BullishPSAR = close > SAR;
def BearishPSAR = close < SAR;

# SuperTrend Yahoo Finance Replica - Modified from Modius SuperTrend
# Modified Modius ver. by RConner7
# Modified by Barbaros to replicate look from TradingView version
# v3.0

input AtrMult = 1.00;
input nATR = 6;
input AvgType = AverageType.HULL;

def ATR = ATR("length" = nATR, "average type" = AvgType);
def UP_Band_Basic = HL2 + (AtrMult * ATR);
def LW_Band_Basic = HL2 + (-AtrMult * ATR);
def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];
def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
else LW_Band;

def Long = if close > ST then ST else Double.NaN;
def Short = if close < ST then ST else Double.NaN;

# End Code SuperTrend Yahoo Finance Replica

AddVerticalLine(BullishPSAR[1]==0 and BullishPSAR and Long, "BUY", Color.GREEN, Curve.FIRM);
AddVerticalLine(BearishPSAR[1]==0 and BearishPSAR and Short, "SELL", Color.RED, Curve.FIRM);
 
This gets rid of most of the excess line by limiting the PSAR portion to when it first occurs through

Rich (BB code):
AddVerticalLine(BullishPSAR[1]==0 and BullishPSAR and Long, "BUY", Color.GREEN, Curve.FIRM);
AddVerticalLine(BearishPSAR[1]==0 and BearishPSAR and Short, "SELL", Color.RED, Curve.FIRM);

or you could have left the verticalline codes as is and changed these
Rich (BB code):
def BullishPSAR = close crosses above SAR;
def BearishPSAR = close crosses below SAR;
great job @SleepyZ . can i ask you a favor, can you also instead of the vertical line also add a arrow for buy and sell? thank you in advance
 
great job @SleepyZ . can i ask you a favor, can you also instead of the vertical line also add a arrow for buy and sell? thank you in advance

This has options for arrows, bubbles and/or verticallines.

The image is only displaying the arrow option you requested.

Capture.jpg

Ruby:
#Plots Parabolic SARs and Super Trend combo as vertical bar.

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;

def SAR = ParabolicSAR(accelerationFactor=accelerationFactor, accelerationLimit=accelerationLimit);

def BullishPSAR = close crosses above SAR;
def BearishPSAR = close crosses below SAR;

# SuperTrend Yahoo Finance Replica - Modified from Modius SuperTrend
# Modified Modius ver. by RConner7
# Modified by Barbaros to replicate look from TradingView version
# v3.0

input AtrMult = 1.00;
input nATR = 6;
input AvgType = AverageType.HULL;

def ATR = ATR("length" = nATR, "average type" = AvgType);
def UP_Band_Basic = HL2 + (AtrMult * ATR);
def LW_Band_Basic = HL2 + (-AtrMult * ATR);
def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];
def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
else LW_Band;

def Long = if close > ST then ST else Double.NaN;
def Short = if close < ST then ST else Double.NaN;

# End Code SuperTrend Yahoo Finance Replica

#Arrows
input showarrows = yes;

Plot Bull = if showarrows and BullishPSAR[1]==0 and BullishPSAR and Long
            then 1 else 0;
Bull.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
Bull.setlineWeight(3);

Plot Bear = if showarrows and BearishPSAR[1]==0 and BearishPSAR and Short
            then 1 else 0;
Bear.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Bear.setlineWeight(3);

#Bubbles
input showbubbles = no;

addchartBubble(if showbubbles and BullishPSAR[1]==0 and BullishPSAR and Long
               then 1 else 0, low, "B", color.green, no);

addchartBubble(if showbubbles and BearishPSAR[1]==0 and BearishPSAR and Short
               then 1 else 0, high, "S", color.red);

#Verticallines
input showverticals = no;

AddVerticalLine(showverticals and BullishPSAR[1]==0 and BullishPSAR and Long, "BUY", Color.GREEN, Curve.FIRM);
AddVerticalLine(showverticals and BearishPSAR[1]==0 and BearishPSAR and Short, "SELL", Color.RED, Curve.FIRM);
 
@ChristianS - One thought came to mind. If i wanted to restrict the signals from showing up in afterhours or pre-market, is there a way to do that with the coding?

Thank

maybe it was misinterpreted, which asked in this thread if anyone knows the code to just allow the last 2 Buy/Sell bourgeoisie so that the chart is clean, but I don't think anyone saw it
 
if Price crosses above EMA within 1 bar { Buy = 1; Sell = 0; }else if close crosses below BBUpper within 1 bar { Buy = 0; Sell = 1; }else{ Buy = Buy[1]; Sell = Sell[1];} plot BUYSIGNAL = If Sell[1] and Buy then Buy else Double.NaN;
Svanoy.... thanks a million for the code you created to eliminate duplicate orders in the same direction. I have been searching for two weeks for such code. I saw several references to not being able to change a variable "unless you do it in an IF statement". I tried and tried I couldn't change variables even in an IF statement. But your code shows that it works fine as long as you don't assign a value to the variable when you define it. Subtle!!! I modified my stochastic strategy and went live about an hour ago and the first profitable trade is in the bank. Thanks again!
 

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

Thread starter Similar threads Forum Replies Date
R How to remove redundant signals Questions 4
P False Signals Questions 2
V MTF Marubozu signals Questions 0
kevinhng77 Time Duration between Signals Questions 1
rvaidyamath Remove Duplicate Signals Questions 1

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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