All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fluxtion.server.lib.pnl.PnlCalculator Maven / Gradle / Ivy

There is a newer version: 0.1.29
Show newest version
/*
 * SPDX-FileCopyrightText: © 2024 Gregory Higgins 
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package com.fluxtion.server.lib.pnl;


import com.fluxtion.server.lib.pnl.calculator.FluxtionPnlCalculator;
import com.fluxtion.server.lib.pnl.refdata.InMemorySymbolLookup;
import com.fluxtion.server.lib.pnl.refdata.Symbol;
import lombok.Getter;
import lombok.SneakyThrows;

import java.util.Collection;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

public class PnlCalculator {

    private final FluxtionPnlCalculator fluxtionPnlCalculator;
    @Getter
    private final InMemorySymbolLookup symbolLookup = new InMemorySymbolLookup();

    public PnlCalculator() {
        fluxtionPnlCalculator = new FluxtionPnlCalculator();
        fluxtionPnlCalculator.init();
        fluxtionPnlCalculator.onEvent(symbolLookup);
        fluxtionPnlCalculator.start();
        positionReset();
    }

    public PnlCalculator addSymbol(Symbol symbol) {
        symbolLookup.addSymbol(symbol);
        return this;
    }

    public PnlCalculator positionSnapshot(PositionSnapshot positionSnapshot) {
        fluxtionPnlCalculator.onEvent(positionSnapshot);
        fluxtionPnlCalculator.publishSignal("positionUpdate");
        return this;
    }

    public PnlCalculator positionReset(PositionSnapshot positionSnapshot) {
        fluxtionPnlCalculator.publishSignal("positionSnapshotReset");
        fluxtionPnlCalculator.onEvent(positionSnapshot);
        fluxtionPnlCalculator.publishSignal("positionUpdate");
        return this;
    }

    public PnlCalculator positionReset() {
        fluxtionPnlCalculator.publishSignal("positionSnapshotReset");
        fluxtionPnlCalculator.publishSignal("positionUpdate");
        return this;
    }

    public PnlCalculator priceUpdate(MidPrice midPrice) {
        fluxtionPnlCalculator.onEvent(midPrice);
        fluxtionPnlCalculator.publishSignal("positionUpdate");
        return this;
    }

    public PnlCalculator priceUpdate(String symbol, double price) {
        return priceUpdate(new MidPrice(symbolLookup.getSymbolForName(symbol), price));
    }

    public PnlCalculator processTrade(Trade... trades) {
        for (Trade trade : trades) {
            fluxtionPnlCalculator.onEvent(trade);
        }
        fluxtionPnlCalculator.publishSignal("positionUpdate");
        return this;
    }

    public PnlCalculator processTradeBatch(Collection trades) {
        for (Trade trade : trades) {
            fluxtionPnlCalculator.onEvent(trade);
        }
        fluxtionPnlCalculator.publishSignal("positionUpdate");
        return this;
    }

    public PnlCalculator processTradeBatch(TradeBatch trades) {
        fluxtionPnlCalculator.onEvent(trades);
        fluxtionPnlCalculator.publishSignal("positionUpdate");
        return this;
    }

    public  PnlCalculator processCustomTrade(T trade, Function tradeToBatchFunction) {
        return processTradeBatch(tradeToBatchFunction.apply(trade));
    }

    public PnlCalculator addPnlListener(Consumer pnlConsumer) {
        fluxtionPnlCalculator.addSink("pnlListener", pnlConsumer);
        return this;
    }

    public PnlCalculator addNetPnlListener(Consumer pnlConsumer) {
        fluxtionPnlCalculator.addSink("netPnlListener", pnlConsumer);
        return this;
    }

    public PnlCalculator addTradeFeesListener(Consumer pnlConsumer) {
        fluxtionPnlCalculator.addSink("tradeFeesListener", pnlConsumer);
        return this;
    }

    public PnlCalculator addPositionListener(Consumer> positionListener) {
        fluxtionPnlCalculator.addSink("positionListener", positionListener);
        return this;
    }

    public PnlCalculator addMtmPositionListener(Consumer> positionListener) {
        fluxtionPnlCalculator.addSink("mtmPositionListener", positionListener);
        return this;
    }

    public PnlCalculator addRateListener(Consumer> rateListener) {
        fluxtionPnlCalculator.addSink("rateListener", rateListener);
        return this;
    }

    @SneakyThrows
    public Map positionMap() {
        return fluxtionPnlCalculator.getStreamed("positionMap");
    }

    @SneakyThrows
    public Map mtmPositionMap() {
        return fluxtionPnlCalculator.getStreamed("mtmPositionMap");
    }

    @SneakyThrows
    public Map ratesMap() {
        return fluxtionPnlCalculator.getStreamed("rates");
    }

    @SneakyThrows
    public Double tradeFees() {
        return fluxtionPnlCalculator.getStreamed("tradeFees");
    }

    @SneakyThrows
    public Double pnl() {
        return fluxtionPnlCalculator.getStreamed("pnl");
    }

    @SneakyThrows
    public Double netPnl() {
        return fluxtionPnlCalculator.getStreamed("netPnl");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy