Specifying a Timeframe in a Plot Indicator

Specifying a Timeframe in a Plot Indicator

I have this code that I use to draw the 5 minutes chart moving averages on the seconds based time frames but not on the higher time frames:

//@version=4
study("5min MAs", overlay=true)

plot(timeframe.isseconds ? security(syminfo.tickerid, '5', ema(close[1],9),  lookahead=barmerge.lookahead_on) : ema(close,9) , title="9EMA", color=#ff6d00, linewidth=1)
plot(timeframe.isseconds ? security(syminfo.tickerid, '5', ema(close[1],20),  lookahead=barmerge.lookahead_on) : ema(close,20) , title="20EMA", color=#ffeb3b, linewidth=1)
plot(timeframe.isseconds ? security(syminfo.tickerid, '5', sma(close[1],200),  lookahead=barmerge.lookahead_on) : sma(close,200) , title="200SMA", color=#ff2fbb, linewidth=1)

I'd like the code to also be able to draw the 5 min based MAs on the 1 and 2 minute charts along with the seconds time frames. I tried using the timeframe.isintraday instead of timeframe.isseconds function but the 5 min based MAs are also drawn on the time frames above 5 minutes (eg. 30min, 60min etc.) which I don't want.

Is there a way to isolate only for the 1 and 2 minutes timeframes? I think I need to declare a variable with timeframe.period but I am not sure how to integrate it.

Thanks, Eric

1 Answer

I managed to figure it out

//@version=4
study("5min MAs", overlay=true)


plot(timeframe.isseconds or (timeframe.isintraday and (timeframe.multiplier <= 2)) ? security(syminfo.tickerid, '5', ema(close[1],9),  lookahead=barmerge.lookahead_on) : ema(close,9) , title="9EMA", color=#ff6d00, linewidth=1)
plot(timeframe.isseconds or (timeframe.isintraday and (timeframe.multiplier <= 2)) ? security(syminfo.tickerid, '5', ema(close[1],20),  lookahead=barmerge.lookahead_on) : ema(close,20) , title="20EMA", color=#ffeb3b, linewidth=1)
plot(timeframe.isseconds or (timeframe.isintraday and (timeframe.multiplier <= 2)) ? security(syminfo.tickerid, '5', sma(close[1],200),  lookahead=barmerge.lookahead_on) : sma(close,200) , title="200SMA", color=#ff2fbb, linewidth=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.