Detect if same signal has occurred within X bars ago.

epete

Member
I have a strategy which plots up and down arrows to indicate buy/sell signals (the actual strategy isn't important, any strategy with up/down signals has this situation). What I'd like to do is to determine if I'm in a "choppy" trend where there are lots of up and down arrow signals in a short amount of time. My idea is when there is a up arrow (buy), to look back somehow to determine when the last time a buy signal was issued. If the previous buy signal occurred a short number of X bars ago (say 15 bars - on a 1 minute chart), I consider that to be a choppy market situation and would not likely want to execute the buy. I've tried using the "within" function but don't know if that's the best approach. Could I save the bar# that the up/down arrows previously occurred in (LastUpArrowBar / LastDnArrowBar)? And, then determine the distance between that and the current bar# (BarNumber)? Any thoughts or help is appreciated.
 
Solution
I have a strategy which plots up and down arrows to indicate buy/sell signals (the actual strategy isn't important, any strategy with up/down signals has this situation). What I'd like to do is to determine if I'm in a "choppy" trend where there are lots of up and down arrow signals in a short amount of time. My idea is when there is a up arrow (buy), to look back somehow to determine when the last time a buy signal was issued. If the previous buy signal occurred a short number of X bars ago (say 15 bars - on a 1 minute chart), I consider that to be a choppy market situation and would not likely want to execute the buy. I've tried using the "within" function but don't know if that's the best approach. Could I save the bar# that...
Hello, I am new to coding and picking apart scripts.
One thing I have just found that may help:

_________________

def bullish2 = signal > 0 and signal[1] <= 0;
plot upArrow = bullish2;


_________________

Plots the arrow at the point the criteria is met as long as the arrow isn't plotted on the previous bar.
One way to avoid chop is to create criteria that is only met as specific points
i.e. MA crosses along with other Osc or volume specifics. This helps to narrow in on one specific moment in time.

I am still running into choppy signals myself.
I have built a very simple MA cross signal and so it triggers often.
to get around this you could layer the script with
"and signal[1] < 0 and signal[2] < 0 and signal[3] <0;"

I just think that will run into problems as well, not triggering where it should.

Good Luck let me know what you come up with!
 

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

I have a strategy which plots up and down arrows to indicate buy/sell signals (the actual strategy isn't important, any strategy with up/down signals has this situation). What I'd like to do is to determine if I'm in a "choppy" trend where there are lots of up and down arrow signals in a short amount of time. My idea is when there is a up arrow (buy), to look back somehow to determine when the last time a buy signal was issued. If the previous buy signal occurred a short number of X bars ago (say 15 bars - on a 1 minute chart), I consider that to be a choppy market situation and would not likely want to execute the buy. I've tried using the "within" function but don't know if that's the best approach. Could I save the bar# that the up/down arrows previously occurred in (LastUpArrowBar / LastDnArrowBar)? And, then determine the distance between that and the current bar# (BarNumber)? Any thoughts or help is appreciated.

Here are some of the ways that you can look at prior arrow plots to see when/how many occurred

This was added to the code below. Further information about the getmaxvalueoffset and sum functions can be found under the Education Tab.

Ruby:
#### Prior Arrowup data
input bubbleupdown = 5;
def prior = GetMaxValueOffset(!IsNaN(ArrowUp[1]), length = 100) ;
AddChartBubble(!IsNaN(ArrowUp), low - TickSize() * bubbleupdown, "Last Arrowup \n" + prior + " bars ago", Color.YELLOW, no);
AddLabel(1, "#Arrowup in Last 15 bars =" + Sum(!IsNaN(ArrowUp), 15), Color.YELLOW);

input debug = yes;
plot bn = if !debug then double.nan else BarNumber();
bn.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
#

Here is the full code and image with the bubble and label data

Capture.jpg
Ruby:
input length = 14;
input trigger1 = 148;
input trigger2 = 0;
input arrowdisplacer = .0001; #Change by .001 increments to move arrows
input showallarrows = yes; #show only one arrow per direction or all

def price = close + low + high;
def linDev = LinDev(price, length);
def CCI = if linDev == 0 then 0 else (price - Average(price, length)) /
linDev / 0.015;

def below150 =  if CCI[1] > -trigger1 and CCI < -trigger1 then 1 else if below150[1] == 1 and CCI < -trigger2 then 1 else 0;
def above150 =  if CCI[1] > trigger1 and CCI < trigger1 then 1 else if above150[1] == 1 and CCI > trigger2 then 1 else 0;

plot ArrowUp = if (below150[1] == 1 and below150 == 0 and showallarrows == yes)  then low * (1 - arrowdisplacer)  else Double.NaN;
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUp.SetDefaultColor(Color.YELLOW);
ArrowUp.SetLineWeight(3);

plot ArrowDn = if showallarrows and above150[1] == 1 and above150 == 0 then high * (1 + arrowdisplacer) else Double.NaN;
ArrowDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDn.SetDefaultColor(Color.YELLOW);
ArrowDn.SetLineWeight(3);

AddLabel(yes, "CCI: " + Round(CCI, 0), if CCI > 0 then if CCI[1] > CCI then Color.DARK_GREEN else Color.GREEN else if CCI[1] < CCI then Color.DARK_RED else  Color.RED);
input n = 4;
def n1 = n + 1;
AddChartBubble(!IsNaN(close[n1]) and IsNaN(close[n]), close[n1], "CCI: " + Round(CCI[n1], 0), if CCI[n1] > 0 then if CCI[n1 + 1] > CCI[n1] then Color.DARK_GREEN else Color.GREEN else if CCI[n1 + 1] < CCI[n1] then Color.DARK_RED else  Color.RED, yes);


#### Prior Arrowup data
input bubbleupdown = 5;
def prior = GetMaxValueOffset(!IsNaN(ArrowUp[1]), length = 100) ;
AddChartBubble(!IsNaN(ArrowUp), low - TickSize() * bubbleupdown, "Last Arrowup \n" + prior + " bars ago", Color.YELLOW, no);
AddLabel(1, "#Arrowup in Last 15 bars =" + Sum(!IsNaN(ArrowUp), 15), Color.YELLOW);

input debug = yes;
plot bn = if !debug then double.nan else BarNumber();
bn.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
#
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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