Announcement

Collapse
No announcement yet.

Using Global Variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Global Variables

    I have two simple test scripts -- one creates a global variable and the other reads it. On loading the chart, only the most current value of the global variable is used in the calculations, meaning that past bar computations aren't necessarily correct since I don't know the value of the global variable for past bars.

    I can't backtest as a result. I just want to make sure this behavior is correct?

    Thanks.

  • #2
    That is the expected behavior.

    There may be other ways of doing what you want without the use of globals however. What is the ultimate goal (other than making lots of money ;-)...what drove you to using variables?
    Garth

    Comment


    • #3
      I have two scripts -- one does a lot of calculations on price and the other does calculations on volume. I want to chart a measure from the volume script (which now plots in a separate window on the chart) but also use the results of the calculations in the price script.

      I would normally do all of the calcuations in one script and solve the problem. But, I can't plot both volume and price from one script since volume and price don't plot together due to scale. I don't want to convert both to a uniform scale.

      I could also just re-write the price script to include the volume calculations, and just run it and the separate volume script together -- but that seems to be wasting a lot of cpu's that I need for other stuff.

      Thanks for any thoughts.
      Last edited by AssetHound; 11-17-2009, 05:13 PM.

      Comment


      • #4
        Wanted to do almost the exact same thing - use calculations from my price study in a non-price study via a global variable since scaling was different.

        Got it to work as follows:

        Set both studies so they receive price updates the same (every new bar or every tick). In other words, both studies should call or not call setComputeOnClose() the same.

        From the price study call setGlobalValue() inside main() on each new bar/tick. If multiple values are needed (for example one value per bar), then use an array:

        PHP Code:
        .
            var 
        aMyGlobalArray = new Array();
            ... 
        populate array ...
            
        setGlobalValue ("MyGlobalArray"aMyGlobalArray ); 
        From the non-price study call getGlobalValue() inside main() on each new bar/tick. Again, you can use an array as follows:

        PHP Code:
        .
            var 
        aMyGlobalArray;
            
        aMyGlobalArray getGlobalValue ("MyGlobalArray");
            ... use array ... 
        (In my studies, I "pass" multiple global arrays between a price study and two non-price studies. Works fine. I also added the symbol and interval being charted to the global variable name so I could use the same study on different charts without them getting confused. Necessary since global variables are truly "global" and accessible from ALL studies - even different charts.)

        By default, it seems non-price studies are updated with the new price before any price studies. As a result, the non-price study that receives data via the global variable will alway be behind by one bar/tick. To fix this, you'll need to rearrange the chart/studies order as follows:

        Use "Edit Studies..." to arrange the studies so the one where the global variable is set is at the bottom of the list. Collapse "Main Chart" and all "Subchart"s (click on "-"). Then Drag "Main Chart" to the bottom. (I also usually order price studies within "Main Chart" so they are at the top and the symbol is at the bottom - this way indicators drawn by my studies are overlaid on top of the price bars and not underneath them.)

        If you do this, it will look a little weird, the non-price study will be above the chart, but it will work.

        One other caveat: If you change the study order as above all will work fine (update order wise) when you reopen the page/layout. However, the first time you add the studies or if you modify the studies you'll need to add/reload them in "reverse" order, otherwise the non-price study will be one bar behind. To do this reload the non-price study (which gets the global variable) and then the price study (which sets the global variable).
        Last edited by shortandlong; 11-18-2009, 06:45 AM.

        Comment


        • #5
          Another way I came up with to keep the scripts in synch order-wise is to include the code in both scripts and whichever script needs the calculations first determines that the global variable need to be recalculated, does it, and sets the global variable to the new value(s).

          The advantage of the above approach is the scripts can be in any order on the screen, the order they're loaded in doesn't matter, etc.

          Haven't actually tried it, though...
          Last edited by shortandlong; 11-18-2009, 06:40 AM.

          Comment


          • #6
            Hmmm. I didn't think about passing an array -- too focused on variables. I will definitely give this a try. Thanks very much.

            Comment

            Working...
            X