Announcement

Collapse
No announcement yet.

What is happening with this script?

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

  • What is happening with this script?

    The code below seems straightforward, but obviously I must be missing something. Take a look at the huge numbers in the debugPrintln output:


    function preMain() {
    setPriceStudy(false);
    setStudyTitle("_testVol");

    }

    var red = 0;
    var lime = 0;
    var pryce = 0;
    var pryceB = 0;
    var nLength = null;
    var diff2 = null;
    var bInit = false;
    var currVol = 0;
    var myVol = 0;

    function main()
    {

    if(bInit == false) {
    if (diff2 == null) diff2 = new Array(nLength+1);
    lime = 0;
    red = 0;
    bInit = true;
    }

    // on each new tick ...

    lastVol = currVol;
    currVol = volume(0);

    if(lastVol > currVol)
    // this is a new bar
    myVol = (currVol/1000).toFixed(0); // this for what is both a new bar AND a new tick
    else if(lastVol < currVol)
    // we're on the same bar and it's a new tick
    myVol = (currVol - lastVol).toFixed(0); // the volume to save is the difference between the current and preceding tick

    // debugPrintln("myVol " +myVol);

    /*
    if(lastVol != currVol || pryceB != pryce);
    debugPrintln("change ");

    debugPrintln();
    */
    pryceB = pryce; // put last tick's price into oldPrice var
    pryce = close(0);

    // debugPrintln("pryceB " +pryceB);

    if(close(0) < pryceB) {// if new close is now lesss than the old close
    red = myVol + red; // (myVol/1000).toFixed(1);
    lastRed = true;
    lastLime = false;
    }
    if(close(0) > pryceB) {// if new tick's close is more than the last tick's
    lime = myVol + lime; // (myVol/1000).toFixed(1);
    lastLime = true;
    lastRed = false;
    }
    if(close(0) == pryceB && lastVol != currVol ) {// if new tick's close is equal to the last tick's
    if(lastRed)
    red = myVol + red; // (myVol/1000).toFixed(1);
    if(lastLime)
    lime = myVol + lime; // (myVol/1000).toFixed(1);
    }

    diff2[0] = lime - red;

    // debugPrintln("myVol "+myVol);
    debugPrintln("lime " +lime);
    debugPrintln("red " + red);

    // debugPrintln("diff2[0] " +diff2[0]);
    debugPrintln();

    return;
    }

  • #2
    Steven
    The .toFixed() method converts a number to a string so when you subsequently add a number to that string it concatenates the values instead of summing them.
    Try multiplying the string by 1 prior to adding a number to it
    Alex

    Comment


    • #3
      I think (I didn't run the script) that you are adding up the volume for each time in the bar. However volume() returns the total volume for that bar. So for the 2nd tick, you are adding:

      1st tick volue + (1st tick volue + 2nd tick volume)

      So you will be seeing huge numbers.

      You may want to do something like this:

      if ( getBarState() == BARSTATE_NEWBAR ) {
      do my adds
      }


      also, I couldn't see a var defitinion for lastvol....

      Garth
      Garth

      Comment


      • #4
        it looks like it is comparing price action and volume.

        The Volume analysis results in MyVol (the difference between the last known volume and the current volume). This is done on a tick by tick basis.

        Then, a price comparison is done to determine if the volume increase resulted in a price increase or decrease. Based on these results, values are calculated for "red" and "lime"

        At the end of the code, the difference between red and lime are stored into "diff2".

        The only output I can see are debugPrintln() statements.

        I would have to see this running, but it appears to track UP VOLUME and DOWN VOLUME for the chart in a way that is dynamic and cumulative for each bar.
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          Originally posted by Alexis C. Montenegro
          Steven
          The .toFixed() method converts a number to a string so when you subsequently add a number to that string it concatenates the values instead of summing them.
          Try multiplying the string by 1 prior to adding a number to it
          Alex
          Alex is right. Get rid of the ".tofixed()" statments in all of your calculations and only use in for your output functions (debugPrintln).
          Brad Matheny
          eSignal Solution Provider since 2000

          Comment


          • #6
            I still think this logic is wrong:

            if(lastVol > currVol)
            // this is a new bar

            The above will only be a newbar when using historical data...

            Garth
            Garth

            Comment


            • #7
              Many thanks

              Thanks, guys.

              I had noticed that there was some concatenation going on, as if I was appending strings, but didn't understand the string nature of the toFixed() function. And Alex, thanks for that multiply-by-1 idea.

              Gspiker, I'm trying to peer into a short-period intraday bar as it is being formed, seeking a sense of how strong the relative force is in either bullish or bearish directions. Since volume increases while a bar is current, it would seem that one can presume that as long as volume is increasing, it is not a new bar. On the other hand, when volume is suddenly less than the previous volume number, it would mean that one is now on the first tick of a new bar. Or at least that's my thinking.

              Steve

              Comment


              • #8
                Steven
                You are most welcome.
                If you just want the integer another solution would be to use the parseInt() function - which returns a number - instead of the .toFixed() method (see modified section of code enclosed below)
                Alex

                PHP Code:
                if(lastVol currVol)
                // this is a new bar
                myVol parseInt(currVol/1000);// this for what is both a new bar AND a new tick 
                else if(lastVol currVol)
                // we're on the same bar and it's a new tick
                myVol currVol lastVol;// the volume to save is the difference between the current and preceding tick 

                Originally posted by Steven Miller
                I had noticed that there was some concatenation going on, as if I was appending strings, but didn't understand the string nature of the toFixed() function. And Alex, thanks for that multiply-by-1 idea.

                Comment


                • #9
                  I probably should have used parseInt(), now that you mention it.

                  Steve

                  Comment


                  • #10
                    Steve
                    It certainly makes it simpler
                    Alex


                    Originally posted by Steven Miller
                    I probably should have used parseInt(), now that you mention it.

                    Steve

                    Comment

                    Working...
                    X