Data analysis - regression to the mean

Mean reversion theory

Regression to the mean: to "fall down sooner or later rise up" stock selection with, not suitable for selection, because they do not know when it is the lowest deviation

Mean reversion theory based on the following observations: price fluctuations generally to its moving average for the center. In other words,

When the subject of price fluctuations due to the deviation from the moving average, it will adjust and re-attributed moving average.

Defines the degree of deviation: (MA-P) / MA --- MA moving average, P Price

Mean reversion strategy: in the transfer positions each day

   Calculation of all stock shares in the pool N-day moving average

   All stocks and the average deviation calculation stock pool

   M select the highest degree of deviation stocks and transfer positions, such as a stock a few years ago less volatile, the sudden appearance of large fluctuations, there is the value of holdings

from jqdata Import * DEF the initialize (context): 
    set_benchmark ( ' 000300.XSHG ' ) 
    Set_Option ( ' use_real_price ' , True) 
    set_order_cost (OrderCost (close_tax = from 0.001, open_commission = 0.0003, = 0.0003 close_commission, min_commission =. 5), type = ' Stock ' ) 
    g.security = get_index_stocks ( ' 000300.XSHG ' ) 
    g.ma_days = 30 # mean regression theory from 30 day reference 
    g.stock_num = 10 # positions 10 stocks 
    run_monthly (handle,


    
    
1)
def handle(context):
    
    sr = pd.Series(index=g.security)
    for stock in sr.index:
        # 计算偏离程度
        ma = attribute_history(stock, g.ma_days)['close'].mean()
        p = get_current_data()[stock].day_open
        ratio = (ma - p)/ma
        sr[stock] = ratio
    to_hold = sr.nlargest(g.stock_num).index # 选好的股票

    for stock in context.portfolio.positions:
        if stock not in to_hold:
            order_target(stock, 0)
    to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions]
    if len(to_buy) > 0:
        cash_per_stock = context.portfolio.available_cash / len(to_buy)
        for stock in to_buy:
            order_value(stock, cash_per_stock)
Stock-picking strategies mean reversion

 

Guess you like

Origin www.cnblogs.com/staff/p/10960674.html