Data analysis - Momentum Strategy vs contrarian strategies (stock picking)

Momentum strategy:

  If a stock a good performance the previous period, the next period of time to throw the stock will perform well.

Reverse strategy:

  If a stock performed poorly the previous period, the next period of time the stock will be reversed, that performance changed for the better.

Momentum Strategies & Reversal Strategy

  Calculation of stock in the pool all the previous stock returns over time

  Select the maximum rate of return (minimum) of N stocks to adjust positions, momentum strategies selected maximum, minimum reversal election strategy

 

from jqdata import *

def initialize(context):
    set_benchmark('000300.XSHG')
    set_option('use_real_price', True)
    set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
    
    
    
    g.N = 10 
    g.benchmark = '000300.XSHG'
    run_monthly(handle, 1)
    
def handle(context):
    
    stocks = get_index_stocks(g.benchmark)
    
    df_close = history(30, field='close', security_list=list(stocks)).T
    
    df_close['ret'] = (df_close.iloc[:,-1]-df_close.iloc[:,0])/df_close.iloc[:,0]
    sorted_stocks = df_close.sort_values('ret', ascending=False).index
    
    to_hold = sorted_stocks[:g.N]

    for stock in context.portfolio.positions:
        if stock not in to_hold:
            order_target_value(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)
Momentum Strategies

The inside of the "sorted_stocks = df_close.sort_values ​​( 'ret', ascending = False) .index" False to True is the reverse strategy

 

Guess you like

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