If a signal continuously meets the condition, the strategy may repeatedly execute the ”Place Order Card” .
How to ensure the strategy buys or sells only once when the signal meets the condition? Here are the two simplest methods.
Method 1: Determine positions
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 Card" > 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 and has held position , it will be represented by 1;
● If no order has been placed and no position is held, it will be represented by 0;
According to the above rules, the value of the global variable "a" is 0 by default (indicating that no order has been placed and no position is held).
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;
Step 3: Add an ”Value Assignment Card” after the ”Place Order Card” , and re-assign value to the global variable "a":
● Path to close positions: re-assign 0 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.