If you have a good idea, you may wonder how to realize it and quickly build a strategy on the canvas.
First of all, we must identify the conditions under which we want the strategy to open and close positions.
Let's take the Double Moving Average (Double MA) strategy as an example. The Double MA strategy is to buy when the shorter-term moving average (MA) above the longer-term MA, and sell when the shorter-term MA crosses below the longer-term MA.
With this idea in mind, let's quickly figure out the paths to open and close positions. The upper path is to determine when to open positions, while the lower path, to close positions.
When MA(5) is above MA(10), open a position; when MA(5) is below MA(10), close all positions.
But this strategy is not yet perfect. There is a serious problem:
MA(5) may be continuously above MA(10) (golden cross) when the stock price is rising. If a position is opened every time it is determined that this condition is met, it won’t be long before your cash is used up(and margin may even be used), which is not what we expected.
If MA(5) is above MA(10) all the time, we would like to buy once only. Similarly, if MA(5) is always below MA(10) (dead cross), we also wish to close positions only once.
How can we achieve this?
All we need is to add a condition to determine whether there is any position: The strategy determines whether to open a position only when there is no position, and whether to close a position only when there is a position.
In this way, when the condition to open a position is met and the position is opened, we will hold the underlying stock in our position, and then the yellow path will run and there will be no repeated opening of positions.
In order to make the whole strategy more robust, we add a condition on buying power before opening a position to ensure that the order will not fail due to insufficient buying power.
With this idea in mind, you may find our Double MA strategy not so hard to understand.
By summarizing the above idea into a methodology, we get a general template for simple strategies to open and close positions.
To create similar strategies later, you only need to apply the template and fill in the corresponding conditions to open and close positions.
It is very simple to build a strategy to add to positions. Just add the green part to the original strategy. When the condition to close positions is not met, the strategy will determine whether the condition to add to positions is met. If this condition is met, an order can be placed to add to positions.
Of course, don’t forget to make this condition to iterateat the end in case it is continuously met and the strategy will soon use up our cash.
It is also very simple to build a strategy to take profit and stop loss. Just replace the condition to close positions with the condition to take profit and close position as shown in the yellow part, and then add a similar path as shown in the purple part.
When the condition to take profit and close position is not met, the strategy will determine whether the condition to stop loss is met. If this condition is met, all positions can be closed.
By integrating the purple part (stop-loss condition) and the green part (adding-to-positions condition) above, we get a template for a strategy that can not only add to positions, but also take profit and stop loss.
When there is a position, the strategy first determines whether the condition to take profit and close positions is met; if so, it will take profit (close all positions). If that condition is not met, the strategy determines whether the condition to stop loss and close positions is met; if so, it will stop loss (close all positions). If that condition is not met, the strategy determines whether the condition to add to positions is met; if so, it will place an order to add to the positions and make theadding-to-positions condition to iterate.
Moomoo is a professional trading app offered by Moomoo Technologies Inc.
Any illustrations, scenarios, or specific securities referenced herein are strictly for illustrative purposes. Past investment performance does not guarantee future results. Investing involves risk and the potential to lose principal.