Discrepancy in Calculated RVI indicator in Python using ib_insync library and Interactive Brokers data

91 views Asked by At

I am attempting to calculate the Relative Volatility Index (RVI) in Python using the ib_insync library. I have verified that the historical price data I am fetching from IB is accurate, but when I calculate the RVI, the values I obtain are different from the RVI values displayed on IB's platform.

Here's the code snippet I am using to calculate the RVI:

code 1:

import pandas as pd

# Connect to IB
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)

# Fetch historical data
symbol = 'AAPL'
contract = Stock(symbol, 'SMART', 'USD')
bars = ib.reqHistoricalData(contract, endDateTime='', durationStr='1 Y', barSizeSetting='1 day', whatToShow='TRADES', useRTH=True)

# Convert data to DataFrame
df = util.df(bars)

# Calculate standard deviation of high and low prices
df['std_high'] = df['high'].rolling(window=10).std()
df['std_low'] = df['low'].rolling(window=10).std()

# Calculate RVI
df['RVI'] = (df['std_high'] / (df['std_high'] + df['std_low']))

# Print the RVI
print(df['RVI'])

code 2:

# Calculate the standard deviation of price changes using stddev_length
df['stddev'] = df['close'].diff().rolling(window=stddev_length).std()

# Determine where close prices went up or down
up_days = df['close'].diff() > 0
down_days = df['close'].diff() <= 0
df['rvi'] = df['upper'] / (df['upper'] + df['lower']) * 100
print(df)

I have double-checked the formula I am using for RVI and the data I am fetching from IB, and both seem to be correct. However, the RVI values I am calculating are not matching with the RVI values on IB's platform with a few point difference. Is there something I am missing or doing incorrectly in my code? Is there a different formula or method used by IB to calculate RVI that I should be aware of?

IB RVI indicator settings is the same as the code.

Any help or insight would be greatly appreciated.

0

There are 0 answers