Low Pass vs SMA

Discussion in 'Strategy Building' started by luckyputanski, Sep 16, 2012.

  1. So, as a programmer, you cannot answer whether my code is correct?
     
    #21     Sep 16, 2012
  2. They're both 100% effective at calculating the number they claim to be calculating.
     
    #22     Sep 16, 2012
  3. I replaced sma260 with this and results are pathetic.
     
    #23     Sep 16, 2012
  4. jcl

    jcl

    Well, you're a programmer too and are not sure, or are you? I can also only compare your code with mine.

    Not even God can see at a first glance if a code works correct or not - that's why we have debuggers. Your code works in a different way than mine, but it appears indeed correct at a second glance. However you would save yourself and me a lot time when we used a similar platform so that there's no uncertainty if it's correctly implemented or not.

    With your code, can you replicate the curve that I posted?

    That would be the first step, the next step would be to determine when and under which circumstances less lag generates better performance - or not.
     
    #24     Sep 17, 2012
  5. jcl

    jcl

    By the way, there are many alternative functions with low lag - you might try them too. This is the Zero-Lag MA by John Ehlers:

    Code:
    // Zero-lag Moving Average
    var rError;
    var rEMA;
    var ZMA(var* Data,int Period)
    {
    	var *vEMA = series(*Data,2);
    	var *vZMA = series(*Data,2);
    	var a = smoothF(Period);
    	vEMA[0] = a*Data[0]+(1.-a)*vEMA[1];	
    	rEMA = vEMA[0]; 
    
    	rError = 1000000;
    	var Gain,GainLimit=5,BestGain=0;
    	for(Gain=-GainLimit; Gain < GainLimit; Gain += 0.1)
    	{
    		vZMA[0] = a*(vEMA[0] + Gain*(Data[0]-vZMA[1])) + (1-a)*vZMA[1];
    		var Error = Data[0] - vZMA[0];
    		if(abs(Error) < rError) {
    			rError = abs(Error);
    			BestGain = Gain;
    		}
    	}
    	return vZMA[0] = a*(vEMA[0] + BestGain*(Data[0]-vZMA[1])) + (1-a)*vZMA[1];
    }
    It has not zero lag of course, but it has low lag, almost like a lowpass filter. And this is a 3 pole Butterworth filter:

    Code:
    var Butterworth(var *Data,int Period)
    {
    	var a = exp(-PI / Period);
    	var b = 2*a*cos(1.738*PI / Period);
    	var c = a*a;
    	var c1 = b + c;
    	var c2 = -(c + b*c);
    	var c3 = c*c;
    	var c0 = 1 - c1 - c2 - c3;
    
    	var* Filt = series(*Data,4);
    	return Filt[0] = c0*Data[0] + c1*Filt[1] + c2*Filt[2] + c3*Filt[3];
    }
    I haven't done much with those functions yet, but they might be worth testing when you need faster signals from an MA.
     
    #25     Sep 17, 2012
  6. kut2k2

    kut2k2

    The problem with "zero-lag" MAs is that they are often noisier than the original signal.
     
    #26     Sep 17, 2012
  7. jcl

    jcl

    Yes, all functions that manipulate the frequency spectrum of the signal add more or less noise. This is also true for normal MAs without zero lag.
     
    #27     Sep 18, 2012
  8. kut2k2

    kut2k2

    Not true. Normal MAs are lowpass filters, and the output of a lowpass filter is always less noisy than the input, otherwise what's the point of using them? "Zero-lag MAs" aren't true lowpass filters, so no surprise that they add noise.
     
    #28     Sep 18, 2012
  9. Price breakout schemes are low pass filters.
     
    #29     Sep 18, 2012
  10. jcl

    jcl

    You're right of course: not the noise, but the signal-to-noise ratio can become worse. The absolute noise, i.e. the high frequencies in the price signal, are always reduced by all functions mentioned here.
     
    #30     Sep 18, 2012