Back to Community
Modifying risk constraints based on time of the year

I read this article:

https://www.investopedia.com/day-trading/best-time-day-week-month-trade-stocks/

And implemented some code to change your optimal_portfolio risk constraints depending on the time of the year. So far it has given me inconsistent results. I'd be interested in hearing what sorts of results other people got when they used it in their algorithms.

I've also thought of adding some stuff about style/sector risks to it in the future. The numbers could be tweaked - they're just based on maintaining an amount of risk acceptable to quantopians algorithm but you might get some risk failures.

def constraints(context):  
    constraints = []  
    RISK_PARAMS = {}  
    RISK_PARAMS['MAX_GROSS_LEVERAGE'] = 1.05  
    RISK_PARAMS['MAX_SHORT'] = 0.04  
    RISK_PARAMS['MAX_LONG'] = 0.04  
    RISK_PARAMS['BETA'] = 0.25  
    RISK_PARAMS['DOLLAR_TOLERANCE'] = 0  
    if algo.get_datetime(tz=None).strftime("%B") == 'January':  
        increaseSell(RISK_PARAMS)  
    elif algo.get_datetime(tz=None).strftime("%B") == 'May':  
        increaseSell(RISK_PARAMS)  
    elif algo.get_datetime(tz=None).strftime("%B") == 'November':  
        increaseBuy(RISK_PARAMS)  
    elif algo.get_datetime(tz=None).strftime("%B") == 'December':  
        increaseBuy(RISK_PARAMS)  
    if algo.get_datetime(tz=None).strftime("%A") == 'Monday':  
        minorincreaseBuy(RISK_PARAMS)  
    elif algo.get_datetime(tz=None).strftime("%A") == 'Friday':  
        minorincreaseSell(RISK_PARAMS)  
    pipe_beta_data = context.pipeline_beta_data.dropna()  
    beta_neutral = opt.FactorExposure(pipe_beta_data[['beta']],min_exposures={'beta': -RISK_PARAMS['BETA']},max_exposures={'beta': RISK_PARAMS['BETA']})  
    constraints.append(beta_neutral)

    constrain_gross_leverage = opt.MaxGrossExposure(RISK_PARAMS['MAX_GROSS_LEVERAGE'])  
    constraints.append(constrain_gross_leverage)  
    dollar_neutral = opt.DollarNeutral(RISK_PARAMS['DOLLAR_TOLERANCE'])  
    constraints.append(dollar_neutral)  
    constrain_pos_size = opt.PositionConcentration.with_equal_bounds(  
        -RISK_PARAMS['MAX_SHORT'],RISK_PARAMS['MAX_LONG'])  
    constraints.append(constrain_pos_size)  
    risk_loadings = context.risk_loadings  
    constrain_sector_style_risk = opt.experimental.RiskModelExposure( risk_model_loadings= risk_loadings,version=opt.Newest)  
    constraints.append(constrain_sector_style_risk)  
    return constraints

def increaseBuy(RISK_PARAMS):  
    RISK_PARAMS['MAX_GROSS_LEVERAGE'] += 0.02  
    RISK_PARAMS['BETA'] += 0.1  
    RISK_PARAMS['DOLLAR_TOLERANCE'] += 0.05  
def increaseSell(RISK_PARAMS):  
    RISK_PARAMS['MAX_GROSS_LEVERAGE'] -= 0.025  
    RISK_PARAMS['BETA'] -= 0.1

def minorincreaseBuy(RISK_PARAMS):  
    RISK_PARAMS['MAX_GROSS_LEVERAGE'] += 0.00  
    RISK_PARAMS['BETA'] += 0.005  
    RISK_PARAMS['DOLLAR_TOLERANCE'] += 0.001  
def minorincreaseSell(RISK_PARAMS):  
    RISK_PARAMS['MAX_GROSS_LEVERAGE'] -= 0.005  
    RISK_PARAMS['BETA'] -= 0.005