如何仅运行一次pine脚本功能?

问题描述 投票:0回答:1

这是一个简短的代码,他在tradingview中的目的(感谢pine脚本编辑器)是在第一个参数为-1时画一条最小线,在第一个参数为1时画一个最大线。in theory i want the red lineenter image description herewhat i have actually

// 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)

该线相对于f函数的最后6个点

但是我的问题是,它重复了整个过程,不仅重复了我应该期望的第一个过程,所以我应该如何做才能成功?

function pine-script trading
1个回答
0
投票

感谢您更新您的问题。现在更清楚了。这是您要找的东西吗?

//@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)
© www.soinside.com 2019 - 2024. All rights reserved.