This Python script transforms Interactive Brokers (IB) XML reports into the Slovenian DOH-KDVP XML format required for tax reporting.
- Converts IB trade and lot data to DOH-KDVP format
- Handles both closed lots and individual trades
- Automatically converts currencies to EUR using exchange rates
- Validates output against DOH-KDVP XSD schema
- Caches exchange rates to minimize API calls
- Processes multiple input files in batch
- Python 3.6 or higher
- Required packages (install using
pip install -r requirements.txt):- requests>=2.31.0
- lxml>=4.9.3
- Clone or download this repository
- Install required packages:
pip install -r requirements.txt
Edit the following variables in ib2dohkdvp.py:
# Personal information
ZAVEZANEC = {
'DavcnaStevilka': '12345678',
'Ime': 'Janez',
'Priimek': 'Novak',
'Naslov': 'Ulica 1',
'Posta': '1000',
'Obcina': 'LJUBLJANA',
}
# Directories
INPUT_DIR = 'files' # Directory containing IB XML files
OUTPUT_DIR = 'output' # Directory for transformed files- Place your IB XML files in the
filesdirectory - Run the script:
python ib2dohkdvp.py
- Check the
outputdirectory for transformed files
The script expects IB XML files containing:
- Trade information (buy/sell transactions)
- Lot information (for closed positions)
- Asset details (symbol, description, ISIN)
- Transaction details (dates, amounts, commissions)
The script generates DOH-KDVP XML files containing:
- Personal information (Zavezanec)
- Trade details (Napovedi)
- Year (Leto)
- Asset type (VrstaVrednostnegaPapirja)
- Country (Drzava)
- Name (Naziv)
- Purchase date and details (DatumNakupa, StroskiNakupa, VrednostNakupa)
- Sale date and details (DatumProdaje, StroskiProdaje, VrednostProdaje)
- The script automatically converts all amounts to EUR
- Exchange rates are fetched from multiple APIs (ECB, Frankfurter, VATComply)
- Rates are cached to minimize API calls
- Fallback rates are used if APIs are unavailable
The script provides clear success/failure messages for each file:
SUCCESS: filename.xml transformed and validated. Output: output/filename.xmlFAIL: filename.xml did not pass XSD validationFAIL: Error processing filename.xml: error message
- The script processes both closed lots and individual trades
- If no lots are found, it will process trades directly
- All amounts are converted to EUR using exchange rates from the transaction date
- The output is validated against the DOH-KDVP XSD schema