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?
Here's the code:
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?
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);