With the trade ticket functionality developed in Tutorial 7, only a couple of modifications are required to build a tool that closes positions with a prompt similar to the one used before. In this case, the requirements for this functionality are fairly straightforward:
- The NetPositions tab keeps track of all of the client's positions using a subscription setup.
- For each position, a CLOSE action is provided, which opens the trade prompt.
- The prompt loads all relevant data and calculates the amount Buy/Sell required to close the position.
- After the user confirms, the OpenApiClosePosition() formula is called to send a trade order to the OpenAPI.
- If a position is already closed (Amount = 0), a warning message is shown.
The final result looks as below:
As before, the UserForm object in VBA is used to display an interactive prompt. On the NetPositions tab, a similar tracking mechanism is used to check whether the user is clicking a cell marked CLOSE (see below). Key points in this script are the same as before, with the addition of the following:
- The tradeamount variable is calculated based on the client's current position. Long positions are translated into a Sell order with the same (positive) amount, whereas short positions (which are negative) are translated to a Buy order with the corresponding (positive) amount.
- If a position is already closed, the entire subroutine is skipped and a message is displayed informing the user.
- As before, the Close_Ticket is loaded in the center of the screen, with all required parameters pre-loaded.
The Close_Ticket itself only includes a single subroutine linked to the Place Market Order button. This button performs the exact same steps as in the previous tutorial, except for:
- The direction of the order is taken from theordertext field, which in turn is pre-loaded when the ticket is launched.
- The tradeamount is derived using the client's current position (similar to the above).
- OpenApiClosePosition() ia called instead of PlaceOrder().
This way, the user is able to quickly open and close positions through the Excel spreadsheet. In addition, all current positions are updated in real-time, as are the trade messages (to keep track of trade confirmations).