package top.baogutang.strategy.models;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PortfolioRiskManager {
    private final double equity;
    private final double maxOpenRiskPercent;
    private final Map<String, Double> openRisk = new ConcurrentHashMap<>();

    public PortfolioRiskManager(double equity, double maxOpenRiskPercent) {
        this.equity = equity;
        this.maxOpenRiskPercent = maxOpenRiskPercent;
    }

    public synchronized boolean canAccept(double riskAmt) {
        double curr = currentOpenRisk();
        return (curr + riskAmt) <= equity * maxOpenRiskPercent;
    }

    public double currentOpenRisk() {
        double sum = 0;
        for (Double d : openRisk.values()) sum += d;
        return sum;
    }

    public void register(String signalId, double amt) {
        openRisk.put(signalId, amt);
    }

    public void close(String signalId) {
        openRisk.remove(signalId);
    }
}
