Announcement

Collapse
No announcement yet.

Global Vars losing values

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

  • Global Vars losing values

    Hi All,

    I've got a simple formula that is supposed to assign a value to a global variable unless the value is null (therefore the global variable will always equal something). However, for some reason the global var seems to be changing to null on each run through. I cannot figure out why it isn't holding it's value! Any help would be much appreciated.

    In the below example NextSupport and NextResistance should always equal something, I cannot work out why they are being set back to null at any stage!

    Cheers,
    - Willl

    Here is the code:
    //Configuration Variables
    var DistanceToResistanceToEnter = 0.02;
    var StopValue = 2;

    //Global Variables
    var NextResistance = null; //CURRENT RESISTANCE POINT
    var NextSupport = null; //CURRENT SUPPORT POINT
    var TradePrice = null;
    var vLastAlert = -1;

    //PreMain
    function preMain(){
    }

    function main()
    {
    //Load studies
    var UpMagnet = efsExternal("/Advanced/Arps Crown Jewels/Arps Price Magnets - UP.efs");
    UpMagnet1 = getSeries(UpMagnet, 0)
    var DownMagnet = efsExternal("/Advanced/Arps Crown Jewels/Arps Price Magnets - DN.efs");
    DownMagnet1 = getSeries(DownMagnet, 0)

    //Load variables that change

    debugPrintln("UpMagnet1: " + UpMagnet1);
    debugPrintln("DownMagnet1: " + DownMagnet1);

    if(DownMagnet1 == 0 || DownMagnet1 == null)
    {
    debugPrintln("NextSupport didn't change: " + DownMagnet1 + "(" + NextSupport + ")")
    } else {
    debugPrintln("NextSupport changing to: " + DownMagnet1 + "(" + NextSupport + ")")
    NextSupport = DownMagnet1
    }
    if(UpMagnet1 == 0 || UpMagnet1 == null)
    {
    debugPrintln("NextResistance didn't change: " + UpMagnet1 + "(" + NextResistance + ")")
    } else {
    debugPrintln("NextResistance changing to: " + UpMagnet1 + "(" + NextResistance + ")")
    NextResistance = UpMagnet1
    }

    debugPrintln("NextSupport: " + NextSupport);
    debugPrintln("NextResistance: " + NextResistance);
    }


    Here is the output:
    NextResistance: null
    NextSupport: null
    NextResistance didn't change null(null)
    NextSupport didn't change null(null)
    DownMagnet1: null
    UpMagnet1: null
    NextResistance: 0.70736
    NextSupport: 0.70463
    NextResistance changing to: 0.70736 (0.70736)
    NextSupport changing to: 0.70463 (0.70463)
    DownMagnet1: 0.70463
    UpMagnet1: 0.70736
    Last edited by willk; 06-01-2009, 06:55 PM.

  • #2
    Hi willk,

    Try running your code through JSLint, a link is below.

    The declaration of the efs2 series only has to happen once. Try surrounding the declaration in a bInit conditional. You will see many examples of this in the forum. Look at efs2 links in my signature below as well.

    Hope this helps

    Comment


    • #3
      Hi Steve,

      Thanks for your reply. I ran the code through JSLint and although it reports a few missing ;'s and such (which are actually unnecessary through EFS anyhow) I've fixed those up. But the end result hasn't changed.

      I changed it to this:

      //Configuration Variables
      var DistanceToResistanceToEnter = 0.02;
      var StopValue = 2;

      //Global Variables
      var NextResistance = null; //CURRENT RESISTANCE POINT
      var NextSupport = null; //CURRENT SUPPORT POINT
      var TradePrice = null;
      var vLastAlert = -1;
      var UpMagnet1 = null;
      var DownMagnet1 = null;
      var UpMagnet = null;
      var DownMagnet = null;

      //PreMain
      function preMain(){
      }

      function main()
      {
      //Load studies
      if (UpMagnet === null)
      {
      UpMagnet = efsExternal("/Advanced/Arps Crown Jewels/Arps Price Magnets - UP.efs");
      UpMagnet1 = getSeries(UpMagnet, 0);
      }

      if (DownMagnet === null)
      {
      DownMagnet = efsExternal("/Advanced/Arps Crown Jewels/Arps Price Magnets - DN.efs");
      DownMagnet1 = getSeries(DownMagnet, 0);
      }

      //Load variables that change

      debugPrintln("UpMagnet1: " + UpMagnet1);
      debugPrintln("DownMagnet1: " + DownMagnet1);

      if(DownMagnet1 === 0 || DownMagnet1 === null)
      {
      debugPrintln("NextSupport didn't change: " + DownMagnet1 + "(" + NextSupport + ")");
      } else {
      debugPrintln("NextSupport changing to: " + DownMagnet1 + "(" + NextSupport + ")");
      NextSupport = DownMagnet1;
      }
      if(UpMagnet1 === 0 || UpMagnet1 === null)
      {
      debugPrintln("NextResistance didn't change: " + UpMagnet1 + "(" + NextResistance + ")");
      } else {
      debugPrintln("NextResistance changing to: " + UpMagnet1 + "(" + NextResistance + ")");
      NextResistance = UpMagnet1;
      }

      debugPrintln("NextSupport: " + NextSupport);
      debugPrintln("NextResistance: " + NextResistance);
      }
      Which ensures that the declaration should only be assigned once as per your suggestion, however, it still NULL's out NextSupport if DownMagnet1 is equal to NULL. I really don't understand it.

      The debugging shows that if DownMagnet1 is equal to null it's not actually getting to 'NextSupport = DownMagnet1;' which is correct, so it's not a logic problem. The only other place that NextSupport is assigned a value is in the initial declaration of the global variable.

      It makes no sense that once NextSupport is assigned a value it is possible for it to be equal to null again.

      Any ideas?

      Thanks,
      - Will

      Comment


      • #4
        Will,

        Your welcome.

        To obtain a value from a series object, as described in the efs2 links, you need to use the method of the series object getValue(i), where i is the index (typically 0).

        Check the efs2 links, they have taken hundreds of hours to put together. If the getValue() method does not work out, try surrounding the series objects declarations with a bInit conditional instead of your null checks.

        As for the missing semicolons, this is a rule that is not absolute (well documented, like several others). Follow it is at your own risk.

        You seem pretty close, hopefully this is sufficient to get you there, let me know.

        Comment


        • #5
          Hi Steve,

          Okay, thanks for that. I didn't realise that by doing this:
          DownMagnet = efsExternal("/Advanced/Arps Crown Jewels/Arps Price Magnets - DN.efs");
          DownMagnet1 = getSeries(DownMagnet, 0);
          NextSupport = DownMagnet1;

          I was setting NextSupport to link to whatever the value of DownMagnet1 was, rather than actually setting NextSupport to the value of DownMagnet1.

          Therefore the simple change:
          NextSupport = DownMagnet1.getValue(0);

          Did the trick as NextSupport is then equal to the value of DownMagnet1 for the current bar.

          Thanks!
          - Will

          Comment


          • #6
            Hi Will,

            Your most welcome.

            Just a note that once you create a series object (and assign to a global variable) that you do not have to continue to re-create it. That can lead to inefficiencies in your code. The EFS engine updates series objects for you. All you have to do is use the series object method to obtain the most recent value.

            Comment


            • #7
              Thanks Steve. I think I had that right with where it only assigns the series to the variable if the efsExternal var is null e.g.:

              if (DownMagnet === null)
              {
              DownMagnet = efsExternal("/Advanced/Arps Crown Jewels/Arps Price Magnets - DN.efs");
              DownMagnet1 = getSeries(DownMagnet, 0);
              }

              If that isn't right let me know.
              Cheers,
              - Will

              Comment

              Working...
              X