Testing what difference a filter makes
Forums › ProRealTime English forum › ProBuilder support › Testing what difference a filter makes
- This topic has 11 replies, 3 voices, and was last updated 4 years ago by Vonasi.
-
-
08/01/2019 at 11:14 AM #103957
I was considering some longer term investment strategy ideas when I got thinking about how to see how effective a filter is at keeping you out of the market or ensuring that you are at least trading in the right direction – or the most likely right direction! So I coded a simple indicator that first of all calculates what the market datum is by working out what percentage of all the candles are green and what percentage are red. It then does the same test but with a filter applied. This way we can test for example if there is a higher probability of green candles if we only look at candles that have their low higher than the close one year ago or if the close is greater than an average line or whatever filter you choose to apply.
In the first image which is of the SP500 monthly we can see that our datum is that green candles appear 60.4% of the time and red candles only 39.6%. So straight away we know that finding shorts is harder than finding longs. The filter applied in this example is testing if the last candles low is above the close 6 months ago or the high is below the close 6 months ago. If we apply this rule then we now get green candles 64.6% of the time if the low > close[6] and red ones 44.9% if the high < close[6]. So we can see that just applying that filter we have improved the probability of a green monthly candle by 4.2% and the probability of a red candle by 5.3%
The second image is of the EURUSD monthly and we can see straight away the mean reverting nature of this market as green candles appear 49.8% of the time and red 50.2%. If we apply the same 6 month look back filter then this changes to 55.9% green candles above the filter and 56.9% red candles below the filter. An improvement of 6.1% for green and 6.7% for red.
So it can be seen that considering filters in much longer time frames should help any strategy be trading only in the most probable direction. With MTF we can now create a strategy that considers various filters on various time frames and by using something like this indicator it should be possible to improve our probability of being right more than we are wrong!
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253//Filter Probability Tester//By Vonasi//20190801x = 6upfiltering = low[1] > close[x]//close[1] > close[x]//close[1] > average[x]//downfiltering = high[1] < close[x]//close[1] < close[x]//high[1] < close[x]//close[1] < average[x]////Everybarif barindex > x thenif close > open thenup = up + 1count = count + 1endifif close < open thendown = down + 1count = count + 1endifupperc = (up/count)*100downperc = (down/count)*100//With filtersif close > open and upfiltering thengreenupfilter = greenupfilter + 1upfiltercount = upfiltercount + 1endifif close < open and upfiltering thenredupfilter = redupfilter + 1upfiltercount = upfiltercount + 1endifif close > open and downfiltering thengreendownfilter = greendownfilter + 1downfiltercount = downfiltercount + 1endifif close < open and downfiltering thenreddownfilter = reddownfilter + 1downfiltercount = downfiltercount + 1endifgreenupfilterperc = (greenupfilter/upfiltercount)*100redupfilterperc = (redupfilter/upfiltercount)*100greendownfilterperc = (greendownfilter/downfiltercount)*100reddownfilterperc = (reddownfilter/downfiltercount)*100endifreturn upperc as "green datum", downperc as "red datum", greenupfilterperc as "green with up filter", greendownfilterperc as "green with down filter", redupfilterperc as "red with up filter", reddownfilterperc as "red with down filter"1 user thanked author for this post.
08/01/2019 at 11:42 AM #103965Here is another example. The 200 period average on the daily chart is an often used filter with traders only going long if the price is above it and short if below it – but how effective is it in reality.
The test on the sp500 daily (first image) says that it improves the probability of your long trades by 0.48% and your short trades by 1.34%. It is an improvement but not a very big one. I’m guessing that this is due to the lag of an average.
If we test with the low[1] > close[200] and high[1] < close[200] then the results (second image) are that green is improved by 0.83% and red by 2.04%. So it seems that a filter that reacts to historical price is better than one based on an average of historical price in this case.
1 user thanked author for this post.
08/03/2019 at 4:34 PM #10408608/03/2019 at 5:09 PM #104087Yes but you also need to add a separate down filter which will in your case just be the opposite.
123m7=average[7](close)upfiltering = close[1]>m7[1]downfiltering = close[1]<m7[1]I coded it this way to allow for bands that you do not trade in to be tested. So for example you might try this to test if above a line 1% above an average or 1% below an average leaving a no trading dead band in the middle.
123m7=average[7](close)upfiltering = close[1]>m7[1] * 1.01downfiltering = close[1]<m7[1] * 0.9908/03/2019 at 5:37 PM #10409408/03/2019 at 5:44 PM #10409508/03/2019 at 6:17 PM #104096We can never buy at the close so close versus open is a more accurate way to do it. We are purely checking the historical quantity of green and red candles and as we would be buying at the open and not the previous close the open is the price that we are interested in.
09/26/2020 at 4:49 PM #145467Today I had another play with filter analysis and came up with this simple indicator.
You can put two filter conditions in it – one that has to be true for long trades and one that has to be true for short trades. The indicator then checks what percentage of green candles are evident when the long condition is true and what percentage of red candles are evident when the short condition is true. It also totals up the gain or loss for all candles whilst each filter condition is true. This can then be compared to the calculated datum results for all candles (all time % of red candles, all time % of green candles and average gain per candle for all candles if you are long or short).
This allows us to see if our filter offers any edge and is worthy of being used in a strategy to decide whether we should be long or short.
The attached image is on the DJI daily and tests the conditions of only going long if the SMA50 is above the SMA100 and only short if it is below it.
The datum is that 53.02% of candles are green and 46.97% red. If we apply our filter then when our long condition is true we get 53.54% green candles and when our short condition is true we get 48.43% red candles. So it seems that our filter is good for telling us when to be short (1.46% better performance) but makes little difference for long trades (0.52% better performance).
If we look at the average gain per candle for when each filter condition is true we see that the datum is that the market gains an average of 2.87 per candle (meaning that if we were always short we would lose -2.87 pips per candle on average). When our long filter is applied this drops to 2.05 per candle and when our short filter is applied it drops to -5.33
So it seems this simple filter actually costs us more than if we had no filter at all. I guess that is due to the lagging nature of average cross overs and the fact that the DJI falls faster than it rises normally so the lag keeps us in at the start of big falls and keeps us out at the start of the market recovering.
It would be interesting if others want to post here the results of other trend filters for comparison.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152//long and short conditions to testlongcondition = average[50] > average[200]shortcondition = average[50] < average[200]if longcondition[1] thenif close <> open thenlongcount = longcount + 1longtot = longtot + (close - open)if close > open thenlongperctot = longperctot + 1endiflongperc = (longperctot/longcount)*100longavggain = longtot/longcountendifendifif shortcondition[1] thenif close <> open thenshortcount = shortcount + 1shorttot = shorttot + (open - close)if close < open thenshortperctot = shortperctot + 1endifshortperc = (shortperctot/shortcount)*100shortavggain = (shorttot/shortcount)endifendif//calculate datumsif close <> open thendatumcount = datumcount + 1greentot= greentot + (close - open)redtot = redtot + (open - close)if close > open thengreenperctot= greenperctot + 1endifif close < open thenredperctot= redperctot + 1endifgreenavggain = (greentot/datumcount)redavggain = (redtot/datumcount)greenperc = (greenperctot/datumcount)*100redperc = (redperctot/datumcount)*100endifreturn longperc coloured(0,128,0) as "long%", shortperc coloured(128,0,0)as "short%", greenperc coloured(0,255,0) as "long datum%", redperc coloured(255,0,0)as "short datum%", longavggain coloured(0,128,0) as "long avg gain", shortavggain coloured(128,0,0)as "short avg gain", greenavggain coloured(0,255,0) as "long datum avg gain", redavggain coloured(255,0,0)as "short datum avg gain"1 user thanked author for this post.
09/27/2020 at 8:49 AM #145490Here is an example of a filter that appears to give an improvement on the DJI daily. The filter simply compares an average of upward candle price movement to downward price movement over the last 100 bars.
The percentage of red and green bars compared to datum is virtually the same (very slight improvement) but the average gain per candle increases from 2.87 to 4.67 for long trades and from -2.87 to -1.43 for short trades. That indicates that this filter might be a worthy one to use to improve strategy performance. Being long and having a 63% improvement in the average gain per candle whilst you are is not to be sniffed at.
1 user thanked author for this post.
09/27/2020 at 9:41 AM #14549509/27/2020 at 11:11 AM #145501Thanks for the welcome back but I never went away – just been a bit busy sailing instead of coding. The weather has gone a bit grey and wet recently so I found some time to code instead of sail!
You can’t use ‘crosses over’ as you do as we are testing trend filters and crosses over is only true for one bar.
You would need to test it like this:
123456789if MACDline[12,26,9](close) crosses over 0 thenlongcondition = 1shortcondition = 0endifif MACDline[12,26,9](close) crosses under 0 thenlongcondition = 0shortcondition = 1endif1 user thanked author for this post.
09/27/2020 at 11:40 AM #145508It can be interesting to combine filters. Here I have trades only when two filters are true at the same time. One is price compared to SAR and the other filter is a version of the one I mentioned earlier that compares an average of upward candle price movement to downward price movement but over the last 200 bars and using a different type of average.
6.33 average gain per candle for long compared to the datum of 2.87 seems like a pretty good filter.
123//filter conditionslongcondition = (SAR[0.02,0.02,0.2] > close) and (shortave > longave)shortcondition = (SAR[0.02,0.02,0.2] < close) and (longave > shortave) -
AuthorPosts
Find exclusive trading pro-tools on