Need help adding a time condition to this study

zeek

Active member
2019 Donor
So i have this study that alerts me when price is between two EMAs and it works great. But i would like to add another condition for the alert and need help with this.
I want the alert to trigger when price is between EMAs but only 5min before every new 30min "window" opens. So as an example, if price is between the EMAs at 9:26AM or 9:58AM i want the alert to trigger but if price is inside EMAs any other time within the current 30min candle/window, it shouldn´t trigger.

So between 9:25-9.30AM, 9:55-10:00AM, 10:25-10:30AM etc. throughout the day and i need it to work with extended hours included (pre + after)

This should be fairly simple to add as a condition so i hope someone can help me modify the code below. Thanks in advance.

Code:
def agg=aggregationPeriod.DAY;
plot fastline = ExpAverage(close,32);
plot slowline = ExpAverage(close,50);
def fastlined = ExpAverage(close(period=agg),32);
def slowlined = ExpAverage(close(period=agg),50);

def numDevDn = -2.0;
def numDevUp = 2.0;
def timeFrame = 3 ;

def cap = getAggregationPeriod();
def errorInAggregation =
    timeFrame == 3 and cap >= AggregationPeriod.WEEK or
    timeFrame == 3 and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
#switch (timeFrame) {
#case DAY:
    periodIndx = yyyyMmDd;
#}
#def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

#if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);

def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

def upperprice= if fastline> slowline then fastline else slowline;
DEF lowerprice= if fastline<slowline then fastline else slowline;

def alertcond=  close(pricetype=pricetype.LAST)>lowerprice and close(pricetype=pricetype.LAST)<upperprice;
alert(alertcond,"Price is inside EMAs", alert.BAR,sound.ring);
addlabel(alertcond,"Price is inside EMAs ",COLOR.YELLOW);
 
Solution
maybe the 'minpast' variable in this code can be used to check the minutes of the hour?
https://usethinkscript.com/threads/timeframe-deconstruction-for-watchlist-column-multi-timeframes.7764/#post-746

So i have this study that alerts me when price is between two EMAs and it works great. But i would like to add another condition for the alert and need help with this.
I want the alert to trigger when price is between EMAs but only 5min before every new 30min "window" opens. So as an example, if price is between the EMAs at 9:26AM or 9:58AM i want the alert to trigger but if price is inside EMAs any other time within the current 30min candle/window, it shouldn´t trigger.

So between 9:25-9.30AM, 9:55-10:00AM...
maybe the 'minpast' variable in this code can be used to check the minutes of the hour?
https://usethinkscript.com/threads/timeframe-deconstruction-for-watchlist-column-multi-timeframes.7764/#post-746

So i have this study that alerts me when price is between two EMAs and it works great. But i would like to add another condition for the alert and need help with this.
I want the alert to trigger when price is between EMAs but only 5min before every new 30min "window" opens. So as an example, if price is between the EMAs at 9:26AM or 9:58AM i want the alert to trigger but if price is inside EMAs any other time within the current 30min candle/window, it shouldn´t trigger.

So between 9:25-9.30AM, 9:55-10:00AM, 10:25-10:30AM etc. throughout the day and i need it to work with extended hours included (pre + after)

This should be fairly simple to add as a condition so i hope someone can help me modify the code below. Thanks in advance.

Code:
def agg=aggregationPeriod.DAY;
plot fastline = ExpAverage(close,32);
plot slowline = ExpAverage(close,50);
def fastlined = ExpAverage(close(period=agg),32);
def slowlined = ExpAverage(close(period=agg),50);

def numDevDn = -2.0;
def numDevUp = 2.0;
def timeFrame = 3 ;

def cap = getAggregationPeriod();
def errorInAggregation =
    timeFrame == 3 and cap >= AggregationPeriod.WEEK or
    timeFrame == 3 and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
#switch (timeFrame) {
#case DAY:
    periodIndx = yyyyMmDd;
#}
#def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

#if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);

def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

def upperprice= if fastline> slowline then fastline else slowline;
DEF lowerprice= if fastline<slowline then fastline else slowline;

def alertcond=  close(pricetype=pricetype.LAST)>lowerprice and close(pricetype=pricetype.LAST)<upperprice;
alert(alertcond,"Price is inside EMAs", alert.BAR,sound.ring);
addlabel(alertcond,"Price is inside EMAs ",COLOR.YELLOW);


Regrettably, I found this brute force method was the most reliable. Some bubbles were added as a visual aid.

Capture.jpg
Ruby:
def agg = AggregationPeriod.DAY;
plot fastline = ExpAverage(close, 32);
plot slowline = ExpAverage(close, 50);
def fastlined = ExpAverage(close(period = agg), 32);
def slowlined = ExpAverage(close(period = agg), 50);

def numDevDn = -2.0;
def numDevUp = 2.0;
def timeFrame = 3 ;

def cap = GetAggregationPeriod();
def errorInAggregation =
    timeFrame == 3 and cap >= AggregationPeriod.WEEK or
    timeFrame == 3 and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
#switch (timeFrame) {
#case DAY:
periodIndx = yyyyMmDd;
#}
#def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

#if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);

def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

def upperprice = if fastline > slowline then fastline else slowline;
def lowerprice = if fastline < slowline then fastline else slowline;

def ORActive = if SecondsFromTime(0925) >= 0 and SecondsTillTime(0930) >= 0 or
                  SecondsFromTime(0955) >= 0 and SecondsTillTime(1000) >= 0 or
                  SecondsFromTime(1025) >= 0 and SecondsTillTime(1030) >= 0 or
                  SecondsFromTime(1055) >= 0 and SecondsTillTime(1100) >= 0 or
                  SecondsFromTime(1125) >= 0 and SecondsTillTime(1130) >= 0 or
                  SecondsFromTime(1155) >= 0 and SecondsTillTime(1200) >= 0 or
                  SecondsFromTime(1225) >= 0 and SecondsTillTime(1130) >= 0 or
                  SecondsFromTime(1255) >= 0 and SecondsTillTime(1200) >= 0 or
                  SecondsFromTime(1325) >= 0 and SecondsTillTime(1330) >= 0 or
                  SecondsFromTime(1355) >= 0 and SecondsTillTime(1400) >= 0 or
                  SecondsFromTime(1425) >= 0 and SecondsTillTime(1430) >= 0 or
                  SecondsFromTime(1455) >= 0 and SecondsTillTime(1500) >= 0 or
                  SecondsFromTime(1525) >= 0 and SecondsTillTime(1530) >= 0 or
                  SecondsFromTime(1555) >= 0 and SecondsTillTime(1600) >= 0
                then 1 else 0;
def alertcond = if ORActive and Between(close, lowerprice, upperprice) then 1 else 0;

Alert(alertcond, "Price is inside EMAs", Alert.BAR, Sound.Ring);
AddChartBubble(alertcond, low, "", Color.WHITE, no);
AddLabel(alertcond, "Price is inside EMAs ", Color.YELLOW);
 
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
438 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