Miltown Wrench
Miltown Wrench

Reputation: 11

Building out a high/low session indicator

I am having trouble with this code, it only works on 5 and 15min, would like to get it working on everything but 4hr and daily.

Sometimes this is resolved by turning off the asian session.

I am not sure how to resolve the issue using max_bars_back which is one of the runtime errors I get on occasion, the other error I get occasionally is Invalid value of the 'length" argument (NaN) in the "highest" function. It must be >0.

When I force the value to be at least 1 I still get the same error, while if I remove the american and custom sessions everything seems to work fine.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Hotchachachaaa

//@version=5
indicator("session tma",overlay=true)

tz                  ="UTC"
label               = input.string( title="Label",                  defval="CME Open", tooltip="For easy identification", group = "Trade Session")
tsAsia              = input.bool(   title="Show Asian Session",     defval=true, group = "Asian Trade Session")
startHourAsia       = input.int(    title="Asia Start hour",        defval=0,   minval=0, maxval=23,    group = "Asian Trade Session")
startMinuteAsia     = input.int(    title="Asia Start minute",      defval=00,  minval=0, maxval=59,    group = "Asian Trade Session")
endHour2Asia        = input.int(    title="Asia End hour",          defval=8,   minval=0, maxval=23,    group = "Asian Trade Session")
endMinute2Asia      = input.int(    title="Asia End minute",        defval=0,   minval=0, maxval=59,    group = "Asian Trade Session")

tsEurope            = input.bool(   title="Show European Session",  defval=true,                        group = "European Trade Session")
startHourEurope     = input.int(    title="Europen Start hour",     defval=7,   minval=0, maxval=23,    group = "European Trade Session")
startMinuteEurope   = input.int(    title="Europen Start minute",   defval=00,  minval=0, maxval=59,    group = "European Trade Session")
endHour2Europe      = input.int(    title="Europen End hour",       defval=16,  minval=0, maxval=23,    group = "European Trade Session")
endMinute2Europe    = input.int(    title="Europen End minute",     defval=0,   minval=0, maxval=59,    group = "European Trade Session")

tsAmericas          = input.bool(   title="Show American Session",  defval=true,                        group = "American Trade Session")
startHourAmericas   = input.int(    title="Americas Start hour",    defval=12,  minval=0, maxval=23,    group = "American Trade Session")
startMinuteAmericas = input.int(    title="Americas Start minute",  defval=00,  minval=0, maxval=59,    group = "American Trade Session")
endHour2Americas    = input.int(    title="Americas End hour",      defval=20,  minval=0, maxval=23,    group = "American Trade Session")
endMinute2Americas  = input.int(    title="Americas End minute",    defval=0,   minval=0, maxval=59,    group = "American Trade Session")

tsCustom            = input.bool(   title="Show Custom Session",    defval=true,                        group = "Custom Trade Session")
startHourCustom     = input.int(    title="Custom Start hour",      defval=16,  minval=0, maxval=23,    group = "Custom Trade Session")
startMinuteCustom   = input.int(    title="Custom Start minute",    defval=00,  minval=0, maxval=59,    group = "Custom Trade Session")
endHour2Custom      = input.int(    title="Custom End hour",        defval=20,  minval=0, maxval=23,    group = "Custom Trade Session")
endMinute2Custom    = input.int(    title="Custom End minute",      defval=0,   minval=0, maxval=59,    group = "Custom Trade Session")

showMon = input(title="Monday",     defval=true, group = "Trade Session")
showTue = input(title="Tuesday",    defval=true, group = "Trade Session")
showWed = input(title="Wednesday",  defval=true, group = "Trade Session")
showThu = input(title="Thursday",   defval=true, group = "Trade Session")
showFri = input(title="Friday",     defval=true, group = "Trade Session")
showSat = input(title="Saturday",   defval=false, group = "Trade Session")
showSun = input(title="Sunday",     defval=false, group = "Trade Session")

tzYear = year(time, tz)
tzMonth = month(time, tz)
tzDay = dayofmonth(time, tz)
tzDayOfWeek = dayofweek(time, tz)

displayBG   = input.bool(title="Display Session as Background Color", defval = true)
displayHiLo = input.bool(title="Display Session High/Low", defval= true)

transpcolor= color.new(color.white,100)


////////////////asia session
startTime1Asia = timestamp(tz, tzYear, tzMonth, tzDay, startHourAsia, startMinuteAsia)
endTime1Asia = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Asia, endMinute2Asia)
activeAsia = if startTime1Asia <= time and time <= endTime1Asia and tsAsia
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Asiahigh  = na
var float Asialow   = na
bgColorAsia         = color.new(color.red,85)
rangeColorAsia      = activeAsia ? color.new(color.red,0): color.new(color.red,100)  

AsiaLookback=(ta.barssince(not activeAsia)) + 1
if activeAsia
    
    Asiahigh := ta.highest(high,AsiaLookback)
    Asialow  := ta.lowest(low,AsiaLookback)
    
plot(Asiahigh  , color= rangeColorAsia, title= "Asia High")   
plot(Asialow  , color= rangeColorAsia, title= "Asia Low") 
bgcolor(color=activeAsia and displayBG ? bgColorAsia : na, title = "Asia Session Background")

////////////////Europe session
startTime1Europe = timestamp(tz, tzYear, tzMonth, tzDay, startHourEurope, startMinuteEurope)
endTime1Europe = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Europe, endMinute2Europe)
activeEurope = if startTime1Europe <= time and time <= endTime1Europe and tsEurope
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Europehigh  = na
var float Europelow   = na
bgColorEurope         = color.new(color.green,85)
rangeColorEurope      = activeEurope ? color.new(color.green,0): color.new(color.red,100)  

EuropeLookback= (ta.barssince(not activeEurope))+ 1
if activeEurope
    
    Europehigh := ta.highest(high,EuropeLookback)
    Europelow  := ta.lowest(low,EuropeLookback)
    
plot(Europehigh  , color= rangeColorEurope, title= "Europe High")   
plot(Europelow  , color= rangeColorEurope, title= "Europe Low") 
bgcolor(color=activeEurope and displayBG ? bgColorEurope : na, title = "Europe Session Background")
////////////////Americas session
startTime1Americas = timestamp(tz, tzYear, tzMonth, tzDay, startHourAmericas, startMinuteAmericas)
endTime1Americas = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Americas, endMinute2Americas)
activeAmericas = if startTime1Americas <= time and time <= endTime1Americas and tsAmericas
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Americashigh  = na
var float Americaslow   = na
bgColorAmericas         = color.new(color.blue,85)
rangeColorAmericas      = activeAmericas ? color.new(color.blue,0): color.new(color.red,100)  

AmericasLookback= (ta.barssince(not activeAmericas))+ 1 
if activeAmericas
    
    Americashigh := ta.highest(high,AmericasLookback)
    Americaslow  := ta.lowest(low,AmericasLookback)
    
plot(Americashigh  , color= rangeColorAmericas, title= "Americas High")   
plot(Americaslow  , color= rangeColorAmericas, title= "Americas Low") 
bgcolor(color=activeAmericas and displayBG ? bgColorAmericas : na, title = "Americas Session Background")

////////////////Custom session
startTime1Custom = timestamp(tz, tzYear, tzMonth, tzDay, startHourCustom, startMinuteCustom)
endTime1Custom = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Custom, endMinute2Custom)
activeCustom = if startTime1Custom <= time and time <= endTime1Custom and tsCustom
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Customhigh  = na
var float Customlow   = na
bgColorCustom         = color.new(color.yellow,85)
rangeColorCustom      = activeCustom ? color.new(color.yellow,0): color.new(color.red,100)  

CustomLookback=  (ta.barssince(not activeCustom))+ 1 
if activeCustom
    
    Customhigh := ta.highest(high,CustomLookback)
    Customlow  := ta.lowest(low,CustomLookback)
    
plot(Customhigh  , color= rangeColorCustom, title= "Custom High")   
plot(Customlow  , color= rangeColorCustom, title= "Custom Low") 
bgcolor(color=activeCustom and displayBG ? bgColorCustom : na, title = "Custom Session Background")

Upvotes: 0

Views: 473

Answers (3)

sammy22
sammy22

Reputation: 40

the problem with ta.highest is actually common and here is the solution for it : instead of Americashigh := ta.highest(high,AmericasLookback)

it should be like this : Americashigh := ta.highest(high,math.max(nz(AmericasLookback),1))

same thing with all highs and lows in your script. you just add math.max between the nz(Lookback period) and "1". so if the script for whatever reason reads the Lookback as zero, it will pick "1".

Upvotes: 0

Miltown Wrench
Miltown Wrench

Reputation: 11

I did find a resolution overnight thanks for your help and suggestions< I ened up solving and causing a whole bunch of problems but these are the resolutions. I do realzie some of my default times are not accurate but that isn't terribly important. I did nest the highest lowest in a if statement as well as a second if statement to reset the values.

this was the final "logic" for the plotting. this didn't have runtime errors on any time frame and didn't have wild line plots from previous sessions values.

var float Asiahigh  = na
var float Asialow   = na
bgColorAsia         = color.new(color.red,85)
rangeColorAsia      = activeAsia ? color.new(color.red,0): color.new(color.red,100)  

