如何从 pine 脚本中的函数更改全局变量? [英] How to Change global variable from function in pine script?

查看:375
本文介绍了如何从 pine 脚本中的函数更改全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本来获得 9 个级别的江恩平方.我已经完成了另一种语言,但无法理解这里的松树脚本,它说无法修改函数中的全局变量.有什么解决方案可以获取这里的值是我的脚本

i am trying to write a script to get gann square of 9 levels. I have done it another languages but cant understand the pine script here it says Cannot modify global variable in function. Is there any solution to get the value here is my script

//@version=4
study(title="Volume-weighted average example", max_bars_back=5000, overlay=true)
timeDiff = time - time[4]

// Translate that time period into seconds
diffSeconds = timeDiff / 1000

// Output calculated time difference
//plot(series=diffSeconds)
var ln = 0
var wdvaltrg = 0.0

WdGann(price) =>
    for i = 1 to 8
        wdvaltrg := (ln+(1/i))*(ln+(1/i))
        if wdvaltrg >= price
            break
    if wdvaltrg < price
        ln := ln+1
        WdGann(price)

var vwap0935 = 0.0
v = vwap
if hour == 9 and minute == 35
    vwap0935 := v



plot(vwap0935)

推荐答案

自 2020 年 9 月 10 日起,数组在 pine 中可用.使用它,您可以将在函数中创建的值存储在全局范围内.

Since September 10, 2020 arrays are available in pine. And using that you can store values created in function in global scope.

这是有效的,因为写入数组元素不会改变实际数组变量的引用.因此,您只需使用全局数组并修改其内容,就像您在函数之外所做的那样.

This works, because writing array elements does not alter the actual array variable's reference. So you just use the global array and modify its content as you would do outside of your function.

数组在松树脚本中打开了很多可能性.

Arrays are opened a lot of possibilities in pine script.

一个简单的例子:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wallneradam
//@version=4
study("Global variables", overlay=false)

// Declare constants to access global variables
IDX_STOCH = 0
IDX_RSI = 1

// Initialize an empty array to store variables
global = array.new_float(2)

// This is the modify the array
calculate(period) =>
    v_stoch = stoch(close, high, low, period)
    v_rsi = rsi(close, period)
    array.set(global, IDX_STOCH, v_stoch)
    array.set(global, IDX_RSI, v_rsi)
     
// Call the function any times you want
calculate(14)
// Plot the results
plot(array.get(global, IDX_STOCH), color=color.red)
plot(array.get(global, IDX_RSI), color=color.yellow)
// Call the function any times you want
calculate(14 * 5)
// Plot the results
plot(array.get(global, IDX_STOCH), color=color.maroon)
plot(array.get(global, IDX_RSI), color=color.olive)

这篇关于如何从 pine 脚本中的函数更改全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