Announcement

Collapse
No announcement yet.

MA difference problem afer scrollling

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

  • MA difference problem afer scrollling

    I pieced together this script to return the sum of the differences (MA1-MA2). It works fine until I scroll the chart back in time; a bad first value for (MA1-MA2) is then given. If I reload the study after scrolling, the first value recomputes properly. What correction needs to be made so that the first value is correct without reloading the study everytime I scroll the chart back?

    PHP Code:
    //*********************************************************
    Alexis CMontenegro © December 2004                      
    Use and/or modify this code freely. If you redistribute it
    please 
    include this and/or any other comment blocks and 
    description of any changes you make
    .                      
    **********************************************************/

    var 
    fpArray = new Array();
    var 
    0;
    var 
    suma 0;
    function 
    preMain() {

        
    setStudyTitle("MAx2SumDif");
        
    setCursorLabelName("SumDif",0);
        
    setDefaultBarFgColor(Color.blue,0);
        
    setPlotType(PLOTTYPE_FLATLINES,0);
        
    setDefaultBarThickness(2,0);
        
    askForInput();
        
    setPriceStudy(false);
                
        var 
    x=0;
        
    fpArray[x] = new FunctionParameter("Type1"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("sma");
            
    addOption("ema");
            
    addOption("wma");
            
    addOption("vwma");
            
    setDefault("sma");
        }
        
    fpArray[x] = new FunctionParameter("Length1"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(5);
        }
        
    fpArray[x] = new FunctionParameter("Source1"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("open"); 
            
    addOption("high");
            
    addOption("low");
            
    addOption("close");
            
    addOption("hl2");
            
    addOption("hlc3");
            
    addOption("ohlc4"); 
            
    setDefault("close"); 
        }
        
    fpArray[x] = new FunctionParameter("Offset1"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setDefault(0);
        }
        
    fpArray[x] = new FunctionParameter("Type2"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("sma");
            
    addOption("ema");
            
    addOption("wma");
            
    addOption("vwma");
            
    setDefault("sma");
        }
        
    fpArray[x] = new FunctionParameter("Length2"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(390);
        }
        
    fpArray[x] = new FunctionParameter("Source2"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("open"); 
            
    addOption("high");
            
    addOption("low");
            
    addOption("close");
            
    addOption("hl2");
            
    addOption("hlc3");
            
    addOption("ohlc4"); 
            
    setDefault("close"); 
        }
        
    fpArray[x] = new FunctionParameter("Offset2"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setDefault(0);
        }
        
    fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    setDefault();
        }
        
    fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    setDefault();
        }
        
    fpArray[x] = new FunctionParameter("LineColor1"FunctionParameter.COLOR);
        
    with(fpArray[x++]){
            
    setName("DI+ Color");
            
    setDefault(Color.black);
        }
        
    fpArray[x] = new FunctionParameter("LineColor2"FunctionParameter.COLOR);
        
    with(fpArray[x++]){
            
    setName("DI- Color");
            
    setDefault(Color.red);
        }
        
    fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
        
    with(fpArray[x++]){
            
    setName("Show Parameters");
            
    setDefault(false);
        }
    }

    var 
    bInit false;
    var 
    xMA1 null;
    var 
    xMA2 null;

    function 
    main(Type1,Length1,Source1,Offset1,Type2,Length2,Source2,Offset2,Symbol,Interval,LineColor1,LineColor2,Params) {
    if (
    getCurrentBarCount() < Length2) return;
    var 
    nState getBarState();
    if (
    nState == BARSTATE_NEWBAR) {
            
    suma += a;
        }
        if(
    bInit == false){
            if(
    Symbol == nullSymbol getSymbol();
            if(
    Interval == nullInterval getInterval();
            
    vSymbol Symbol+","+Interval;
            
    xMA1 offsetSeries(eval(Type1)(Length1,eval(Source1)(sym(vSymbol))),Offset1);
            
    xMA2 offsetSeries(eval(Type2)(Length2,eval(Source2)(sym(vSymbol))),Offset2);
            
    setDefaultBarFgColor(LineColor1,0);
            
    setDefaultBarFgColor(LineColor2,1);
            
    setShowTitleParameters(eval(Params));
            
    bInit true;
        }
        
    = (xMA1.getValue(MAStudy.MA) - xMA2.getValue(MAStudy.MA)) / 1;
        if(
    == null) return;
        return (
    suma a);


  • #2
    Hello checkraise,

    The formula is working properly has far as I can see. What you may be seeing is something like below.



    Because the moving average study, MA2, is set to 390 periods, the formula cannot return valid data until at least that number of bars has been processed. If you are using a dynamic time template and scroll back to the beginning of the chart data you will force more data to be dynamically loaded to the chart, thus increasing your chart's history. This will change the values on the chart because you have forced the study to start at a point in time that is further back in the time line. The study is a cumulative calculation, so it's values all depend on where the study reaches the 390th bar from the oldest bar loaded in the chart.

    If you're seeing something entirely different, please post some chart images and let me know what specific time template settings you are using.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Hi Jason, Thanks for your reply.

      I am using the dynamic Equity-RTH-East Coast time template. When I first load the study onto a chart, the values of the study are all fine. But when I scroll the chart backwards in time so that more data is forced to be dynamically loaded into the chart, the very first value of the indicator is wrong. All subsequent difference-values adding on to this first wrong value are correct, but they are adding onto a wrong initial value. It seems the further I scroll back the chart, the larger the initial error is.

      The screenshot below shows this error. The SumDif study MA lengths are 5 and 100, and I added these MAs to the chart so we could easily see the error in the cursor window data. As you can see, with the cursor on the first bar that has a SumDIf and 100MA value, the study should be (MA5 - MA100) = (46.01- 44.54) = 1.47, but it's -80.21 instead. If I reload the study, all values including the first are fine, but once again if I scroll back and data is dynamically loaded, the new first value will be wrong.

      I must have made errors when I pieced this study together, omitting something essential from Alex' original script or adding in the summation code improperly, because this is not the only problem with this study. When I change it to a Price study, scale it on the left axis, and change the study symbol, over half of the difference values are zero, as you can see from the lower chart and Data Export in the screenshot. Similar problems happen if you leave it as a non-price study and simply change the symbol of the study.

      Any ideas what's causing this mess?
      Attached Files

      Comment


      • #4
        checkraise
        To resolve the scrolling issue add the following lines of code at the very beginning of your main() function (make sure to insert it before the check for getCurrentBarCount() < Length2)
        PHP Code:
        if(getBarState()==BARSTATE_ALLBARS){
            
        0;
            
        suma 0;

        This will reset the global variables a and suma to 0 when the chart is refreshed or scrolled back
        Also rather than checking for getCurrentBarCount() < Length2 it is preferable to run a null check on the values of xMA1 and xMA2 prior to using them in your calculations. To do this comment out your conditional statement that checks getCurrentBarCount() and add the following immediately after the bInit routine
        PHP Code:
        if(xMA1.getValue(0)==null || xMA2.getValue(0)==null) return; 
        This way the script will not execute until there are valid values for both averages.
        Lastly replace xMA1.getValue(MAStudy.MA) and xMA2.getValue(MAStudy.MA) which are the methods to call the values of efs1 studies with xMA1.getValue(0) and xMA2.getValue(0) which are the methods for the efs2 studies
        Alex

        Comment


        • #5
          Thanks very much Alex, those fixes work beautifully.

          The only other question I have regards using a symbol for the study that is different than the chart symbol: What code must be added to the study so that when I scroll the chart back in time, new data for both symbols is processed on the chart, keeping the study "up to speed" with the chart? As it stands now, when the symbols are different and I scroll back, no new data is added to the study. It seems I am stuck with just the same amount of data there as initially loaded.

          Comment


          • #6
            checkraise
            You are most welcome and thank you for letting me know that the fixes worked.
            With regards to your question here is what you can do.
            Add a setIntervalsBackfill(true) statement in preMain (for a description of this function see this article in the EFS KnowledgeBase).
            As you scroll back the chart it will still not automatically load the external symbol's data however the difference is that it will now do that when you reload the efs.
            Be cautious when using the setIntervalsBackfill() function. The function will attempt to load as much external data as poossible to match the data plotted on the chart so if for example you have 300 days of daily data loaded in the chart and request 1 minute external data it will automatically request all the available data for the 1 minute interval regardless of your Time Template settings for that interval.
            Alex

            Comment

            Working...
            X