Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

fetch MTM (Mark-to-Market) data using the Interactive Brokers (IBKR) API

Last updated on 5 days ago
K
KevinMember
Posted 5 days ago
To fetch **MTM (Mark-to-Market)** data using the **Interactive Brokers (IBKR) API**, you'll typically use the **TWS API (Trader Workstation API)** or the IBKR Gateway. Here’s a general workflow:
---
### Steps to Get MTM:
1. **Set Up IBKR API**:
- Ensure TWS or IBKR Gateway is running and API access is enabled.
- Install the IBKR API SDK (Python ib_insync is recommended for simplicity).

2. **Fetch Positions**:
- Use the API to fetch open positions using the reqPositions() function.

3. **Fetch Market Data**:
- Use the reqMktData() function to get the latest market prices for the instruments in your positions.

4. **Calculate MTM**:
- MTM = (Current Market Price - Average Cost) × Quantity
---
### Python Example Using ib_insync:

1. **Install the SDK**:
bash
 pip install ib_insync
 
K
KevinMember
Posted 5 days ago
2. **Code**:
python
from ib_insync import IB

# Connect to TWS or IBKR Gateway
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1) # Adjust port if using IBKR Gateway

# Fetch Positions
positions = ib.positions()

# Calculate MTM
total_mtm = 0
for pos in positions:
contract = pos.contract
avg_cost = pos.avgCost
quantity = pos.position

# Fetch Market Price
market_data = ib.reqMktData(contract, '', False, False)
ib.sleep(2) # Wait for market data to be retrieved
current_price = market_data.last if market_data.last else market_data.close

# Calculate MTM
mtm = (current_price - avg_cost) * quantity
total_mtm += mtm

print(f"Symbol: {contract.localSymbol}, MTM: {mtm:.2f}")

print(f"Total MTM: {total_mtm:.2f}")

# Disconnect
ib.disconnect()

---

### Key Points:
1. **Port & Client ID**:
- TWS default port:
7497 (for live) or 7496 (for paper trading).
- Use unique
clientId if running multiple connections.

2. **API Access**:
- Enable API settings in TWS under *File > Global Configuration > API > Settings*.
- Set trusted IPs and ensure the API is active.

3. **Handling Market Data**:
- The example uses
ib.reqMktData()` for the latest price. Adjust based on your subscription or latency requirements.

4. **Error Handling**:
- Add error handling for network issues, invalid symbols, or missing market data.

---

This code will calculate MTM for all open positions.
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 13
Newest Member: algoDev