Announcement

Collapse
No announcement yet.

Mathematical operations with user series

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

  • Mathematical operations with user series

    Hello.
    why is not possible to do mathematical operations with user series ???
    For instance, I'd like to display the Bollinger Bands difference:

    var bInit = false;
    var vSymbol = null;
    var xUpperBB = null;
    var xLowerBB = null;
    var xDiffBB = null;

    function main(PeriodoBB,CoefBB,Source,Symbol,Interval){
    if ( !bInit ){
    if(Symbol == null) Symbol = getSymbol();
    if(Interval == null) Interval = getInterval();

    vSymbol = Symbol+","+Interval;

    xUpperBB = upperBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) )); // <<< User serie one
    xLowerBB = lowerBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) )); // <<< User serie two

    //I think the next sentence doesn't work !!!
    xDiffBB = upperBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) )) - lowerBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) ));

    bInit = true;
    }

    return xDiffBB.getValue(0); " <<< DON'T WORK !!!

    }

    I don't understand the cause. Please, could you help me? Thanks very much.

  • #2
    Re: Mathematical operations with user series

    cls
    There are several errors in the script you posted.
    Anything that is included in the bInit routine is going to be executed only one time on first load of the formula (or on a reload). So even if the expression to calculate xDiffBB is correct it will only return a value for the first time it is computed because it is located inside the bInit routine.
    When you apply some math to a series the result is not a series but a value in the same way that if you apply some math to an array the result is not an array but a single value. So retrieving the value in the return statement using xDiffBB.getValue(0) is not valid in the context of your script since xDiffBB is not a series.
    To calculate the difference between the two series you need to do that outside of (and after) the bInit routine using the values of the two series. You retrieve the values from the series using the getValue() method that JasonK indicated in this reply to you
    Here is an example of the required logic
    PHP Code:
    if(bInit==false){
        
    xUpperBB upperBB(PeriodoBBCoefBB, eval(Source)(sym(vSymbol))); 
        
    xLowerBB lowerBB(PeriodoBBCoefBB, eval(Source)(sym(vSymbol))); 
        
    bInit true;
    }
    if(
    xUpperBB.getValue(0)==null||xLowerBB.getValue(0)==null) return;//null check on the values

    xDiffBB xUpperBB.getValue(0)-xLowerBB.getValue(0);//xDiffBB will be a value NOT a series
        //rest of your code 
    Alternatively you could create a separate function to which you pass the two series using the efsInternal() function and in which you calculate the difference between the values of the two series. The result in this case would be a series.
    Having said this there is a already function called dsDiffSeries() programmed by Chris Kryza and included in his dsFunctions library (which is available here) that will compute the difference between two series and return a series. You may want to look into that option also.
    Alex


    Originally posted by cls
    Hello.
    why is not possible to do mathematical operations with user series ???
    For instance, I'd like to display the Bollinger Bands difference:

    var bInit = false;
    var vSymbol = null;
    var xUpperBB = null;
    var xLowerBB = null;
    var xDiffBB = null;

    function main(PeriodoBB,CoefBB,Source,Symbol,Interval){
    if ( !bInit ){
    if(Symbol == null) Symbol = getSymbol();
    if(Interval == null) Interval = getInterval();

    vSymbol = Symbol+","+Interval;

    xUpperBB = upperBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) )); // <<< User serie one
    xLowerBB = lowerBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) )); // <<< User serie two

    //I think the next sentence doesn't work !!!
    xDiffBB = upperBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) )) - lowerBB(PeriodoBB,CoefBB,eval(Source)(sym(vSymbol) ));

    bInit = true;
    }

    return xDiffBB.getValue(0); " <<< DON'T WORK !!!

    }

    I don't understand the cause. Please, could you help me? Thanks very much.

    Comment


    • #3
      Re: Re: Mathematical operations with user series

      Thanks Alex,

      Comment


      • #4
        cls
        You are most welcome
        Alex


        Originally posted by cls
        Thanks Alex,

        Comment

        Working...
        X