{"id":1707,"date":"2023-08-29T08:09:17","date_gmt":"2023-08-29T08:09:17","guid":{"rendered":"https:\/\/eodhd.com\/financial-academy\/?p=1707"},"modified":"2024-03-12T11:28:22","modified_gmt":"2024-03-12T11:28:22","slug":"implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python","status":"publish","type":"post","link":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python","title":{"rendered":"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python"},"content":{"rendered":"\n<p>People who are in the field of stock trading will most certainly know about the Stochastic Oscillator given its popularity, and today we are going to explore an indicator that is not only similar but also performs like the Stochastic Oscillator too. It\u2019s the Relative Vigor Index, shortly known as RVI. In this article, we will first build some basic intuitions about the indicator and the mathematics behind it. Then we will proceed to the programming where we will use Python to build the indicator from scratch, backtest a trading strategy based on it, and compare the strategy results with those of SPY ETF (an ETF specifically designed to track the movement of the S&amp;P 500 market index). Without further ado, let\u2019s dive into the article.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a class=\"maxbutton-1 maxbutton maxbutton-subscribe-to-api external-css btn\" href=\"https:\/\/eodhd.com\/register\"><span class='mb-text'>Register &amp; Get Data<\/span><\/a><\/p>\n\n\n\n\n\n\n<h1 class=\"wp-block-heading\" id=\"cde0\">Relative Vigor Index (RVI)<\/h1>\n\n\n\n<p id=\"4a98\">The&nbsp;Relative Vigor Index is a momentum indicator that acts as an instrument to determine the current market momentum,&nbsp;either upward or downward. Unlike the Stochastic Oscillator, the Relative Vigor Index is an unbounded oscillator and does not fluctuate between certain thresholds rather it oscillates across a center-line (zero in most cases).&nbsp;The Relative Vigor Index is composed of two components: the RVI line&nbsp;and the Signal line. Now let\u2019s see how each of the components is being calculated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5378\">RVI line calculation<\/h2>\n\n\n\n<p id=\"b1e7\">To calculate the readings of the RVI line, we must first determine the numerator and the denominator.<\/p>\n\n\n\n<p id=\"988e\"><strong>The numerator can be calculated in five steps:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find the difference between the current closing price and the current opening price of a stock. We&#8217;ll name the difference \u2018a\u2019.<\/li>\n\n\n\n<li>Find the difference between the closing price two periods ago and the opening price two periods ago, this difference is multiplied by 2. We&#8217;ll name the result \u2018b\u2019.<\/li>\n\n\n\n<li>Find the difference between the closing price three periods ago and the opening price three periods, then multiplied them two. We&#8217;ll name the result \u2018c\u2019.<\/li>\n\n\n\n<li>Multiply the difference that we get by subtracting the opening price four periods ago from the closing price four periods ago by two, and the result will be named \u2018d\u2019.<\/li>\n\n\n\n<li>The final step is to add the determined a, b, c, and d variables to each other and a rolling sum for a specified number of periods is calculated (a typical setting is 4). The numerator calculation can be represented as follows:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\"><strong>a <\/strong>= CURRENT CLOSE - CURRENT OPEN<br><strong>b<\/strong> =  2 * ( CLOSE 2 PERIODS AGO - OPEN 2 PERIODS AGO )<br><strong>c <\/strong>=  2 * ( CLOSE 3 PERIODS AGO - OPEN 3 PERIODS AGO )<br><strong>d <\/strong>=  2 * ( CLOSE 4 PERIODS AGO - OPEN 4 PERIODS AGO )<br><strong>Numerator<\/strong> = ROLLING SUM 4 [ a + b + c + d ]<\/pre>\n\n\n\n<p id=\"7dc2\">The procedure to calculate the denominator is almost similar to that of the numerator, but we just have to replace the closing price and the opening price data with high and low price data respectively.<\/p>\n\n\n\n<p id=\"ce21\"><strong>The denominator can be calculated in five steps:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find the difference between the current high price and the current low price of a stock. We&#8217;ll name the difference \u2018e\u2019.<\/li>\n\n\n\n<li>Find the difference between the high price two periods ago and the low price two periods ago, and this difference is multiplied by 2. We&#8217;ll name the result \u2018f\u2019.<\/li>\n\n\n\n<li>Find the difference between the high price three periods ago and the low price three periods, then multiply it by two. We&#8217;ll name the result  \u2018g\u2019.<\/li>\n\n\n\n<li>Multiply the difference that we get by subtracting the low price four periods ago from the high price four periods ago by two, and we&#8217;ll name the result \u2018h\u2019.<\/li>\n\n\n\n<li>The final step is to add the determined e, f, g, and h variables to each other and a rolling sum with 4 as the lookback period is calculated. The denominator calculation can be represented as follows:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\"><strong>e <\/strong>= CURRENT HIGH - CURRENT LOW<br><strong>f<\/strong> =  2 * ( HIGH 2 PERIODS AGO - LOW 2 PERIODS AGO )<br><strong>g <\/strong>=  2 * ( HIGH 3 PERIODS AGO - LOW 3 PERIODS AGO )<br><strong>h <\/strong>=  2 * ( HIGH 4 PERIODS AGO - LOW 4 PERIODS AGO )<br><strong>Denominator <\/strong>= ROLLING SUM 4 [ e + f + g + h ]<\/pre>\n\n\n\n<p id=\"ab1c\">After getting the numerator and the denominator, we can move on to calculating the readings of the RVI line: divide the numerator by the denominator, and the result is smoothed by a Simple Moving Average for a specified number of lookback periods (typically 10). The calculation can be mathematically represented as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\"><strong>RVI LINE<\/strong> = SMA 10 [ NUMERATOR \/ DENOMINATOR ]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"b3c7\">Signal line calculation<\/h2>\n\n\n\n<p id=\"5fd4\">Usually, the indicators having the signal line as a component are calculated by taking either Simple Moving Average or Exponential Moving Average of the main component (RVI line in our case) for a specified number of periods. But it\u2019s different with the Relative Vigor Index.<\/p>\n\n\n\n<p id=\"2717\"><strong>There are four main steps involved in the calculation of the Signal line:<\/strong>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first step is to multiply the RVI line readings one period with two. We&#8217;ll call this \u2018rvi1\u2019.<\/li>\n\n\n\n<li> The next step is to multiply the readings of the RVI line two periods ago with two. We&#8217;ll call this  \u2018rvi2\u2019. <\/li>\n\n\n\n<li>The third step is to determine the RVI line readings three periods ago, we&#8217;ll call this \u2018rvi3\u2019. <\/li>\n\n\n\n<li>The final step is to add the determined \u2018rvi1\u2019, \u2018rvi2\u2019, \u2018rvi3\u2019 along with the readings of the RVI line to each other, and the total sum or the result is divided by 6. The calculation can be represented as follows:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\"><strong>rvi1 <\/strong>= 2 * RVI LINE 1 PERIOD AGO<br><strong>rvi2 <\/strong>= 2 * RVI LINE 2 PERIODS AGO<br><strong>rvi3 <\/strong>= RVI LINE 3 PERIODS AGO<br><strong>SIGNAL LINE <\/strong>= ( RVI LINE + rvi1 + rvi2 + rvi3 ) \/ 6<\/pre>\n\n\n\n<p id=\"78d1\">That\u2019s the whole process of calculating the components of the Relative Vigor Index. <\/p>\n\n\n\n<p id=\"78d1\">Now let\u2019s analyze a chart where Apple\u2019s closing price data is plotted along with its Relative Vigor Index calculated with 10 as the lookback period to build a solid understanding of the indicator and how it\u2019s being used.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1812\" height=\"884\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-11.png\" alt=\"\" class=\"wp-image-1710\"\/><\/figure>\n\n\n\n<p id=\"cea4\">The above chart is separated into two panels: the upper panel with the plot of Apple\u2019s closing price data, and the lower panel with the components of the Relative Vigor Index calculated with 10 as the lookback period. The main characteristic feature of the indicator is to help traders with identifying the current market momentum and this can be seen clearly in the chart where the components of the RVI indicator rises positively when the market shows a sturdy upward momentum, and decreases when the market is in the state of strong downward momentum. As I said before, the Relative Vigor Index is an unbounded oscillator and the above chart proves this statement where it can be seen that the components of the RVI indicator do not fluctuate between certain upper and lower thresholds like other momentum oscillators but instead, across the zero-line or the center-line.<\/p>\n\n\n\n<p id=\"d3ce\">Speaking about trading strategies, to my knowledge, it is possible to apply three types of trading strategies based on the Relative Vigor Index. The first is the divergence trading strategy, the second one is the classic crossover strategy, and the third one is the overbought and oversold strategy. In this article, we are going to implement the second type of trading strategy &#8211; the crossover strategy. This strategy reveals a buy signal whenever the RVI line crosses from below to above the Signal line, and similarly, reveals a sell signal whenever the RVI line goes from above to below the Signal line. The RVI crossover trading strategy can be represented as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\">IF <strong>PREV.RLINE<\/strong> &lt; <strong>PREV.SLINE<\/strong> AND <strong>CUR.RLINE<\/strong> &gt; <strong>CUR.SLINE<\/strong> ==&gt; <strong>BUY SIGNAL<\/strong><br>IF <strong>PREV.RLINE<\/strong> &gt; <strong>PREV.SLINE<\/strong> AND <strong>CUR.RLINE<\/strong> &lt; <strong>CUR.SLINE<\/strong> ==&gt; <strong>SELL SIGNAL<\/strong><\/pre>\n\n\n\n<p id=\"9338\">In most cases, traders use the Relative Vigor Index accompanied by another technical indicator to build a trading strategy but that is not the scope of this article (recommended to try doing). That\u2019s it! This concludes our theory part on the Relative Vigor Index and let\u2019s move on to the programming part where we will use Python to first build the indicator from scratch, construct the crossover trading strategy, backtest the strategy on Apple stock data, and finally compare the results with that of SPY ETF. Let\u2019s do some coding!&nbsp;<strong>Before moving on, a note on disclaimer: This article\u2019s sole purpose is to educate people and must be considered as an information piece but not as investment advice.<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-center\"><a class=\"maxbutton-1 maxbutton maxbutton-subscribe-to-api external-css btn\" href=\"https:\/\/eodhd.com\/register\"><span class='mb-text'>Register &amp; Get Data<\/span><\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"48a2\">Implementation in Python<\/h1>\n\n\n\n<p id=\"6a30\">The coding part is classified into various steps as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\"><strong>1. Importing Packages\n2. Extracting Stock Data from EODHD\n3. Relative Vigor Index Calculation\n4. Creating the Crossover Trading Strategy\n5. Plotting the Trading Lists\n6. Creating our Position\n7. Backtesting\n8. SPY ETF Comparison<\/strong><\/pre>\n\n\n\n<p id=\"d489\">We will be following the order mentioned in the above list, so buckle up your seatbelts to follow every upcoming coding part.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2330\">Step-1: Importing Packages<\/h2>\n\n\n\n<p id=\"4733\">Importing the required packages into the Python environment is a non-skippable step. The primary packages are going to be Pandas to work with data, NumPy to work with arrays and for complex functions, Matplotlib for plotting purposes, and Requests to make API calls. The secondary packages are going to be Math for mathematical functions and Termcolor for font customization (optional).<\/p>\n\n\n\n<p id=\"17a0\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong># IMPORTING PACKAGES<br><\/strong><br>import pandas as pd<br>import matplotlib.pyplot as plt<br>import numpy as np<br>import requests<br>from termcolor import colored as cl<br>from math import floor<br><br>plt.style.use('fivethirtyeight')<br>plt.rcParams['figure.figsize'] = (20,10)<\/pre>\n\n\n\n<p id=\"27be\">With the required packages imported into Python, we can proceed to fetch historical data for Apple using EODHD&#8217;s OHLC split-adjusted data API endpoint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"97fc\">Step-2: Extracting data from EODHD<\/h2>\n\n\n\n<p id=\"f818\">In this phase, we&#8217;re set to retrieve the historical stock data for Apple using the <a href=\"https:\/\/eodhd.com\/financial-apis\/technical-indicators-api\/#Split_Adjusted_Data\" target=\"_blank\" rel=\"noreferrer noopener\">OHLC split-adjusted API endpoint<\/a> provided by&nbsp;<a href=\"https:\/\/eodhd.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">EODHD<\/a>. It&#8217;s important to note that&nbsp;<a href=\"https:\/\/eodhd.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">EOD Historical Data<\/a> (EODHD) is a reliable provider of financial APIs, encompassing an extensive array of market data, including historical data and economic news. Be sure to possess an <a href=\"https:\/\/eodhd.com\/register\">EODHD account<\/a> and access your secret API key, a crucial element for data extraction via the API.<\/p>\n\n\n\n<p id=\"e4a5\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"># EXTRACTING STOCK DATA\n\ndef get_historical_data(symbol, start_date):\n    api_key = 'YOUR API KEY'\n    api_url = f'https:\/\/eodhistoricaldata.com\/api\/technical\/{symbol}?order=a&amp;fmt=json&amp;from={start_date}&amp;function=splitadjusted&amp;api_token={api_key}'\n    raw_df = requests.get(api_url).json()\n    df = pd.DataFrame(raw_df)\n    df.date = pd.to_datetime(df.date)\n    df = df.set_index('date')\n    return df\n\naapl = get_historical_data('AAPL', '2019-01-01')\naapl.tail()<\/pre>\n\n\n\n<p id=\"0200\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"869\" height=\"458\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-12.png\" alt=\"\" class=\"wp-image-1711\"\/><\/figure>\n\n\n\n<p id=\"e413\"><strong>Code Explanation:<\/strong>&nbsp;We begin by defining a function named &#8216;get_historical_data,&#8217; which takes the stock symbol (&#8216;symbol&#8217;) and the start date for historical data (&#8216;start_date&#8217;) as parameters. Inside the function, we define the API key and URL, then retrieve the historical data in JSON format using the &#8216;get&#8217; function and store it in the &#8216;raw_df&#8217; variable. After cleaning and formatting the raw JSON data, we return it as a Pandas dataframe. Finally, we call this function to fetch Apple&#8217;s historical data from the start of 2019 and store it in the &#8216;aapl&#8217; variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"b9f9\">Step-3: Relative Vigor Index Calculation<\/h2>\n\n\n\n<p id=\"cace\">In this step, we are going to calculate the components of the Relative Vigor Index by following the methods and formulas we discussed before.<\/p>\n\n\n\n<p id=\"02ca\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong># RELATIVE VIGOR INDEX CALCULATION\n<\/strong>\ndef get_rvi(open, high, low, close, lookback):\n    a = close - open\n    b = 2 * (close.shift(2) - open.shift(2))\n    c = 2 * (close.shift(3) - open.shift(3))\n    d = close.shift(4) - open.shift(4)\n    numerator = a + b + c + d\n    \n    e = high - low\n    f = 2 * (high.shift(2) - low.shift(2))\n    g = 2 * (high.shift(3) - low.shift(3))\n    h = high.shift(4) - low.shift(4)\n    denominator = e + f + g + h\n    \n    numerator_sum = numerator.rolling(4).sum()\n    denominator_sum = denominator.rolling(4).sum()\n    rvi = (numerator_sum \/ denominator_sum).rolling(lookback).mean()\n    \n    rvi1 = 2 * rvi.shift(1)\n    rvi2 = 2 * rvi.shift(2)\n    rvi3 = rvi.shift(3)\n    rvi_signal = (rvi + rvi1 + rvi2 + rvi3) \/ 6\n    \n    return rvi, rvi_signal\n\naapl['rvi'], aapl['signal_line'] = get_rvi(aapl['open'], aapl['high'], aapl['low'], aapl['close'], 10)\naapl = aapl.dropna()\naapl = aapl[aapl.index &gt;= '2020-01-01']\naapl.tail()<\/pre>\n\n\n\n<p id=\"5430\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1220\" height=\"458\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-13.png\" alt=\"\" class=\"wp-image-1712\"\/><\/figure>\n\n\n\n<p id=\"521a\"><strong>Code Explanation:<\/strong>&nbsp;We are first defining a function named \u2018get_rvi\u2019 that takes a stock\u2019s opening (\u2018open\u2019), high (\u2018high\u2019), low (\u2018low\u2019), closing (\u2018close\u2019) price data, and the lookback period (\u2018lookback\u2019) as parameters.<\/p>\n\n\n\n<p id=\"7dd7\">Inside the function, we are first calculating the variables involved in the calculation of the numerator which is a, b, c, and d. These are variables are calculated by following the formulas we discussed before and added to each other to determine the numerator. Next comes the calculation of the denominator that is almost similar to that of the numerator calculation but we are just replacing certain values. Before calculating the readings of the RVI line, we are determining the rolling sum of both the numerator and denominator with 4 as the lookback period and the results are then substituted into the formula of the RVI line to get the readings.<\/p>\n\n\n\n<p id=\"4b78\">After that, we are calculating the three prerequisite variables which are rvi1, rvi2, and rvi3 we discussed before, and substituting into the formula of the Signal line along with the previously calculated RVI line values to get the readings. Finally, we are returning and calling the function the store Apple\u2019s Relative Vigor Index readings with 10 as the lookback period.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c3aa\">Step-4: Creating the trading strategy<\/h2>\n\n\n\n<p id=\"8500\">In this step, we are going to implement the discussed Relative Vigor Index crossover trading strategy in Python.<\/p>\n\n\n\n<p id=\"303b\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong># RELATIVE VIGOR INDEX STRATEGY<br><\/strong><br>def implement_rvi_strategy(prices, rvi, signal_line):<br>    buy_price = []<br>    sell_price = []<br>    rvi_signal = []<br>    signal = 0<br>    <br>    for i in range(len(prices)):<br>        if rvi[i-1] &lt; signal_line[i-1] and rvi[i] &gt; signal_line[i]:<br>            if signal != 1:<br>                buy_price.append(prices[i])<br>                sell_price.append(np.nan)<br>                signal = 1<br>                rvi_signal.append(signal)<br>            else:<br>                buy_price.append(np.nan)<br>                sell_price.append(np.nan)<br>                rvi_signal.append(0)<br>        elif rvi[i-1] &gt; signal_line[i-1] and rvi[i] &lt; signal_line[i]:<br>            if signal != -1:<br>                buy_price.append(np.nan)<br>                sell_price.append(prices[i])<br>                signal = -1<br>                rvi_signal.append(signal)<br>            else:<br>                buy_price.append(np.nan)<br>                sell_price.append(np.nan)<br>                rvi_signal.append(0)<br>        else:<br>            buy_price.append(np.nan)<br>            sell_price.append(np.nan)<br>            rvi_signal.append(0)<br>            <br>    return buy_price, sell_price, rvi_signal<br><br>buy_price, sell_price, rvi_signal = implement_rvi_strategy(aapl['close'], aapl['rvi'], aapl['signal_line'])<\/pre>\n\n\n\n<p id=\"336a\"><strong>Code Explanation:<\/strong>&nbsp;First, we are defining a function named \u2018implement_rvi_strategy\u2019 which takes the stock prices (\u2018prices\u2019), and the components of the Relative Vigor Index (\u2018rvi\u2019, \u2018signal_line\u2019) as parameters.<\/p>\n\n\n\n<p id=\"be0d\">Inside the function, we are creating three empty lists (buy_price, sell_price, and rvi_signal) in which the values will be appended while creating the trading strategy.<\/p>\n\n\n\n<p id=\"d12e\">After that, we are implementing the trading strategy through a for-loop. Inside the for-loop, we are passing certain conditions, and if the conditions are satisfied, the respective values will be appended to the empty lists. If the condition to buy the stock gets satisfied, the buying price will be appended to the \u2018buy_price\u2019 list, and the signal value will be appended as 1 representing to buy the stock. Similarly, if the condition to sell the stock gets satisfied, the selling price will be appended to the \u2018sell_price\u2019 list, and the signal value will be appended as -1 representing to sell the stock.<\/p>\n\n\n\n<p id=\"e942\">Finally, we are returning the lists appended with values. Then, we are calling the created function and stored the values into their respective variables. The list doesn\u2019t make any sense unless we plot the values. So, let\u2019s plot the values of the created trading lists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"50f6\">Step-5: Plotting the trading signals<\/h2>\n\n\n\n<p id=\"45dd\">In this step, we are going to plot the created trading lists to make sense out of them.<\/p>\n\n\n\n<p id=\"f92b\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong># RELATIVE VIGOR INDEX TRADING SIGNALS PLOT<br><\/strong><br>ax1 = plt.subplot2grid((11,1), (0,0), rowspan = 5, colspan = 1)<br>ax2 = plt.subplot2grid((11,1), (6,0), rowspan = 5, colspan = 1)<br>ax1.plot(aapl['close'], linewidth = 2)<br>ax1.plot(aapl.index, buy_price, marker = '^', markersize = 12, color = 'green', linewidth = 0, label = 'BUY SIGNAL')<br>ax1.plot(aapl.index, sell_price, marker = 'v', markersize = 12, color = 'r', linewidth = 0, label = 'SELL SIGNAL')<br>ax1.legend()<br>ax1.set_title('AAPL RVI TRADING SIGNALS')<br>ax2.plot(aapl['rvi'], linewidth = 2, color = 'orange', label = 'RVI LINE')<br>ax2.plot(aapl['signal_line'], linewidth = 2, color = '#BA5FE3', label = 'SIGNAL LINE')<br>ax2.set_title('AAPL RVI 10')<br>ax2.legend()<br>plt.show()<\/pre>\n\n\n\n<p id=\"0994\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1812\" height=\"884\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-14.png\" alt=\"\" class=\"wp-image-1713\"\/><\/figure>\n\n\n\n<p id=\"baec\"><strong>Code Explanation:<\/strong>&nbsp;We are plotting the components of the Relative Vigor Index along with the buy and sell signals generated by the crossover trading strategy. We can observe that whenever the previous reading of the RVI line is below the previous reading of the Signal Line and the current reading of the RVI line is above the current reading of the Signal line, a green-colored buy signal is plotted in the chart. Similarly, whenever the previous reading of the RVI line is above the previous reading of the Signal Line and the current reading of the RVI line is below the current reading of the Signal line, a red-colored sell signal is plotted in the chart.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c35a\">Step-6: Creating our Position<\/h2>\n\n\n\n<p id=\"93f4\">In this step, we are going to create a list that indicates 1 if we hold the stock or 0 if we don\u2019t own or hold the stock.<\/p>\n\n\n\n<p id=\"0b3c\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong># STOCK POSITION<br><\/strong><br>position = []<br>for i in range(len(rvi_signal)):<br>    if rvi_signal[i] &gt; 1:<br>        position.append(0)<br>    else:<br>        position.append(1)<br>        <br>for i in range(len(aapl['close'])):<br>    if rvi_signal[i] == 1:<br>        position[i] = 1<br>    elif rvi_signal[i] == -1:<br>        position[i] = 0<br>    else:<br>        position[i] = position[i-1]<br>        <br>close_price = aapl['close']<br>rvi = aapl['rvi']<br>signal_line = aapl['signal_line']<br>rvi_signal = pd.DataFrame(rvi_signal).rename(columns = {0:'rvi_signal'}).set_index(aapl.index)<br>position = pd.DataFrame(position).rename(columns = {0:'rvi_position'}).set_index(aapl.index)<br><br>frames = [close_price, rvi, signal_line, rvi_signal, position]<br>strategy = pd.concat(frames, join = 'inner', axis = 1)<br><br>strategy<\/pre>\n\n\n\n<p id=\"a98a\"><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1055\" height=\"468\" src=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-15.png\" alt=\"\" class=\"wp-image-1714\"\/><\/figure>\n\n\n\n<p id=\"b56e\"><strong>Code Explanation:<\/strong>&nbsp;First, we are creating an empty list named \u2018position\u2019. We are passing two for-loops, one is to generate values for the \u2018position\u2019 list to just match the length of the \u2018signal\u2019 list. The other for-loop is the one we are using to generate actual position values. Inside the second for-loop, we are iterating over the values of the \u2018signal\u2019 list, and the values of the \u2018position\u2019 list get appended concerning which condition gets satisfied. The value of the position remains 1 if we hold the stock or remains 0 if we sold or don\u2019t own the stock. Finally, we are doing some data manipulations to combine all the created lists into one dataframe.<\/p>\n\n\n\n<p id=\"49c4\">From the output being shown, we can see that in the first row our position in the stock has remained 1 (since there isn\u2019t any change in the Relative Vigor Index signal) but our position suddenly turned to -1 as we sold the stock when the Relative Vigor Index trading signal represents a sell signal (-1). Our position will remain 0 until some changes in the trading signal occur. Now it\u2019s time to do implement some backtesting process!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d024\">Step-7: Backtesting<\/h2>\n\n\n\n<p id=\"a82d\">Before moving on, it is essential to know what backtesting is. Backtesting is the process of seeing how well our trading strategy has performed on the given stock data. In our case, we are going to implement a backtesting process for our Relative Vigor Index trading strategy over the Apple stock data.<\/p>\n\n\n\n<p id=\"5910\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong># BACKTESTING\n<\/strong>\naapl_ret = pd.DataFrame(np.diff(aapl['close'])).rename(columns = {0:'returns'})\nrvi_strategy_ret = []\n\nfor i in range(len(aapl_ret)):\n    returns = aapl_ret['returns'][i]*strategy['rvi_position'][i]\n    rvi_strategy_ret.append(returns)\n    \nrvi_strategy_ret_df = pd.DataFrame(rvi_strategy_ret).rename(columns = {0:'rvi_returns'})\ninvestment_value = 100000\nrvi_investment_ret = []\n\nfor i in range(len(rvi_strategy_ret_df['rvi_returns'])):\n    number_of_stocks = floor(investment_value\/aapl['close'][i])\n    returns = number_of_stocks*rvi_strategy_ret_df['rvi_returns'][i]\n    rvi_investment_ret.append(returns)\n\nrvi_investment_ret_df = pd.DataFrame(rvi_investment_ret).rename(columns = {0:'investment_returns'})\ntotal_investment_ret = round(sum(rvi_investment_ret_df['investment_returns']), 2)\nprofit_percentage = floor((total_investment_ret\/investment_value)*100)\nprint(cl('Profit gained from the RVI strategy by investing $100k in AAPL : {}'.format(total_investment_ret), attrs = ['bold']))\nprint(cl('Profit percentage of the RVI strategy : {}%'.format(profit_percentage), attrs = ['bold']))<\/pre>\n\n\n\n<p id=\"ef32\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\"><strong>Profit gained from the RVI strategy by investing $100k in AAPL : 58307.54\nProfit percentage of the RVI strategy : 58%<\/strong><\/pre>\n\n\n\n<p id=\"fd24\"><strong>Code Explanation:<\/strong>&nbsp;First, we are calculating the returns of the Apple stock using the \u2018diff\u2019 function provided by the NumPy package and we have stored it as a dataframe in the \u2018aapl_ret\u2019 variable. Next, we are passing a for-loop to iterate over the values of the \u2018aapl_ret\u2019 variable to calculate the returns we gained from our RVI trading strategy, and these returns values are appended to the \u2018rvi_strategy_ret\u2019 list. Next, we are converting the \u2018rvi_strategy_ret\u2019 list into a dataframe and storing it in the \u2018rvi_strategy_ret_df\u2019 variable.<\/p>\n\n\n\n<p id=\"c66c\">Next comes the backtesting process. We are going to backtest our strategy by investing a hundred thousand USD into our trading strategy. So first, we are storing the amount of investment into the \u2018investment_value\u2019 variable. After that, we are calculating the number of Apple stocks we can buy using the investment amount. You can notice that I\u2019ve used the \u2018floor\u2019 function provided by the Math package because, while dividing the investment amount by the closing price of Apple stock, it spits out an output with decimal numbers. The number of stocks should be an integer but not a decimal number. Using the \u2018floor\u2019 function, we can cut out the decimals. Remember that the \u2018floor\u2019 function is way more complex than the \u2018round\u2019 function. Then, we pass a for-loop to find the investment returns followed by some data manipulation tasks.<\/p>\n\n\n\n<p id=\"7dc1\">Finally, we are printing the total return we got by investing a hundred thousand into our trading strategy and it is revealed that we have made an approximate profit of fifty-eight thousand USD in one year. That\u2019s not bad! Now, let\u2019s compare our returns with SPY ETF (an ETF designed to track the S&amp;P 500 stock market index) returns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ce90\">Step-8: SPY ETF Comparison<\/h2>\n\n\n\n<p>While this step is optional, it is highly recommended as it enables us to assess the performance of our trading strategy against a benchmark (SPY ETF). Here, we&#8217;ll extract SPY ETF data using the &#8216;get_historical_data&#8217; function and compare the returns from the SPY ETF with the returns generated by our trading strategy applied to Apple stock.<\/p>\n\n\n\n<p id=\"60dd\"><strong>Python Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-white-color has-black-background-color has-text-color has-background\"><strong># SPY ETF COMPARISON\n<\/strong>\ndef get_benchmark(start_date, investment_value):\n    spy = get_historical_data('SPY', start_date)['close']\n    benchmark = pd.DataFrame(np.diff(spy)).rename(columns = {0:'benchmark_returns'})\n    \n    investment_value = investment_value\n    benchmark_investment_ret = []\n    \n    for i in range(len(benchmark['benchmark_returns'])):\n        number_of_stocks = floor(investment_value\/spy[i])\n        returns = number_of_stocks*benchmark['benchmark_returns'][i]\n        benchmark_investment_ret.append(returns)\n\n    benchmark_investment_ret_df = pd.DataFrame(benchmark_investment_ret).rename(columns = {0:'investment_returns'})\n    return benchmark_investment_ret_df\n\nbenchmark = get_benchmark('2020-01-01', 100000)\ninvestment_value = 100000\ntotal_benchmark_investment_ret = round(sum(benchmark['investment_returns']), 2)\nbenchmark_profit_percentage = floor((total_benchmark_investment_ret\/investment_value)*100)\nprint(cl('Benchmark profit by investing $100k : {}'.format(total_benchmark_investment_ret), attrs = ['bold']))\nprint(cl('Benchmark Profit percentage : {}%'.format(benchmark_profit_percentage), attrs = ['bold']))\nprint(cl('RVI Strategy profit is {}% higher than the Benchmark Profit'.format(profit_percentage - benchmark_profit_percentage), attrs = ['bold']))<\/pre>\n\n\n\n<p id=\"2d9a\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background\" style=\"background-color:#abb7c217\"><strong>Benchmark profit by investing $100k : 40181.36\nBenchmark Profit percentage : 40%\nRVI Strategy profit is 18% higher than the Benchmark Profit<\/strong><\/pre>\n\n\n\n<p id=\"ef71\"><strong>Code Explanation:<\/strong>&nbsp;The code used in this step is almost similar to the one used in the previous backtesting step but, instead of investing in Apple, we are investing in SPY ETF by not implementing any trading strategies. From the output, we can see that our Relative Vigor Index crossover trading strategy has outperformed the SPY ETF by 18%. That\u2019s great!<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a class=\"maxbutton-1 maxbutton maxbutton-subscribe-to-api external-css btn\" href=\"https:\/\/eodhd.com\/register\"><span class='mb-text'>Register &amp; Get Data<\/span><\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"1140\">Final Thoughts!<\/h1>\n\n\n\n<p id=\"8972\">After an exhaustive process of crushing both theory and coding parts, we have successfully learned what the Relative Vigor Index is all about, the mathematics behind the indicator, and how a simple trading strategy based on it can be implemented in Python.<\/p>\n\n\n\n<p id=\"b8ae\">Even though we managed to surpass the results of the SPY ETF, we are still lagging from those of the actual Apple returns. This may be because the Relative Vigor Index is prone to revealing a lot of false signals when the market is ranging and this can also be observed in the chart represented while discussing the buy and sell signals generated by our crossover trading strategy.<\/p>\n\n\n\n<p id=\"13d2\">The only way to tackle this problem is to accompany the Relative Vigor Index with another technical indicator that acts as a filter whose only task is to classify the false signals from the authentic ones. Since the Relative Vigor Index is a directional indicator (the indicator\u2019s movement is directly proportional to that of the actual market), choosing a non-directional indicator and especially a volatility type would act as a great filter which ultimately leads us in achieving the desired results from the real-world market.<\/p>\n\n\n\n<p id=\"16bc\">With that said, you\u2019ve reached the end of the article. If you forgot to follow any of the coding parts, don\u2019t worry. I\u2019ve provided the full source code at the end of the article. Hope you learned something new and useful from this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>People who are in the field of stock trading will most certainly know about the Stochastic Oscillator given its popularity, and today we are going to explore an indicator that is not only similar but also performs like the Stochastic Oscillator too. It\u2019s the Relative Vigor Index, shortly known as RVI. In this article, we [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":1708,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[65,43,42,59,61],"tags":[],"coding-language":[30],"ready-to-go-solution":[],"qualification":[31,32],"financial-apis-category":[36],"financial-apis-manuals":[39,40],"class_list":["post-1707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backtesting-strategies-examples","category-how-to-get-stocks-data-examples","category-stocks-data-analysis-examples","category-technical-analysis-examples","category-trading-indicators-implimentations","coding-language-python","qualification-experienced","qualification-guru","financial-apis-category-stock-market-prices","financial-apis-manuals-end-of-day","financial-apis-manuals-technical-indicators","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.9 (Yoast SEO v26.7) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Backtesting Relative Vigor Index Trading Strategy in Python| EODHD APIs Academy<\/title>\n<meta name=\"description\" content=\"Learn how to use Python to backtest a trading strategy based on the Relative Vigor Index. Enhance your trading skills and increase your profitability\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python\" \/>\n<meta property=\"og:description\" content=\"Learn how to use Python to backtest a trading strategy based on the Relative Vigor Index. Enhance your trading skills and increase your profitability\" \/>\n<meta property=\"og:url\" content=\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python\" \/>\n<meta property=\"og:site_name\" content=\"Financial Academy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/eodhistoricaldata\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-29T08:09:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-12T11:28:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png\" \/>\n\t<meta property=\"og:image:width\" content=\"828\" \/>\n\t<meta property=\"og:image:height\" content=\"552\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nikhil Adithyan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EOD_data\" \/>\n<meta name=\"twitter:site\" content=\"@EOD_data\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikhil Adithyan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#article\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python\"},\"author\":{\"name\":\"Nikhil Adithyan\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/67681e71050cf7d8d0efb91fee5f0402\"},\"headline\":\"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python\",\"datePublished\":\"2023-08-29T08:09:17+00:00\",\"dateModified\":\"2024-03-12T11:28:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python\"},\"wordCount\":3121,\"publisher\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png\",\"articleSection\":[\"Backtesting Strategies Examples\",\"How to Get Stocks Data Examples\",\"Stocks Data Analysis Examples\",\"Technical Analysis Examples\",\"Trading Indicators Implimentations\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python\",\"name\":\"Backtesting Relative Vigor Index Trading Strategy in Python| EODHD APIs Academy\",\"isPartOf\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png\",\"datePublished\":\"2023-08-29T08:09:17+00:00\",\"dateModified\":\"2024-03-12T11:28:22+00:00\",\"description\":\"Learn how to use Python to backtest a trading strategy based on the Relative Vigor Index. Enhance your trading skills and increase your profitability\",\"breadcrumb\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png\",\"contentUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png\",\"width\":828,\"height\":552,\"caption\":\"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/eodhd.com\/financial-academy\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#website\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/\",\"name\":\"Financial APIs Academy | EODHD\",\"description\":\"Financial Stock Market Academy\",\"publisher\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/eodhd.com\/financial-academy\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#organization\",\"name\":\"EODHD (EOD Historical Data)\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png\",\"contentUrl\":\"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png\",\"width\":159,\"height\":82,\"caption\":\"EODHD (EOD Historical Data)\"},\"image\":{\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/eodhistoricaldata\",\"https:\/\/x.com\/EOD_data\",\"https:\/\/www.reddit.com\/r\/EODHistoricalData\/\",\"https:\/\/eod-historical-data.medium.com\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/67681e71050cf7d8d0efb91fee5f0402\",\"name\":\"Nikhil Adithyan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eb53ce41bde412555cee22b8b4c09c2ff51625fff05ba3696b20cac7a7c0d938?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eb53ce41bde412555cee22b8b4c09c2ff51625fff05ba3696b20cac7a7c0d938?s=96&d=mm&r=g\",\"caption\":\"Nikhil Adithyan\"},\"description\":\"Founder at BacktestZone | Streamlit Student Ambassador | FinTech &amp; Quantitative Finance enthusiast\",\"url\":\"https:\/\/eodhd.com\/financial-academy\/author\/nikhiladithyan\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Backtesting Relative Vigor Index Trading Strategy in Python| EODHD APIs Academy","description":"Learn how to use Python to backtest a trading strategy based on the Relative Vigor Index. Enhance your trading skills and increase your profitability","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python","og_locale":"en_US","og_type":"article","og_title":"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python","og_description":"Learn how to use Python to backtest a trading strategy based on the Relative Vigor Index. Enhance your trading skills and increase your profitability","og_url":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python","og_site_name":"Financial Academy","article_publisher":"https:\/\/www.facebook.com\/eodhistoricaldata","article_published_time":"2023-08-29T08:09:17+00:00","article_modified_time":"2024-03-12T11:28:22+00:00","og_image":[{"width":828,"height":552,"url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png","type":"image\/png"}],"author":"Nikhil Adithyan","twitter_card":"summary_large_image","twitter_creator":"@EOD_data","twitter_site":"@EOD_data","twitter_misc":{"Written by":"Nikhil Adithyan","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#article","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python"},"author":{"name":"Nikhil Adithyan","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/67681e71050cf7d8d0efb91fee5f0402"},"headline":"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python","datePublished":"2023-08-29T08:09:17+00:00","dateModified":"2024-03-12T11:28:22+00:00","mainEntityOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python"},"wordCount":3121,"publisher":{"@id":"https:\/\/eodhd.com\/financial-academy\/#organization"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage"},"thumbnailUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png","articleSection":["Backtesting Strategies Examples","How to Get Stocks Data Examples","Stocks Data Analysis Examples","Technical Analysis Examples","Trading Indicators Implimentations"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python","url":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python","name":"Backtesting Relative Vigor Index Trading Strategy in Python| EODHD APIs Academy","isPartOf":{"@id":"https:\/\/eodhd.com\/financial-academy\/#website"},"primaryImageOfPage":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage"},"thumbnailUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png","datePublished":"2023-08-29T08:09:17+00:00","dateModified":"2024-03-12T11:28:22+00:00","description":"Learn how to use Python to backtest a trading strategy based on the Relative Vigor Index. Enhance your trading skills and increase your profitability","breadcrumb":{"@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#primaryimage","url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png","contentUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png","width":828,"height":552,"caption":"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python"},{"@type":"BreadcrumbList","@id":"https:\/\/eodhd.com\/financial-academy\/backtesting-strategies-examples\/implementing-the-relative-vigor-index-and-backtesting-a-trading-strategy-with-python#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/eodhd.com\/financial-academy\/"},{"@type":"ListItem","position":2,"name":"Implementing the Relative Vigor Index and Backtesting a Trading Strategy with Python"}]},{"@type":"WebSite","@id":"https:\/\/eodhd.com\/financial-academy\/#website","url":"https:\/\/eodhd.com\/financial-academy\/","name":"Financial APIs Academy | EODHD","description":"Financial Stock Market Academy","publisher":{"@id":"https:\/\/eodhd.com\/financial-academy\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/eodhd.com\/financial-academy\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/eodhd.com\/financial-academy\/#organization","name":"EODHD (EOD Historical Data)","url":"https:\/\/eodhd.com\/financial-academy\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/","url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png","contentUrl":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/12\/EODHD-Logo.png","width":159,"height":82,"caption":"EODHD (EOD Historical Data)"},"image":{"@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/eodhistoricaldata","https:\/\/x.com\/EOD_data","https:\/\/www.reddit.com\/r\/EODHistoricalData\/","https:\/\/eod-historical-data.medium.com\/"]},{"@type":"Person","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/67681e71050cf7d8d0efb91fee5f0402","name":"Nikhil Adithyan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eodhd.com\/financial-academy\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eb53ce41bde412555cee22b8b4c09c2ff51625fff05ba3696b20cac7a7c0d938?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb53ce41bde412555cee22b8b4c09c2ff51625fff05ba3696b20cac7a7c0d938?s=96&d=mm&r=g","caption":"Nikhil Adithyan"},"description":"Founder at BacktestZone | Streamlit Student Ambassador | FinTech &amp; Quantitative Finance enthusiast","url":"https:\/\/eodhd.com\/financial-academy\/author\/nikhiladithyan"}]}},"jetpack_featured_media_url":"https:\/\/eodhd.com\/financial-academy\/wp-content\/uploads\/2023\/08\/image-10.png","jetpack_shortlink":"https:\/\/wp.me\/pdOdVT-rx","jetpack_sharing_enabled":true,"acf":[],"_links":{"self":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/1707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/comments?post=1707"}],"version-history":[{"count":22,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/1707\/revisions"}],"predecessor-version":[{"id":3059,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/posts\/1707\/revisions\/3059"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/media\/1708"}],"wp:attachment":[{"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/media?parent=1707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/categories?post=1707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/tags?post=1707"},{"taxonomy":"coding-language","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/coding-language?post=1707"},{"taxonomy":"ready-to-go-solution","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/ready-to-go-solution?post=1707"},{"taxonomy":"qualification","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/qualification?post=1707"},{"taxonomy":"financial-apis-category","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-category?post=1707"},{"taxonomy":"financial-apis-manuals","embeddable":true,"href":"https:\/\/eodhd.com\/financial-academy\/wp-json\/wp\/v2\/financial-apis-manuals?post=1707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}