Announcement

Collapse
No announcement yet.

Question about Bar.Value in setBar command

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

  • Question about Bar.Value in setBar command

    I have a script that has to go back and update a previous bar's value. Here are the two main lines of my code:

    setBar(Bar.FgColor, -1, 2, Color.magenta);

    // For Bar.Value, the offset is different.
    // Offset 0 refers to the previous bar!!
    setBar(Bar.Value, 0, 2, 1);


    The issue I am having is that it does go back and color the previous bar with magenta. I know because no other line is coloring that bar with a magenta foreground color.

    The problem is that the series involved normally is a true or false value. But I can not get the updated cursor value to show true when I am setting it to that.

    SO I have tried using true, "true", and 1 for the value in the setBar command. The value always comes out as Off in the cursor window.

    Is there a reason for this?

  • #2
    crazytiger
    I am not sure what you mean with
    // For Bar.Value, the offset is different.
    // Offset 0 refers to the previous bar!!

    As far as I know the offset parameter works in the same way also with Bar.Value. If your results are different you may want to post a complete working example that illustrates this behavior.
    Alex

    Comment


    • #3
      Hi Alex,
      I searched for Bar.Value in the forum and saw JasonK post a response like this:

      JasonK
      Administrator

      Registered: Nov 2002
      Location:
      Posts: 3247
      Status: OFFLINE

      Hello bullman,

      Hopefully this code example will help. One thing to point out first is that the offset parameter may seem a bit counter intuitive. setBar() already assumes that you're changing info for previous bars so it sees an offset of 0 as the bar with an index of -1. The other thing you may be having trouble with is not having the proper size array on the previous bar's returned data series to change the values of. In the code example below, you'll notice that I'm just returning null values for the return series. This essentially primes the array on that bar for the returned data series. If you comment out that return line below, this code example doesn't work. Before you can change the values of a previous bar with setBar(), something had to be returned to that bar to initialize its array. Let me know if this helps.

      PHP:

      function preMain() {
      setStudyTitle(" setBar ");
      setCursorLabelName("data0", 0);
      setCursorLabelName("data1", 1);
      setCursorLabelName("data2", 2);
      setStudyMax(100);
      setStudyMin(0);
      }

      var aData = new Array(3);
      aData[0] = 40;
      aData[1] = 35;
      aData[2] = 30;

      function main() {
      if (getCurrentBarIndex() > -5 && getBarState() == BARSTATE_NEWBAR) {
      setBar(Bar.Value, 0, aData);

      // or you can do this
      //for (i=0; i<3; ++i) {
      // setBar(Bar.Value, 0, i, aData[i]);
      //}
      }
      return new Array(null, null, null);
      }


      I tried using:

      setBar(Bar.Value, -1, 2, true)

      to set the previous bar's value to true but this also didn't work.
      I may have to trimp down my script and post it.

      It is strange. With the setBar(...., true) command, no previous value seems to be set to true for that cursor value. Setting the foreground color for the previous bar works fine.

      Comment


      • #4
        I have reduced my script to that part that does not work. Here it is.
        Attached Files

        Comment


        • #5
          Here is the piece of code attached.

          I tried :

          setBar(Bar.Value, -1, 2, true);

          and :

          setBar(Bar.Value, 0, 2, true);


          Neither version set's the previous bar's value for cursor #2 to true but the foreground color does get set to magenta.

          There must be an easy answer for this one!
          Attached Files

          Comment


          • #6
            crazytiger
            As I understand it setBar(Bar.Value,...) can only apply a value.
            Try replacing your true or false with 1 or 0 and you should see that setBar(Bar.Value,-1...) will change the value of the prior bar (see enclosed example)
            As to JasonK's post that was a workaround for a setBar() bug that has since been fixed hence it no longer applies.
            Alex

            PHP Code:
            function main(){

                var 
            0;

                if(
            isLastBarOnChart()){
                    
            setBar(Bar.Value, -101);
                }

                return 
            x;

            Comment


            • #7
              ok, thank you. That worked in my sample too.
              I thought I tried the combination of offset -1 with a value of 1. guess I did not!

              Comment


              • #8
                crazytiger
                You are most welcome
                Alex

                Comment

                Working...
                X