# 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;

Solution
maybe the 'minpast' variable in this code can be used to check the minutes of the hour?

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?

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;

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

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;

Regrettably, I found this brute force method was the most reliable. Some bubbles were added as a visual aid.
Had a feeling either you or @halcyonguy would come up with a smart solution to this, appreciate the help guys.
The study works exactly like i wanted to

87k+ Posts
435 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?