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

io.fair_acc.sample.financial.dos.PositionContainer Maven / Gradle / Ivy

Go to download

Small sample applications to showcase the features of the chart-fx library.

The newest version!
package io.fair_acc.sample.financial.dos;

import java.io.Serializable;
import java.util.*;

import io.fair_acc.sample.financial.dos.Position.PositionStatus;

public class PositionContainer implements Serializable {
    private static final long serialVersionUID = -6964168549256831250L;

    // positions by its ID
    private Map positionIdMap = new HashMap<>();

    // all positions per symbol
    private Map> positionMap = new HashMap<>();

    // open positions per symbol only - performance step
    private Map> openPositionMap = new HashMap<>();

    // all positions per strategy
    private Map> strategyPositionMap = new HashMap<>();

    // open positions per strategy only - performance step
    private Map> strategyOpenPositionMap = new HashMap<>();

    public void addPosition(Position position) {
        positionIdMap.put(position.getPositionId(), position);

        Set positionSet = positionMap.computeIfAbsent(
                position.getSymbol(), k -> new LinkedHashSet<>());
        addAndReplaceSet(positionSet, position);

        // strategy scope
        String strategy = position.getStrategy();
        if (strategy != null) {
            positionSet = strategyPositionMap.computeIfAbsent(
                    strategy, k -> new LinkedHashSet<>());
            addAndReplaceSet(positionSet, position);
        }

        // fast map for opened positions
        if (position.getPositionStatus() == PositionStatus.OPENED) {
            positionSet = openPositionMap.computeIfAbsent(
                    position.getSymbol(), k -> new LinkedHashSet<>());
            addAndReplaceSet(positionSet, position);

            // strategy scope
            if (strategy != null) {
                positionSet = strategyOpenPositionMap.computeIfAbsent(
                        strategy, k -> new LinkedHashSet<>());
                addAndReplaceSet(positionSet, position);
            }
        } else { // position closed - remove it from fast maps
            notifyPositionClosed(position);
        }
    }

    private void addAndReplaceSet(Set positionSet, Position position) {
        if (!positionSet.add(position)) {
            positionSet.remove(position);
            positionSet.add(position);
        }
    }

    public boolean removePosition(Position position) {
        positionIdMap.remove(position.getPositionId());
        String strategy = position.getStrategy();
        if (position.getPositionStatus() == PositionStatus.OPENED) {
            Set positionSet = openPositionMap.get(position.getSymbol());
            if (positionSet != null) {
                positionSet.remove(position);
            }
            if (strategy != null) {
                positionSet = strategyOpenPositionMap.get(strategy);
                if (positionSet != null) {
                    positionSet.remove(position);
                }
            }
        }
        if (strategy != null) {
            Set positionSet = positionMap.get(strategy);
            if (positionSet != null) {
                positionSet.remove(position);
            }
        }
        Set positionSet = positionMap.get(position.getSymbol());
        return positionSet != null && positionSet.remove(position);
    }

    public void notifyPositionClosed(Position position) {
        Set positionSet = openPositionMap.get(position.getSymbol());
        if (positionSet != null) {
            positionSet.remove(position);
        }
        if (position.getStrategy() != null) {
            positionSet = strategyOpenPositionMap.get(position.getStrategy());
            if (positionSet != null) {
                positionSet.remove(position);
            }
        }
    }

    public int size() {
        int size = 0;
        for (Set positions : positionMap.values()) {
            size += positions.size();
        }
        return size;
    }

    public int size(String symbol) {
        Set positions = positionMap.get(symbol);
        return positions == null ? 0 : positions.size();
    }

    public int sizeByStrategy(String strategy) {
        Set positions = strategyPositionMap.get(strategy);
        return positions == null ? 0 : positions.size();
    }

    public void clear() {
        positionIdMap = new HashMap<>();
        positionMap = new HashMap<>();
        openPositionMap = new HashMap<>();
        strategyPositionMap = new HashMap<>();
        strategyOpenPositionMap = new HashMap<>();
    }

    public Position getPositionById(int positionId) {
        return positionIdMap.get(positionId);
    }

    public Set getPositionByMarketSymbol(String symbol) {
        Set positions = null;
        if (symbol != null) {
            positions = positionMap.get(symbol);
        }
        if (positions == null) {
            positions = new LinkedHashSet<>();
        }
        return positions;
    }

    public Set getPositionByStrategy(String strategy) {
        Set positions = null;
        if (strategy != null) {
            positions = strategyPositionMap.get(strategy);
        }
        if (positions == null) {
            positions = new LinkedHashSet<>();
        }
        return positions;
    }

