marcq
marcq

Reputation: 483

Plot High | Low of previous day Asian Session (2000-0000) pm to today end time (0400) am

I would like to plot the previous High and Low of the previous day Asian Session Period (2000-0000) to today end time "0400".

I started with plotting the High with the code hereafter, but my code produces of course a "random" line, since the Asian Session is in the future (and relevant Asian Session Period was yesterday) .

Hope I was clear and would appreciate some expertise.

Many thanks.

// --- Set Highest and Lowest
SessionHigh(sessionTime, sessionTimeZone=syminfo.timezone) =>
    insideSession = not na(time(timeframe.period, sessionTime, sessionTimeZone))
    var float sessionHighPrice = na

    if insideSession and not insideSession[1]
        sessionHighPrice := high
    else if insideSession
        sessionHighPrice := math.max(sessionHighPrice, high)
    
    sessionHighPrice

InSession(sessionTimes, sessionTimeZone=syminfo.timezone) =>
    not na(time(timeframe.period, sessionTimes, sessionTimeZone))

// Configure session with inputs
session  = input.session("2000-0400", title="iCT Asian Range")
timeZone = input.string("GMT-5", title="Time Zone")

// Get the session high
sessHigh = SessionHigh(session, timeZone)

// Is today
isToday = false
if year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time) 
    isToday := true

// Show the session high on the chart
plot(sessHigh, color=isToday ? color.new(#C70039,0) : na, title="Session High")

Progress : Made some progress with the code hereafter. The issue I have now is that I want

  1. the plotted horizonal lines to start from the High or Low price, but not to be plotted in the past
  2. the plotted horizontal lines not to break when the price break the High or Low :
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © marc_Q

//@version=5
indicator("H&L", overlay = true)

// --- Timezones
tz_incr = input.int(-5, 'UTC (+/-)', group = 'Timezone')

use_exchange = input(false, 'Use Exchange Timezone', group = 'Timezone')

sesa_ses = input.session('2000-0000', '', inline = 'sesa', group = 'ICT Asian Range')
// --- Sessions
tf = timeframe.period

var tz = use_exchange ? syminfo.timezone :
  str.format('UTC{0}{1}', tz_incr >= 0 ? '+' : '-', math.abs(tz_incr))

is_sesa = math.sign(nz(time(tf, sesa_ses, tz)))

// --- Set Highest and Lowest
SessionHigh(is_sesa, sessionTimeZone=syminfo.timezone) =>
    insideSession = not na(time(timeframe.period, is_sesa, sessionTimeZone))
    var float sessionHighPrice = na

    if insideSession and not insideSession[1]
        sessionHighPrice := high
    else if insideSession
        sessionHighPrice := math.max(sessionHighPrice, high)
    
    sessionHighPrice

SessionLow(sessionTime, sessionTimeZone=syminfo.timezone) =>
    insideSession = not na(time(timeframe.period, sessionTime, sessionTimeZone))
    var float sessionLowPrice = na

    if insideSession and not insideSession[1]
        sessionLowPrice := low
    else if insideSession
        sessionLowPrice := math.min(sessionLowPrice, low)
    
    sessionLowPrice

InSession(sessionTimes, sessionTimeZone=syminfo.timezone) =>
    not na(time(timeframe.period, sessionTimes, sessionTimeZone))

// Configure session with inputs
session  = input.session("2000-0400", title="iCT Asian Range")
timeZone = input.string("GMT-5", title="Time Zone")

// Get the session high
sessHigh = SessionHigh(session, timeZone)
// Fetch the session low
sessLow = SessionLow(session, timeZone)

// Is today
isToday = false
if year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time) 
    isToday := true

// Show the session high on the chart
plot(sessHigh, color=isToday ? color.new(#C70039,0) : na, title="Session High")

// Show the session low on the chart
plot(sessLow, color=color.rgb(141, 241, 9), title="Session Low")
// For visual verification, highlight the background of session bars
// bgcolor(InSession(session, timeZone) ? color.new(color.orange, 90) : na)

On this picture, you can see plotted the High of the Asian Session Period (2000 - 0000) in Red and the Low in Green, you can see that the High was broken at 0322 and the line starts to move up. I would like the plotted line to stay "horizontal" even if the price breaks the High or the Low and this until "0400" :

High and Low Asian Session Period "2000-0000"

Upvotes: 0

Views: 200

Answers (1)

marcq
marcq

Reputation: 483

I could solve my issue :

First, I fetch the Session time :

ses_ICTAR = input.session("2000-0400", title="Session Range")

Second, I fetch the High and Low of the Session :

sess_High_ICTAR = SessionHigh(ses_ICTAR, timeZone)
sess_Low_ICTAR = SessionLow(ses_ICTAR, timeZone)

Third, I define from which time on I want my line to be drawn :

losb_t0 = bool(hour(time, "GMT-4") >0) // from
losb_t1 = bool(hour(time, "GMT-4") <=4) // to

Finally, I plot my High and Low :

plot_1 = plot(sess_High_ICTAR, color=losb_t0 and losb_t1 ? color.new(#f1074d, 50) : na, linewidth=2, title="Session", display=display.all)
plot_2 = plot(sess_Low_ICTAR, color=losb_t0 and losb_t1 ? color.new(#f1074d, 50) : na, linewidth=2, title="Session", display=display.all)

I can now define, from "where" to "where" my Highs and Lows are drawn.

Upvotes: 0

Related Questions