lowof a session (such as the Asian session in Forex) and then using those levels to set up a breakout range. In this article, we will take a look at one way you can accomplish this. We will also take an alternative slant on it by on it tracking the
lowof an EMA (exponential moving average) instead of focusing on
OHLCdata exclusively. Since this article will not contain a strategy, we will jump straight into the code.
//@version=3 study("Session EMA High Low", overlay=true) my_session = input("1200-1300", type=session, title='Custom Session') ma_len = input(7, minval=1, title='EMA Length') // Get the EMA ema1 = ema(close, ma_len) // Determine if we are in a session // ---------------------------------- in_session = time(period, my_session) is_new_session(res, sess) => t = time(res, sess) na(t) and not na(t) or t < t new_session = is_new_session("1440", my_session) // Get the High/Low of the EMA for the session ema_high = na ema_low = na ema_high := new_session ? ema1 : in_session ? max(ema1, ema_high) : ema_high ema_low := new_session ? ema1 : in_session ? min(ema1,ema_low) : ema_low plot(ema1, linewidth=2, color=orange, title='EMA') plot(ema_high, color=green, style=circles, title='EMA High') plot(ema_low, color=red, style=circles, title='EMA Low')
Code CommentaryFirst things first, we create a couple of inputs to set our custom session range and
ema1length. In the example, the default will track a window over lunchtime. Following this, the next thing we need to do is track when we enter and are within our session. Tradingview wiki actually provides some good examples of how to do this and the example above is taken from the wiki. You can find a link to it here. We need to know when the session starts so that we can set our initial value. Once the session starts, the
ema1for the period is actually the same. As such, we set the values of
ema1. Following this, we just need to know whether we are still in the session. If so, we save whatever is greater between the
highand the last
ema_highand whatever is lower between the
lowand the last
ema_low. Finally, if we are not in the session we just keep using the same value as the previous bar. We set all of these values to
ema_lowusing a ternary conditional operator. Returning to the session code, the function we use to detect a new session might appear to be daunting at first:
is_new_session(res, sess) => t = time(res, sess) na(t) and not na(t) or t < tHowever, all we are doing in this function is using
time()to detect whether we are in a session and then doing a simple check. If during the last bar we were not in the session, but now we are in a session, then we have a new session! In fact, that whole function could be re-written to simply use our
new_session = in_session and not in_sessionThis works the same and is much more intuitive. However, the function from the wiki was left in the example code to give us a chance to discuss and explain it.
On The ChartsOnce you load up the code, you should see something like this: That is all there is to it. Take a closer look and you will see the high/low adjusting to the movement of the EMA during the lunch break. After this, the lines remain fixed until the next day. Now it is time to get creative. Try tracking some things you wouldn’t normally think about. In addition to this, you can move away from tracking sessions. The exact same technique could be used to track between entry signals or the last swing high. Anything you can think of!
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.
Backtest Rookies is a registered with Brave publisher!
Brave users can drop us a tip.
Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!