Fake Financial Transactions with Faker

Creating realistic financial transaction data for testing banking or financial applications

Python
from faker import Faker
import random
from datetime import datetime, timedelta

fake = Faker()

def generate_transaction():
    transaction_types = ["deposit", "withdrawal", "transfer", "payment"]
    transaction_type = random.choice(transaction_types)
    amount = round(random.uniform(10, 5000), 2)
    
    date = fake.date_time_between(start_date="-1y", end_date="now")
    
    return {
        "transaction_id": fake.uuid4(),
        "date": date.isoformat(),
        "type": transaction_type,
        "amount": amount,
        "currency": fake.currency_code(),
        "description": fake.bs(),
        "account_number": fake.bban(),
        "merchant": fake.company() if transaction_type in ["payment", "withdrawal"] else None,
        "category": fake.word(ext_word_list=["food", "transport", "entertainment", "utilities", "shopping"]) if transaction_type != "transfer" else None,
        "balance_after": round(random.uniform(100, 10000), 2)
    }

# Generate 10 transactions
transactions = [generate_transaction() for _ in range(10)]

# Sort transactions by date
transactions.sort(key=lambda x: x['date'])

# Print the generated transactions
for transaction in transactions:
    print(f"Date: {transaction['date']}")
    print(f"Type: {transaction['type'].capitalize()}")
    print(f"Amount: {transaction['amount']} {transaction['currency']}")
    print(f"Description: {transaction['description']}")
    if transaction['merchant']:
        print(f"Merchant: {transaction['merchant']}")
    if transaction['category']:
        print(f"Category: {transaction['category']}")
    print(f"Balance After: {transaction['balance_after']} {transaction['currency']}")
    print("---")
Click Run or press shift + ENTER to run code.