Back to Community
discrepancies in futures prices? can't get the price or rsi to match other sites

Maybe I'm doing this wrong, but I cant't seem to get the quantopian futures prices to match the prices on other sites.
I also can't get the rsi to match, and the stochrsi only returns nans, even though the price history has a distance of 10?

for example, with this, its saying crude is:

2017-05-18 06:31 PRINT price 48.94
investing.com price: 48.59
2017-05-18 06:31 PRINT rsi 41.4201183432
investing.com rsi(8): 45.1275
2017-05-18 06:31 PRINT stoch nan

2017-05-18 06:32 PRINT price 48.93
investing.com price 48.61
2017-05-18 06:32 PRINT rsi 48.1481481481
investing.com rsi(8): 51.7055
2017-05-18 06:32 PRINT stoch nan

2017-05-18 06:33 PRINT price 48.92
investing.com price 48.63
2017-05-18 06:33 PRINT rsi 52.4691358025
investing.com rsi(8): 57.5247
2017-05-18 06:33 PRINT stoch nan

2017-05-18 06:34 PRINT price 48.98
investing.com price 48.66
2017-05-18 06:34 PRINT rsi 46.1538461538
investing.com rsi(8): 64.7960
2017-05-18 06:34 PRINT stoch nan

2017-05-18 06:35 PRINT price 48.97
investing.com price 48.67
2017-05-18 06:35 PRINT rsi 50.3546099291
investing.com rsi(8): 66.9510
2017-05-18 06:35 PRINT stoch nan

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 592145bcd8141f62166b9421
There was a runtime error.
15 responses

neon,

Try this:

def every_minute(context,data):  
    rsi_period, fk, fd = 8, 4, 4  
    bars = rsi_period + fk + fd + 1

    prices = data.history(context.cl, 'price', bars, '1m')  
    print "price " + str(prices.iloc[-1])

    rsi = talib.RSI(prices[-rsi_period-1:], rsi_period)[-1]  
    print "rsi " + str(rsi)

    stoch = talib.STOCHRSI(prices, rsi_period, fk, fd)[0][-1]  
    print "stoch " + str(stoch)  

May be it will help you.

hmm.... still seems off? Using rsi length 8 still...

2017-05-18 06:31 PRINT price 48.91
48.59
2017-05-18 06:31 PRINT rsi 45.4545454545
45.12
2017-05-18 06:31 PRINT stoch 0.0
45.83

2017-05-18 06:32 PRINT price 48.92
48.60
2017-05-18 06:32 PRINT rsi 50.0
51.70
2017-05-18 06:32 PRINT stoch 9.67988872086
32.14

neon,

Try find out why the prices differ first.

yeah, that's why I made this thread :P

Hi Neon,

Could you share the source to which you're comparing? If you could show some of the specific data points that seem off as well as the prices from other sources to which you are comparing, that would be helpful.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

If the futures data is delayed by 20 mins... what's the point of having it? haha. but maybe I'm doing something really stupid in my interpretation/code...

Jamie: comparing to this = https://www.investing.com/commodities/crude-oil-streaming-chart

using 8,4,4 rsi and stochrsi:

2017-05-18 06:31 PRINT price 48.91
investing.com price - 48.59

2017-05-18 06:31 PRINT rsi 45.4545454545
investing.com rsi - 45.125

2017-05-18 06:31 PRINT stoch 0.0
investing.com stochrsi - 45.48362

it doesnt hit 48.91 until 10:09, nearly 40 mins later...

heres a screenshot

Hi Neon,

Thanks for sharing that resource. I took a look at the investing.com as well as the prices on Quantopian. I can't get either of them to match the numbers you reported. Without seeing the code you used to print out these prices, it 's hard for me to tell exactly which data points you are accessing. Could you share an example of what you used to print these? Here is the code that I used in research to get the prices:

from quantopian.research.experimental import continuous_future, history  
cl = continuous_future('CL', adjustment=None)

