简体   繁体   中英

How to run only once a pine script function?

here is a short code, his purpose in tradingview (thanks to pine script editor) is to draw a minimum line when the first argument is -1 and a maximum one when it is 1理论上我想要红线 在此处输入图像描述 在此处输入图像描述 我实际上有什么

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

//@version=4
study("Mon Script",overlay=true)
tunnel(n,f,long) =>
    if n==-1
        indref=0
        ind=indref+1
        pente=f[indref]-f[indref+1]
        for i=2 to long
            if  (f[indref]-f[indref+i])/i>pente
                pente := (f[indref]-f[indref+i])/i
                ind :=indref+i
        line.new(x1=bar_index[indref],y1=f[indref],x2=bar_index[ind],y2=f[ind],color=#FF5733 )
    else
        indref=0
        ind=indref+1
        pente=f[indref]-f[indref+1]
        for i=2 to long
            if  (f[indref]-f[indref+i])/i<pente
                pente := (f[indref]-f[indref+i])/i
                ind :=indref+i
        line.new(x1=bar_index[indref],y1=f[indref],x2=bar_index[ind],y2=f[ind],color=#FF5733 )


tunnel(-1,close[0],6)

the line is relative to the last 6 points of the f function

But my problem is the fact that it repeat the process for all the point and not only for the first one as I should expect, so how should I do to success?

Thank you for updating your question. It's more clear now.
Is this what you're looking for?

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

var lookBack = input(defval = 6, title = "LookBack bars", type = input.integer)

// Define lines
var hiLine = line.new(0, 0, 0, 0, color=color.yellow)
var loLine = line.new(0, 0, 0, 0, color=color.yellow)

hiBarOffset = 0 - highestbars(high, lookBack) // high can be omitted, because it's default. Returned values are negative, so flip sign.
loBarOffset = 0 - lowestbars(low, lookBack)   // low can be omitted, because it's default. Returned values are negative, so flip sign.

if barstate.islast
    // Redraw high line
    line.set_xy1(hiLine, bar_index[hiBarOffset], high[hiBarOffset])
    line.set_xy2(hiLine, bar_index, high)

    // Redraw low line
    line.set_xy1(loLine, bar_index[loBarOffset], low[loBarOffset])
    line.set_xy2(loLine, bar_index, low)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM