Animesh Shah
Animesh Shah

Reputation: 136

how to plot lines from start to end of the timeframe specified

I am writing a code to plot weekly high lows and based on that trying to get range. Problem is while plotting lines what should be referenced as x1 and x2 so that lines are plotted from start to end of the timeframe for eg. start of the week to end of the week.

Here is my code. I have inputted some barindex but it plots lines backward.Help is appreciated. Thanks

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

//@version=4
study("Range" , overlay = true)

_1              = input(title = "═════ TimeFrame Settings ═════",    type = input.bool,          defval = true)
i_resolution    = input(title = "Select Timeframe",       type = input.resolution,    defval ="W")

tfHighLow() =>
    float   _highestHigh    =   na
    float   _lowestLow      =   na
    
    _highestHigh    :=  highest(high,2)
    _lowestLow      :=  lowest(low,2)
    [_highestHigh, _lowestLow]
    
[_hH,_lL]   =   security(syminfo.tickerid, expression = tfHighLow(), resolution = i_resolution, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on )

// Values to plot
_h  =   _hH[1]
_l  =   _lL[1]
_r  =   (_h  - _l)
_t1 =   _lL +   _r*1.382
_t2 =   _lL +   _r*1.618
_m  =   (_h +   _l)/2
_s1 =   _hH -   _r*1.382
_s2 =   _hH -   _r*1.618

// lines to plot

var line    _Lh     =   na
var line    _Ll     =   na
var line    _Lt1    =   na
var line    _Lt2    =   na
var line    _Lm     =   na
var line    _Ls1    =   na
var line    _Ls2    =   na

bool    newSess     =   change(time(i_resolution)) > 0

if  newSess and bar_index > 1
    _Lh     :=  line.new(x1=bar_index[20], y1=_h, x2=bar_index[0], y2 = _h, xloc = xloc.bar_index, color= color.teal)

Upvotes: 1

Views: 263

Answers (1)

Animesh Shah
Animesh Shah

Reputation: 136

Did some R&D and improved some problems in code and following is the script

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

//@version=4
study("Range" , overlay = true)

_1              = input(title = "═════ TimeFrame Settings ═════",    type = input.bool,          defval = true)
i_resolution    = input(title = "Select Timeframe",       type = input.resolution,    defval ="W")

tfHighLow() =>
    float   _highestHigh    =   na
    float   _lowestLow      =   na
    
    _highestHigh    :=  high[1]
    _lowestLow      :=  low[1]
    [_highestHigh, _lowestLow]
    
[_hH,_lL]   =   security(syminfo.tickerid, expression = tfHighLow(), resolution = i_resolution, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on )

// Values to plot
_h  =   _hH
_l  =   _lL
_r  =   (_h  - _l)
_t1 =   _lL +   _r*1.382
_t2 =   _lL +   _r*1.618
_m  =   (_h +   _l)/2
_s1 =   _hH -   _r*1.382
_s2 =   _hH -   _r*1.618

// lines to plot

var line    _Lh     =   na
var line    _Ll     =   na
var line    _Lt1    =   na
var line    _Lt2    =   na
var line    _Lm     =   na
var line    _Ls1    =   na
var line    _Ls2    =   na



if  _m[1]   !=  _m
    line.set_x2(_Lh, bar_index)
    line.set_x2(_Ll, bar_index)
    line.set_x2(_Lt1, bar_index)
    line.set_x2(_Lt2, bar_index)
    line.set_x2(_Lm, bar_index)
    line.set_x2(_Ls1, bar_index)
    line.set_x2(_Ls2, bar_index)
    _Lm     :=  line.new(bar_index, _m, bar_index,  _m, xloc = xloc.bar_index, extend= extend.none,color= color.black, width=2)
    _Lh     :=  line.new(bar_index, _h, bar_index,  _h, xloc = xloc.bar_index, extend= extend.none,color= color.teal)
    _Ll     :=  line.new(bar_index, _l, bar_index,  _l, xloc = xloc.bar_index, extend= extend.none,color= color.red)
    _Lt1    :=  line.new(bar_index, _t1, bar_index,  _t1, xloc = xloc.bar_index, extend= extend.none,color= color.teal, style =line.style_dashed)
    _Lt2    :=  line.new(bar_index, _t2, bar_index,  _t2, xloc = xloc.bar_index, extend= extend.none,color= color.teal, style = line.style_dashed)
    _Ls1    :=  line.new(bar_index, _s1, bar_index,  _s1, xloc = xloc.bar_index, extend= extend.none,color= color.red, style = line.style_dashed)
    _Ls2    :=  line.new(bar_index, _s2, bar_index,  _s2, xloc = xloc.bar_index, extend= extend.none,color= color.red, style = line.style_dashed)
    
    
     
     
if  not na(_Lm) and line.get_x2(_Lm)    !=  bar_index
    line.set_x2(_Lh, bar_index)
    line.set_x2(_Ll, bar_index)
    line.set_x2(_Lt1, bar_index)
    line.set_x2(_Lt2, bar_index)
    line.set_x2(_Lm, bar_index)
    line.set_x2(_Ls1, bar_index)
    line.set_x2(_Ls2, bar_index)

Upvotes: 1

Related Questions