AsiaLookback= nz(ta.barssince(ta.change(activeAsia))) + 1

if activeAsia
    
    Asiahigh := nz(ta.highest(high,AsiaLookback))
    Asialow  := nz(ta.lowest(low,AsiaLookback))

if not activeAsia
    Asiahigh := close[1]
    Asialow := close[1]

most of the rest of the code was left the same but clean up a bit.

Upvotes: 1

Rohit Agarwal
Rohit Agarwal

Reputation: 1368

you have put the ta.highest inside if clause we can calculate it for each candle and then while plotting we can set value to na wherever we dont need to plot (by adding line broken style)

enter image description here

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Hotchachachaaa

//@version=5
indicator("session tma",overlay=true)

tz                  ="UTC"
label               = input.string( title="Label",                  defval="CME Open", tooltip="For easy identification", group = "Trade Session")
tsAsia              = input.bool(   title="Show Asian Session",     defval=true, group = "Asian Trade Session")
startHourAsia       = input.int(    title="Asia Start hour",        defval=0,   minval=0, maxval=23,    group = "Asian Trade Session")
startMinuteAsia     = input.int(    title="Asia Start minute",      defval=00,  minval=0, maxval=59,    group = "Asian Trade Session")
endHour2Asia        = input.int(    title="Asia End hour",          defval=8,   minval=0, maxval=23,    group = "Asian Trade Session")
endMinute2Asia      = input.int(    title="Asia End minute",        defval=0,   minval=0, maxval=59,    group = "Asian Trade Session")

tsEurope            = input.bool(   title="Show European Session",  defval=true,                        group = "European Trade Session")
startHourEurope     = input.int(    title="Europen Start hour",     defval=7,   minval=0, maxval=23,    group = "European Trade Session")
startMinuteEurope   = input.int(    title="Europen Start minute",   defval=00,  minval=0, maxval=59,    group = "European Trade Session")
endHour2Europe      = input.int(    title="Europen End hour",       defval=16,  minval=0, maxval=23,    group = "European Trade Session")
endMinute2Europe    = input.int(    title="Europen End minute",     defval=0,   minval=0, maxval=59,    group = "European Trade Session")

tsAmericas          = input.bool(   title="Show American Session",  defval=true,                        group = "American Trade Session")
startHourAmericas   = input.int(    title="Americas Start hour",    defval=12,  minval=0, maxval=23,    group = "American Trade Session")
startMinuteAmericas = input.int(    title="Americas Start minute",  defval=00,  minval=0, maxval=59,    group = "American Trade Session")
endHour2Americas    = input.int(    title="Americas End hour",      defval=20,  minval=0, maxval=23,    group = "American Trade Session")
endMinute2Americas  = input.int(    title="Americas End minute",    defval=0,   minval=0, maxval=59,    group = "American Trade Session")

tsCustom            = input.bool(   title="Show Custom Session",    defval=true,                        group = "Custom Trade Session")
startHourCustom     = input.int(    title="Custom Start hour",      defval=16,  minval=0, maxval=23,    group = "Custom Trade Session")
startMinuteCustom   = input.int(    title="Custom Start minute",    defval=00,  minval=0, maxval=59,    group = "Custom Trade Session")
endHour2Custom      = input.int(    title="Custom End hour",        defval=20,  minval=0, maxval=23,    group = "Custom Trade Session")
endMinute2Custom    = input.int(    title="Custom End minute",      defval=0,   minval=0, maxval=59,    group = "Custom Trade Session")

showMon = input(title="Monday",     defval=true, group = "Trade Session")
showTue = input(title="Tuesday",    defval=true, group = "Trade Session")
showWed = input(title="Wednesday",  defval=true, group = "Trade Session")
showThu = input(title="Thursday",   defval=true, group = "Trade Session")
showFri = input(title="Friday",     defval=true, group = "Trade Session")
showSat = input(title="Saturday",   defval=false, group = "Trade Session")
showSun = input(title="Sunday",     defval=false, group = "Trade Session")

tzYear = year(time, tz)
tzMonth = month(time, tz)
tzDay = dayofmonth(time, tz)
tzDayOfWeek = dayofweek(time, tz)

displayBG   = input.bool(title="Display Session as Background Color", defval = true)
displayHiLo = input.bool(title="Display Session High/Low", defval= true)

transpcolor= color.new(color.white,100)