    public List getAllPositionList() {
        ArrayList positionList = new ArrayList<>();
        for (Set positions : positionMap.values()) {
            positionList.addAll(positions);
        }
        return positionList;
    }

    public List getAllPositionListTimeOrdered() {
        List positionList = getAllPositionList();
        positionList.sort(Comparator.comparing(Position::getExitTime));
        return positionList;
    }

    public List getPositionListByMarketSymbol(String symbol) {
        return new ArrayList<>(getPositionByMarketSymbol(symbol));
    }

    public List getPositionListByStrategy(String strategy) {
        return new ArrayList<>(getPositionByStrategy(strategy));
    }

    // optimized solution for opened positions
    public Set getFastOpenedPositionByMarketSymbol(String symbol) {
        Set openedPositionSet = openPositionMap.get(symbol);
        if (openedPositionSet == null) {
            return new LinkedHashSet<>();
        }
        return openedPositionSet;
    }

    // optimized solution for opened positions
    public Set getFastOpenedPositionByStrategy(String strategy) {
        Set openedPositionSet = strategyOpenPositionMap.get(strategy);
        if (openedPositionSet == null) {
            return new LinkedHashSet<>();
        }
        return openedPositionSet;
    }

    public List getOpenedPositionsByMarketSymbol(String symbol) {
        return getPositionStatusByMarketSymbol(symbol, PositionStatus.OPENED);
    }

    public List getClosedPositionsByMarketSymbol(String symbol) {
        return getPositionStatusByMarketSymbol(symbol, PositionStatus.CLOSED);
    }

    public List getOpenedPositionsByStrategy(String strategy) {
        return getPositionStatusByStrategy(strategy, PositionStatus.OPENED);
    }

    public List getClosedPositionsByStrategy(String strategy) {
        return getPositionStatusByStrategy(strategy, PositionStatus.CLOSED);
    }

    public List getClosedPositions() {
        ArrayList positions = new ArrayList<>();
        for (String symbol : getTradedMarketSymbols()) {
            positions.addAll(getPositionStatusByMarketSymbol(symbol, PositionStatus.CLOSED));
        }
        return positions;
    }

    private List getPositionStatusByMarketSymbol(String symbol, PositionStatus positionStatus) {
        List positions = new ArrayList<>();
        for (Position position : getPositionByMarketSymbol(symbol)) {
            if (positionStatus.equals(position.getPositionStatus())) {
                positions.add(position);
            }
        }
        return positions;
    }

    private List getPositionStatusByStrategy(String strategy, PositionStatus positionStatus) {
        List positions = new ArrayList<>();
        for (Position position : getPositionByStrategy(strategy)) {
            if (positionStatus.equals(position.getPositionStatus())) {
                positions.add(position);
            }
        }
        return positions;
    }

    public Set getTradedMarketSymbols() {
        return positionMap.keySet();
    }

    public Set getTradedStrategies() {
        return strategyPositionMap.keySet();
    }

    public boolean contains(Position position) {
        Set positionSet = getPositionByMarketSymbol(position.getSymbol());
        return positionSet.contains(position);
    }

    public void compare(PositionContainer positionContainer) throws IllegalStateException {
        for (String market : positionMap.keySet()) {
            Set positions = getPositionByMarketSymbol(market);
            Set otherPositions = positionContainer.getPositionByMarketSymbol(market);
            if (positions.size() != otherPositions.size()) {
                throw new IllegalStateException("The size of position containers is different " + positions.size() + "; " + otherPositions.size());
            }
            for (Position otherPosition : otherPositions) {
                if (positions.contains(otherPosition)) {
                    boolean found = false;
                    for (Position position : positions) {
                        if (position.equals(otherPosition)) {
                            if (!position.comparePosition(otherPosition)) {
                                throw new IllegalStateException("The positions are different " + otherPosition + ", " + position);
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        throw new IllegalStateException("The position doesn't exist " + otherPosition);
                    }

                } else {
                    throw new IllegalStateException("The position doesn't exist " + otherPosition);
                }
            }
        }
    }

    @Override
    public String toString() {
        return positionMap.values().toString();
    }

    public String toLineString() {
        StringBuilder sb = new StringBuilder();
        sb.append("PositionContainer {").append(System.lineSeparator());
        for (Position position : getAllPositionListTimeOrdered()) {
            sb.append(position).append(System.lineSeparator());
        }
        sb.append("}").append(System.lineSeparator());
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy