Announcement

Collapse
No announcement yet.

old ergodic

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

  • old ergodic

    This is a really old ergodic TSI, that seems to work for me. Unfortunately, when I leave it running in realtime, and the refresh the chart at the end of the day, the results differ. i can't seem to find the problem.


    Code:
    /********************************************************************
    Title:		Ergodic TSI (True Strength Index) for eSignal 7.x
    Coded By:   Chris D. Kryza (Divergence Software, Inc.)
    Email:      [email][email protected][/email]; [email][email protected][/email]
    Web:		[url]www.sr-analyst.com[/url]
    Incept:     11/02/2003
    Version:    1.1.0
    
    
    =====================================================================
    Fix History:
    
    07/23/2004 -	Added bar delay before first calculation as well as
    1.1.0			divide-by-zero check.
    
    11/02/2003 -   	Initial Release
    1.0.0
    
    =====================================================================
    
    Dislaimer: For educational purposes only! Obviously, no guarantees 
    whatsoever and use at your own risk.
    
    **********************************************************************/
    
    
    //Global Variables
    var nBarCounter			= 0;
    
    var nCoeff1				= 0;
    var nCoeff2				= 0;
    var nCoeff3				= 0;
    
    var aEMA1				= new Array();
    var aEMA2				= new Array();
    var aEMA3				= new Array();
    var aEMA4				= new Array();
    var aEMA5				= new Array();
    
    var aFPArray			= new Array();
    
    var bInitialized		= false;
    
    
    //== PreMain function required by eSignal to set things up
    function preMain() {
    var x;
    
        setPriceStudy(false);
        setStudyTitle("Ergodic TSI");
        setCursorLabelName("egTSI", 0);
        setCursorLabelName("egSIG", 1);    
        setDefaultBarFgColor( Color.blue,  0 );
        setDefaultBarFgColor( Color.red,   1 );   
        setShowTitleParameters( false ); 
        //addBand( 0, PS_SOLID, 1, Color.teal, -99998 );
        //addBand( -20, PS_SOLID, 1, Color.teal, -99999 );
        
        //initialize formula parameters
    	x=0; 
    	aFPArray[x] = new FunctionParameter( "Period1", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Long MA" );
    		setLowerLimit( 2 );
    		setUpperLimit( 200 );
    		setDefault( 32 );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "Period2", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Short MA" );
    		setLowerLimit( 1 );
    		setUpperLimit( 200 );
    		setDefault( 5 );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "SigPeriod", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Signal Period" );
    		setLowerLimit( 2 );
    		setUpperLimit( 200 );
    		setDefault( 5 );
    	}  
    
    	x++;
    	aFPArray[x] = new FunctionParameter( "Period3", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Momentum Period" );
    		setLowerLimit( 1 );
    		setUpperLimit( 200 );
    		setDefault( 1 );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "lineColor", FunctionParameter.COLOR );
    	with( aFPArray[x] ) {
    		setName( "Line Color" );
    		setDefault( Color.blue );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "sigColor", FunctionParameter.COLOR );
    	with( aFPArray[x] ) {
    		setName( "Signal Line Color" );
    		setDefault( Color.red );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "lineThick", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Line Thickness" );
    		setLowerLimit( 1 );
    		setUpperLimit( 10 );
    		setDefault( 2 );
    	}  
    	
        
        for (x=0; x<3; x++) {
        	aEMA1[x] = 0.0;
        	aEMA2[x] = 0.0;
        	aEMA3[x] = 0.0;
        	aEMA4[x] = 0.0;
        	aEMA5[x] = 0.0;
        }
    
    }
    
    //== Main processing function
    function main( Period1, Period2, SigPeriod, Period3, lineColor, sigColor, lineThick ) {
    var x;
    	
    	//script initializing
        if (getBarState() == BARSTATE_ALLBARS) {
            return null;
        }
        
        if ( bInitialized == false ) {
        
    		//determine exponential moving average coefficients
    		nCoeff1 = 2 / (Period1+1);
    		nCoeff2 = 2 / (Period2+1);
    		nCoeff3 = 2 / (SigPeriod+1);
    		
    		setDefaultBarFgColor( lineColor, 0 );
    		setDefaultBarFgColor( sigColor, 1 );
    		setDefaultBarThickness( lineThick, 0 );
    		setDefaultBarThickness( lineThick, 1 );		
    		
    		nBarCounter = 0;
        
        	bInitialized = true;
        }
        
        //shift storage arrays on new bar 
        if (getBarState() == BARSTATE_NEWBAR) {
    
        	nBarCounter++;
        	
    		aEMA1.pop();
    		aEMA1.unshift( 0 );
    		
    		aEMA2.pop();
    		aEMA2.unshift( 0 );
    
    		aEMA3.pop();
    		aEMA3.unshift( 0 );
    
    		aEMA4.pop();
    		aEMA4.unshift( 0 );
    		
    		aEMA5.pop();
    		aEMA5.unshift( 0 );
    		
        	
        }
       
       
       	if ( nBarCounter < Math.max( Period1, Period2, Period3, SigPeriod)+10 ) return;
       	
    	//X-Day ROC
    	nValue = close(0)-close(-Period3);
    	
    
    	//calc the exponential averages
    	aEMA1[0] = ( nValue * nCoeff1 ) + ( aEMA1[1] * ( 1.0-nCoeff1 ) );
    	aEMA2[0] = ( aEMA1[0] * nCoeff2 ) + ( aEMA2[1] * ( 1.0-nCoeff2 ) );
    	
    	aEMA3[0] = ( Math.abs(nValue) * nCoeff1 ) + ( aEMA3[1] * ( 1.0-nCoeff1 ) );
    	aEMA4[0] = ( aEMA3[0] * nCoeff2 ) + ( aEMA4[1] * ( 1.0-nCoeff2 ) );
    	
    	if ( aEMA4[0]==0 )
    		nTSI = 100.0 * aEMA2[0]
    	else
    		nTSI = 100.0 * ( aEMA2[0] / aEMA4[0] );
    	
    	//calc signal line as exp average of TSI
    	aEMA5[0] = ( nTSI * nCoeff3 ) + ( aEMA5[1] * (1.0-nCoeff3) );
    	
    	
    	if ( nBarCounter > Period1+Period2 ) {
    		return new Array( nTSI, aEMA5[0] );
    	}
    
    }
    Last edited by atlas; 01-25-2011, 10:20 PM.

  • #2
    Re: old ergodic

    atlas
    Because the study uses exponential averages which are iterative by nature [ie they depend on their previous values] the results will be different unless when you refresh the chart you also load the same exact bars you had when you first loaded the formula in that chart [so that the calculations start precisely on the same bar].
    Furthermore refreshing a chart does not reset any global variables so if there are any calculations that depend on these they will inevitably cause some differences unless you reset them
    Alex


    Originally posted by atlas
    This is a really old ergodic TSI, that seems to work for me. Unfortunately, when I leave it running in realtime, and the refresh the chart at the end of the day, the results differ. i can't seem to find the problem.


    Code:
    /********************************************************************
    Title:		Ergodic TSI (True Strength Index) for eSignal 7.x
    Coded By:   Chris D. Kryza (Divergence Software, Inc.)
    Email:      [email][email protected][/email]; [email][email protected][/email]
    Web:		[url]www.sr-analyst.com[/url]
    Incept:     11/02/2003
    Version:    1.1.0
    
    
    =====================================================================
    Fix History:
    
    07/23/2004 -	Added bar delay before first calculation as well as
    1.1.0			divide-by-zero check.
    
    11/02/2003 -   	Initial Release
    1.0.0
    
    =====================================================================
    
    Dislaimer: For educational purposes only! Obviously, no guarantees 
    whatsoever and use at your own risk.
    
    **********************************************************************/
    
    
    //Global Variables
    var nBarCounter			= 0;
    
    var nCoeff1				= 0;
    var nCoeff2				= 0;
    var nCoeff3				= 0;
    
    var aEMA1				= new Array();
    var aEMA2				= new Array();
    var aEMA3				= new Array();
    var aEMA4				= new Array();
    var aEMA5				= new Array();
    
    var aFPArray			= new Array();
    
    var bInitialized		= false;
    
    
    //== PreMain function required by eSignal to set things up
    function preMain() {
    var x;
    
        setPriceStudy(false);
        setStudyTitle("Ergodic TSI");
        setCursorLabelName("egTSI", 0);
        setCursorLabelName("egSIG", 1);    
        setDefaultBarFgColor( Color.blue,  0 );
        setDefaultBarFgColor( Color.red,   1 );   
        setShowTitleParameters( false ); 
        //addBand( 0, PS_SOLID, 1, Color.teal, -99998 );
        //addBand( -20, PS_SOLID, 1, Color.teal, -99999 );
        
        //initialize formula parameters
    	x=0; 
    	aFPArray[x] = new FunctionParameter( "Period1", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Long MA" );
    		setLowerLimit( 2 );
    		setUpperLimit( 200 );
    		setDefault( 32 );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "Period2", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Short MA" );
    		setLowerLimit( 1 );
    		setUpperLimit( 200 );
    		setDefault( 5 );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "SigPeriod", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Signal Period" );
    		setLowerLimit( 2 );
    		setUpperLimit( 200 );
    		setDefault( 5 );
    	}  
    
    	x++;
    	aFPArray[x] = new FunctionParameter( "Period3", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Momentum Period" );
    		setLowerLimit( 1 );
    		setUpperLimit( 200 );
    		setDefault( 1 );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "lineColor", FunctionParameter.COLOR );
    	with( aFPArray[x] ) {
    		setName( "Line Color" );
    		setDefault( Color.blue );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "sigColor", FunctionParameter.COLOR );
    	with( aFPArray[x] ) {
    		setName( "Signal Line Color" );
    		setDefault( Color.red );
    	}  
    	x++;
    	aFPArray[x] = new FunctionParameter( "lineThick", FunctionParameter.NUMBER );
    	with( aFPArray[x] ) {
    		setName( "Line Thickness" );
    		setLowerLimit( 1 );
    		setUpperLimit( 10 );
    		setDefault( 2 );
    	}  
    	
        
        for (x=0; x<3; x++) {
        	aEMA1[x] = 0.0;
        	aEMA2[x] = 0.0;
        	aEMA3[x] = 0.0;
        	aEMA4[x] = 0.0;
        	aEMA5[x] = 0.0;
        }
    
    }
    
    //== Main processing function
    function main( Period1, Period2, SigPeriod, Period3, lineColor, sigColor, lineThick ) {
    var x;
    	
    	//script initializing
        if (getBarState() == BARSTATE_ALLBARS) {
            return null;
        }
        
        if ( bInitialized == false ) {
        
    		//determine exponential moving average coefficients
    		nCoeff1 = 2 / (Period1+1);
    		nCoeff2 = 2 / (Period2+1);
    		nCoeff3 = 2 / (SigPeriod+1);
    		
    		setDefaultBarFgColor( lineColor, 0 );
    		setDefaultBarFgColor( sigColor, 1 );
    		setDefaultBarThickness( lineThick, 0 );
    		setDefaultBarThickness( lineThick, 1 );		
    		
    		nBarCounter = 0;
        
        	bInitialized = true;
        }
        
        //shift storage arrays on new bar 
        if (getBarState() == BARSTATE_NEWBAR) {
    
        	nBarCounter++;
        	
    		aEMA1.pop();
    		aEMA1.unshift( 0 );
    		
    		aEMA2.pop();
    		aEMA2.unshift( 0 );
    
    		aEMA3.pop();
    		aEMA3.unshift( 0 );
    
    		aEMA4.pop();
    		aEMA4.unshift( 0 );
    		
    		aEMA5.pop();
    		aEMA5.unshift( 0 );
    		
        	
        }
       
       
       	if ( nBarCounter < Math.max( Period1, Period2, Period3, SigPeriod)+10 ) return;
       	
    	//X-Day ROC
    	nValue = close(0)-close(-Period3);
    	
    
    	//calc the exponential averages
    	aEMA1[0] = ( nValue * nCoeff1 ) + ( aEMA1[1] * ( 1.0-nCoeff1 ) );
    	aEMA2[0] = ( aEMA1[0] * nCoeff2 ) + ( aEMA2[1] * ( 1.0-nCoeff2 ) );
    	
    	aEMA3[0] = ( Math.abs(nValue) * nCoeff1 ) + ( aEMA3[1] * ( 1.0-nCoeff1 ) );
    	aEMA4[0] = ( aEMA3[0] * nCoeff2 ) + ( aEMA4[1] * ( 1.0-nCoeff2 ) );
    	
    	if ( aEMA4[0]==0 )
    		nTSI = 100.0 * aEMA2[0]
    	else
    		nTSI = 100.0 * ( aEMA2[0] / aEMA4[0] );
    	
    	//calc signal line as exp average of TSI
    	aEMA5[0] = ( nTSI * nCoeff3 ) + ( aEMA5[1] * (1.0-nCoeff3) );
    	
    	
    	if ( nBarCounter > Period1+Period2 ) {
    		return new Array( nTSI, aEMA5[0] );
    	}
    
    }

    Comment

    Working...
    X