def handle_data(context, data):
MA1 = data[context.security].mavg(50)
MA2 = data[context.security].mavg(20)
MA3 = data[context.security].mavg(200)
current_price = data[context.security].price
current_positions = context.portfolio.positions[symbol('SPY')].amount
cash = context.portfolio.cash
if (MA2 > MA1) and current_positions == 0:
number_of_shares = int(cash/current_price)
order(context.security, number_of_shares)
log.info("Buying Shares")
elif (MA1 < MA3) and current_positions != 0:
order_target(context.security, 0)
log.info("Selling Shares")
record(MA50=MA1, MA20 = MA2, MA200 = MA3, Price = current_price)