com.fluxtion.server.lib.pnl.calculator.MtmCalc Maven / Gradle / Ivy
The newest version!
/*
* SPDX-FileCopyrightText: © 2024 Gregory Higgins
* SPDX-License-Identifier: AGPL-3.0-only
*/
package com.fluxtion.server.lib.pnl.calculator;
import com.fluxtion.runtime.dataflow.groupby.GroupBy;
import com.fluxtion.server.lib.pnl.FeeInstrumentPosMtm;
import com.fluxtion.server.lib.pnl.InstrumentPosMtm;
import com.fluxtion.server.lib.pnl.NetMarkToMarket;
import com.fluxtion.server.lib.pnl.refdata.Instrument;
import java.util.Map;
public class MtmCalc {
public static GroupBy forTradeInstrumentPosMtm(
GroupBy instrumentMtm,
GroupBy rates) {
Map ratesMap = rates.toMap();
try {
ratesMap.put(Instrument.INSTRUMENT_USD, 1.0);
instrumentMtm.values().forEach(instrumentPosMtm -> {
Map mtmPositionsMap = instrumentPosMtm.resetMtm().getMtmPositionsMap();
instrumentPosMtm.getPositionMap()
.forEach((key, value) -> {
mtmPositionsMap.put(key, value * ratesMap.getOrDefault(key, Double.NaN));
});
instrumentPosMtm.calcTradePnl();
});
} catch (Exception e) {
}
return instrumentMtm;
}
public static GroupBy forFeeInstrumentPosMtm(
GroupBy feeInstrumentMtm,
GroupBy rates) {
Map ratesMap = rates.toMap();
try {
ratesMap.put(Instrument.INSTRUMENT_USD, 1.0);
feeInstrumentMtm.values().forEach(instrumentPosMtm -> {
Map mtmPositionsMap = instrumentPosMtm.resetMtm().getFeesMtmPositionMap();
instrumentPosMtm.getFeesPositionMap()
.forEach((key, value) -> {
mtmPositionsMap.put(key, value * ratesMap.getOrDefault(key, Double.NaN));
});
instrumentPosMtm.calcTradePnl();
});
} catch (Exception e) {
}
return feeInstrumentMtm;
}
public static NetMarkToMarket markToMarketSum(Map instrumentNetMarkToMarketMap) {
InstrumentPosMtm instrumentPosMtm = new InstrumentPosMtm();
FeeInstrumentPosMtm feesMtm = new FeeInstrumentPosMtm();
instrumentNetMarkToMarketMap.values().forEach(m -> {
instrumentPosMtm.combine(m.instrumentMtm());
feesMtm.combine(m.feesMtm());
});
instrumentPosMtm.setBookName("global");
NetMarkToMarket sumMtm = new NetMarkToMarket(instrumentPosMtm, feesMtm);
return sumMtm;
}
}