Trading-Bot-M20 / _close_all.py
raghava4u's picture
Upload folder using huggingface_hub
d53dc44 verified
Raw History Blame Contribute Delete
1.18 kB
"""Quick script to close all positions and cancel all orders."""
from execution import broker
import config
print("Cancelling all open orders...")
try:
broker.cancel_all_orders()
print("Orders cancelled.")
except Exception as e:
print(f"Cancel orders: {e}")
print("Closing all positions...")
try:
broker.close_all_positions()
print("All positions closed.")
except Exception as e:
print(f"Close positions: {e}")
try:
acct = broker.get_account()
eq = float(acct.get("equity", 0))
bp = float(acct.get("buying_power", 0))
print(f"Account equity: ${eq:,.2f}")
print(f"Buying power: ${bp:,.2f}")
except Exception as e:
print(f"Account check: {e}")
try:
positions = broker.get_positions()
if positions:
print(f"WARNING: {len(positions)} positions still open:")
for p in positions:
sym = p.get("symbol")
qty = p.get("qty")
pl = float(p.get("unrealized_pl", 0))
print(f" {sym}: {qty} shares, P&L: ${pl:,.2f}")
else:
print("Confirmed: 0 open positions.")
except Exception as e:
print(f"Position check: {e}")