Having issues with retaining value on my custom indicator

TraderBro

New member
Hi,

I am trying to create a script for a scanner that will scan stocks intraday and will result in stocks that are in intraday consolidation.
In order to do that I am extracting the values from the 10 and 20 moving averages and creating a value for their angle on every bar (see lines 20/21 in the code below).

However, there's a caveat. I am only interested in showing stocks in the scan if they initially made a faster move which should trigger my other criteria, which is, the angle of the first moving average needs to be 0.3 or more (line 39). I only want to start tracking stocks in consolidation after this condition turns true for the first time.

In the picture below you'll see the following:

Plum bubble: the angle value of the first moving average
Cyan bubble: indicator that shows where the scanner is first triggered (1 for true, 0 for false), this is incorrect. It should be 0 until the Plum bubble goes above 0.3 for the first time, and then it should turn to 1
Green bubble: areas in the chart that I identify as "tight"
Gray bubble: areas in the chart that I identify as being in "consolidation" -- this is ultimately the trigger that I am looking for in my scan. I would like to see stocks that are currently in "consolidation" mode.

I want the Gray "C" bubbles to ONLY start showing after my trigger is true. However, for some reason, my trigger is true from the first candle of the day instead of the first candle AFTER the MA angle goes above 0.3.
I'm pretty sure the issue is with my variable initialTrigger which is defined on line 39, but I can't spot the issue.

Can anyone please kindly help me with that?

ORDIUsg.png


Here's the code:

Code:
# Find tight consolidation area on the chart after a push up
# 7.29.2020

input MovAvg1_length = 10;
input MovAvg2_length = 20;
input displace = 0;
input initialMoveAngleTrigger = 0.3;
input price = FundamentalType.CLOSE;
input averageType = AverageType.SIMPLE;

def MovAvg1 = SimpleMovingAvg(CLOSE, MovAvg1_length, displace, no);
def MovAvg2 = SimpleMovingAvg(CLOSE, MovAvg2_length, displace, no);

def highOfDay = DailyHighLow(AggregationPeriod.DAY, 1, 0, no).DailyHigh;
def lowOfDay = DailyHighLow(AggregationPeriod.DAY, 1, 0, no).DailyLow;

def height = MovAvg1 - MovAvg1[1];
def height2 = MovAvg2 - MovAvg2[1];

def Ma1_deg = AbsValue(Round(Atan(height/MovAvg1_length) * 180 / Double.Pi, 3));
def Ma2_deg = AbsValue(Round(Atan(height2/MovAvg2_length) * 180 / Double.Pi, 3));

def isOpen = if secondsFromTime(0930) >= 0 and secondstillTime(1600) >= 0 then 1 else 0;
def today = if GetDay() == GetLastDay() then 1 else 0;

# def limit = !IsNaN(close) and IsNaN(close [-1] ) && HighestAll(BarNumber());
# def limit = isOpen and BarNumber() % 2 == 0;
def limit = isOpen and today;

# Only track stocks that triggered initially, otherwise ignore them
#def initialTrigger;
#if (Ma1_deg >= initialMoveAngleTrigger) {
#    initialTrigger = 1;
#} else {
#    initialTrigger = initialTrigger[1];
#}

#def initialTrigger = CompoundValue(1, Ma1_deg >= initialMoveAngleTrigger, 0);
def initialTrigger = if Ma1_deg > initialMoveAngleTrigger then 1 else initialTrigger[1];

def maDegreeDiff = Ma1_deg - Ma2_deg;
def tightAction = maDegreeDiff <= 0.1;   
def consolidation = if initialTrigger and tightAction then 1 else 0;

AddChartBubble(limit, MovAvg1, Ma1_deg, Color.PLUM, no);
# AddChartBubble(limit, MovAvg2, Ma2_deg, Color.YELLOW, no);
# AddChartBubble(limit, high, AbsValue(maDegreeDiff), Color.YELLOW, no);

# show when initially triggered
AddChartBubble(limit, highOfDay, initialTrigger , Color.CYAN, no);

# show tight price action
AddChartBubble(limit and tightAction, lowOfDay, if tightAction then "T" else "", Color.DARK_GREEN, no);

