This is an example of my learning to use limit and stop orders. In normal operation, there is a limit order to buy at a lower price and a limit order to sell at a higher price. Here is the description:
Long-only counter-trend algorithm that adjusts every X multiple of the standard deviation of price over a lookback period (using limit and stop orders). It allows one price step per bar and increases the long position according to the bar's down price steps. The position is reduced back to a single long if a bar with an up price step occurs before downSteps exceeds downLimit. When downLimit is exceeded, a reset closes all positions. To try to avoid being long during downturns, trading after a reset is restarted when a dual moving average state becomes positive (fast > slow). The algorithm checks if the position size has exceeded cash, or if the bet is too big, although the share size is calculated to try to be fully invested when downSteps equals downLimit. The last trade price, from which steps are calculated, follows price steps up while holding the single long position.
It doesn't use some of the latest features for Quantopian since it is also trying to be compatible with Zipline, and I am hesitant to put it forward as recommended practice (the blind leading the blind). It was also an excuse to throw stuff in to see how things work between Quantopian and Zipline. Nevertheless, there should be some useful info in there for other beginners.
Best regards,
Bob Schmidt