package top.baogutang.strategy.models;


public class RiskManager {
    private final double maxLeverage;

    public RiskManager(double maxLeverage) {
        this.maxLeverage = maxLeverage;
    }

    public PositionPlan plan(double equity, double riskPercent, double entry, double stop, double minQtyStep) {
        double riskAmt = equity * riskPercent;
        double dist = Math.abs(entry - stop);
        if (dist <= 0) throw new IllegalArgumentException("Stop=Entry");
        double qty = riskAmt / dist;
        if (minQtyStep > 0) {
            qty = Math.floor(qty / minQtyStep) * minQtyStep;
        }
        double notional = qty * entry;
        double lev = notional / equity;
        if (lev > maxLeverage) {
            double scale = maxLeverage / lev;
            qty *= scale;
            notional = qty * entry;
            lev = notional / equity;
        }
        return new PositionPlan(qty, notional, lev, riskAmt);
    }
}
