Label that counts signals from chart overlay

jordan

New member
I need a label that can count the number of signals given from a chart overlay but I can't get the code to work and am wondering if it's possible. For example, I can overlay "LI" to my current chart, give it a 200sma, and plot a potential signal where the low is less than the 200sma but I can't get the total sum of signals. Any advice?

input Security = "LI";
input SecurityType = {"BAR", default"CANDLE", "LINE"};
AddChart(high = high(Security), low = low(Security),open = open(Security), close = close(Security), type = SecurityType);

input LengthMA = 200;
input AvgTypeMA = AverageType.SIMPLE;
plot MA = MovingAverage(AvgTypeMA, close(security), LengthMA);

plot lows = if Low(security) is less than MA then low(security) else double.NaN;
lows.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
lows.SetLineWeight(5);
lows.SetDefaultColor(Color.ORANGE);

AddLabel(yes, "Security Signals: " + TotalSum(Low(security) is less than MA), Color.MAGENTA);


This code works when applied to the current chart but not to chart overlays. When applied to the current chart it looks like

input LengthMA = 200;
input AvgTypeMA = AverageType.SIMPLE;
plot MA = MovingAverage(AvgTypeMA, close, LengthMA);

plot lows = if Low is less than MA then low else double.NaN;
lows.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
lows.SetLineWeight(5);
lows.SetDefaultColor(Color.ORANGE);

AddLabel(yes, "Signals2: " + TotalSum(low is less than MA), Color.MAGENTA);
 
Solution
I need a label that can count the number of signals given from a chart overlay but I can't get the code to work and am wondering if it's possible. For example, I can overlay "LI" to my current chart, give it a 200sma, and plot a potential signal where the low is less than the 200sma but I can't get the total sum of signals. Any advice?

input Security = "LI";
input SecurityType = {"BAR", default"CANDLE", "LINE"};
AddChart(high = high(Security), low = low(Security),open = open(Security), close = close(Security), type = SecurityType);

input LengthMA = 200;
input AvgTypeMA = AverageType.SIMPLE;
plot MA = MovingAverage(AvgTypeMA, close(security), LengthMA);

plot lows = if Low(security) is less than MA then low(security) else...
I need a label that can count the number of signals given from a chart overlay but I can't get the code to work and am wondering if it's possible. For example, I can overlay "LI" to my current chart, give it a 200sma, and plot a potential signal where the low is less than the 200sma but I can't get the total sum of signals. Any advice?

input Security = "LI";
input SecurityType = {"BAR", default"CANDLE", "LINE"};
AddChart(high = high(Security), low = low(Security),open = open(Security), close = close(Security), type = SecurityType);

input LengthMA = 200;
input AvgTypeMA = AverageType.SIMPLE;
plot MA = MovingAverage(AvgTypeMA, close(security), LengthMA);

plot lows = if Low(security) is less than MA then low(security) else double.NaN;
lows.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
lows.SetLineWeight(5);
lows.SetDefaultColor(Color.ORANGE);

AddLabel(yes, "Security Signals: " + TotalSum(Low(security) is less than MA), Color.MAGENTA);


This code works when applied to the current chart but not to chart overlays. When applied to the current chart it looks like

input LengthMA = 200;
input AvgTypeMA = AverageType.SIMPLE;
plot MA = MovingAverage(AvgTypeMA, close, LengthMA);

plot lows = if Low is less than MA then low else double.NaN;
lows.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
lows.SetLineWeight(5);
lows.SetDefaultColor(Color.ORANGE);

AddLabel(yes, "Signals2: " + TotalSum(low is less than MA), Color.MAGENTA);
It appears, as is often the case, that the security overlaid on to another security may have blank (nan) periods. So I used a 'recursive' (def) variable to reference itself to assign the prior real value to the missing period. As addchart does not allow 'recursives', a workaround adding a '+ 0' to each variable seems to work,

The 'Signal2' sum varied by the instrument to which 'LI', in this case, is overlaid onto by the number of bars of the overlaid instrument. Make sure to check the 'enable left axis' in chart settings, price axis tab, and check 'left axis' in the input section of your study.

Hope this helps

Capture.jpg
Ruby:
input Security = "LI";
input SecurityType = {"BAR", default "CANDLE", "LINE"};
def h = if IsNaN(high(Security)) then h[1] else high(Security);
def l = if IsNaN(low(Security)) then l[1] else low(Security);
def o = if IsNaN(open(Security)) then o[1] else open(Security);
def c = if IsNaN(close(Security)) then c[1] else close(Security);
def h1 = h + 0;
def l1 = l + 0;
def o1 = o + 0;
def c1 = c + 0;
AddChart(high = h1, low = l1, open = o1, close = c1, type = SecurityType);

input LengthMA = 200;
input AvgTypeMA = AverageType.SIMPLE;
def MA = if IsNaN(close) then MA[1] else MovingAverage(AvgTypeMA, c1, LengthMA);

plot lows = if l1 is less than MA then l1 else Double.NaN;
lows.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
lows.SetLineWeight(5);
lows.SetDefaultColor(Color.ORANGE);

AddLabel(yes, "Security Signals: " + TotalSum(l1 is less than MA), Color.MAGENTA);
 
Solution

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