Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to use moving average in my_rebalance for exit position ?

Hi,
I`m trying to programme my exit logic in algorithm. I want exit position when SimpleMovingAverage_10 > SimpleMovingAverage_30

Here is my syntax

def my_rebalance(context,data):

for stock in context.portfolio.positions:  

    SMA_10 = SimpleMovingAverage(inputs=[USEquityPricing.close],window_length=10,mask=stock)  
    SMA_30 = SimpleMovingAverage(inputs=[USEquityPricing.close],window_length=30,mask=stock)  
    bSell = SMA_10 > SMA_30  
    print('Sell stock: ' , bSell)  

    if bSell and data.can_trade(stock):  
        order_target_percent(stock,0)

When I print my variable bSell, which is supposed to be a boolean, output is:
PRINT ('SMA_10: ', NumExprFilter(expr='x_0 > x_1', bindings={'x_0': SimpleMovingAverage([close], 10), 'x_1': SimpleMovingAverage([close], 30)}))

How can I use bSell as boolean for exit position logic?

Thanks,

Michael

4 responses

@Michael

This may help:

# bBuy - bSell  
# ------------------------------------------------------------  
assets, ma_f, ma_s, lev = symbols('AAPL', 'IEF'), 10, 200, 1.0  
# ------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_close( minutes = 15))   

def trade(context, data):  
    if get_open_orders(): return

    mavg_f = data.history(assets, 'price', ma_f, '1d').mean()  
    mavg_s = data.history(assets, 'price', ma_s, '1d').mean()  
    bBuy   = mavg_f > mavg_s  
    bSell  = mavg_f < mavg_s

    for asset in assets:  
        if data.can_trade(asset):  
            if bBuy[asset]:  
                order_target_percent(asset, lev/len(assets))  
            elif bSell[asset]:  
                order_target_percent(asset, 0)

    record(leverage = context.account.leverage)  

@Vladimir

Thank you so much for answer. Your solution is creative, but quite different what I want. I`m interested in how to take indicator such as Simple moving average, RSI or EMA etc. and feed into them stock from context.portfolio.positions.

Thanks

May be this way:

# bBuy - bSell  
# ------------------------------------------------------------  
assets, ma_f, ma_s, lev = symbols('AAPL', 'IEF'), 10, 200, 1.0  
# ------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_close( minutes = 15))   

def trade(context, data):  
    if get_open_orders(): return

    mavg_f = data.history(assets, 'price', ma_f, '1d').mean()  
    mavg_s = data.history(assets, 'price', ma_s, '1d').mean()  
    bBuy   = mavg_f > mavg_s  
    bSell  = mavg_f < mavg_s

    for asset in assets:  
        if data.can_trade(asset):  
            if bBuy[asset]:  
                order_target_percent(asset, lev/len(assets))

    for asset in context.portfolio.positions:  
        if data.can_trade(asset):  
            if bSell[asset]:  
                order_target_percent(asset, 0)

    record(leverage = context.account.leverage)

@Vladimir , Thanks for all your help. I searched on forums and find solution. It's based on your code which was shared by you in another forum question. So thanks again for helping community!

I import talib library

def record_SMA(context, data):  

    fast = 10  
    slow = 30  


    for stock in context.portfolio.positions:  
          prices = data.history(stock, 'close', slow, '1d')  
          SMA_fast = talib.SMA(prices, fast)[-1]  
          SMA_slow = talib.SMA(prices, slow)[-1]  
          bClose = SMA_slow < SMA_fast  

          if bClose and data.can_trade(stock):  
            order_target_percent(stock,0)  
            print('close position for stock:' , stock)