Viewed   4.4k times

I would like to draw a vertical line, every day at a certain local time (e.g. 08:00 GMT+1).

Since my last post about vertical lines, pine-script has been updated to include vline(), however, the issue here is getting the time right. Most servers (for FX) seem to be US based and the Trading view local time settings (shown on bottom left) seem totally independent of what is done in pine-script.

//@version=4
study("Time Adjusted Vertical Line", overlay=true)

vline(BarIndex, Color, LineStyle, LineWidth) => // Verticle Line, 54 lines maximum allowable per indicator
    return = line.new(BarIndex, -1000, BarIndex, 1000, xloc.bar_index, extend.both, Color, LineStyle, LineWidth)

if(bar_index%10==0.0)
    vline(bar_index, #FF8000ff, line.style_solid, 1) // Variable assignment not required

I couldn't get the above to work, but I got this to at least show the lines:

//@version=4
study(title="Time Based Session Bars", shorttitle="NowOpen", overlay=true)
line_height = 2    // We must define a height that reaches far above the highest price level in main chart!

gmt_offs = 2 // GMT + X
nys_offs = 6 // EST (in GMT)

t1 = time(timeframe.period, "0930-0935:23456")
//t1 = time(timeframe.isintraday, "0930-0935:23456")
//t2 = t1 + gmt_offs*60*60*60
t2 = t1 + 2
plot(na(t2) ? 0 : line_height, title='Hello!', color=#101010, linewidth=1, style=plot.style_histogram, transp=50, offset=0, trackprice=false)

However, the lines are totally wrong:

How can I extend the above code, to plot a vertical line, every day at a certain time, and get the correct time placement?


Possibly Useful links:

  • Sessions and time functions (The official Documentation)

  • Sessions_and_time_functions (The Github Repo Docs)

  • Introducing Pine-script-4


UPDATE

Thanks to PineCoders-LucF, I was able to get approximately what I wanted, with the following code:

//@version=4
study("Line at time",overlay=true)

t1 = timestamp("GMT+2", year, month, dayofmonth, 07, 00, 00)
//t2 = timestamp("GMT+2", year, month, dayofmonth, 10, 00, 00)  // Uncomment this to make a range
t2=t1                                                           // Comment out this to use a range
bgcolor( (time >= t1) and (time <= t2) ? color.silver : na, transp = 0)

However, there are two issues wit this code.

  1. It doesn't use the nice timeframe.period functionality that allow you to specify certain days and time intervals in one place.
  2. It put the line in the "middle" of a candle so if you're on the 1H chart and want a line at 08.00 it will just place it in the middle. Thus not suitable as an alarm/signal, unless you use a <= 1 minute time frame.

 Answers

4

Version 1

One version of the timestamp() function can use a timezone parameter:

//@version=4
study("Line at time", overlay=true)
targetTime = timestamp("GMT+1", year, month, dayofmonth, 08, 00, 00)
bgcolor(targetTime == time ? color.silver : na, transp = 0)

// Debugging: these plots lines in separate window
plot(targetTime, "targetTime", color.orange)
plot(time, "time")

Chart is shown with UTC+1 times and indicator is set to "No Scale" not to disrupt price scale:

Version 2

With this version you can choose:

  • A from/to hour range
  • To show the line on weekdays only
  • Between bgcolor or vline mode
//@version=4
study("Line at time",overlay=true)
fromHour = input(7)
toHour = input(10)
weekdaysOnly = input(true)
useVline = input(false)
dayIsOk = not weekdaysOnly or (dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday)
t1 = timestamp("GMT+2", year, month, dayofmonth, fromHour, 00, 00)
t2 = timestamp("GMT+2", year, month, dayofmonth, toHour, 00, 00)
timeIsOk = (time >= t1) and (time <= t2)
bgcolor( not useVline and timeIsOk and dayIsOk ? color.orange : na, transp = 80)
if useVline and timeIsOk and dayIsOk
    line.new(bar_index, low * .9999, bar_index, high * 1.0001, xloc.bar_index, extend.both, #FF8000ff, line.style_solid, 1)

Saturday, October 8, 2022
 
noobob
 
4

This answer is now outdated. Please consult other more recent answers.


There is no way to print text or variables in pine-script to any form of "console". But you can use plot to print, very short text above or below each tick. The text, however, is static and you cannot use variables to change it.

You can also use various tricks to show values in the very limited indicator fields (on top-left) of the chart window. Or move lines and plots out of visible are of chart, but still see the axis highlights.

Please check their Wiki and consult the 1000's of other user's scripts to find out the details on how to do it.

Friday, December 2, 2022
 
ddn
 
ddn
2

Solution below.
I was able to remove my f_str2float() and f_str2int() functions, because yesterday they released a new built-in function tonumber().
Script looks cleaner now.

//@version=4
study("Lines", "LI", true)

int             i_max_lines         = input(10,    "Max number of lines drawn", minval=1, maxval=20)
bool            i_extend_lines      = input(false, "Extend lines")

var string[]    a_ticker            = array.new_string()
var int[]       a_x1                = array.new_int()
var int[]       a_x2                = array.new_int()
var float[]     a_y1                = array.new_float()
var float[]     a_y2                = array.new_float()
var string[]    a_str               = array.new_string()

var string[]    a_str_set_ticker    = array.new_string()
var string[]    a_str_set_x1        = array.new_string()
var string[]    a_str_set_x2        = array.new_string()
var string[]    a_str_set_y1        = array.new_string()
var string[]    a_str_set_y2        = array.new_string()

var line[]      a_lines             = array.new_line()

// Initialize the data
f_init_data() =>
    // 4000 tickers, broken down into 5 sets, because a string has a max size of 4096 characters.
    // 800 tickers, set 1
    array.push(a_str_set_ticker, "QQQ,QQQ,QQQ,AMZN,AMZN")
    array.push(a_str_set_x1, "20,30,40,20,30")
    array.push(a_str_set_x2, "2,3,4,2,3")
    array.push(a_str_set_y1, "267,300,222,3000,2000")
    array.push(a_str_set_y2, "338.2,200,111,2500,1000")

// Initialize the lines
f_init_lines() =>
    for x = 0 to i_max_lines-1
        array.push(a_lines, line.new(na, na, na, na, extend=i_extend_lines ? extend.both : extend.none))

// Draw lines
f_draw_lines() =>
    if array.includes(a_ticker, syminfo.ticker)
        var int[]   subarray_x1 = array.new_int()
        var int[]   subarray_x2 = array.new_int()
        var float[] subarray_y1 = array.new_float()
        var float[] subarray_y2 = array.new_float()
        var int     x1          = na
        var int     x2          = na
        var float   y1          = na
        var float   y2          = na

        // Get elements with current ticker and put in a subarray.
        for x = array.indexof(a_ticker, syminfo.ticker) to array.lastindexof(a_ticker, syminfo.ticker)
            if array.get(a_ticker, x) == syminfo.ticker
                array.push(subarray_x1, array.get(a_x1, x))
                array.push(subarray_x2, array.get(a_x2, x))
                array.push(subarray_y1, array.get(a_y1, x))
                array.push(subarray_y2, array.get(a_y2, x))

        // Draw all lines in the subarray.
        for x = 0 to min(array.size(subarray_x1),i_max_lines)-1
            x1 := array.get(subarray_x1, x)
            x2 := array.get(subarray_x2, x)
            y1 := array.get(subarray_y1, x)
            y2 := array.get(subarray_y2, x)
            line.set_xy1(array.get(a_lines, x), bar_index - x1, y1)
            line.set_xy2(array.get(a_lines, x), bar_index - x2, y2)     


if barstate.islast
    // Initialization
    f_init_data()
    f_init_lines()

    // Process the data
    for z = 0 to array.size(a_str_set_ticker)-1
        a_ticker := array.concat(str.split(array.get(a_str_set_ticker, z),","), a_ticker)
    
        a_str := str.split(array.get(a_str_set_x1, z),",")
        for i = 0 to array.size(a_str)-1
            array.push(a_x1,int(tonumber(array.get(a_str,i))))
    
        a_str := str.split(array.get(a_str_set_x2, z),",")
        for i = 0 to array.size(a_str)-1
            array.push(a_x2,int(tonumber(array.get(a_str,i))))
    
        a_str := str.split(array.get(a_str_set_y1, z),",")
        for i = 0 to array.size(a_str)-1
            array.push(a_y1,tonumber(array.get(a_str,i)))
    
        a_str := str.split(array.get(a_str_set_y2, z),",")
        for i = 0 to array.size(a_str)-1
            array.push(a_y2,tonumber(array.get(a_str,i)))
            
    // Draw lines
    f_draw_lines()

Edit 1
In response to this comment.
It seems there was a problem with the array.concat() function, which inverted the ticker sequence. I solved it by processing the tickers the same way as the x/y values.
Also added a debug checkbox in the inputs, to show the content of the arrays.

//@version=4
study("Lines", "LI", true)

int             i_max_lines         = input(10,    "Max number of lines drawn", minval=1, maxval=20)
bool            i_extend_lines      = input(false, "Extend lines")
bool            i_debug             = input(false, "Show debug info")

var string[]    a_ticker            = array.new_string()
var int[]       a_x1                = array.new_int()
var int[]       a_x2                = array.new_int()
var float[]     a_y1                = array.new_float()
var float[]     a_y2                = array.new_float()

var string[]    a_str_set_ticker    = array.new_string()
var string[]    a_str_set_x1        = array.new_string()
var string[]    a_str_set_x2        = array.new_string()
var string[]    a_str_set_y1        = array.new_string()
var string[]    a_str_set_y2        = array.new_string()

var line[]      a_lines             = array.new_line()

// Initialize the data
f_init_data() =>

    //SET 1
    // array.push(a_str_set_ticker, "TSLA,TSLA,TSLA,SPY,SPY,SPY,SPY,AAPL,AAPL,AAPL")
    // array.push(a_str_set_x1, "2,2,2,2,2,2,2,2,2,2")
    // array.push(a_str_set_x2, "1,1,1,1,1,1,1,1,1,1")
    // array.push(a_str_set_y1, "872.94,874.37,643.0,392.01,391.88,378.81,374.8,130.65,131.07,120.1")
    // array.push(a_str_set_y2, "872.47,873.18,659.51,391.8,391.52,378.23,375.16,129.89,130.36,121.2")
    
    //SET 2
    array.push(a_str_set_ticker, "TSLA,TSLA,TSLA,SPY")
    array.push(a_str_set_x1, "2,2,2,2")
    array.push(a_str_set_x2, "1,1,1,1")
    array.push(a_str_set_y1, "872.94,874.37,643.0,392.01")
    array.push(a_str_set_y2, "872.47,873.18,659.51,391.8")
    
    array.push(a_str_set_ticker, "SPY,SPY,SPY,AAPL,AAPL,AAPL")
    array.push(a_str_set_x1, "2,2,2,2,2,2")
    array.push(a_str_set_x2, "1,1,1,1,1,1")
    array.push(a_str_set_y1, "391.88,378.81,374.8,130.65,131.07,120.1")
    array.push(a_str_set_y2, "391.52,378.23,375.16,129.89,130.36,121.2")


// Initialize the lines
f_init_lines() =>
    for x = 0 to i_max_lines-1
        array.push(a_lines, line.new(na, na, na, na, extend=i_extend_lines ? extend.both : extend.none))

// Draw lines
f_draw_lines() =>
    if array.includes(a_ticker, syminfo.ticker)
        var int[]   subarray_x1 = array.new_int()
        var int[]   subarray_x2 = array.new_int()
        var float[] subarray_y1 = array.new_float()
        var float[] subarray_y2 = array.new_float()
        var int     x1          = na
        var int     x2          = na
        var float   y1          = na
        var float   y2          = na

        // Get elements with current ticker and put in a subarray.
        for x = array.indexof(a_ticker, syminfo.ticker) to array.lastindexof(a_ticker, syminfo.ticker)
            if array.get(a_ticker, x) == syminfo.ticker
                array.push(subarray_x1, array.get(a_x1, x))
                array.push(subarray_x2, array.get(a_x2, x))
                array.push(subarray_y1, array.get(a_y1, x))
                array.push(subarray_y2, array.get(a_y2, x))

        // Draw all lines in the subarray.
        for x = 0 to min(array.size(subarray_x1),i_max_lines)-1
            x1 := array.get(subarray_x1, x)
            x2 := array.get(subarray_x2, x)
            y1 := array.get(subarray_y1, x)
            y2 := array.get(subarray_y2, x)
            line.set_xy1(array.get(a_lines, x), bar_index - x1, y1)
            line.set_xy2(array.get(a_lines, x), bar_index - x2, y2)     

// Process the data
f_process_data() =>
    for z = 0 to array.size(a_str_set_ticker)-1
        for i = 0 to array.size(str.split(array.get(a_str_set_ticker, z),","))-1
            array.push(a_ticker,              array.get(str.split(array.get(a_str_set_ticker, z),","),i)    )
            array.push(a_x1,     int(tonumber(array.get(str.split(array.get(a_str_set_x1,     z),","),i)))  )
            array.push(a_x2,     int(tonumber(array.get(str.split(array.get(a_str_set_x2,     z),","),i)))  )
            array.push(a_y1,         tonumber(array.get(str.split(array.get(a_str_set_y1,     z),","),i))   )
            array.push(a_y2,         tonumber(array.get(str.split(array.get(a_str_set_y2,     z),","),i))   )

// Debug info
f_debug(_show) =>
    if _show
        label_text = "a_ticker=" + tostring(a_ticker) + "n" + "a_x1=" + tostring(a_x1) + "n" + "a_x2=" + tostring(a_x2) + "n" + "a_y1=" + tostring(a_y1) + "n" + "a_y2=" + tostring(a_y2)
        label.new(bar_index, high, label_text) 


if barstate.islast
    // Initialization
    f_init_data()
    f_init_lines()

    // Process the data
    f_process_data()
    
    // Draw lines
    f_draw_lines()

    // Show debug info
    f_debug(i_debug)    

Edit 2
In response to this comment.
Loops made smaller to avoid Loop takes too long to execute error.
Please update the f_process_data() function to the one below.

// Process the data
f_process_data() =>
    var string[]    a_str = array.new_string()
    
    for z = 0 to array.size(a_str_set_ticker)-1
        a_size  = array.size(str.split(array.get(a_str_set_ticker, z),","))
        
        a_str := str.split(array.get(a_str_set_ticker, z),",")
        for i = 0 to a_size-1
            array.push(a_ticker,              array.get(a_str,i)    )

        a_str := str.split(array.get(a_str_set_x1, z),",")
        for i = 0 to a_size-1
            array.push(a_x1,     int(tonumber(array.get(a_str,i)))  )

        a_str := str.split(array.get(a_str_set_x2, z),",")
        for i = 0 to a_size-1
            array.push(a_x2,     int(tonumber(array.get(a_str,i)))  )

        a_str := str.split(array.get(a_str_set_y1, z),",")
        for i = 0 to a_size-1
            array.push(a_y1,         tonumber(array.get(a_str,i))   )

        a_str := str.split(array.get(a_str_set_y2, z),",")
        for i = 0 to a_size-1
            array.push(a_y2,         tonumber(array.get(a_str,i))   )
Saturday, August 20, 2022
 
4

You have to find out the math behind that figures. The triangle and the star are quite easy to draw. Here is how you can draw a heart: http://www.mathematische-basteleien.de/heart.htm

To draw special paths you should create them by adding points, ellipses etc. The canvas supports a clipping mask of a specified path, so you can set the clipping mask of a heart, push the paths to the matrix, draw the content of the heart, and then pop it again.

That's what I'm doing to achieve a simulated 2D page curl effect on andriod: http://code.google.com/p/android-page-curl/

Hope this helps!

Monday, October 3, 2022
5

This should give you one event every millisecond. You can use a stopwatch to measure the elapsed time. Trigger an event on the main UI thread by using invoke so you don't block the timer.

    public delegate void TimerEventHandler(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2);

    /// <summary>
    /// A multi media timer with millisecond precision
    /// </summary>
    /// <param name="msDelay">One event every msDelay milliseconds</param>
    /// <param name="msResolution">Timer precision indication (lower value is more precise but resource unfriendly)</param>
    /// <param name="handler">delegate to start</param>
    /// <param name="userCtx">callBack data </param>
    /// <param name="eventType">one event or multiple events</param>
    /// <remarks>Dont forget to call timeKillEvent!</remarks>
    /// <returns>0 on failure or any other value as a timer id to use for timeKillEvent</returns>
    [DllImport("winmm.dll", SetLastError = true,EntryPoint="timeSetEvent")]
    static extern UInt32 timeSetEvent(UInt32 msDelay, UInt32 msResolution, TimerEventHandler handler, ref UInt32 userCtx, UInt32 eventType);

    /// <summary>
    /// The multi media timer stop function
    /// </summary>
    /// <param name="uTimerID">timer id from timeSetEvent</param>
    /// <remarks>This function stops the timer</remarks>
    [DllImport("winmm.dll", SetLastError = true)]
    static extern void timeKillEvent(  UInt32 uTimerID );

    TimerEventHandler tim = new TimerEventHandler(this.Link);
    public void Link(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2)
    {
        _counter++;
        if( (_counter % 10 ) == 0)
            setLblTxt();
    }
Wednesday, September 28, 2022
 
pr0t0
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :