• LIMITED TIME ONLY: Take $40 off VIP with code: BTD40.

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...

SleepyZ

Well-known member
VIP
Lifetime
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

Similar threads

Top