Closing a Position
Closing a position is a simple way to close an open position. This is useful if you want to close a position at once, for example when you want to exit a position when a certain condition is met.
Method
tm.close_position(order, comment="", tags={})
Inputs
Parameter | Argument type | Description | Default Value |
---|---|---|---|
order | Order | Order to close | - |
comment | Optional[str] | Comment to add to the order | "" |
tags | Optional[Dict[str, str]] | Tags to add to the order | {} |
Outputs
Order that was created to close the position.
Example usage
The below example shows how to close orders in the following scenarios:
- Close all pending orders
- Close all open position orders manually
import tradomate as tm
@tm.strategy()def my_strategy(config: tm.TradomateConfig, data: tm.TradomateData):
# ... Your strategy code here
# Close all pending orders pending_orders = tm.get_all_pending_orders() for order in pending_orders: # Close a position order = tm.close_position(order, comment="Closing position", tags={"reason": "Stop loss"})
# Close all open position orders manually open_position_orders = tm.get_all_open_orders() for order in open_position_orders: # Close a position order = tm.close_position(order, comment="Closing position", tags={"reason": "Stop loss"})
# ... Your strategy code here