Announcement

Collapse
No announcement yet.

Array Problem please help

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

  • Array Problem please help

    If you will load the following script you will see that it is working properly by return all the aRSI values. Now remove the comment marks from either aRSIS or aRSIR and reload the script. You will see that the araays values for all but one stay constant. I am not sure why. The logic seems right so I am very confused.

    Thanks for the help.
    Attached Files

  • #2
    Hello Whatever,

    The code below should get you going. The solution was a bit tricky. Seems to me like there might be a bug related to the sort() method. It was sorting the aRSI array in addition to the aRSIS array when setting aRSIS = aRSI. Not sure about this bug yet, but I'll run this past development.

    Two things to point out about the solution below. I used the for loop method for setting the aRSIS and aRSIR arrays equal to the aRSI array. This seemed to solve the problem of the aRSI array getting sorted erroneously. The second thing is the use of the sort() method . If you don't specify a compare function, it will do the sort based on a string comparison instead of a numerical comparison. See the function, Numbers, in the code below. Hope this helps.

    PHP Code:
    function preMain() {
        
    setStudyTitle("Adaptive Banded RSI ");
        
    setCursorLabelName("Upper Band",  0);
        
    setCursorLabelName("Middle Band"1);
        
    setCursorLabelName("Lower Band",  2);
        
    setCursorLabelName("RSI",         3);
        
    setDefaultBarFgColor(Color.khaki0);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarFgColor(Color.khaki2);
        
    setDefaultBarFgColor(Color.red,  3);
        
    addBand(50.0PS_SOLID2Color.black);


        var 
    fp1 = new FunctionParameter("PriceSource"FunctionParameter.STRING);
        
    fp1.setName("RSI Price Source");
        
    fp1.addOption("Open");
        
    fp1.addOption("High");
        
    fp1.addOption("Low");
        
    fp1.addOption("Close");
        
    fp1.addOption("HL/2");
        
    fp1.addOption("HLC/3");
        
    fp1.addOption("OHLC/4");
        
    fp1.setDefault("Close");
        
        var 
    fp2 = new FunctionParameter("rsiLength"FunctionParameter.NUMBER);
        
    fp2.setName("RSI Length");
        
    fp2.setLowerLimit(1);
        
    fp2.setDefault(13);
        
        var 
    fp3 = new FunctionParameter("LKBK"FunctionParameter.NUMBER);
        
    fp3.setName("Lookback");
        
    fp3.setLowerLimit(1);
        
    fp3.setDefault(8);

        var 
    fp4 = new FunctionParameter("vPercT"FunctionParameter.NUMBER);
        
    fp4.setName("Percentage");
        
    fp4.setLowerLimit(.00001);
        
    fp4.setDefault(20);
        
    }

    var 
    aRSI null;
    var 
    aRSIS;// = null;
    var aRSIR;// = null;
    var vRSI null;
    var 
    bEdit true;
    var 
    anumrnd nullnUB nullnLB nullnMB null;

    function 
    main(PriceSourcersiLengthLKBKvPercT) {

        if (
    bEdit == true || aRSI == null ){//|| aRSIR == null || aRSIS == null) { 
            
    aRSI = new Array(LKBK);
            
    aRSIR = new Array(LKBK);
            
    aRSIS = new Array(LKBK);
            
    bEdit false;
        }
        
        if (
    getBarState() == BARSTATE_ALLBARS){
            
    RSIstudy = new RSIStudy(rsiLengthPriceSource);
        }
        
        
    vRSI RSIstudy.getValue(RSIStudy.RSI);
        if (
    vRSI == null) return;
        
        if (
    getBarState() == BARSTATE_NEWBAR) {// && vRSI != null
            
    aRSI.pop();
            
    aRSI.unshift(vRSI);
            for (var 
    0LKBK; ++i) {
                
    aRSIS[i] = aRSI[i];
                
    aRSIR[i] = aRSI[i];
            }
            
    aRSIS.sort(Numbers);
            
    aRSIR.reverse();
        }
        
        
    //anumrnd = Math.ceil((vPercT/100)*LKBK);
        //nUB = aRSI[anumrnd-1];
        //nLB = aRSIR[anumrnd-1];
        //nMB = (nUB+nLB)/2;
        
        
    return (aRSI);//(nUB, nMB, nLB, vRSI);
        //return new Array(nUB, nMB, nLB, vRSI);
    }

    function 
    Numbers(a,b) {
        return (
    a-b);   // smallest to largest
        //return (b-a); // largest to smallest

    Attached Files
    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
      Jason,

      This is similar in nature to problems I have had before which have to do with populating global arrays with global values. To get around this, I populate arrays with local variables whenever possible.

      Problems you can get into there by using globals is that every value in the array, since populated with a global, take on the same value as the most recent global.

      Evidently, you did get around that by coding it the way you did, good job Jason. I had been working on the efs too, so I took what you had done, and added the trace function to the end and played with it a little. Sure enough, it does exactly what you have indicated. Attached is the efs that Jason corrected that outputs the arrays to the C:\Program Files\eSignal\FormulaOutput directory when you run the efs. The file's name is Adaptive Band Trace.txt
      Attached Files

      Comment


      • #4
        Thanks for the great advice guys. Here is the final script of what I was trying to accomplish. The original script was a bit off with regard to the reversing of the Array and the band levels.

        Again thanks for the expert help.
        Attached Files

        Comment

        Working...
        X