This Proof of Concept is intended to build an application for P&L calculations that allow us get familiarized with the relevant concepts, formulas and ultimately to iterate a design flexible enough to enable:
- Multiple Valuation configurations and services
- Different Asset type P&L calculations
- Data input requirement discovery to allow service evolution
Following Fuse conventions for component structure
- Domain: entity object with no business logic.
- Repository: classes interacting with the persistance layer.
- Services: classes performing business logic.
- Entry points: components interacting with the outside world.
In a high level and abstract view, this application perform the P&L calculation of a set of instruments corresponding to accounts (books) in a given interval of time defined by a reference date and a target that (usually EOD).
As this diagram P&L workflow shows, the PnLService iterates through each account instrument and calculate the Unrealized and Realized P&Ls.
The Unrealized P&L need to get the valuation of the Instrument and for this we can apply different models and configuration. To enable model evolution, we have implemented and communication strategy between the PnLService and ValuationService that allows both parties to validate the input data provided and augmented accordingly. This exchange of information is detailed in the following diagram: Unrealized PnL workflow
Finally, the PnL service calculates the Realized P&L retrieving the trades involving the instrument and adding different values such as Commissions, Fees and Cash Proceeds. The following diagram details this process: Realized PnL workflow
Value | Formula |
---|---|
MTM P&L | CurrentValuation.Valuation - ReferenceValuation.Valuation |
MTM P&L (Book Currency) | mtmPnL * CurrentValuation.FxRate |
FX P&L | CurrentValuation.Valuation * (ReferenceValuation.FxRate - CurrentValuation.FxRate) |
LTD MTM P&L | ReferencePnl.ltdMtMPnL + CurrentPnl.mtmPnL |
LTD MTM P&L (Book Currency) | ReferencePnl.ltdMtMPnLBook + CurrentPnl.mtmPnLBook |
LTD FX P&L | ReferencePnl.ltdFxPnl + CurrentPnl.fxPnl |
Unrealized P&L (Instrument Currency) | CurrentValuation.Valuation - ReferenceValuation.Valuation |
Unrealized P&L (Book Currency) | CurrentValuation.ValuationBook - ReferenceValuation.ValuationBook |
Realized P&L | Fees + Commissions + Proceeds |
See pom.xml for entire list of dependencies.
Source: https://github.com/GalateaRaj/fuse-starter-java#eclipse
Set code style settings, which will allow auto-formatting of code to match the Google style guide
- Navigate to Window -> Preferences -> Java -> Code Style -> Formatter
- Click Import and select <project_directory>/style/eclipse-java-google-style.xml
- Navigate to Window -> Preferences -> Java -> Code Style -> Organize Imports
- Click Import and select <project_directory>/style/eclipse-java-google-style.importorder
- Navigate to Window -> Preferences -> Java -> Editor -> Save Actions
- Select the "Perform the selected actions on save", "Format source code", "Format edited lines", and "Organize imports" options
Install lombok: https://projectlombok.org/setup/eclipse. Note if you're doing this step last because you raced ahead and nothing compiles you'll have to do some cleans and re-compiles to get lombok involved in generating all the class files.
Eclipse: r-click -> run as 'Java Application' on src/main/java/org/galatea/pocpnl/App.java
After startup, UI should be available http://localhost:8080/pnl
Initial Data can be imported by the @Service DataImportService which reads text files containing a single json object per line. The DataImportService reads each file, parses each json into an object and uses the corresponfing Repository to persist the object.
src/main/resources/data/books.txt
{"bookId": "EQTY1", "currency": "USD"}
{"bookId": "EQTY2", "currency": "USD"}
public void importData() {
importData(bookRepository, getLines("books.txt"), Book.class);
}
This application is tested using a series of scenarios defined in json files. Each test will read a single json file and parse it into a TestScenario object.
A TestScenario object should have all the information required to perform the test:
- Inputs
- Instruments
- Static Data
- FxRates
- Trades
- Existing Valuations
- Expected Output
- Expected P&L results
Eclipse: r-click 'Run As -> JUnit Test' on src/test/java/org/galatea/pocpnl/PocPnlApplicationTests
To add a new tests:
- Define a new json file witht the test scenario following the TestScenario.java structure.
- Create a new test in PocPnlApplicationTests following the existing test conventions.
See open issues
- Swaps (cashflows)
- Dividends/Coupons
Section with relevant links to information
- Cost Basis https://www.investopedia.com/terms/c/costbasis.asp
- Constant Yield Method https://www.investopedia.com/terms/a/accretion-of-discount.asp
- Amortization/Accretion:
- Clean vs. dirty price https://www.investopedia.com/terms/c/cleanprice.asp
- Yield