Building a Forex Portfolio Tracker with Historical Data API
K
Katy Spark
Jan 29, 2026
2 min read
1,413 views
A portfolio tracker helps you monitor your forex positions, calculate profits and losses, and analyze performance over time. In this tutorial, we'll build a simple tracker using the historical data API.
Core Features
- Track open positions with entry prices
- Calculate unrealized P&L in real-time
- Store trade history
- Display performance metrics
Data Structure
class Position:
def __init__(self, symbol, direction, entry_price, size, entry_time):
self.symbol = symbol
self.direction = direction # 'long' or 'short'
self.entry_price = entry_price
self.size = size
self.entry_time = entry_time
def calculate_pnl(self, current_price):
if self.direction == 'long':
return (current_price - self.entry_price) * self.size
else:
return (self.entry_price - current_price) * self.size
Fetching Current Prices
def update_portfolio_values(positions, api):
total_pnl = 0
for pos in positions:
quote = api.get_quote(pos.symbol)
current_price = quote['price']
pnl = pos.calculate_pnl(current_price)
total_pnl += pnl
print(f"{pos.symbol}: {pnl:.2f}")
return total_pnl
Performance Metrics
Track these key metrics:
- Total P&L: Sum of all position profits/losses
- Win Rate: Percentage of profitable trades
- Average Win/Loss: Mean profit vs mean loss
- Maximum Drawdown: Largest decline from peak
Storing Historical Data
Use a database (SQLite for simplicity, PostgreSQL for production) to store:
- Trade entries and exits
- Daily portfolio snapshots
- Performance history
This data enables long-term performance analysis and helps identify patterns in your trading.
Tags:
portfolio tracker
historical data
P&L
performance
application
K
Katy Spark
Content Writer at PulseMarkets
Expert in forex trading, market analysis, and financial API integration. Helping traders and developers make better decisions with data.