Why A Cascading SMA Approximate A Gaussian Filter ?Introduction
The gaussian filter don't see many uses in technical analysis and financial data smoothing in general, however it possess really interesting properties and a really close relationship with the simple moving average.
The gaussian filter is a filter which possess a function approximately gaussian (bell shaped curve) as : impulse response, step response and frequency response. This characteristic is pretty cool actually, the gaussian function is always mysterious.
Now why do I talk about sma and estimation ? Well it is true, you can estimate a gaussian filter by applying an sma to another sma and so on such as : sma(...sma())
But why ? Just why is that so ? Well there are a lot of explanations, some of them involving the central limit theorem which would lead to a statistical explanation but I'll give a simpler explanation of this case by using signal processing.
Understanding Impulses Responses
The impulse response of a filter is the filter output using an impulse function as input or more simply : filter(impulse)
The impulse function is a simple function equal to 1 at a certain point in time, for example we can use : impulse = 1 if t = 10 else 0, where t = 1,2,3...inf
The impulse response of a filter tell us how to actually make the filter, for example :
a = filter(impulse)
b = sum(input*a) = filter(input)
This process is called convolution, and is simply the sum of the product of two functions, the input function and the kernel function, a kernel is just a way to say filter coefficients.
The Explanation
Now that you know that, let's explain why sma(...sma()) approximate a gaussian filter.
To do so let's take an impulse function and let's start applying an sma to it such as sma(impulse) (the sma period doesn't matter here)
Only one sma give a constant, let's use two sma's such as sma(sma(impulse))
This give us a triangular function, this is why sma(sma()) is often called triangular moving average, now let's repeat the process and add more sma's.
Do you see ? We are approximating a gaussian curve, if we do it many times the approximation will be even more correct.
Now let's recall :
The impulse response of a gaussian filter is a gaussian function f
The impulse response of many sma's give a function f' who approximate a gaussian function, therefore f ≈ f'
So sum(input*f') ≈ sum(input*f) and therefore sma(...sma(input)) ≈ gaussfilter(input)
Note : the process of applying a filter several time is called cascading
Conclusion
Simple isn't it ? The simple moving average is always fun to use and posses many properties, now you don't want to use such method because it's mega inefficient.
But maybe that you want to know about an efficient gaussian filter implementation ? I can work on it. Thanks for reading !
Filter
Digital Filters And DSPIntroduction
Digital signal processing (dsp) is used to manipulate signals and extract information from them. Among all the tools available, digital filters are the most common ones to use, in fact you are certainly using one during your analysis. In this post i will share some of the knowledge i have associated with filters and their properties.
I will try to be the more simple possible with my explanations for those with a light mathematical background :)
Before starting talking about filters, lets see some elementary things about dsp, this will help you understand what we will see next.
Time Domain
It is possible to see signals in different domains. The time domain show the signal with respect to time. For example when you see a price chart you see the price with respect to time, so you are on the time domain. A signal in the time domain is "one dimensional" because there is only one independent variables.
Frequency Domain
Frequency domain is another way to see a signal, this show the signal with respect to a frequency, in fact you are seeing the frequency content of the signal. In order to understand this i will introduce you to another powerful tool of digital signal processing, the "Fourier Transform". Lets look at some graphical explanations.
Lets take a signal we will call S
This signal is actually the sum of 3 Sine waves of different Frequencies
The one in red have a frequency of 0.07, the one in yellow have a frequency of 0.04 and the one in purple a frequency of 0.02, frequency is defined as the number of cycles in 1 unit time and is expressed in hertz Hz , here we will use "price bar" as unit time, it will be easier to see it this way. The Period is the reciprocal of the frequency, its the number of price bars in 1 cycle. To convert a frequency to a period just divide 1 by the frequency. The amplitude is the maximum absolute value of the signal, often said peak amplitude, for our sine waves the amplitude is 1.
So what does the Fourier transform ? Well the Fourier transform convert a signal in the time domain to the frequency domain. In other terms it decompose our signal in a sum of sine waves of different frequency and amplitude.In our signal S its Fourier transform would look like this :
Each bar is called a frequency bin or frequency component, they show the frequency and amplitude of one sine wave, each color represent the color of each sine waves i showed earlier. The abscissa x represent the frequency, the ordinate y represent the amplitude. Frequency bins on the left have a lower frequency than those in the right. If we sum each sine waves with the frequency and amplitude each bins give us we would get back our signal S
Its important to know this since filters interact with the frequency content, and the Fourier transform can tell us how.
What Is A Filter ?
A filter is processing tool that allow us to modify the frequency content of a signal, this mean that if we filter a signal, we will remove or change the amplitude of each frequency bin.
Interaction With The Frequency Domain
We will later see filters in the time domain but lets see them in frequency domain for the moment. In order to see how a filter interact with the frequency content of a signal we must look at the frequency response of the filter. A frequency response look like this :
The frequency response is shown in the frequency domain. So what does the red curve represent ? Well this curve show us how the filter interact with each frequency component, lets look at some terms of the frequency response.
The pass band area let each frequency components in that area unchanged, the stop band attenuation or transition band area only change the amplitude of each frequency components in that area, the stop band area remove each frequency components in that area. For example :
Here we have the frequency domain of a signal, after passing a filter to this signal the frequency domain will look like this :
In the image showed earlier you have a blue line, this line delimit the boundary between the pass band and the stop band attenuation, and this boundary is named cutoff frequency , the cutoff frequency is a value expressed in hertz. Actually when you are using a moving average, the length parameter control the position of the cutoff, but we will see moving averages later in more depth.
Types Of Filters
Now that we understand how a filter interact with frequency domain we must talk about different types of filters, filters can interact in different ways with the frequency components.
Low Pass Filters :
The name is straightforward, those filters let pass low frequency components and remove the high ones. Frequency response of low pass filters look like this :
High Pass Filters :
They let pass high frequency components while removing low ones.
Bandpass Filters :
A mix of the two previous filters, they let pass frequency components in a certain range.
Those can have two-cutoff f1, f2 with bandwidth f2 - f1 or one cutoff and one bandwidth parameter.
Band-Stop Filters :
Sometime called Notch filters or band-reject, they do the contrary of the bandpass filter, instead of letting pass frequency components in a certain range they remove them.
Design Of Different Types Of Filters
Low Pass Filters :
Low pass filters are used to remove high frequencies, when you remove high frequency components of a signal the result is a smoother signal, high frequency create irregularities in a signal and create roughness this is why the result is smoother. Low pass filters are also the core of the design of other filter types. The simplest low pass filter is called a simple moving average or running/rolling - mean/average, or boxcar filter, you will understand why the name "Boxcar" later.
The average value or mean of a signal is the sum of all values divided by the number of values, a moving average instead of using all the values will use a proportion of them.A simple moving average can be computed in different ways, here are the easiest ones.
1/n * sum(S,n)
sum(1/n * S,n)
sum(S,n)/n
change(cum(S),n)/n
where S is the signal we want to filter and n the period or number of data points we want to average.
The frequency response of a simple moving average look like this :
The higher the period the more little bumps in the stop band will be created, this is why the moving average still appear a bit rough, its because not all the frequencies in the stop band are removed, some are just attenuated. Those "bumps" are called ripples, when they are situated in the pass band we call them pass band ripples, when they are in the stop band we call them stop band ripples. We will later see filters who does not have ripples.
High Pass Filters
They remove low frequencies of a signal, its really easy to calculate those, the only thing you have to do is subtract your signal S with a low pass filter, the result show all the signal who have been filtered by the low pass filters. The frequency response of the low pass filter is then reversed. Here the frequency response of a high pass moving average filter :
They are really useful because its quite hard to forecast trends in a signal, but its way easier to forecast mid term cycles.
Bandpass Filters
They let pass frequencies in a range and they are also really easy to compute, first compute a high-pass filter by subtracting the price to a moving average low pass filter, then apply a low pass filter to the result and you end up with a bandpass filter, easy isn't it ? When creating a bandpass filter you can get the pictures of the frequency response of the filter during each step.
First step -> high pass filtering
Second Step -> Low pass filtering of the first step result
Band-Stop Filter
They are the opposite of the bandpass filter, so in order to construct those you just need you signal S and your signal S filtered by a bandpass filter. Subtract your signal S with the bandpass filtered version and you get a band-stop filter.
Filter Lag
Lag is defined as the effect of filters to show past frequencies, this delay is quite unfortunate for us since a filter with zero lag would violate causality and could provide what is called "The Holy Grail". However it is possible to minimize lag, one way is to amplify a frequency range of the signal and then filter the result, a filter describing this process is the zero-lag exponential moving average in the form of :
L = (Period - 1)/2
Y0 = S + (S - S(L)) -> Amplification Process
Y1 = EMA(Y0,Period) -> Filtering Process
Other methods exist but at the end getting optimal smoothing with minimum lag is not something classical filters can do. This is why adaptive filters such as Kalman and Wiener filters where made, but this is another subject that is more complicated.
Impulse Response
We are entering a really interesting topic, the topic of impulses responses and convolution. First what is an impulse ? An impulse is just defined as a really brief signal, here an example
The impulse response denoted h(t) is the reaction of a system to an impulse, so the impulse response of a filter is the filter using an impulse an input. Now lets see the impulse response of a simple moving average of period 5 (n = 5) , you will understand why they are called boxcar filters.
its just a "box", puns apart there is also something more than incredible with impulse responses, do you remember how to construct a moving average ? One of the ways was : sum(1/n * S,n) , 1/5 = 0.2, now look at the impulse response from closer
Its 0.2, or 1/5, nice isn't it ? When the impulse is equal to 1, the impulse response of a filter h(t) give us values called filter coefficients and this introduce the concept of convolution.
Convolution
The subject is vast but in resume convolution is the process of combining two function to create a third function. The symbol of convolution is ∗ not to be confounded with * who is used for multiplication.
To make it easier to understand convolution lets take back our 5 period moving average case and its impulse response h(t) . Now lets make an operation describing convolution
S ∗ h(t) = sum(S * h(t),5)
This operation give us a moving average of period 5. So impulse responses can give you really useful information about your filter.
IIR Filters
There are 2 filter family, F inite I mpulse R esponse filters or FIR and I nfinite I mpulse R esponse filters. IIR filters use recursion, this mean that they use output values as input, if we check the impulse response of those filter h(t) you will see that h(t) goes on forever after the impulse, this is why they are called infinite impulse response. The most well known IIR filter is the exponential moving average (ema), or exponential averager which is described as :
y = aS(t) + (1-a)y(t-1)
where a is called a smoothing constant and 0 < a < 1 .
An IIR filter can also be in the more common form :
y(t) = a0 * S(t) + a1 * S(t-1) + b1 * y(t-1)
Advanced Filter Design
Based on which filter you want to design (FIR/IIR) you will use different methods. FIR filters are easier to design, first select what frequency response your filter should have, then compute the Fourier transform of this frequency response, this will give you the impulse response and therefore the coefficients of the filter you want to design, then you only need to apply convolution. So basically, the Fourier transform of the filter impulse response give you the frequency response of the filter and vice versa.
IIR filters are way more complicated to design, you will need the z-transform and this is something that i don't really know about, how unfortunate.
Conclusions
I hope it wasn't that complicated because it shouldn't be, of course you can make filters without knowing all this, but its always cool to have more information. If you don't understand something or think i have made an error somewhere don't hesitate to mention it in the comment :)
Thanks for reading !
Approximating A Least Square Moving Average In PineLeast Squares Moving Average, Running Least Squares, Regression Line or even Running Line, this method is among the most popular ones in statistics and technical analysis.
The LSMA is extremely useful, it approximate the price pretty well and can be considered as one of the best low-lagging filters out there. Knowing how this filter is made can be really interesting. May the methods i share below inspire you to create great indicators or start coding in pine :)
A Least Squares Moving Average is defined by Tradingview as :
A line that best fits the prices specified over a user-defined time period. It is calculated using the least squares method. The result of this function is calculated using the formula: linreg = intercept + slope * (length - 1 - offset), where length is the y argument, offset is the z argument, intercept and slope are the values calculated with the least squares method on source series (x argument).
Alright, we wont use the offset parameter for our approximations, so how to calculate a least squares moving average ? If you find the mathematical formula of it you will certainly ask yourself "what are all of those maths" . But its ok, in the Pinescript you can just use the linreg() function, or you could calculate it like that :
slope = correlation(close,n,length) * (stdev(close,length)/stdev(n,length))
intercept = sma(close,length) - slope*sma(n,length)
linreg = slope*n + intercept
Ok, but can we use different estimation methods ? Certainly, the key of the LSMA is only the correlation coefficient after all, all the other parameters can be estimated.
Standard Score Or Rescaling A Line To The Price
Rescaling a line to the price is easy to do, it will give a similar result as the LSMA but it is faster to write, here the code :
A = (n - sma(n,length))/stdev(n,length) * correlation(close,n,length)
B = sma(close,length) + A*stdev(close,length)
Easier no ? We first standardized a line (n) and multiplied it by its correlation with the price, our first parameter A is dimensionless .
Then we rescaled the result to the price by multiplying our parameter with the price standard deviation and summing this result to the price moving average.
here the difference between our method and the classic LSMA of both period 100
If you put both together you wont see any difference. Overshoots can be reduced by modifying the standard deviation size.
Correlation Rescaling
The correlation coefficient is the core of a LSMA, if we rescale it we can approximate a LSMA, here the code :
a = (correlation(close,n,length) + 1)/2
b = sma(close,length) + stdev(close,length)*1.7
c = sma(close,length) - stdev(close,length)*1.7
k = c + a*(b-c)
The correlation coefficient oscillate in a range of 1/-1, we first scale it in a range of 1/0. Then you may have recognized the b and c formulas, they are the one used in bollinger bands,
the standard deviation is multiplied by 1.7 because it was the number who best approximated a LSMA, but it could be any number defined by the user, something interesting is that this method to can fix overshoots in a classic LSMA using lower multiplier. Since our correlation is in a range of 1/0 we can rescale it to the price thanks to the method used in k.
In red our method, in blue the LSMA of both period 100.
Here the standard deviation is not multiplied by a number, this result in less overshoot.
In order to have even more manipulation over the LSMA i will try to estimate the correlation coefficient the best i can :)
So here you go, i hope you will find a use for it.
Credibility filter for cryptocurrencies (part 2)Dear friends,
In my last article, I described quite a simple filter that will help even a beginner identify whether a certain altcoin is worth buying or not. I just want to say it is not a trading system and doesn’t provide any trade signals. This tool performs only a single, but very important, task: to identify among all that diversity of altcoins those, which attract a lot of buyers and are worth investing.
As the crypto market will stop falling down sooner or later, and the first matter for any investor will be where to invest their capitals. In this article, I’m going to apply this filter in practice and find out which cryptocurrency asset we should pay our attention to. I’ll note, I developed this filter by myself, but I’m not ruling out that one of the numerous books on trading may describe something similar.
So, I’d like to insert the disclaimer in the beginning that all the similarities with other authors’ tools in my previous and present posts are accidental.
I started testing this filter not that long ago and have applied it only to cryptocurrencies. Use of this filter in other markets is an open question. But, taking into account the basic rules and concepts of the market, it is likely to be of use at least in the stock market. The only limitation, I should mention in the beginning, is that the filter is appropriate for only long timeframes, starting from one day. Application of the filter to shorter timeframes won’t provide a fair view, as the cryptocurrencies moves inside a day can differ a lot due to intraday speculations of big traders.
So, let’s move on directly to the filter:
Step 1. Identify the market benchmark (trading instrument in the form of a large company – the leader of a market or a stock index that approximately reflect the market general trend).
To start using the filter, the first that you need is to identify the market benchmark; in the stock market it is, as a rule, a stock index. In the cryptocurrency market, the benchmarking indicator of the market general trend is Bitcoin.
Step 2. Mark in the Benchmark 1D chart the last four price lows with different colors.
Pay attention that the more recent is the level, the stronger it is, compared with the others. That is why, it is important to follow the lines’ order in the chart and their location relative to the ticker. Different colors will help you do it.
Step 3. Study the ticker location relative to the levels.
• Identify the ticker’s location relative to the last low (in our case, it is orange).
• Identify the location of the orange level, compared to the previous low.
• Identify the number of levels above the ticker and below it at the current moment.
In the chart above, you see:
1. The ticker is higher than the last low (orange level).
2. Orange level is lower than the previous low (green line).
3. There is one level above the ticker/there are three levels below the ticker.
Having found out these three items, we have made up so-called market profile that is of use by itself, as it helps us assess the general market situation.
I’ll explain on the items:
1. Breaking through the closest support level is a crucial event, as at this level, there all stop orders of the short-term and middle-term traders, and so, their working out means that the market is rather weak. If this level is broken out in the uptrend, it is one of the strong trend reversals signals.
2. The location of the last local low relative to the previous shows the stage of the trend development. If the last low is lower than the previous one, it means that the bearish trend is extending and vice versa, if the last low is higher than the previous one, it suggests that there are buyers and either a consolidation, or a bullish trend develops.
3. Levels above the ticker are the resistance levels, and the levels below the ticker are the support levels.
Therefore, the more levels are below the ticker and the fewer ones are above it, the more are the support levels, the more investors are confident and the more promising is the market.
Step 4. Identify the lows for the studied trading instrument (make the instrument profile).
I suggest EOSUSD pair as an example. We mark the lows according to same pattern as for the benchmark; the deviation mustn’t be more than 2-3 bars. If for the instrument you’re analyzing, the lows are where the benchmark’s highs are, there is a negative between the benchmark and this instrument, so, you can’t apply this filter to it
In our case, everything is all right.
Finally, we see the following situation:
1. The ticker is lower than its last low (orange level).
2. Orange level is higher than the previous low (green line).
3. There are two levels above the ticker/there are two levels below the ticker.
Step 5. Compare the instrument profile with the market profile.
Finally, there is the main step, the culmination, of our analysis.
Let’s compare EOS performance with the benchmark, Bitcoin.
It is quite easy.
Remember, for Bitcoin, it is like this:
1. The ticker is higher than the last low (orange level).
2. Orange level is lower than the previous low (green line).
3. There is one level above the ticker/there are three levels below the ticker.
and for EOS it is like this:
1. The ticker is lower than its last low (orange level).
2. Orange level is higher than the previous low (green line).
3. There are two levels above the ticker/there are two levels below the ticker.
The conclusion is the following:
1. BTC: the ticker is higher than the last low / EOS: the ticker is lower than the last low.
2. BTC: orange level is below the precious low / EOS: orange level is above the precious low
3. BTC: There is one level above the ticker/there are three levels below the ticker / EOS: There are two levels above the ticker/there are two levels below the ticker.
Therefore, EOS is behind Bitcoin in two points out of three, so we can conclude that EOS performs worse than the market, and is less promising than Bitcoin, in investment terms. With such comparative analysis, there can be situations when the analyzed instrument has the same number of advantages as the benchmark; if so, the last, third point is the most important to decide which asset is better.
If the number of the suggested advantages for the analyzed asset and the benchmark is the same, such asset is considered to be investment-worth and interesting for investors. Such analysis, of course, can’t be limited by a single instrument. To understand how bad is the situation of a certain asset. You need to analyze at least three or four assets in the same market segment (it is more convenient to present it like a table).
In the end, just in case, I’ll again note that this filter doesn’t provide any buy or sell signals for a certain asset. If EOS performs worse than the market, it doesn’t means that it won’t be able to surge in near future or even be ahead of the benchmark by its momentum. This filter is just a try to find out the fundamental situation for a certain instrument by means of technical analysis; it is more like a tool for long-term investing, rather than for short-term speculations.
Good luck and good profits!
Best regards,
Mikhail @Hyipov
PS. If you agree with the forecast write “+” in the comments, if you don’t agree, put “-”. If you liked the post, just write thank you, and don’t forget to share the post with your friends. It is easy for you and I will be very pleased :)
Stay informed on the latest cryptocurrency news, follow my posts in the blog.
Credibility filter for cryptocurrencies (part 1)I will find out how relevant and efficient the filter is and explain the main rules and regularities.
Today, it is going to be a quite unusual post, as I’d like to suggest an analysis of not a single altcoin, but about 90 percent of cryptocurrencies, which are now present in the market.
This idea was suggested by my yesterday’s comparative analysis of EOS and ETH (see here) and one of educational articles (see HLEMA strategy). It is based on superficial analysis of altcoins, compared with the cryptocurrency benchmark Bitcoin, and division the cryptocurrencies into two groups: investment-appealing coins and those that are not worth buying.
This analysis is very easy to conduct and I think it to be the crucial moment, especially for beginners, as they will be able to avoid the mistakes that may result in huge losses in future.
Well, let’s analyze altcoins in order to learn how to cut the risks of a wrong purchase/sale of a cryptocurrency asset.
I’ll start with a tricky question. What do Monero and Litecoin have in common?
I can say right away, there is no point in comparing the teams, block-sizes or the network congestion.
Look at Litecoin price chart:
Now – at Monero price chart:
If you compare these two charts above, the similarity is clear. But the most important, we should pay attention to, is the local lows, to be more precise, they location relative to each other.
You see, there four clear lows in the price charts of these two cryptocurrencies. The very first one is the local summer low (orange line), the next low is the April’s bottom (green line), the next is the price drop in February (blue line) and November 2017, when all cryptocurrencies were sleeping before the rally in spring (purple line).
You can change the lines’ colours, it is only important that you see and understand the picture.
So, as I’ve said, these two assets (Monero and Litecoin) have very similar price charts, but the main similarity, is the location of low in relation to each other. It is the order of lows in the chart that allows identifying investment prospects of an altcoin and including it a certain group of coins.
We have a kind of credibility filter for cryptocurrencies that is based on two simple trading laws, which, I hope, nobody has doubts in.
Law 1: a broken out support level always becomes the future resistance level; and the broken out resistance level will always be the support level in future.
Law 2: the better an asset seems, compared with the market, the more buyers are interested in this instrument; and vice versa, the worse the asset looks, the fewer buyers are interested in this instrument.
These two rules seem obvious. However, for those, who have just started trading cryptocurrency or any other financial instrument, I’ll give some explanation.
In Litecoin 1D chart, there is a descending movement (bearish trend) for LTCUSD.
As you see, Litecoin price broke through the summer support level and dropped below the orange line. From below, the price move is limited by the level that I’ve marked as the low of November, 2017 (purple line); it will be the support level for the price chart. Once the price approaches the level, it is likely to rebound and go up.
When the price rolls back, the law 1 works and the broken out orange level will turn into the resistance and won’t let the ticker easily move on (see green arrows).
The same will be if the ticker breaks through purple line, the support dated November. If so, the broken out level will create a strong barrier to the ticker when it tries to regain the positions and go back to the starting point.
Therefore, taking this law into account, I understand that the most interesting is the asset, whose price chart has the most numerous support levels below and the least numerous resistances above.
To study the second law in detail, you just need to understand a simple fundamental thing. The piece chart in the screen results from trading between buyers and sellers.
Many beginners, as wells as not beginners, often confuse the action with the result and say that the chart directs buyers and sellers...which completely wrong.
Here is BTCUSD one-day chart with its yearly history:
The same BTCUSD one-day chart, zoomed in:
If you mark the above levels in the BTCUSD price chart and analyze the ticker’s location, relative to the present key levels, you’ll see that the price hasn’t broken through the latest support level, emerged in June; moreover, the other support levels haven’t been broken through too.
It is clear that BTCUSD has just penetrated the spring, green, level, but hasn’t went on lower.
Now, look at LTCUSD one-day chart, zoomed in:
If you compare the BTC USD situation with that of LTC USD, you’ll see that the Litecoin cryptocurrency is performing worse than the market.
From the point of view of trading psychology, it means that buyers have little interest in Litecoin, and so, sellers, trying to sell the asset, according to the general market pressure, don’t find any demand for their Litecoins. It makes them reduce the sell prices (ask) until there is a buyer. As a result, there is the price drop in the chart.
This buyer weakness may result from many reasons; I enumerate the most common one below:
• No big traders for the asset (diversification of risks/portfolio, exiting the market, expecting a suitable price);
• Manipulations by big traders in order to press the market as low as possible, following the general trend;
• Fundamental and inside factors of the asset that make buyers cancel purchases/start selling.
Of course, it is not the complete list, but in any case, this situation doesn’t suggest anything positive, and so, the asset is bears little interest for investment, and one should avoid buying it.
That is all for today. Next time, I’ll go on describing the way to identify promising assets, in the investment terms. I’m going to mark the key levels for altcoins to Bitcoin and find out which one is really worth buying.
I wish you good luck and good profits!
PS. If you agree with the forecast write “+” in the comments, if you don’t agree, put “-”. If you liked the post, just write thank you, and don’t forget to share the post with your friends. It is easy for you and I will be very pleased :)
Stay informed on the latest cryptocurrency news, follow my posts in the blog.
Good luck and good profits!
Best regards,
Mikhail @Hyipov