I am seeing what appears to be unusual behavior of StopMarketOrder in combination with MarketOrder during backtesting. Can you verify that my understanding of the use of MarketOrder and StopMarketOrder is correct?

Suppose I issue a MarketOrder for some number N shares of an equity E at some price P.

If N is positive, I am going long (buying the equity), which means I am betting that the equity price will increase. To protect myself from a price decrease, I can also issue a StopMarketOrder to sell the equity if the price decreases to some price lower by a delta D. That would be a StopMarketOrder at -N shares at price P - D.

If the share price of E goes up, I can Liquidate E, which will sell the N shares at a profit, and cancel the StopMarketOrder at the same time. If the share price for E goes down by more than or equal to D, then the StopMarketOrder is triggered, and the shares are sold at market value, limiting the loss. A later Liquidation of E will act as a no-op since the shares have already been liquidated.

On the other hand, suppose N is negative. That is, I issue a MarketOrder for some number -N shares of equity E at some price P. I am going short (selling the equity), which means I am betting that the equity price will decrease. To protect myself from a price increase, I can also issue a StopMarketOrder to buy the equity if the price increases to some price higher by a delta D. That would be a StopMarketOrder at positive N shares at price P + D.

If the share price of E goes down, I can Liquidate E, which will buy the N shares at a profit, and cancel the StopMarketOrder at the same time. If the share price for E goes up by more than or equal to D, then the StopMarketOrder is triggered, and the shares are bought at market value, limiting the loss. A later liquidation of E will act as a no-op since the shares have already been liquidated.

Please let me know if this is a correct description of how MarketOrder and StopMarketOrder are supposed to work.