I am looking to do an algorithm that holds SPXL when SPY is at its 200 day moving average and liquidates SPXL when SPY falls bellow the 200 day moving average. This action should take place at the end of every trading day. For some reason my algo is not executing as it should and it is buying at certain points when the SPY is bellow it's 200 sma. Can anybody tell me why?