Skip to content

Commit 725b9e4

Browse files
committed
update
1 parent b4d00bf commit 725b9e4

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/project_x_py/position_manager.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,13 @@ def _generate_sizing_warnings(self, risk_percentage: float, size: int) -> list[s
815815
# ================================================================================
816816

817817
def close_position_direct(
818-
self, position_id: int, account_id: int | None = None
818+
self, contract_id: str, account_id: int | None = None
819819
) -> dict[str, Any]:
820820
"""
821821
Close an entire position using the direct position close API.
822822
823823
Args:
824-
position_id: ID of the position to close
824+
contract_id: ID of the position to close
825825
account_id: Account ID. Uses default account if None.
826826
827827
Returns:
@@ -844,7 +844,7 @@ def close_position_direct(
844844
url = f"{self.project_x.base_url}/Position/closeContract"
845845
payload = {
846846
"accountId": account_id,
847-
"positionId": position_id,
847+
"contractId": contract_id,
848848
}
849849

850850
try:
@@ -860,13 +860,13 @@ def close_position_direct(
860860
success = data.get("success", False)
861861

862862
if success:
863-
self.logger.info(f"✅ Position {position_id} closed successfully")
863+
self.logger.info(f"✅ Position {contract_id} closed successfully")
864864
# Remove from tracked positions if present
865865
with self.position_lock:
866866
positions_to_remove = [
867867
contract_id
868868
for contract_id, pos in self.tracked_positions.items()
869-
if pos.id == position_id
869+
if pos.id == contract_id
870870
]
871871
for contract_id in positions_to_remove:
872872
del self.tracked_positions[contract_id]
@@ -886,13 +886,13 @@ def close_position_direct(
886886
return {"success": False, "error": str(e)}
887887

888888
def partially_close_position(
889-
self, position_id: int, close_size: int, account_id: int | None = None
889+
self, contract_id: str, close_size: int, account_id: int | None = None
890890
) -> dict[str, Any]:
891891
"""
892892
Partially close a position by reducing its size.
893893
894894
Args:
895-
position_id: ID of the position to partially close
895+
contract_id: ID of the position to partially close
896896
close_size: Number of contracts to close (must be less than position size)
897897
account_id: Account ID. Uses default account if None.
898898
@@ -921,7 +921,7 @@ def partially_close_position(
921921
url = f"{self.project_x.base_url}/Position/partialCloseContract"
922922
payload = {
923923
"accountId": account_id,
924-
"positionId": position_id,
924+
"contractId": contract_id,
925925
"closeSize": close_size,
926926
}
927927

@@ -939,7 +939,7 @@ def partially_close_position(
939939

940940
if success:
941941
self.logger.info(
942-
f"✅ Position {position_id} partially closed: {close_size} contracts"
942+
f"✅ Position {contract_id} partially closed: {close_size} contracts"
943943
)
944944
# Trigger position refresh to get updated sizes
945945
self.refresh_positions(account_id=account_id)
@@ -991,16 +991,20 @@ def close_all_positions(
991991

992992
for position in positions:
993993
try:
994-
close_result = self.close_position_direct(position.id, account_id)
994+
close_result = self.close_position_direct(
995+
position.contractId, account_id
996+
)
995997
if close_result.get("success", False):
996998
results["closed"] += 1
997999
else:
9981000
results["failed"] += 1
9991001
error_msg = close_result.get("errorMessage", "Unknown error")
1000-
results["errors"].append(f"Position {position.id}: {error_msg}")
1002+
results["errors"].append(
1003+
f"Position {position.contractId}: {error_msg}"
1004+
)
10011005
except Exception as e:
10021006
results["failed"] += 1
1003-
results["errors"].append(f"Position {position.id}: {e!s}")
1007+
results["errors"].append(f"Position {position.contractId}: {e!s}")
10041008

10051009
self.logger.info(
10061010
f"✅ Closed {results['closed']}/{results['total_positions']} positions"
@@ -1043,10 +1047,12 @@ def close_position_by_contract(
10431047
# Determine if full or partial close
10441048
if close_size is None or close_size >= position.size:
10451049
# Full close
1046-
return self.close_position_direct(position.id, account_id)
1050+
return self.close_position_direct(position.contractId, account_id)
10471051
else:
10481052
# Partial close
1049-
return self.partially_close_position(position.id, close_size, account_id)
1053+
return self.partially_close_position(
1054+
position.contractId, close_size, account_id
1055+
)
10501056

10511057
# ================================================================================
10521058
# UTILITY AND STATISTICS METHODS

0 commit comments

Comments
 (0)