Announcement

Collapse
No announcement yet.

Problem with plot returning yesterdays values

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

  • Problem with plot returning yesterdays values

    Hi,

    I've attempted to make a "signal strength" indicator for MACD, Stochastics and RSI.

    I make rules for the position of the indicators (RSI away from 50, k/d difference and MACd histogram) and then add them together for a simple strength indicator which is plotted as a line.

    The problem is that the value of the line today is the value of the indicators yesterday. (for example, when all the indicators are "negative" the buy strength should be 0 but it only goes to zero on the next day)

    Any help with this would be much appreciated.

    var study = null;
    var study2 = null;
    var study3 = null;
    var vHistS = 0;
    var vRSIS = 0;
    var vHistM = 0;
    var vMACDS = 0;
    var vTOTS = 0;
    var fpArray = new Array()

    function preMain() {

    setStudyTitle("Euro Sell Strength");
    setCursorLabelName("Sell Strength Absolute");
    setShowTitleParameters(false);
    setComputeOnClose(true);
    addBand(0, PS_DASH, 1, Color.black);
    addBand(300, PS_DASH, 1, Color.black);
    addBand(150, PS_DASH, 1, Color.black);
    /*addBand(100, PS_DASH, 1, Color.black);
    addBand(50, PS_DASH, 1, Color.black);*/
    setDefaultBarFgColor(Color.black);
    setPlotType(PLOTTYPE_LINE);
    var x =0;
    fpArray[x] = new FunctionParameter("nInputPercentK1",FunctionParame ter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(26);

    }
    fpArray[x] = new FunctionParameter("nInputPercentK2",FunctionParame ter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(6);

    }
    fpArray[x] = new FunctionParameter("nInputPercentD",FunctionParamet er.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(3);

    }
    fpArray[x] = new FunctionParameter("nInputRSI",FunctionParameter.NU MBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(30);

    }
    fpArray[x] = new FunctionParameter("nMACD1",FunctionParameter.NUMBE R);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(26);

    }
    fpArray[x] = new FunctionParameter("nMACD2",FunctionParameter.NUMBE R);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(52);

    }

    fpArray[x] = new FunctionParameter("nMACD3",FunctionParameter.NUMBE R);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(15);

    }
    }

    function main(nInputPercentK1, nInputPercentK2, nInputPercentD, nInputRSI, nMACD1, nMACD2, nMACD3) {


    if (nInputPercentK1 == null) {
    nInputPercentK1 = 10;
    }
    else {
    nInputPercentK1 = Math.round(nInputPercentK1);
    }

    if (nInputPercentK2 == null) {
    nInputPercentK2 = 6;
    }
    else {
    nInputPercentK2 = Math.round(nInputPercentK2);
    }

    if (nInputPercentD == null) {
    nInputPercentD = 3;
    }
    else {
    nInputPercentD = Math.round(nInputPercentD);
    }

    if (nInputRSI == null) {
    nInputRSI = 21;
    }
    else {
    nInputRSI = Math.round(nInputRSI);
    }

    if(nMACD1 == null) {
    nMACD1 = 12;
    }
    else {
    nMACD1 == Math.round(nMACD1)
    }

    if(nMACD2 == null) {
    nMACD2 = 26;
    }
    else {
    nMACD2 == Math.round(nMACD2)
    }

    if(nMACD3 == null) {
    nMACD3 = 9;
    }
    else {
    nMACD3 == Math.round(nMACD3)
    }

    if( getCurrentBarCount() <=35 ) return;


    if (study == null) study = new StochStudy(nInputPercentK1, nInputPercentK2, nInputPercentD);
    if (study2 == null) study2 = new RSIStudy(nInputRSI);
    if (study3 == null) study3 = new MACDStudy(nMACD1,nMACD2,nMACD3, "Close", false);

    var vFast = study.getValue(StochStudy.FAST);
    var vSlow = study.getValue(StochStudy.SLOW);
    var vRSI = study2.getValue(0);
    var vMACD = study3.getValue(MACDStudy.HIST);

    var vTOTS = vHistS + vRSIS + vMACDS;
    var vHist = vFast - vSlow;


    if(vHist >= 0) vHistS = 0;
    if(vHist < 0 && vHist >= -2.79) vHistS = 12.5;
    if(vHist < -2.79 && vHist >= -4.78) vHistS = 37.5;
    if(vHist < -4.78 && vHist >= -8.38) vHistS = 75;
    if(vHist < -8.38 && vHist >= -14.28) vHistS = 100;
    if(vHist < -14.28) vHistS = 75;



    if(vRSI >= 50) vRSIS = 0;
    if(vRSI < 50 && vRSI >= 47.56) vRSIS = 12.5;
    if(vRSI < 47.56 && vRSI >= 42.89) vRSIS = 37.5;
    if(vRSI < 42.89 && vRSI >= 37.77) vRSIS = 75;
    if(vRSI < 37.77 && vRSI >= 21.26 ) vRSIS = 100;
    if(vRSI < 21.26 ) vRSIS = 75;

    if(vMACD >= 0) vMACDS = 0;
    if(vMACD < 0 && vMACD >= -3.8287) vMACDS = 12.5;
    if(vMACD < -3.8287 && vMACD >= -13.1095) vMACDS = 37.5;
    if(vMACD < -13.1095 && vMACD >= -24.51) vMACDS = 75;
    if(vMACD < -24.51 && vMACD >= -81.62) vMACDS = 100;
    if(vMACD < -81.62) vMACDS = 75;



    if (vTOTS >= 0) {
    setBarFgColor(Color.red);
    } else {
    setBarFgColor(Color.black);
    }


    return vTOTS;
    }

  • #2
    Re: Problem with plot returning yesterdays values

    DolmenProp

    The problem is that the value of the line today is the value of the indicators yesterday. (for example, when all the indicators are "negative" the buy strength should be 0 but it only goes to zero on the next day)
    Actually there is no value returned on the most recent bar [ie today's] and that is because you have a setComputeOnClose() statement in preMain [BTW note that setComputeOnClose() does not take any parameters]
    Once you remove or comment out that statement the formula will return a value on the current bar
    Alex



    Originally posted by DolmenProp
    Hi,

    I've attempted to make a "signal strength" indicator for MACD, Stochastics and RSI.

    I make rules for the position of the indicators (RSI away from 50, k/d difference and MACd histogram) and then add them together for a simple strength indicator which is plotted as a line.

    The problem is that the value of the line today is the value of the indicators yesterday. (for example, when all the indicators are "negative" the buy strength should be 0 but it only goes to zero on the next day)

    Any help with this would be much appreciated.

    var study = null;
    var study2 = null;
    var study3 = null;
    var vHistS = 0;
    var vRSIS = 0;
    var vHistM = 0;
    var vMACDS = 0;
    var vTOTS = 0;
    var fpArray = new Array()

    function preMain() {

    setStudyTitle("Euro Sell Strength");
    setCursorLabelName("Sell Strength Absolute");
    setShowTitleParameters(false);
    setComputeOnClose(true);
    addBand(0, PS_DASH, 1, Color.black);
    addBand(300, PS_DASH, 1, Color.black);
    addBand(150, PS_DASH, 1, Color.black);
    /*addBand(100, PS_DASH, 1, Color.black);
    addBand(50, PS_DASH, 1, Color.black);*/
    setDefaultBarFgColor(Color.black);
    setPlotType(PLOTTYPE_LINE);
    var x =0;
    fpArray[x] = new FunctionParameter("nInputPercentK1",FunctionParame ter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(26);

    }
    fpArray[x] = new FunctionParameter("nInputPercentK2",FunctionParame ter.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(6);

    }
    fpArray[x] = new FunctionParameter("nInputPercentD",FunctionParamet er.NUMBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(3);

    }
    fpArray[x] = new FunctionParameter("nInputRSI",FunctionParameter.NU MBER);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(30);

    }
    fpArray[x] = new FunctionParameter("nMACD1",FunctionParameter.NUMBE R);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(26);

    }
    fpArray[x] = new FunctionParameter("nMACD2",FunctionParameter.NUMBE R);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(52);

    }

    fpArray[x] = new FunctionParameter("nMACD3",FunctionParameter.NUMBE R);
    with(fpArray[x++]) {
    setLowerLimit(1);
    setDefault(15);

    }
    }

    function main(nInputPercentK1, nInputPercentK2, nInputPercentD, nInputRSI, nMACD1, nMACD2, nMACD3) {


    if (nInputPercentK1 == null) {
    nInputPercentK1 = 10;
    }
    else {
    nInputPercentK1 = Math.round(nInputPercentK1);
    }

    if (nInputPercentK2 == null) {
    nInputPercentK2 = 6;
    }
    else {
    nInputPercentK2 = Math.round(nInputPercentK2);
    }

    if (nInputPercentD == null) {
    nInputPercentD = 3;
    }
    else {
    nInputPercentD = Math.round(nInputPercentD);
    }

    if (nInputRSI == null) {
    nInputRSI = 21;
    }
    else {
    nInputRSI = Math.round(nInputRSI);
    }

    if(nMACD1 == null) {
    nMACD1 = 12;
    }
    else {
    nMACD1 == Math.round(nMACD1)
    }

    if(nMACD2 == null) {
    nMACD2 = 26;
    }
    else {
    nMACD2 == Math.round(nMACD2)
    }

    if(nMACD3 == null) {
    nMACD3 = 9;
    }
    else {
    nMACD3 == Math.round(nMACD3)
    }

    if( getCurrentBarCount() <=35 ) return;


    if (study == null) study = new StochStudy(nInputPercentK1, nInputPercentK2, nInputPercentD);
    if (study2 == null) study2 = new RSIStudy(nInputRSI);
    if (study3 == null) study3 = new MACDStudy(nMACD1,nMACD2,nMACD3, "Close", false);

    var vFast = study.getValue(StochStudy.FAST);
    var vSlow = study.getValue(StochStudy.SLOW);
    var vRSI = study2.getValue(0);
    var vMACD = study3.getValue(MACDStudy.HIST);

    var vTOTS = vHistS + vRSIS + vMACDS;
    var vHist = vFast - vSlow;


    if(vHist >= 0) vHistS = 0;
    if(vHist < 0 && vHist >= -2.79) vHistS = 12.5;
    if(vHist < -2.79 && vHist >= -4.78) vHistS = 37.5;
    if(vHist < -4.78 && vHist >= -8.38) vHistS = 75;
    if(vHist < -8.38 && vHist >= -14.28) vHistS = 100;
    if(vHist < -14.28) vHistS = 75;



    if(vRSI >= 50) vRSIS = 0;
    if(vRSI < 50 && vRSI >= 47.56) vRSIS = 12.5;
    if(vRSI < 47.56 && vRSI >= 42.89) vRSIS = 37.5;
    if(vRSI < 42.89 && vRSI >= 37.77) vRSIS = 75;
    if(vRSI < 37.77 && vRSI >= 21.26 ) vRSIS = 100;
    if(vRSI < 21.26 ) vRSIS = 75;

    if(vMACD >= 0) vMACDS = 0;
    if(vMACD < 0 && vMACD >= -3.8287) vMACDS = 12.5;
    if(vMACD < -3.8287 && vMACD >= -13.1095) vMACDS = 37.5;
    if(vMACD < -13.1095 && vMACD >= -24.51) vMACDS = 75;
    if(vMACD < -24.51 && vMACD >= -81.62) vMACDS = 100;
    if(vMACD < -81.62) vMACDS = 75;



    if (vTOTS >= 0) {
    setBarFgColor(Color.red);
    } else {
    setBarFgColor(Color.black);
    }


    return vTOTS;
    }

    Comment


    • #3
      DolmenProp
      There is also one other issue that I can see in the script ie the syntax in the following line
      var vRSI = study2.getValue(0);
      is incorrect and should be
      var vRSI = study2.getValue(RSIStudy.RSI);
      While it may still work as is it will not work if you are trying to retrieve prior values of that indicator
      Alex

      Comment


      • #4
        Thanks for the help alexis, i didnt notice the mistake with the RSI.

        I realise that there is no value for today, that's alright.

        If you look at the criteria for the indicators

        (RSI < 50 ... )

        And actually compute the values of the RSI, MACD and Stochastics on any day in the past, then give them the weightings that i have specified (12.5, 37.5 ...) then the value of the indicator on that day is not correct. however, it gives the correct answer the next day.

        For example, if the signal strength from the indicator on Bar 35 says it is 150. If you manually calculate the strength of each indicator on bar 35, it will not match the signal strength indicator. It will give you the right answer on bar 34 however. Thats the problem i'm trying to fix.

        Cheers for the help

        John

        Comment


        • #5
          John
          That is happening because you are calculating vTOTS prior to updating the components of the expression used to compute that variable (ie vHistS, etc). Move the line in which you compute vTOTS to a place after you assign all the strength values
          Alex


          Originally posted by DolmenProp
          Thanks for the help alexis, i didnt notice the mistake with the RSI.

          I realise that there is no value for today, that's alright.

          If you look at the criteria for the indicators

          (RSI < 50 ... )

          And actually compute the values of the RSI, MACD and Stochastics on any day in the past, then give them the weightings that i have specified (12.5, 37.5 ...) then the value of the indicator on that day is not correct. however, it gives the correct answer the next day.

          For example, if the signal strength from the indicator on Bar 35 says it is 150. If you manually calculate the strength of each indicator on bar 35, it will not match the signal strength indicator. It will give you the right answer on bar 34 however. Thats the problem i'm trying to fix.

          Cheers for the help

          John

          Comment


          • #6
            Worked like a dream, thank a million!

            Comment


            • #7
              John
              My pleasure
              Alex


              Originally posted by DolmenProp
              Worked like a dream, thank a million!

              Comment

              Working...
              X