g.security = ['600570.SS', '600130.SS'] def initialize(context): pass def handle_data(context, data): security = g.security for stock in security: sid = symbol(stock) current_price = data[sid]['open'] amount = context.portfolio.positions[sid].amount last = data[sid].mavg(2) * 2 - data[sid]['price'] if amount == 0: order(sid, 100) log.info('buy %s' % stock) elif last < current_price and amount > 100: order(sid, -100) log.info('sell %s' % stock) elif last > current_price: order(sid, 100) log.info('buy %s' % stock)