# show areas of consolidation, should include tight areas AFTER then initial trigger
AddChartBubble(limit and consolidation, 2.26, "C", Color.DARK_GRAY, no);
 
Solution
Hi,

I am trying to create a script for a scanner that will scan stocks intraday and will result in stocks that are in intraday consolidation.
In order to do that I am extracting the values from the 10 and 20 moving averages and creating a value for their angle on every bar (see lines 20/21 in the code below).

However, there's a caveat. I am only interested in showing stocks in the scan if they initially made a faster move which should trigger my other criteria, which is, the angle of the first moving average needs to be 0.3 or more (line 39). I only want to start tracking stocks in consolidation after this condition turns true for the first time.

In the picture below you'll see the following:

Plum bubble: the angle value...
Hi,

I am trying to create a script for a scanner that will scan stocks intraday and will result in stocks that are in intraday consolidation.
In order to do that I am extracting the values from the 10 and 20 moving averages and creating a value for their angle on every bar (see lines 20/21 in the code below).

However, there's a caveat. I am only interested in showing stocks in the scan if they initially made a faster move which should trigger my other criteria, which is, the angle of the first moving average needs to be 0.3 or more (line 39). I only want to start tracking stocks in consolidation after this condition turns true for the first time.

In the picture below you'll see the following:

Plum bubble: the angle value of the first moving average
Cyan bubble: indicator that shows where the scanner is first triggered (1 for true, 0 for false), this is incorrect. It should be 0 until the Plum bubble goes above 0.3 for the first time, and then it should turn to 1
Green bubble: areas in the chart that I identify as "tight"
Gray bubble: areas in the chart that I identify as being in "consolidation" -- this is ultimately the trigger that I am looking for in my scan. I would like to see stocks that are currently in "consolidation" mode.

I want the Gray "C" bubbles to ONLY start showing after my trigger is true. However, for some reason, my trigger is true from the first candle of the day instead of the first candle AFTER the MA angle goes above 0.3.
I'm pretty sure the issue is with my variable initialTrigger which is defined on line 39, but I can't spot the issue.

Can anyone please kindly help me with that?


i'm away from my computer, so i can't check what you are seeing.

ideas,
sometimes when a formula has a variable with an offset, it reads the 1st value as a na and it cascades through all the bars, resulting in unexpected results.
try adding a check if bn == 1,

def bn = barnumber();
def initialTrigger = if bn == 1 then 0
else if Ma1_deg > initialMoveAngleTrigger then 1
else initialTrigger[1];



what happens when
Ma1_deg and/or Ma2_deg
are negative?
tightAction will be true. is this ok?
maybe add a absvalue() to the formula?

def maDegreeDiff = Ma1_deg - Ma2_deg;
def tightAction = maDegreeDiff <= 0.1;
 
Solution

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

i'm away from my computer, so i can't check what you are seeing.

ideas,
sometimes when a formula has a variable with an offset, it reads the 1st value as a na and it cascades through all the bars, resulting in unexpected results.
try adding a check if bn == 1,

def bn = barnumber();
def initialTrigger = if bn == 1 then 0
else if Ma1_deg > initialMoveAngleTrigger then 1
else initialTrigger[1];



what happens when
Ma1_deg and/or Ma2_deg
are negative?
tightAction will be true. is this ok?
maybe add a absvalue() to the formula?

def maDegreeDiff = Ma1_deg - Ma2_deg;
def tightAction = maDegreeDiff <= 0.1;

Thanks for the solution, I will test it out and report back.
On lines 20-21 already wrapped the Ma1_deg and Ma2_deg result with an AbsValue so both of them will always be positive.

Then, on line 47:
Code:
def maDegreeDiff = Ma1_deg - Ma2_deg;

maDegreeDiff will be negative if the 2nd moving average angle is larger than the 1st moving average angle.
I changed line 48 to this:
Code:
def tightAction = maDegreeDiff <= 0.1 and maDegreeDiff >= 0.1;
It should show be true when there's tight price action, whether to the upside or downside. I'll test it out for a little bit and adjust it according to my strategy.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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