10

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)
Mijanur Rahaman
  • 353
  • 1
  • 5
  • 19

2 Answers2

18

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 have opened many possibilities in pine script.

A simple example:

// 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)
juanmirocks
  • 4,786
  • 5
  • 46
  • 46
Adam Wallner
  • 2,292
  • 23
  • 20
  • 1
    AFAIK you need to use the keyword `var` in order to tell the compiler to only initialize the `global` variable in your example above once. Otherwise every script iteration across bars will change the value again. – xh3b4sd Dec 05 '20 at 21:06
  • 2
    @xh3b4sd It depends on what you want to achieve. In the example above you want to calculate some indicators in every timesteps, so `var` is not good here. Though if you want to store variables accross timesteps, then var is used. But the question was about modifying global variables inside functions. This method with arrays will work both normal (without var) and persistent (with var) array variables. – Adam Wallner Dec 05 '20 at 21:39
  • Thanks for the excellent answer. I was just looking for a solution to this. It's unfortunate that it's a bit clunky - like having to store separate constants if you want to give a name to the array indices - but it's far better than not being able to do it at all. I might actually try writing a set of utility functions to help with this, where it maintains two arrays: a set of variable names and a set of variable values, and then you can dynamically get/set/update those variables from within your functions. – Simon East Jul 09 '21 at 10:06
  • @SimonEast This is exactly what I do as well. I have setters and getters, and it is not that hard to use. It was just a short overview of my idea. – Adam Wallner Jul 09 '21 at 11:27
0

Just define the var in your function body.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '23 at 00:41