Back to Community
Laguerre RSI Strategy

I've been playing around with the Laguerre RSI and this is the best I could do. Perhaps someone could improve.

Strategy pseudo:
Overlay Laguerre RSI with regular RSI
Long signal when Laguerre in uptrend (1) and RSI oversold. Enter at RSI trough formation
Short signal when Laguerre in downtrend (0) and RSI overbought. Enter at RSI peak formation
Close entries with ATR trailing stop

enjoy

Clone Algorithm
145
Loading...
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
import talib
from datetime import datetime
import numpy as np


def initialize(context):
    context.max_notional = 100000
    context.sid = symbol('qqq')#spy
    set_benchmark(context.sid)
    context.L0 = 0
    context.L1 = 0
    context.L2 = 0
    context.L3 = 0
    context.lagrsi = 0    
    context.gamma = .6
    context.rsitimeperiod = 3
    context.rsi_oversold = .4
    context.rsi_overbought = .6
    context.lag_uptrend = 1
    context.lag_downtrend = 0
    context.trackrsi = []
    context.longsetup = False
    context.shortsetup = False
    context.rsitrough = False
    context.rsipeak = False
    context.tracklag = []

    schedule_function(
        func=getlag,
        date_rule=date_rules.every_day(),
        time_rule=time_rules.market_open(minutes=1),
        half_days=True)

    context.long_entry = []
    context.short_entry = []
    context.long_stop = 0
    context.short_stop = 0
    context.maxlvg = 3
    
    context.atr_multiplier = 3
    context.atr_period = 21
    context.percent_order = 1


def lag_rsi(context,prices_close):
    L0_n = (1 - context.gamma)*prices_close + context.gamma*context.L0
    L1_n = -context.gamma*L0_n+context.L0+context.gamma*context.L1
    L2_n = -context.gamma*L1_n+context.L1+context.gamma*context.L2
    L3_n = -context.gamma*L2_n+context.L2+context.gamma*context.L3

    context.L0 = L0_n
    context.L1 = L1_n
    context.L2 = L2_n
    context.L3 = L3_n

    cu = 0
    cd = 0

    if L0_n >= L1_n:
        cu = L0_n - L1_n
    else:
        cd = L1_n - L0_n

    if L1_n >= L2_n:
        cu = cu + L1_n - L2_n
    else:
        cd = cd + L2_n - L1_n

    if L2_n >= L3_n:
        cu = cu + L2_n - L3_n
    else:
        cd = cd + L3_n - L2_n
    if cu+cd != 0:
        lrsi = cu/(cu+cd)
        return lrsi
    
 

def getlag(context,data):
    

    
    highs = data.history(context.sid, 'high', context.atr_period, '1d')
    lows = data.history(context.sid, 'low', context.atr_period, '1d')
    closes = data.history(context.sid, 'close', context.atr_period, '1d')
    
    ATR = talib.ATR(highs, lows, closes)

    
    prices = data.history(context.sid, 'close', 500, '1d')
       
    closeprice = data.history(context.sid, 'close', 3, '1d')[-2]
    highprices = data.history(context.sid, 'high', 10, '1d')
    lowprices = data.history(context.sid, 'low', 10, '1d')
    

    rsi = talib.RSI(prices, timeperiod=context.rsitimeperiod)
    
    context.lagrsi = lag_rsi(context,closeprice)
    context.tracklag.append(context.lagrsi)

    
    record(lrsi_fast = context.lagrsi)
    record(rsi = (rsi[-1]*.01))

    context.trackrsi.append(rsi[-1]*.01)
    
    if len(context.trackrsi) >= 3:
        if context.trackrsi[-3] < context.trackrsi[-2] and context.trackrsi[-1] < context.trackrsi[-2] and context.trackrsi[-2] > context.rsi_overbought:
            context.rsipeak = True
        else:
            context.rsipeak = False

    if len(context.trackrsi) >= 3:
        if context.trackrsi[-3] > context.trackrsi[-2] and context.trackrsi[-1] > context.trackrsi[-2] and context.trackrsi[-2] < context.rsi_oversold:
            context.rsitrough = True
        else:
            context.rsitrough = False

    if context.rsitrough == True and context.tracklag[-2] >= context.lag_uptrend:
        context.longsetup = True
    else:
        context.longsetup = False        

    if context.rsipeak == True and context.tracklag[-2] <= context.lag_downtrend:
        context.shortsetup = True
    else:
        context.shortsetup = False


    if context.longsetup == True:

        context.long_position = 1
        context.long_entry.append(data[context.sid].price)
        if  context.account.leverage < context.maxlvg:
            order_target_percent(context.sid, context.percent_order)
        
    if context.shortsetup == True:
        context.short_position = 1
        context.short_entry.append(data[context.sid].price)
        if  context.account.leverage < context.maxlvg:       
            order_target_percent(context.sid, -context.percent_order)

        
    for entry in context.long_entry:   
        if  context.long_stop > data[context.sid].price:
            currentshares = context.portfolio.positions[context.sid].amount
            if currentshares > 0.0:
                order_target(context.sid, 0)
            
            context.long_entry.remove(entry)

    for entry in context.short_entry:
        if  context.short_stop < data[context.sid].price:
            order_target(context.sid, context.percent_order)

            context.short_entry.remove(entry)

    context.long_stop = closeprice-(ATR[-1]*context.atr_multiplier) 
    context.short_stop = closeprice+((ATR[-1]*context.atr_multiplier)/4)


            
There was a runtime error.