////////////////asia session
startTime1Asia = timestamp(tz, tzYear, tzMonth, tzDay, startHourAsia, startMinuteAsia)
endTime1Asia = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Asia, endMinute2Asia)
activeAsia = if startTime1Asia <= time and time <= endTime1Asia and tsAsia
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Asiahigh  = na
var float Asialow   = na
bgColorAsia         = color.new(color.red,85)
rangeColorAsia      = activeAsia ? color.new(color.red,0): color.new(color.red,100)  

AsiaLookback=(ta.barssince(not activeAsia)) + 1
Asiahigh := ta.highest(high,AsiaLookback)
Asialow  := ta.lowest(low,AsiaLookback)
    
plot(activeAsia?Asiahigh:na  , color= rangeColorAsia, title= "Asia High",style=plot.style_linebr)   
plot(activeAsia?Asialow:na  , color= rangeColorAsia, title= "Asia Low",style=plot.style_linebr)   
bgcolor(color=activeAsia and displayBG ? bgColorAsia : na, title = "Asia Session Background")

////////////////Europe session
startTime1Europe = timestamp(tz, tzYear, tzMonth, tzDay, startHourEurope, startMinuteEurope)
endTime1Europe = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Europe, endMinute2Europe)
activeEurope = if startTime1Europe <= time and time <= endTime1Europe and tsEurope
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Europehigh  = na
var float Europelow   = na
bgColorEurope         = color.new(color.green,85)
rangeColorEurope      = activeEurope ? color.new(color.green,0): color.new(color.red,100)  

EuropeLookback= (ta.barssince(not activeEurope))+ 1
Europehigh := ta.highest(high,EuropeLookback)
Europelow  := ta.lowest(low,EuropeLookback)
    
plot(activeEurope?Europehigh:na  , color= rangeColorEurope, title= "Europe High",style=plot.style_linebr)     
plot(activeEurope?Europelow:na  , color= rangeColorEurope, title= "Europe Low",style=plot.style_linebr)   
bgcolor(color=activeEurope and displayBG ? bgColorEurope : na, title = "Europe Session Background")
////////////////Americas session
startTime1Americas = timestamp(tz, tzYear, tzMonth, tzDay, startHourAmericas, startMinuteAmericas)
endTime1Americas = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Americas, endMinute2Americas)
activeAmericas = if startTime1Americas <= time and time <= endTime1Americas and tsAmericas
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Americashigh  = na
var float Americaslow   = na
bgColorAmericas         = color.new(color.blue,85)
rangeColorAmericas      = activeAmericas ? color.new(color.blue,0): color.new(color.red,100)  

AmericasLookback= (ta.barssince(not activeAmericas))+ 1 
Americashigh := ta.highest(high,AmericasLookback)
Americaslow  := ta.lowest(low,AmericasLookback)
    
plot(activeAmericas?Americashigh:na  , color= rangeColorAmericas, title= "Americas High",style=plot.style_linebr)   
plot(activeAmericas?Americaslow:na  , color= rangeColorAmericas, title= "Americas Low",style=plot.style_linebr)   
bgcolor(color=activeAmericas and displayBG ? bgColorAmericas : na, title = "Americas Session Background")

////////////////Custom session
startTime1Custom = timestamp(tz, tzYear, tzMonth, tzDay, startHourCustom, startMinuteCustom)
endTime1Custom = timestamp(tz, tzYear, tzMonth, tzDay, endHour2Custom, endMinute2Custom)
activeCustom = if startTime1Custom <= time and time <= endTime1Custom and tsCustom
    if tzDayOfWeek == dayofweek.monday and showMon
        true
    else if tzDayOfWeek == dayofweek.tuesday and showTue
        true
    else if tzDayOfWeek == dayofweek.wednesday and showWed
        true
    else if tzDayOfWeek == dayofweek.thursday and showThu
        true
    else if tzDayOfWeek == dayofweek.friday and showFri
        true
    else if tzDayOfWeek == dayofweek.saturday and showSat
        true
    else if tzDayOfWeek == dayofweek.sunday and showSun
        true
    else
        false
else
    false

var float Customhigh  = na
var float Customlow   = na
bgColorCustom         = color.new(color.yellow,85)
rangeColorCustom      = activeCustom ? color.new(color.yellow,0): color.new(color.red,100)  

CustomLookback=  (ta.barssince(not activeCustom))+ 1 
Customhigh := ta.highest(high,CustomLookback)
Customlow  := ta.lowest(low,CustomLookback)
    
plot(activeCustom?Customhigh:na  , color= rangeColorCustom, title= "Custom High",style=plot.style_linebr)   
plot(activeCustom?Customlow:na  , color= rangeColorCustom, title= "Custom Low",style=plot.style_linebr)   
bgcolor(color=activeCustom and displayBG ? bgColorCustom : na, title = "Custom Session Background")

Upvotes: 0

Related Questions