Skip to content

Commit

Permalink
simplify pnl example
Browse files Browse the repository at this point in the history
  • Loading branch information
lightbridge101 committed Nov 10, 2024
1 parent 45b3146 commit 292284e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public class PnlExampleMain {
public static void main(String[] args) {
var pnlCalculator = Fluxtion.interpret(c -> {
var tradeStream = DataFlow.subscribe(Trade.class);
var dealtPosition = tradeStream.groupBy(Trade::getDealtInstrument, TradeToPosition::aggregateDealtPosition);
var contraPosition = tradeStream.groupBy(Trade::getContraInstrument, TradeToPosition::aggregateContraPosition);
var dealtPosition = tradeStream.groupBy(Trade::dealtInstrument, TradeToPosition::aggregateDealt);
var contraPosition = tradeStream.groupBy(Trade::contraInstrument, TradeToPosition::aggregateContra);

DerivedRateNode derivedRate = new DerivedRateNode();
JoinFlowBuilder.outerJoin(dealtPosition, contraPosition, InstrumentPosMtm::merge)
.publishTrigger(DataFlow.subscribe(MidPrice.class))
.mapValues(new DerivedRateNode()::calculateInstrumentPosMtm)
.map(PnlExampleMain::calculateTotalPnl)
.sink("globalNetMtmListener");
.publishTrigger(derivedRate)
.mapValues(derivedRate::calculateInstrumentPosMtm)
.map(PnlExampleMain::calculateTotalPnl);
}
);

Expand All @@ -55,6 +55,7 @@ public static void main(String[] args) {
}

private static PnlSummary calculateTotalPnl(GroupBy<Instrument, InstrumentPosMtm> instrumentInstrumentPosMtmGroupBy) {
System.out.println("---------- summary -----------");
System.out.println(instrumentInstrumentPosMtmGroupBy.toMap());
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ public boolean midRate(MidPrice midPrice) {
graph.setEdgeWeight(graph.addEdge(dealtInstrument, contraInstrument), logRate);
graph.setEdgeWeight(graph.addEdge(contraInstrument, dealtInstrument), logInverseRate);
}
return false;
return true;
}

public InstrumentPosMtm calculateInstrumentPosMtm(InstrumentPosMtm instrumentPosMtm) {
Map<Instrument, Double> mtmPositionsMap = instrumentPosMtm.resetMtm().getMtmPositionsMap();
instrumentPosMtm.getPositionMap()
.forEach((key, value) -> {
mtmPositionsMap.put(key, value * getRateForInstrument(key));
});
instrumentPosMtm.calcTradePnl();
Instrument positionInstrument = instrumentPosMtm.resetMtm().getInstrument();
double position = instrumentPosMtm.getPosition();
instrumentPosMtm.setMtmPosition(position * getRateForInstrument(positionInstrument));
return instrumentPosMtm;
}

Expand All @@ -97,7 +94,6 @@ public Double getRateForInstrument(Instrument instrument) {
instrument,
positionInstrument -> {
if (graph.containsVertex(positionInstrument) & graph.containsVertex(mtmInstrument)) {
GraphPath<Instrument, DefaultWeightedEdge> path = shortestPath.getPath(positionInstrument, mtmInstrument);
double log10Rate = shortestPath.getPathWeight(positionInstrument, mtmInstrument);
return Double.isInfinite(log10Rate) ? Double.NaN : Math.pow(10, log10Rate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
import com.fluxtion.example.cookbook.pnl.refdata.Instrument;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class InstrumentPosMtm {
private String bookName;
private double tradePnl;
private Map<Instrument, Double> positionMap = new HashMap<>();
private Map<Instrument, Double> mtmPositionsMap = new HashMap<>();
private Instrument instrument;
private double position = 0;
private double mtmPosition = 0;

public static InstrumentPosMtm merge(InstrumentPosMtm mtm1, InstrumentPosMtm mtm2) {
return new InstrumentPosMtm(mtm1).combine(mtm2);
Expand All @@ -26,37 +22,23 @@ public InstrumentPosMtm() {}

public InstrumentPosMtm(InstrumentPosMtm from) {
if (from != null) {
this.bookName = from.bookName;
this.tradePnl = from.tradePnl;
this.positionMap.putAll(from.positionMap);
this.mtmPositionsMap.putAll(from.mtmPositionsMap);
this.instrument = from.instrument;
this.position = from.position;
this.mtmPosition = from.mtmPosition;
}
}

public InstrumentPosMtm combine(InstrumentPosMtm from) {
if (from != null) {
this.tradePnl += from.tradePnl;

from.positionMap.forEach((key, value) -> {
positionMap.merge(key, value, Double::sum);
});

from.mtmPositionsMap.forEach((key, value) -> {
mtmPositionsMap.merge(key, value, Double::sum);
});

this.bookName = bookName == null ? from.bookName : bookName;
this.position += from.position;
this.mtmPosition += from.mtmPosition;
this.instrument = instrument == null ? from.instrument : instrument;
}
return this;
}

public InstrumentPosMtm resetMtm() {
getMtmPositionsMap().clear();
tradePnl = Double.NaN;
mtmPosition = 0;
return this;
}

public double calcTradePnl() {
return tradePnl = mtmPositionsMap.values().stream().mapToDouble(Double::doubleValue).sum();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ public TradeToPosition(boolean dealtSide) {
this.dealtSide = dealtSide;
}

public static TradeToPosition aggregateDealtPosition() {
public static TradeToPosition aggregateDealt() {
return new TradeToPosition(true);
}

public static TradeToPosition aggregateContraPosition() {
public static TradeToPosition aggregateContra() {
return new TradeToPosition(false);
}

@Override
public InstrumentPosMtm aggregate(Trade input) {
final double previousPosition = instrumentPosMtm.getPosition();
if (dealtSide) {
instrumentPosMtm.setBookName(input.getDealtInstrument().instrumentName());
instrumentPosMtm.setInstrument(input.dealtInstrument());
final double dealtPosition = input.dealtVolume();
instrumentPosMtm.getPositionMap().compute(input.getDealtInstrument(), (s, d) -> d == null ? dealtPosition : d + dealtPosition);
instrumentPosMtm.setPosition( Double.isNaN(previousPosition) ? dealtPosition : dealtPosition + previousPosition);
} else {
instrumentPosMtm.setBookName(input.getContraInstrument().instrumentName());
instrumentPosMtm.setInstrument(input.contraInstrument());
final double contraPosition = input.contraVolume();
instrumentPosMtm.getPositionMap().compute(input.getContraInstrument(), (s, d) -> d == null ? contraPosition : d + contraPosition);
instrumentPosMtm.setPosition( Double.isNaN(previousPosition) ? contraPosition : contraPosition + previousPosition);
}
return instrumentPosMtm;
}
Expand All @@ -48,5 +49,4 @@ public InstrumentPosMtm reset() {
instrumentPosMtm = new InstrumentPosMtm();
return instrumentPosMtm;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

public record Trade(Symbol symbol, double dealtVolume, double contraVolume) {

public Instrument getDealtInstrument() {
public Instrument dealtInstrument() {
return symbol.dealtInstrument();
}

public Instrument getContraInstrument() {
public Instrument contraInstrument() {
return symbol.contraInstrument();
}
}

0 comments on commit 292284e

Please sign in to comment.