Coding and using vortex indicator in PineScript

Vortex Indicator

Vortex indicator is used to identify the start, end or continuation of a trend. It consists of two lines which are generally red and green. Greenline measures the strength of the uptrend while the other measures for downtrends. When the two cross, trend change can be expected. 

Vortex Indicator Calculation

Vortex is calculated by subtracting the previous low/high from the current high/low. Please note, the low of the previous bar is substructed from the current high and the high of the previous bar is substructed from the current low of the present bar. 

i.e. VM+ = high - low of previous bar and VM- = low-high of previous bar

In the above, we are considering VM+ as positive vortex movement and VM- as negative vortex movement. A vortex movement is a scientific term associated with the flow of fluid or water. As you can notice the calculation above can be positive or negative depending on the values of high/low. For this reason, we calculate the absolute value (all negative are also considered as positive) of the same. The above therefore becomes :

VM+ = absolute(high - low of previous bar) and VM- = absolute(low-high of previous bar)

The calculation of VM+ and VM- is similar to the way we calculate TR i.e. true range. The VM can be considered as cross TR with previous bars. 

Thus Vortex indicator can be considered as a comparison of cross TR with TR for a given period.

Before we compare, VM with TR we have to calculate TR i.e. TR = high - low.

Now we can compare these cross TR with actual TR. The comparison is done for a fixed lookback period. Generally, traders choose 14 or 21 days as a lookback period (because they are multiples of days in a week). If you are not working on the daily time frame, you may like to choose a different lookback period. 

For comparison just sum up value of VM+ and VM- and TR for lookback period. 

For calculation of Vortex Indicator Plus divide VM+ by TR summed up for lookback period.

VIP = (sum of  VM+ for lookback period)/(sum of  TR for lookback period)

VIM = (sum of  VM- for lookback period)/(sum of  TR for lookback period) 

Implementing Vortex Indicator in Pinescript

//@version=5
indicator(title = "Vortex Indicator", shorttitle="VI")
period = input.int(14, title="Length", minval=2)
VMPlus = math.sum( math.abs( high - low[1]), period )
VMMinus = math.sum( math.abs( low - high[1]), period )
SumTR = math.sum( ta.tr, period)
VIPlus = VMPlus / SumTR
VIMinus = VMMinus / SumTR
plot(VIPlus, title="VI +", color=#2962FF)
plot(VIMinus, title="VI -", color=#E91E63)

The above is code in pinescript for quick implementation of vortex indicator on trading view platform. 
The VM+ and VM- is calculated by calculating cross TR as high - low[1] and low - high[1]. Here the [1] notation after high and low represent the value of high or low one bar back. 

If you are new to pinescript or want to learn pinescript from scratch. Books and courses are recommended in the resource section. Even if you have no prior experience of coding or programming, you will be able to make code in pinescripts after going through the book or course.

You are encouraged to copy and use the above code in your pinescript editor for results. 

Vortex indicator best settings

The common question that many traders often ask is what is the best setting for an indicator. What could be vortex indicator settings for intraday ? No one can give you the correct answer for such types of questions but I can provide you with some guidelines on the selection of the best setting for any indicator. 

1.   Setting Shall depend on underlying asset and market timings. If the market works for  24X 7, its setting will always be different from other equity exchanges those work for 8 to 10 hours a day.

2. Timeframe do have impact on settings of any indicator. Setting for one timeframe may not work for another. 

Example : If you are using daily timeframe for an exchange that works for 5 days a week, a week is of 5 days , two weeks are of 10 days, month is of 20 days. I would suggest a setting of 15,20 for long term and 5 or 10 for short term. Your setting should be in line with higher time frame. 

If you are using a 5 min chart, try using 60 min / 5 = 12 as setting or 30 min / 5 min = 6 as settings.

In case of vortex, I am using 14 as setting which is equal to 2 weeks (7 X 7) or approx 3 weeks (5 X 3) if we consider 7 days or 5 days as days in a week. 

Vortex Indicator Strategy

VM being absolute number is always a positive number and being divided by TR, the Vortex Indicator for strength and weakness are always positive i.e. value greater than zero, however, as we had normalized the value of VM by TR, they are generally in a narrow range.

Any value of VI above 1.1 can be considered as a strength and value below 0.90 can be considered as weakness. Some traders use crossover of VI+ and VI- as single for trend reversal. Some traders also use other indicators in combination with VI for signal filtering.




vortex indicator screener

vortex indicator vs dmi

vortex indicator python

vortex indicator afl

vortex indicator accuracy

vortex indicator adalah

vortex angle indicator

vortex indicator vs adx

Resources

Highest Rated Udemy Course on PineScript - Grab your Seat Now 

Udemy Discount Coupon Code : UDEMY-JAN23 (Valid upto 30th Nov 2023)

Learn more about coding on tradingview in PineScript through Books on pinescript available on amazon and kindle.


200+ pages book100 pages book200+ pages book


Point and Figure Charts : A Time-Tested Tool for Technical Analysis

In the dynamic world of financial markets, investors and traders constantly seek tools that can provide valuable insights into market trends...