OPEN-SOURCE SCRIPT

Updated

This is a fairly self-contained system based upon a moving average crossover with several unique features. The most significant of these is the adjustable volume weighting system, allowing for transformations between standard and weighted versions of each included MA. With this feature it is possible to apply partial weighting which can help to improve responsiveness without dramatically altering shape. Included types are SMA, EMA, WMA, RMA, hSMA, DEMA and TEMA. Potentially more will be added in future (check updates below).

In addition there are a selection of alternative 'weighted' inputs, a pair of Bollinger-style deviation bands, a separate price tracker and a bunch of alert presets.

This can be used out-of-the-box or tweaked in multiple ways for unusual results. Default settings are a basic 8/21 EMA cross with partial volume weighting. Dev bands apply to MA2 and are based upon the type and the volume weighting. For standard Bollinger bands use SMA with length 20 and try adding a small amount of volume weighting.

A more detailed breakdown of the functionality follows.

ADJUSTABLE VOLUME WEIGHTING

In principle any moving average should have a volume weighted analogue, the standard VWMA is just an SMA with volume weighting for example. Actually, we can consider the SMA to be a special case where volume is a constant 1 per bar (the value is somewhat arbitrary, the important part is that it's constant). Similar principles apply to the 'elastic' EVWMA which is the volume weighted analogue of an RMA. In any case though, where we have standard and weighted variants it is possible to transform one into the other by gradually increasing or decreasing the weighting, which forms the basis of this system. This is not just a simple multiplier however, that would not work due to the relative proportions being the same when set at any non zero value. In order to create a meaningful transformation we need to use an exponent instead, eg: volume^x , where x is a variable determined in this case by the 'volume' parameter. When x=1, the full volume weighting applies and when x=0, the volume will be reduced to a constant 1. Values in between will result in the respective partial weighting, for example 0.5 will give the square root of the volume.

The obvious question here though is why would you want to do this? To answer that really it is best to actually try it. The advantages that volume weighting can bring to a moving average can sometimes come at the cost of unwanted or erratic behaviour. While it can tend towards much closer price tracking which may be desirable, sometimes it needs moderating especially in markets with lower liquidity. Here the adjustability can be useful, in many cases i have found that adding a small amount of volume weighting to a chosen MA can help to improve its responsiveness without overpowering it. Another possible use case would be to have two instances of the same MA with the same length but different weightings, the extent to which these diverge from each other can be a useful indicator of trend strength. Other uses will become apparent with experimentation and can vary from one market to another.

THE INCLUDED MODES

At the time of publication, there are 7 included moving average types with plans to add more in future. For now here is a brief explainer of what's on offer (continuing to use x as shorthand for the volume parameter), starting with the two most common types.

SMA: As mentioned above this is essentially a standard VWMA, calculated here as sma(source*volume^x,length)/sma(volume^x,length). In this case when x=0 then volume=1 and it reduces to a standard SMA.

RMA: Again mentioned above, this is an EVWMA (where E stands for elastic) with constant weighting. Without going into detail, this method takes the 1/length factor of an RMA and replaces it with volume^x/sum(volume^x,length). In this case again we can see that when x=0 then volume=1 and the original 1/length factor is restored.

EMA: This follows the same principle as the RMA where the standard 2/(length+1) factor is replaced with (2*volume^x)/(sum(volume^x,length)+volume^x). As with an RMA, when x=0 then volume=1 and this reduces back to the standard 2/(length+1).

DEMA: Just a standard Double EMA using the above.

TEMA: Likewise, a standard Triple EMA using the above.

hSMA: This is the same as the SMA except it uses harmonic mean calculations instead of arithmetic. In most cases the differences are negligible however they can become more pronounced when volume weighting is introduced. Furthermore, an argument can be made that harmonic mean calculations are better suited to downtrends or bear markets, in principle at least.

WMA: Probably the most contentious one included. Follows the same basic calculations as for the SMA except uses a WMA instead. Honestly, it makes little sense to combine both linear and volume weighting in this manner, included only for completeness and because it can easily be done. It may be the case that a superior composite could be created with some more complex calculations, in which case i may add that later. For now though this will do.

An additional 'volume filter' option is included, which applies a basic filter to the volume prior to calculation. For types based around the SMA/VWMA system, the volume filter is a WMA-4, for types based around the RMA/EVWMA system the filter is a RMA-2.

As and when i add more they will be listed in the updates at the bottom.

WEIGHTED INPUTS

The ohlc method of source calculations is really a leftover from a time when data was far more limited. Nevertheless it is still the method used in charting and for the most part is sufficient. Often the only important value is 'close' although sometimes 'high' and 'low' can be relevant also. Since we are volume weighting however, it can be useful to incorporate as much information as possible. To that end either 'hlc3' or 'hlcc4' tend to be the best of the defaults (in the case of 24/7 charting like crypto or intraday trading, 'ohlc4' should be avoided as it is effectively the same as a lagging version of 'hlcc4'). There are many other (infinitely many, in fact) possible combinations that can be created, i have included a few here.

The premise is fairly straightforward, by subtracting one value from another, the remaining difference can act as a kind of weight. In a simple case consider 'hl2' as simply the midrange ((high+low)/2), instead of this using 'high+low-open' would give more weight to the value furthest from the open, providing a good estimate of the median. An even better estimate can be achieved by combining that with 'high+low-close' to give the included result 'hl-oc2'. Similarly, 'hlc3' can be considered the basic mean of the three significant values, an included weighted version 'hlc2-o2' combines a sum with subtraction of open to give an estimated mean that may be more accurate. Finally we can apply a similar principle to the close, by subtracting the other values, this one potentially gets more complex so the included 'cc-ohlc4' is really the simplest. The result here is an overbias of the close in relation to the open and the midrange, while in most cases not as useful it can provide an estimate for the next bar assuming that the trend continues.

Of the three i've included, hlc2-o2 is in my opinion the most useful especially in this context, although it is perhaps best considered to be experimental in nature. For that reason, i've kept 'hlcc4' as the default for both MAs.

Additionally included is an 'aux input' which is the standard TV source menu and, where possible, can be set as outputs of other indicators.

THE SYSTEM

This one is fairly obvious and straightforward. It's just a moving average crossover with additional deviation (bollinger) bands. Not a lot to explain here as it should be apparent how it works.

Of the two, MA1 is considered to be the fast and MA2 is considered to be the slow. Both can be set with independent inputs, types and weighting. When MA1 is above, the colour of both is green and when it's below the colour of both is red. An additional gradient based fill is there and can be adjusted along with everything else in the visuals section at the bottom. Default alerts are available for crossover/crossunder conditions along with optional marker plots.

MA2 has the option for deviation bands, these are calculated based upon the MA type used and volume weighted according to the main parameter. In the case of a unweighted SMA being used they will be standard Bollinger bands.

An additional 'source direct' price tracker is included which can be used as the basis for an alert system for price crossings of bands or MAs, while taking advantage of the available weighted inputs. This is displayed as a stepped line on the chart so is also a good way to visualise the differences between input types.

That just about covers it then. The likelihood is that you've used some sort of moving average cross system before and are probably still using one or more. If so, then perhaps the additional functionality here will be of benefit.

Thanks for looking, I welcome any feedack

Release Notes

Minor fix to 'nz' condition on ema/rma types to use price instead of 0.Release Notes

Added 2 more filters, HMA and VIDYA, along with some minor aesthetic tweaks.The standard HMA is composed of several WMAs, in this case these have been replaced with the basic WMAs already added. Volume weighting actually has little impact on the HMA, nevertheless it is a useful and popular filter so its inclusion is warranted.

VIDYA is a little more interesting, essentially it's an RMA where the 1/length factor is multiplied by a 'volatility index'. The first step then is to elasticate it as per a standard RMA. The volatility index varies depending upon the version of VIDYA, either the standard deviation divided by the SMA of the standard deviation or a Chande Momentum Oscillator. In this case the former is used since it too can be volume weighted using the already included SMA and its deviation. The bands here use an exponential deviation in line with the RMA basis of the filter.

Release Notes

Now works on charts without volume data. Added a condition to return a constant value of 1 when volume data is unavailable. Obviously this means the weighting functionality will be inactive in such cases but everything else should work fine.Added 'Disable' option in dropdown for Price Tracker so no need to hide it anymore if not used.

Release Notes

Some significant work done under the hood. Main process has been almost completely recoded from scratch with all features interconnected and more co-mingling of functions. Performance-wise there appears to be little change for better or worse however this makes it a lot easier for any further expansions or additions to be integrated.

Volume filter is now applied in all cases post-multiplier. Due to some inconsistent implementation in prior versions this was not always the case.

* note: while this may give different results, in most cases the difference is tiny and of no real significance. However differences can become more noticeable on extremely low time-frames and markets with particularly low trading volume. This current implementation however is more logical (imo, at least) and will remain from now on.

Added two extra modes:

- sEMA is just as basic ema weighted using the simple volume weighting method instead of the elastic method, results are slightly different. Included here since i've also been using this method in other projects.

- DRMA is a double RMA, same principle as DEMA and is similar to that with 2x length, included here since it was trivial to create with newer code.

Added extra alert conditions for crosses of price tracker outside of bands and price tracker crossing MA2.

Layout of interface has been slightly changed to be more space-efficient.

Code formatting is now more in line with Pine conventions: more camelCase, less snake_case.

Release Notes

Just some minor code tweaks.Most notable here is an improvement of the previous condition allowing it to function on charts without volume data, it now also works fully on charts where only partial volume data is available.

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.