Real-time transaction fraud scoring with explainable risk assessment. Built for fintech applications requiring stateful fraud detection with velocity checks, geolocation analysis, and behavioral profiling.
This fraud detection engine analyzes financial transactions in real-time using rule-based pattern matching and behavioral analytics. Unlike simple threshold-based systems, it maintains user profiles, tracks device patterns, calculates geographic impossibilities using the Haversine formula, and provides transparent explainability through reason codes.
β
Real-time scoring - 0-100 fraud score with 4-tier risk levels
β
Stateful detection - SQLite-backed transaction history and user profiling
β
8 fraud rules - Velocity, amount anomalies, location patterns, device fingerprinting
β
Geospatial analysis - Impossible travel detection using Haversine distance
β
Explainability - Reason codes and severity breakdown for every assessment
β
REST API - Flask endpoints with webhook support for alerts
β
Synthetic testing - Built-in fraud scenario generator
β
Production-ready - Comprehensive test suite, JSON logging, input validation
# Python 3.8+
pip3 install -r requirements.txt# See fraud detection in action
python3 fraud_detector.pyOutput shows 4 scenarios:
- β Legitimate transaction (score: 0)
β οΈ Velocity attack (score: 40)β οΈ Large first transaction (score: 50)- πΆ Impossible travel (score: 60)
python3 test_fraud_detector.pyRuns 18 comprehensive tests covering rules, scenarios, and edge cases.
python3 api.pyServer runs on http://localhost:5000
python3 generate_test_data.pyCreates test_scenarios.json with 6 fraud scenarios.
Transaction Input
β
[Transaction Store] β SQLite in-memory database
β
[Rules Engine] β 8 modular fraud rules
β
[Fraud Detector] β Aggregates scores + generates assessment
β
Fraud Assessment Output (score, risk level, reasons, recommendations)
| Rule | Description | Severity | Score Impact |
|---|---|---|---|
| Velocity (10min) | 3+ transactions in 10 minutes | High | +40 pts |
| Velocity (60min) | 10+ transactions in 1 hour | Medium | +25 pts |
| Large Transaction | >3x user's average amount | Medium | +25 pts |
| New Device | Transaction from unknown device | Medium | +20 pts |
| Device Velocity | Device used by 5+ accounts in 1hr | High | +35 pts |
| Impossible Travel | Requires >900 km/h travel speed | Critical | +60 pts |
| Round Dollar | Exact $500, $1000, etc (card testing) | Low | +10 pts |
| High-Risk Category | Gift cards, wire transfers, crypto | Medium | +15 pts |
- LOW (0-25): Approve - Process normally
- MEDIUM (26-50): Challenge - Require 2FA
- HIGH (51-75): Review - Manual review required
- CRITICAL (76-100): Block - Automatically decline
Transaction Store extracts:
- User profile (lifetime spend, avg amount, known devices/locations)
- Transaction velocity (count in time windows)
- Device fingerprints
- Location history
Rules Engine calculates:
- Coefficient of variation (income stability)
- Haversine distance (geographic movement)
- Time-series patterns (rapid-fire detection)
- Behavioral anomalies (deviation from norms)
curl -X POST http://localhost:5000/api/assess \
-H "Content-Type: application/json" \
-d '{
"transaction_id": "txn_001",
"user_id": "user_alice",
"amount": 1500.00,
"merchant": "Apple Store",
"category": "Electronics",
"timestamp": "2026-01-23T10:30:00Z",
"location": {"lat": 37.7749, "lon": -122.4194},
"device_id": "device_new_abc123",
"ip_address": "192.168.1.100"
}'Response:
{
"transaction_id": "txn_001",
"fraud_score": 50,
"risk_level": "medium",
"triggered_rules": [
"First large transaction",
"Round dollar amount",
"High-risk category"
],
"reason_codes": [
"First large transaction: $1500.00 transaction on new account",
"Round dollar amount: Exact $1500.00 (common in card testing)",
"High-risk category: Category 'Electronics' is high-risk"
],
"recommended_action": "CHALLENGE: Require additional authentication (2FA, security questions).",
"details": {
"rules_evaluated": 8,
"rules_triggered": 3,
"severity_breakdown": {"low": 1, "medium": 2}
}
}curl -X POST http://localhost:5000/api/assess/batch \
-H "Content-Type: application/json" \
-d '{
"transactions": [
{transaction_1},
{transaction_2},
...
]
}'Response includes summary:
{
"results": [...],
"summary": {
"total": 100,
"low_risk": 85,
"medium_risk": 10,
"high_risk": 4,
"critical_risk": 1
}
}curl -X POST http://localhost:5000/api/webhooks \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/fraud-alert",
"events": ["high", "critical"]
}'Webhooks are triggered automatically for high-risk transactions.
curl "http://localhost:5000/api/user/history?user_id=user_alice&time_window_minutes=60"Returns transaction history and user profile.
Input:
{
"amount": 45.50,
"merchant": "Starbucks",
"category": "Food & Dining"
}Output:
- Score: 0/100 (LOW)
- Triggered Rules: None
- Action: APPROVE
Pattern: 5 transactions in 3 minutes
Output:
- Score: 40/100 (MEDIUM)
- Triggered: "Velocity: 3+ txns in 10min"
- Action: CHALLENGE - Require 2FA
Pattern: SF β NYC in 30 minutes (requires 8,258 km/h)
Output:
- Score: 60/100 (HIGH)
- Triggered: "Impossible travel: 4129km in 0.5h"
- Action: REVIEW - Hold for manual verification
Pattern: 10 small transactions ($1, $5, $10) in rapid succession
Output:
- Score: 40-50/100 (MEDIUM)
- Triggered: Velocity + Round amounts
- Action: CHALLENGE
Pattern: 7 different accounts using same device in 1 hour
Output:
- Score: 35+/100 (MEDIUM-HIGH)
- Triggered: "Device velocity: 5+ accounts in 60min"
- Action: REVIEW
18 comprehensive tests across 5 categories:
1. Transaction Store Tests (4 tests)
- Adding transactions
- Duplicate rejection
- User profile creation
- Time window queries
2. Fraud Rules Tests (5 tests)
- Velocity detection
- Large transaction detection
- New device detection
- Impossible travel calculation
- Round dollar detection
3. Fraud Detector Tests (3 tests)
- Legitimate transactions
- High fraud scores
- Risk level boundaries
4. Synthetic Scenarios Tests (3 tests)
- Velocity attack detection
- Impossible travel detection
- Card testing patterns
5. Input Validation Tests (3 tests)
- Invalid coordinates
- Negative amounts
- Missing required fields
python3 test_fraud_detector.pyExpected output:
Ran 18 tests in 0.013s
OK
β
All tests passed!
This project demonstrates technologies and patterns different from previous projects:
- SQLite with indexes - Stateful in-memory database (vs PostgreSQL in Budget Buddy/Stress Simulator)
- Geospatial calculations - Haversine formula for impossible travel detection
- Dataclasses with validation - Python type-safe models (different approach than Pydantic)
- JSON structured logging - Production event logging for fraud detection
- Time-series velocity detection - Real-time pattern matching algorithms
- Device fingerprinting - Security-focused identity tracking
- Webhook notification system - Event-driven alerting architecture
- Synthetic fraud scenarios - Automated test data generation for fraud patterns
Domain expertise: Real-time fraud detection and transaction security (vs consumer budgeting/planning tools)
fraud-detection-engine/
βββ fraud_detector.py # Main detection engine
βββ transaction_store.py # SQLite storage + user profiling
βββ rules_engine.py # Modular fraud rules
βββ api.py # Flask REST API
βββ test_fraud_detector.py # Comprehensive test suite
βββ generate_test_data.py # Synthetic scenario generator
βββ test_scenarios.json # Pre-generated test data
βββ requirements.txt # Dependencies
βββ .gitignore # Git ignore file
βββ README.md # This file
- Scoring Speed: <2ms per transaction
- API Latency: ~20ms (including network)
- Batch Processing: 500+ transactions/second
- Memory Footprint: ~30MB (in-memory DB)
- Database: SQLite (in-memory for speed, persistent option available)
Current (Demo):
- In-memory SQLite
- Synchronous processing
- Single-threaded
Production Recommendations:
- PostgreSQL/MySQL for persistence
- Redis for caching + rate limiting
- Async task queue (Celery/RabbitMQ)
- Horizontal scaling with load balancer
- Webhook retries with exponential backoff
- Rate limit API endpoints (100 req/min per IP)
- Encrypt PII fields (IP addresses, device IDs)
- Audit logs for all assessments
- HTTPS only in production
- API authentication (OAuth2/JWT)
- Track fraud score distribution
- Monitor false positive/negative rates
- Alert on rule effectiveness degradation
- Dashboard for real-time fraud activity
- A/B test rule threshold adjustments
- PCI DSS: Never store card numbers
- GDPR: Right to deletion, data minimization
- Fair Lending: Avoid discriminatory patterns
- Audit Trail: Log all decisions for review
- Machine learning model (compare to rule-based)
- Graph analysis for fraud rings
- IP geolocation enrichment
- Merchant category code (MCC) risk scoring
- Time-of-day risk patterns
- Amount clustering for anomaly detection
- Network analysis (user connections)
- False positive feedback loop
- Dashboard UI for analysts
- Prometheus metrics export
-
Payment Processors (Stripe, Square)
- Real-time transaction screening
- Chargeback prevention
- Merchant risk scoring
-
Neobanks (Chime, Current)
- Account takeover detection
- P2P fraud prevention
- New account monitoring
-
Buy Now Pay Later (Affirm, Klarna)
- First-party fraud detection
- Synthetic identity detection
- Checkout abuse prevention
-
Crypto Exchanges (Coinbase, Kraken)
- Withdrawal fraud prevention
- Account verification
- AML transaction monitoring
-
Marketplaces (eBay, Etsy)
- Seller fraud detection
- Buyer protection
- Dispute resolution
Both projects show fintech risk assessment, but focus on different domains:
| Feature | Creditworthiness Scorer | Fraud Detector |
|---|---|---|
| Purpose | Lending decisioning | Transaction security |
| Timing | One-time (application) | Real-time (every txn) |
| Data | Historical cash flow | Current + historical txns |
| Storage | Stateless | Stateful (SQLite) |
| Features | DTI, income CV, buffer | Velocity, location, device |
| Output | Loan approval/denial | Approve/challenge/block |
| Domain | Underwriting | Fraud prevention |
MIT License - Free for commercial and personal use.
Built by Pelz as part of a fintech portfolio demonstrating:
- Real-time risk assessment
- Stateful pattern detection
- Geospatial analytics
- Production-quality code
- Comprehensive testing
Note: This is a demonstration project for educational/portfolio purposes. For production fraud detection, consider:
- Professional fraud services (Sift, Riskified, Stripe Radar)
- Machine learning models trained on your data
- Legal review for compliance
- Insurance for fraud losses