Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Extending security properties off of data -- expando style

In my search to arrive at object orientedness I often created my own security objects and hosted properties on them.

However, as a replacement for this technique, it seems that, just like the context object, the security object off of data[]... is also expandable.

Anyone see issues with using expando's on the security object for such things as metrics and trailing stop loss settings?

[Edited: this logic presents a self contained trailing stop routine and the schedule function to drive it.]
[Edited#2: Updated, Gary. Thanks.]

def initialize(context):  
    context.stock = symbol('SPY')  
    context.stocks = symbols('SPY','DIA','QQQ')  
    schedule_function(func=HandleDataScheduled,  
                      date_rule=date_rules.week_start(days_offset=0),  
                      time_rule=time_rules.market_open(hours=0, minutes=5))  
    schedule_function(TrailingStop,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_close(hours=0, minutes=5))

def handle_data(context, data):  
    pass

def HandleDataScheduled(context, data):  
    for stock in context.stocks:  
        order(stock, 1)

    record(Quantity  = context.portfolio.positions[context.stock].amount)  
    record(CostBasis = context.portfolio.positions[context.stock].cost_basis)  
    record(Price     = data[context.stock].close_price)  
    record(Leverage  = context.account.leverage)  
    if ('trailingStop' in data[context.stock]):  
        record(Stop  = data[context.stock].trailingStop)

def TrailingStop(context, data):  
    for stock in data:  
        if context.portfolio.positions[stock].amount > 0:  
            if ('trailingStop' in data[stock]):  
                if (data[stock].close_price < data[stock].trailingStop):  
                    order_target_percent(stock, 0.0)  
                    print("TrailingStop")  
                    del data[stock].trailingStop  
                    continue  
                else:  
                    data[stock].trailingStop = max(data[stock].trailingStop, data[stock].low - 5.0)  
            else:  
                data[stock].trailingStop = data[stock].low - 5.0