Time intervals

Branch

Member
I would like one label (response) with the following criteria: price closes above or below Simple moving averages, on the following various times intervals.. The times intervals are 1 minute, 5 minute and 30 minute. When this is TRUE on ALL of those times intervals I want a label 'Buy' when condition meets or 'Sell' when conditions meets based on the def B and def S below.

# PRICE + MA
input price = close;
def SMA9 = MovingAverage(AverageType.SIMPLE, close, 9);
def SMA13 = MovingAverage(AverageType.SIMPLE, close, 13);
def SMA21 = MovingAverage(AverageType.SIMPLE, close, 21);
def SMA35 = MovingAverage(AverageType.SIMPLE, close, 35);
def PD = close < close [1];
def PU = close > close [1];
def PMS = PD and SMA9 < SMA13 and SMA13 < SMA21 and SMA21 < SMA35;
def PMB = PU and SMA9 > SMA13 and SMA13 > SMA21 and SMA21 > SMA35;
def UP = close > sma9 and sma13 and sma21 and sma35 ;
def DN = close < sma9 and sma13 and sma21 and sma35 ;
def B = UP ;
def S = DN
;

# Create the label
AddLabel(1, "" + (if B then "B" + "" else if S then "S" + "" else " X "),
if B then Color.GREEN else if S then Color.RED else Color.PLUM);
AssignBackgroundColor(if B then Color.BLACK else if S then Color.BLACK else Color.BLACK);
 
Solution
I would like one label (response) with the following criteria: price closes above or below Simple moving averages, on the following various times intervals.. The times intervals are 1 minute, 5 minute and 30 minute. When this is TRUE on ALL of those times intervals I want a label 'Buy' when condition meets or 'Sell' when conditions meets based on the def B and def S below.

# PRICE + MA
input price = close;
def SMA9 = MovingAverage(AverageType.SIMPLE, close, 9);
def SMA13 = MovingAverage(AverageType.SIMPLE, close, 13);
def SMA21 = MovingAverage(AverageType.SIMPLE, close, 21);
def SMA35 = MovingAverage(AverageType.SIMPLE, close, 35);
def PD = close < close [1];
def PU = close > close [1];
def PMS = PD and SMA9 < SMA13 and...
I would like one label (response) with the following criteria: price closes above or below Simple moving averages, on the following various times intervals.. The times intervals are 1 minute, 5 minute and 30 minute. When this is TRUE on ALL of those times intervals I want a label 'Buy' when condition meets or 'Sell' when conditions meets based on the def B and def S below.

# PRICE + MA
input price = close;
def SMA9 = MovingAverage(AverageType.SIMPLE, close, 9);
def SMA13 = MovingAverage(AverageType.SIMPLE, close, 13);
def SMA21 = MovingAverage(AverageType.SIMPLE, close, 21);
def SMA35 = MovingAverage(AverageType.SIMPLE, close, 35);
def PD = close < close [1];
def PU = close > close [1];
def PMS = PD and SMA9 < SMA13 and SMA13 < SMA21 and SMA21 < SMA35;
def PMB = PU and SMA9 > SMA13 and SMA13 > SMA21 and SMA21 > SMA35;
def UP = close > sma9 and sma13 and sma21 and sma35 ;
def DN = close < sma9 and sma13 and sma21 and sma35 ;
def B = UP ;
def S = DN
;

# Create the label
AddLabel(1, "" + (if B then "B" + "" else if S then "S" + "" else " X "),
if B then Color.GREEN else if S then Color.RED else Color.PLUM);
AssignBackgroundColor(if B then Color.BLACK else if S then Color.BLACK else Color.BLACK);

This uses the script() function to help define the 3 ma B/S conditions
When these are all true for B's or S's then a label will plot including how many bars ago
Since this uses higher timeframes, the labels will repaint.
The test bubbles are to show how many bars ago either the BB/SS were true.

Screenshot 2024-05-03 102035.png
Code:
# PRICE + MA

script ma {
    input agg   = AggregationPeriod.MIN;
    input price = FundamentalType.CLOSE;
    input avg   = AverageType.SIMPLE;
    def   c     = close;
    plot SMA9   = MovingAverage(avg, Fundamental(price, period = agg), 9);
    plot SMA13  = MovingAverage(avg, Fundamental(price, period = agg), 13);
    plot SMA21  = MovingAverage(avg, Fundamental(price, period = agg), 21);
    plot SMA35  = MovingAverage(avg, Fundamental(price, period = agg), 35);
    def PD  = c < c[1];
    def PU  = c > c[1];
    def PMS = PD and SMA9 < SMA13 and SMA13 < SMA21 and SMA21 < SMA35;
    def PMB = PU and SMA9 > SMA13 and SMA13 > SMA21 and SMA21 > SMA35;
    def UP  = c > SMA9 and c > SMA13 and c > SMA21 and c > SMA35 ;
    def DN  = c < SMA9 and c < SMA13 and c < SMA21 and c < SMA35 ;
    plot B   = UP ;
    plot S   = DN ;
}
def B1 = ma(agg = "MIN").B;
def B2 = ma(agg = "FIVE_MIN").B;
def B3 = ma(agg = "THIRTY_MIN").B;
def BB = B1 + B2 + B3 == 3;
def BC = if BB[1] == 0 and BB == 1 then 1 else if BC[1] >= 1 and BB == 1 then BC[1] + 1 else 0;

def S1 = ma(agg = "MIN").S;
def S2 = ma(agg = "FIVE_MIN").S;
def S3 = ma(agg = "THIRTY_MIN").S;
def SS = S1 + S2 + S3 == 3;
def SC = if SS[1] == 0 and SS == 1 then 1 else if SC[1] >= 1 and SS == 1 then SC[1] + 1 else 0;

# Create the label
AddLabel(1, "" + (if BB then "B" + BC + "" else if SS then "S" + SC + "" else " X "),
if BB then Color.GREEN else if SS then Color.RED else Color.PLUM);
AssignBackgroundColor(if BB then Color.BLACK else if SS then Color.BLACK else Color.BLACK);

input test = yes;
AddChartBubble(test and BB, low, BB + "\n" + BC, Color.WHITE, no);
AddChartBubble(test and SS, low, SS + "\n" + SC, Color.YELLOW, no);

#
 
Solution

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
177 Online
Create Post

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