Sometimes it can be useful to track price or indicator movements during a specific time range. A classic example of this would be tracking the
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!
high
or low
of 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 high
/low
of an EMA (exponential moving average) instead of focusing on OHLC
data exclusively.
Since this article will not contain a strategy, we will jump straight into the code.
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[1]) and not na(t) or t[1] < 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[1]) : ema_high[1] ema_low := new_session ? ema1 : in_session ? min(ema1,ema_low[1]) : ema_low[1] 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 Commentary
First things first, we create a couple of inputs to set our custom session range andema1
length. 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 high
andlow
of the ema1
for the period is actually the same. As such, we set the values of ema_high
and ema_low
to ema1
. Following this, we just need to know whether we are still in the session. If so, we save whatever is greater between the high
and the last ema_high
and whatever is lower between the low
and 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_high
andema_low
using 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[1]) and not na(t) or t[1] < 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 in_session
variable.
new_session = in_session and not in_session[1]This 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 Charts
Once you load up the code, you should see something like this:
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.
Brave
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!
Tradingview
Referral Link
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!
PayPal
BTC
3HxNVyh5729ieTfPnTybrtK7J7QxJD9gjD
ETH
0x9a2f88198224d59e5749bacfc23d79507da3d431
LTC
M8iUBnb8KamHR18gpgi2sSG7jopddFJi8S
Hey Dave, Just wanted to say that your posts are super helpful and I have used examples from both your backtrader and pine script posts.
I am trying to do something similar as above to plot each days pre-market high and low. Do you have any idea how I would go about that?
Good work, i need a small help.
I want to store a high and low value of a particular session. Say for example, i need 1600 – 16.:30 high and low to be stored in variables like sh and sl. Sh stands for session high and sl stands for session low. Hope you understand my question. Kindly do the needful.