我正在尝试编写一个脚本来获得 9 个级别的江恩平方。我已经用其他语言完成了它,但无法理解此处的 pine 脚本,它说无法修改函数中的全局变量。有什么解决方案可以获取这里的值是我的脚本
//@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 中可用。使用它,您可以在全局范围内存储在函数中创建的值。
这是可行的,因为写入数组元素不会改变实际数组变量的引用。因此,您只需使用全局数组并修改其内容,就像在函数之外执行的操作一样。
数组为 pine 脚本开启了许多可能性。
一个简单的例子:
// 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)