If a signal continuously meets the condition, the strategy may repeatedly execute the "Place Order".
How to ensure the strategy buys or sells only once when the signal meets the condition? Here are the two simplest methods.
If the strategy places a type of order that can be filled quickly (e.g., a market order), then you can indirectly determine whether a position has been opened or closed successfully through the underlying stocks you are holding.
● If the number of underlying stocks you are holding is > 0, then the position has been opened successfully, and the path to close positions will run;
● If the number of underlying stocks you are holding is 0, then the position has been closed successfully or has not been opened, and the path to open positions will run;
Note: This method requires that the order must be filled quickly; otherwise repeated orders may still be placed.
Method 2: Use a counter
If the strategy places a type of order that may not be filled quickly (e.g., a limit order), you can create a value global variable as a counter to indicate whether an order has been placed.
Step 1: Go to Start > Properties > Global Variables, and create a counter named "a" to indicate whether a buy/sell order has been placed.
● If a buy order has been placed, it will be represented by 1;
● If no order has been placed, it will be represented by 0;
● If a sell order has been placed, it will be represented by -1.
According to the above rules, the value of the global variable "a" is 0 by default (indicating that no order has been placed).
Step 2: Determine the global variable "a" and the number of holdings first when the strategy starts to run:
● If "a" = 0, and the number of holdings = 0, it means that no order has been placed and no position is held, and the path to open positions will run;
● If "a" = 1, and the number of holdings > 0, it means that a buy order has been placed and there are positions held (the buy order has been filled), and the path to close positions will run;
● If "a" = -1, and the number of holdings = 0, it means that a sell order has been placed and no position is held (the sell order has been filled), and the path to open positions will run;
Step 3: Add an "Value Assignment" after the "Place Order", and re-assign value to the global variable "a":
● Path to close positions: re-assign -1 to the global variable "a" to indicate a sell order has been placed.
● Path to open positions: re-assign 1 to the global variable "a" to indicate a buy order has been placed.
Here are two order scenarios: placing an order for a fixed number of shares and for shares as a percentage of cash.
If you need to trade a fixed number of underlying stocks, just select "Shares" and enter a value in the "Qty" box of the "Place Order".
Scenario 2: Placing an order for a pro rata number of shares
If you need to trade a pro rata number of shares (e.g.,100% of available cash), you only need to select "Buyable on Cash" and enter the percentage in the "Qty" box of the "Place Order".
Different users have different needs for the timing of when a signal triggers buying or selling. Here are two ways to monitor signals: monitoring once for every tick vs monitoring once for every certain time frame candlestick.
If you want to monitor signals once for every tick, you only need to select "Run once for every tick" in the "Live Trading/Backtest Settings" window.
If you want to monitor signals once for every certain time frame candlestick, you need to select the corresponding time frame in the "Live Trading/Backtest Settings" window.
This presentation is for informational and educational use only and is not a recommendation or endorsement of any particular investment or investment strategy. Investment information provided in this content is general in nature, strictly for illustrative purposes, and may not be appropriate for all investors. It is provided without respect to individual investors’ financial sophistication, financial situation, investment objectives, investing time horizon, or risk tolerance. You should consider the appropriateness of this information having regard to your relevant personal circumstances before making any investment decisions. Past investment performance does not indicate or guarantee future success. Returns will vary, and all investments carry risks, including loss of principal. Moomoo makes no representation or warranty as to its adequacy, completeness, accuracy or timeliness for any particular purpose of the above content.