Back to Community
Need help with Bollinger Bands Algorithm

I am having trouble with my strategy and why it isn't selling at the upper band. Any help on how to fix this would be great.
Thanks

Clone Algorithm
5
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
def initialize (context):
    set_benchmark = (sid(24))
    context.cmg = sid(24)
    
                  
    
    
def handle_data(context, data):
    
    price = data.current(context.cmg, 'price')
    hist = data.history(context.cmg,'price', 50, '1d')
    sma_50 = hist.mean()
    sma_20 = hist[-20:] .mean()
    
    prices = data.history(context.cmg,'price', 20 , '1d')
    avg = prices.mean()
    std = prices.std()
    lower_band = avg - 2*std
    upper_band = avg + 2*std
    
    
    open_orders = get_open_orders()
    
    
    if (sma_20 > sma_50, price <= lower_band):
        if(context.cmg) not in open_orders:
          order_target_percent(context.cmg, 1.0)
    elif (sma_50 > sma_20, price >= upper_band): 
        if(context.cmg) not in open_orders:
          order_target_percent(context.cmg, -1.0)
           
        
        record(leverage = context.account.leverage)
There was a runtime error.
4 responses
if (sma_20 > sma_50, price <= lower_band):  

I don't know a case where you would use a comma in such a statement, but it doesn't do what you expect since it seems to be always true.
See what happens if you write

if (5 < 4, 3 >= 6):  
    print('5 < 4, 3 >= 6')

Replace the comma with 'and' in both conditions

if (sma_20 > sma_50 and price <= lower_band):  

Thanks for the help, but I am still having trouble with the algorithm selling the security.

The thing is that your conditions aren't met very often in that time. However, it also does go short. In this version I only added some prints and plots to see long/short positions. You can see in december it flips from long to short

Clone Algorithm
1
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
def initialize (context):
    set_benchmark = (sid(24))
    context.cmg = sid(24)
    
                  
    
    
def handle_data(context, data):
    
    price = data.current(context.cmg, 'price')
    hist = data.history(context.cmg,'price', 50, '1d')
    sma_50 = hist.mean()
    sma_20 = hist[-20:] .mean()
    
    prices = data.history(context.cmg,'price', 20 , '1d')
    avg = prices.mean()
    std = prices.std()
    lower_band = avg - 2*std
    upper_band = avg + 2*std
    
    
    open_orders = get_open_orders()
    
    
    if (sma_20 > sma_50 and price <= lower_band):
        if(context.cmg) not in open_orders:
          order_target_percent(context.cmg, 1.0)
          print('going long')
    elif (sma_50 > sma_20 and price >= upper_band): 
        if(context.cmg) not in open_orders:
          order_target_percent(context.cmg, -1.0)
          print('going short')
           
    wts = context.portfolio.current_portfolio_weights
    longs = len(wts[wts > 0])
    shorts = len(wts[wts < 0])
    record(longs=longs,
           shorts=shorts,
           )

    record(leverage = context.account.leverage)
There was a runtime error.

Thanks, do you know if the Bollinger Bands are too large for the conditions to be met often.