Back to Community
DV2, DVB or other mean reversion indicator needed, suggestion?

Hi. Im looking for a way to use a mean reversion indicator in my algorithm.
Basically i want my strategy to be trendbased using MAVG and price to determine the action but I want to combine this with a mean reversion indicator to trigger when to exit my position.

Is there any TA-Lib function or example for something like DV2/DVB?

6 responses

I found TA-Lib RSI. Maybe that is a good indicator for what I want to achieve?

Hello Jonas,

Saw your post, but wasn't sure how to respond. Have you figured this out?

Grant

Eh, well I managed to use RSI in my algorithm to check whether stock is oversold or overbought but Im a bit pussled about which timeperiod is used for ex:
rsi = ta.RSI(timeperiod=3)
momentum5 = ta.CMO(timeperiod=5)

What is timeperiod=3 here? is it 3 days or 3 minutes?

The TA-Lib methods have the same period as the test in which they are used. So in live trading it would be always be minutes.

[Response below was edited]

@jonas and @David,

David is correct that the Ta-Lib method uses the same period as the backtest mode. In live trading, the timeperiods are in minute windows.

TA-Lib methods are powered by batch_transform, which does not warm up in backtesting. This means it will start reporting the 3 day RSI once the window has been filled, which is the 4th day. Here is more information on batch_transform and the trailing window.

If you use technical indicators in live trading, these will warmup. We make sure all your functions are warmed up prior to market open. So on your first day of live trading, your RSI will be accurate.

In the future, we hope to deprecate batch_transform in favor of history - we will also improve history to allow minutely trailing data.

Cheers,
Alisa

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.

Hello David/Alisa,

TA-Lib can be imported and applied to any time series such as daily data derived from the 'history' function in a minutely backtest. In that scenario it is 'warmed' up in both backtesting and live.

import numpy  
import talib

def initialize(context):  
    context.spy = sid(8554)

def handle_data(context, data):  
    spy = context.spy  
    # Get 30 days plus the current minute  
    prices = history(31, '1d', 'close_price')  
    # Drop off the current minute  
    spyprices = numpy.array(prices[spy][0:-1])  
    talibMA7 = talib.MA(spyprices, 7)[-1]  
    talibMA30 = talib.MA(spyprices, 30)[-1]

P.