Announcement

Collapse
No announcement yet.

JavaScript Null values and setting up values to work in main as well as functions

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

  • JavaScript Null values and setting up values to work in main as well as functions

    Wowsers, JavaScript is so lame, in the 1980's, when i started as a young programmer, i cut my tooth on Cobol, C, Basic, Fortran and RPG languages, they were better formed than JavaScript, I cant believe that when declaring variables, one has to do a null test prior to use, fair enough but if one passes a variable parameter into a function,, it then needs to be tested again prior to use or one will get strange programmatic behaviour, tsk tsk indeed, sloppy beatniks who designed this

    but on the other hand, once, one gets to know the peculiarities of the language, its works a treat and is so easy to use, fun indeed,

    So, for all new programmers to eSignal, remember to declare and test your variables wherever they are used, don't assume that as they were declared elsewhere in the program they will work within a called function, nope, you will need to re-declare all variables used again, within the function, to make sure it all works as planned,
    Last edited by Ainsley; 07-19-2023, 04:54 PM.

  • #2
    Thanks Ainsley, For this this very useful information. I still got confused regarding local and global variables (when to use local and when global) because study run again under premain() on every new tick or new bar (when compute on close() used). I am not a programmer but trying to code which is required for my algo trading.

    Lucky
    [email protected]

    Comment


    • #3
      try the below code to see how a varialbe is passed within the program and its own called functions

      function preMain() {
      }
      function CheckSomthing(Green) {
      // even though the variable has been tested within the main section of the code, it will need to be retested within the function or else you will get strange things happening
      if (Green == null) {
      Green = 10;
      } else {
      // do nothing as the variable is not null
      }
      if ((Green) = 10) {
      Alert.email("Awsome Green has valid content ", "Green is 10");
      }
      }
      // Declare global varialbles (global to the program only, not outside of the program
      var Green = null;
      function main() {
      if (Green == null) {
      Green = 10;
      } else {
      // do nothing as the variable is not null
      }
      // call funciton CheckSomething
      CheckSomthing(Green);
      }
      function postMain() {

      // clear data variables etc
      Green = 0;
      }?

      Comment


      • #4
        and here is a version of the code but with a Global Variable used, this can be used outside of the program ie if you want to pass some details to another program

        function preMain() {
        }
        function CheckSomthing(Green) {
        // even though the variable has been tested within the main section of the code, it will need to be retested within the function or else you will get strange things happening
        if (Green == null) {
        Green = 10;
        } else {
        // do nothing as the variable is not null
        }
        // yup, test it again if you call it within the funciton
        CurrentMessage = 2;
        removeGlobalValue("GlobalCurrentMessage");
        setGlobalValue("GlobalCurrentrMessage", CurrenMessage);
        CurrentMessage = getGlobalValue("GlobalCurrentMesage");
        if ((CurrentMessage) == null) {
        CurrentMessage = getGlobalValue("GlobalCurrentMessage");
        } else {
        CurrentMessage = getGlobalValue("GlobalCurrentMessage");
        }
        if ((Green) = 10) {
        Alert.email("Awsome Green has valid content ", "Green is 10");
        }
        }
        // Declare global varialbles (global to the program only, not outside of the program
        var Green = null;
        function main() {
        if (Green == null) {
        Green = 10;
        } else {
        // do nothing as the variable is not null
        }
        CurrentMessage = 2;
        removeGlobalValue("GlobalCurrentMessage");
        setGlobalValue("GlobalCurrentrMessage", CurrenMessage);
        CurrentMessage = getGlobalValue("GlobalCurrentMesage");
        if ((CurrentMessage) == null) {
        CurrentMessage = getGlobalValue("GlobalCurrentMessage");
        } else {
        CurrentMessage = getGlobalValue("GlobalCurrentMessage");
        }
        // call funciton CheckSomething
        CheckSomthing(Green);
        }
        function postMain() {

        // clear data variables etc
        Green = 0;
        }?

        Comment


        • #5
          copy the above code into eSignal editor or Microsoft Visual Studio to get a clearer view

          remember, in Visual Studio, you can load the EFS plugin to support eSignal code.

          Comment


          • #6
            Hello,
            The program below that I wrote demonstrates how a global variable can be accessed and changed by both main() and another function (GlobalVarTest). Initially, GlobalVarTest prints the first value (0) that main() assigned to nGlobalVar at the start of the program and then it increments nGlobalVar by 1. Then main() prints what the GlobalVarTest function assigned to nGlobalVar which is 1, after which main() increased nGlobalVar by 2. The printout to the formula output window that you see under the program clearly shows that both main() and the GlobalVarTest function are accessing the nGlobalVar global variable.

            The global variable that this program illustrates is program global and not an eSignal application level global (
            setGlobalValue) that could be accessed throughout the eSignal application, which you may not find a need for very often, but program global variables are something that you'll be using very often.
            ​
            "use strict";
            var nGlobalVar = null; // Global Variable
            var bInIt = false;

            function preMain(){
            setPriceStudy(true);
            setStudyTitle("TestGlobalVar");
            }

            function main(){
            if(getBarState() == BARSTATE_ALLBARS){
            nGlobalVar = 0;
            }
            if(!bInIt && isLastBarOnChart()){
            for(var i = 0; i < 10; i++){
            GlobalVarTest(nGlobalVar);
            debugPrintln(nGlobalVar + "\tFrom main.");
            nGlobalVar = nGlobalVar + 2;
            }
            bInIt = true;
            }
            return;
            }

            function GlobalVarTest(nAnyNumber){
            debugPrintln(nAnyNumber + "\tFrom Function.");
            nGlobalVar++;
            return;
            }​

            28 From main.
            27 From Function.
            25 From main.
            24 From Function.
            22 From main.
            21 From Function.
            19 From main.
            18 From Function.
            16 From main.
            15 From Function.
            13 From main.
            12 From Function.
            10 From main.
            9 From Function.
            7 From main.
            6 From Function.
            4 From main.
            3 From Function.
            1 From main.
            0 From Function.​

            Comment


            • #7
              Interesting example, thanks

              Comment


              • #8
                Hello laxmicc,

                I wrote the program below to help you understand how local variables behave in main(), I also included some global variables so you could see how they both work together.
                Below the program is what printed out to the formula output window when I ran this on a 1-minute chart. Note that only the first 5 tics of each bar are printed out, and if there aren't 5 tics during a bar, then nCount will be less than 5.

                "use strict";
                var nBarCount = 0; // Global Variable
                var nCount = null; // Global Variable
                var nGlobalNum = null; // Global Variable

                function preMain(){
                setPriceStudy(true);
                setStudyTitle("VariableScope");
                }

                function main(){
                if(getBarState() == BARSTATE_ALLBARS){
                var nLocalNum = 10; // Local Variable
                var nLocalNum_Updating = null; // Local Variable
                }
                if(getBarState() == BARSTATE_NEWBAR){
                if(isLastBarOnChart()){
                nBarCount++;
                if(nBarCount <= 5) debugPrintln(nLocalNum + "\tAfter entering BARSTATE_NEWBAR\n");
                }
                nLocalNum = nBarCount; // main() will remember this value only for the first tic of a new bar.
                nGlobalNum = 3 * nLocalNum; // main() will always know this value because it's global.
                nCount = 1;
                }

                nLocalNum_Updating = nGlobalNum + nCount; // main() will always know this value because it's updating continuously.

                if(nBarCount <= 5 && nCount <= 5 && isLastBarOnChart()){
                debugPrintln(nCount++ + '\t' + nLocalNum_Updating + '\t' + nGlobalNum + '\t' + nLocalNum);
                }
                return;
                }

                5 20 15 undefined
                4 19 15 undefined
                3 18 15 undefined
                2 17 15 undefined
                1 16 15 5
                undefined After entering BARSTATE_NEWBAR

                5 17 12 undefined
                4 16 12 undefined
                3 15 12 undefined
                2 14 12 undefined
                1 13 12 4
                undefined After entering BARSTATE_NEWBAR

                5 14 9 undefined
                4 13 9 undefined
                3 12 9 undefined
                2 11 9 undefined
                1 10 9 3
                undefined After entering BARSTATE_NEWBAR

                5 11 6 undefined
                4 10 6 undefined
                3 9 6 undefined
                2 8 6 undefined
                1 7 6 2
                undefined After entering BARSTATE_NEWBAR

                3 6 3 undefined
                2 5 3 undefined
                1 4 3 1
                undefined After entering BARSTATE_NEWBAR??

                Comment


                • #9
                  Hello Ainsley,
                  If you have a function that updates global variables and you create a series object from that function, the global variables that the function are updating will not update unless you call your function. The series object is not going to call your function and update your global variables. When you create a series object it creates its own copy of your function and the global variables and then the EFS engine encapsulates them into the series object.
                  ?

                  Comment


                  • #10
                    Originally posted by LetUsLearn View Post
                    Hello laxmicc,

                    I wrote the program below to help you understand how local variables behave in main(), I also included some global variables so you could see how they both work together.
                    Below the program is what printed out to the formula output window when I ran this on a 1-minute chart. Note that only the first 5 tics of each bar are printed out, and if there aren't 5 tics during a bar, then nCount will be less than 5.

                    "use strict";
                    var nBarCount = 0; // Global Variable
                    var nCount = null; // Global Variable
                    var nGlobalNum = null; // Global Variable

                    function preMain(){
                    setPriceStudy(true);
                    setStudyTitle("VariableScope");
                    }

                    function main(){
                    if(getBarState() == BARSTATE_ALLBARS){
                    var nLocalNum = 10; // Local Variable
                    var nLocalNum_Updating = null; // Local Variable
                    }
                    if(getBarState() == BARSTATE_NEWBAR){
                    if(isLastBarOnChart()){
                    nBarCount++;
                    if(nBarCount <= 5) debugPrintln(nLocalNum + "\tAfter entering BARSTATE_NEWBAR\n");
                    }
                    nLocalNum = nBarCount; // main() will remember this value only for the first tic of a new bar.
                    nGlobalNum = 3 * nLocalNum; // main() will always know this value because it's global.
                    nCount = 1;
                    }

                    nLocalNum_Updating = nGlobalNum + nCount; // main() will always know this value because it's updating continuously.

                    if(nBarCount <= 5 && nCount <= 5 && isLastBarOnChart()){
                    debugPrintln(nCount++ + '\t' + nLocalNum_Updating + '\t' + nGlobalNum + '\t' + nLocalNum);
                    }
                    return;
                    }

                    5 20 15 undefined
                    4 19 15 undefined
                    3 18 15 undefined
                    2 17 15 undefined
                    1 16 15 5
                    undefined After entering BARSTATE_NEWBAR

                    5 17 12 undefined
                    4 16 12 undefined
                    3 15 12 undefined
                    2 14 12 undefined
                    1 13 12 4
                    undefined After entering BARSTATE_NEWBAR

                    5 14 9 undefined
                    4 13 9 undefined
                    3 12 9 undefined
                    2 11 9 undefined
                    1 10 9 3
                    undefined After entering BARSTATE_NEWBAR

                    5 11 6 undefined
                    4 10 6 undefined
                    3 9 6 undefined
                    2 8 6 undefined
                    1 7 6 2
                    undefined After entering BARSTATE_NEWBAR

                    3 6 3 undefined
                    2 5 3 undefined
                    1 4 3 1
                    undefined After entering BARSTATE_NEWBAR??
                    Hi LetUsLearn,

                    You are a ultimate programmer with crystal clear concept of functions. The way you explained the Local and Global variables was very easy to understand. Thankyou very much for your great help. Do you do automated trading using EFSAT functionality of eSignal? Its different subject but I seek your guidance if you do so.

                    Regards
                    Lucky
                    [email protected]

                    Comment


                    • #11
                      Originally posted by Ainsley View Post
                      copy the above code into eSignal editor or Microsoft Visual Studio to get a clearer view

                      remember, in Visual Studio, you can load the EFS plugin to support eSignal code.
                      Hi Ainsley,

                      Thanks for codes, Emails are very frequent this may be due to no loops to restrict calculation. Even In my indicator codes for same signal i get many alerts even after using true/false conditions for alerts.


                      Regards
                      Lucky

                      Comment


                      • #12
                        Yes, frequent emails, annoying and you will need to create some logic to manage the situation or you will spam yourself to oblivion.

                        Try the old Cobol method of setting up two messages, one is for current and the other is for Last, if the current is equal to the last then don't send an email but if it is not equal then send the email, that will fixup your issue

                        Comment

                        Working...
                        X