Skip to content

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

ParameterArgument typeDescriptionDefault Value
orderOrderOrder to close-
commentOptional[str]Comment to add to the order""
tagsOptional[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:

  1. Close all pending orders
  2. Close all open position orders manually
strategy.py
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