price_history = history(  
    cl,  
    fields='price',  
    start_date='2017-05-18',  
    end_date='2017-05-20',  
    frequency='minute'  
)

# Time here is in UTC (this is 6:30ET)  
print price_history['2017-05-18 10:30:00']

# 9:30ET  
print price_history['2017-05-18 13:30:00']  

I'm also sending you an email with screenshots of what I'm seeing on investing.com. I'm not concerned with the RSI or STOCHRSI yet, as there are a couple of ways that these could be calculated. However, the price I'm seeing on the site is different from what I see in the screenshot you shared. Very odd...

I'll run that in a bit and look at the numbers.... the code I was using is what's in the original post, plus what vladimir posted:

from quantopian.algorithm import calendars  
import talib

def initialize(context):  
    context.cl = continuous_future('CL')  


    for minute in range(1, 390, 1):  
        schedule_function(every_minute, date_rules.every_day(),time_rules.market_open(minutes=minute),calendar=calendars.US_EQUITIES)



def every_minute(context,data):  
    rsi_period, fk, fd = 8, 4, 4  
    bars = rsi_period + fk + fd + 1

    prices = data.history(context.cl, 'price', bars, '1m')  
    print "price " + str(prices.iloc[-1])

    rsi = talib.RSI(prices[-rsi_period-1:], rsi_period)[-1]  
    print "rsi " + str(rsi)

    stoch = talib.STOCHRSI(prices, rsi_period, fk, fd)[0][-1]  
    print "stoch " + str(stoch)  

Jamie, is there a difference in running something in a notebook vs an algorithm? I'm running that code I pasted in a backtest algorithm...

Hi Neon,

No difference, I just find it easier to look up prices in research. I can now reproduce the prices that you reported from the backtest (9:31:00 ET). Still can't quite get the prices to match what you reported from investing.com. I'm wondering if this could be a difference between data sources. I'm going to keep poking around and I may follow up with our vendor if it continues to look different.

I'm not too worried about the price difference, as long as it goes up when it goes up.... haha.

Still can't figure out what's wrong with the stochrsi though. I'd want that to be closer than it is at the moment, especially with different time periods...

Neon,

I tracked some pricing values from the investing.com chart last week and they match the prices we have pretty closely (much more closely than the one you pointed out from last week). Additionally, it looks like the last month of pricing trends matches what's on Bloomberg (compare the 1M chart on Bloomberg to the plot in this notebook. I will continue to track this issue but it looks to be ok right now.

For the RSI, do you have insight into how it is being computed on investing.com?

Loading notebook preview...
Notebook previews are currently unavailable.

I'm not sure, but the talib stoch is filled with nans:

def every_minute(context,data):  
    rsi_period, fk, fd = 15,5,5  
    bars = rsi_period + fk + fd + 1

    prices = data.history(context.cl, 'price', bars, '1m')  
    prices.index.tz = 'US/Eastern'  
    print "price " + str(prices.iloc[-1])

    stoch = talib.STOCHRSI(prices, rsi_period, fk, fd)[0]  
    print "stoch " + str(stoch)  

returns something like:
stoch [ nan nan nan nan nan
nan nan nan nan nan
nan nan nan nan nan
nan nan nan nan nan
nan nan nan 21.46534969 0. 0. ]

even though there should be 26 entries in prices to calculate the stochrsi...

neon,

as I mention above, if you need fast_k you should use:

    stoch = talib.STOCHRSI(prices, rsi_period, fk, fd)[0][-1]  

if you need fast_d you should use:

    stoch = talib.STOCHRSI(prices, rsi_period, fk, fd)[1][-1]  
    print "stoch " + str(stoch)  

Neon,

Vladimir's solution is a good one. The talib.STOCHRSI function computes a rolling value. You're seeing a lot of NaNs because the history window provided to the function isn't long enough to compute all (or even most) of the rolling values. Vladimir's solution will get you the most recent stochrsi value. If you want the rolling values, you should be able to just pass a longer history window.