Hi Michael,
It looks like you're trying to implement a moving average crossover algorithm. First thing to note is that it is good practice to add a threshold to the moving average when we are determining if a crossover occurred. We do this because the price can oscillate around the moving average, which would produce many false signals.
Instead of using a boolean, self.over, to keep track of whether a crossover occurred, we can use rolling windows to keep track of the previous prices and compare them to the moving average. This method is easily scalable to multiple symbols. We can create a rolling window to hold the latest 2 prices for our security.
self.priceWindow = RollingWindow[float](2)
and then we can compare the prices to the moving average to determine if a crossover has occurred.
## long crossover
iif self.priceWindow[0] > ma * (1 + self.threshold) and self.priceWindow[1] < ma * (1 - self.threshold):
Lastly, we will need to update our stop loss and limit orders as each fills since they are not automatically cancelled. We can do this in the OnOrderEvents method, which is fired each time an orderevent occurs. Inside this method, we can check if our stop loss is filled and cancel our limit order, and likewise if our limit is filled.