Announcement

Collapse
No announcement yet.

Display a Moving Average Value

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

  • Display a Moving Average Value

    I would like to simply display the value of a moving average from the current candle. Here is my code:

    xSMA200 = sma(200);
    var nSMA200 = (xSMA200.getValue(0));

    drawTextRelative(-15, BottomRow2, nSMA200, Color.black, null, Text.PRESET | Text.BOLD | Text.ONTOP, "Arial", 10, 200);

    This results in "Parameter Number 3 is invalid".
    I get the same error if I use: xSMA200.getValue(0) as the third parameter.

    If I try: xSMA200.getValue() then [object Series] is displayed on the chart.

    It has to be simple solution, but I can not figure it out.
    Thanks for your assistance.
    Phil

  • #2
    Re: Display a Moving Average Value

    Phil
    Run a null check on the variable to which you assigned the value of the average prior to using it in the drawTextRelative() command eg
    PHP Code:
    xSMA200 sma(200);
    var 
    nSMA200 = (xSMA200.getValue(0));

    if(
    nSMA200 ==null) return;//null check

    drawTextRelative(-15BottomRow2nSMA200Color.blacknullText.PRESET Text.BOLD Text.ONTOP"Arial"10200); 
    Once you do this you should no longer get the error message
    The error message is due to the fact that while the average is priming it will return null which is not a valid parameter for the drawTextRelative() function
    Alex


    Originally posted by pjkowalski
    I would like to simply display the value of a moving average from the current candle. Here is my code:

    xSMA200 = sma(200);
    var nSMA200 = (xSMA200.getValue(0));

    drawTextRelative(-15, BottomRow2, nSMA200, Color.black, null, Text.PRESET | Text.BOLD | Text.ONTOP, "Arial", 10, 200);

    This results in "Parameter Number 3 is invalid".
    I get the same error if I use: xSMA200.getValue(0) as the third parameter.

    If I try: xSMA200.getValue() then [object Series] is displayed on the chart.

    It has to be simple solution, but I can not figure it out.
    Thanks for your assistance.
    Phil

    Comment


    • #3
      Thank you Alex. That did it.
      Phil

      Comment


      • #4
        I have a similar issue... the following code works only when the chart is on a 5 min interval. I want this to populate on any time-frame by picking up the interval using "getInterval()". I keep getting an error mssg "Trin has no properties". What am I missing here? Any help would be greatly appreciated. Thank-you.

        If it is better to use "getSeries" then cool, just don't know how to do it...

        [for some reason, the post removed all my tabs from the code formatting]


        var vTrinSMA = sma(1, sym("$trin,5"));
        var vAddSMA = sma(1, sym("$Add,5"));

        var gInt = null;
        var bInt = false;

        var fpArray = new Array();

        function preMain() {

        debugClear();

        setComputeOnClose(true)

        setCursorLabelName("trin", 0);
        setDefaultBarStyle(PS_SOLID, 0);
        setDefaultBarFgColor(Color.red, 0);
        setDefaultBarThickness(3, 0);
        setPlotType(PLOTTYPE_LINE, 0);

        setCursorLabelName("add", 1);
        setDefaultBarStyle(PS_SOLID, 1);
        setDefaultBarFgColor(Color.green, 1);
        setDefaultBarThickness(3, 1);
        setPlotType(PLOTTYPE_LINE, 1);

        gInt = getInterval();

        function main() {

        if(!bInt) {

        /* ** Tried this here but didn’t work… only accepts static
        interval as defined up top**

        var vTrinSMA = sma(1, sym("$trin,gInt"));
        var vAddSMA = sma(1, sym("$Add,gInt"));

        */

        bInt = true;
        }

        var Trin = vTrinSMA.getValue(MAStudy.MA);
        var Add = vAddSMA.getValue(MAStudy.MA);

        debugPrintln(

        "Trin:" + Trin.toFixed(2) + " (" + gnTrinVal + ") " +
        "Tick:" + Tick.toFixed(2) + " (" + gnTickVal + ")"

        );

        return new Array (Trin , Tick);


        }

        Comment


        • #5
          rdehavelyn
          Run a null check on the variable Trin prior to changing its properties through the .toFixed() method
          Also replace MAStudy.MA with 0 (or with the appropriate bar index) where you retrieve the values of the vTrinSMA and vAddSMA series using the getValue() method.
          Alex


          Originally posted by rdehavelyn
          I have a similar issue... the following code works only when the chart is on a 5 min interval. I want this to populate on any time-frame by picking up the interval using "getInterval()". I keep getting an error mssg "Trin has no properties". What am I missing here? Any help would be greatly appreciated. Thank-you.

          If it is better to use "getSeries" then cool, just don't know how to do it...

          [for some reason, the post removed all my tabs from the code formatting]


          var vTrinSMA = sma(1, sym("$trin,5"));
          var vAddSMA = sma(1, sym("$Add,5"));

          var gInt = null;
          var bInt = false;

          var fpArray = new Array();

          function preMain() {

          debugClear();

          setComputeOnClose(true)

          setCursorLabelName("trin", 0);
          setDefaultBarStyle(PS_SOLID, 0);
          setDefaultBarFgColor(Color.red, 0);
          setDefaultBarThickness(3, 0);
          setPlotType(PLOTTYPE_LINE, 0);

          setCursorLabelName("add", 1);
          setDefaultBarStyle(PS_SOLID, 1);
          setDefaultBarFgColor(Color.green, 1);
          setDefaultBarThickness(3, 1);
          setPlotType(PLOTTYPE_LINE, 1);

          gInt = getInterval();

          function main() {

          if(!bInt) {

          /* ** Tried this here but didn’t work… only accepts static
          interval as defined up top**

          var vTrinSMA = sma(1, sym("$trin,gInt"));
          var vAddSMA = sma(1, sym("$Add,gInt"));

          */

          bInt = true;
          }

          var Trin = vTrinSMA.getValue(MAStudy.MA);
          var Add = vAddSMA.getValue(MAStudy.MA);

          debugPrintln(

          "Trin:" + Trin.toFixed(2) + " (" + gnTrinVal + ") " +
          "Tick:" + Tick.toFixed(2) + " (" + gnTickVal + ")"

          );

          return new Array (Trin , Tick);


          }

          Comment


          • #6
            Phil
            You are most welcome and I am glad to hear that resolved the issue
            Alex


            Originally posted by pjkowalski
            Thank you Alex. That did it.
            Phil

            Comment


            • #7
              Works perfectly now... Thanks so much!

              Regards,
              Ryan.

              Comment


              • #8
                Ryan
                My pleasure
                Alex


                Originally posted by rdehavelyn
                Works perfectly now... Thanks so much!

                Regards,
                Ryan.

                Comment

                Working...
                X