Java tutorial
/* * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.wso2.extension.siddhi.execution.var.backtest; import org.apache.commons.math3.distribution.BinomialDistribution; import org.json.JSONObject; import org.wso2.extension.siddhi.execution.var.models.VaRCalculator; import org.wso2.extension.siddhi.execution.var.models.parametric.ParametricVaRCalculator; import org.wso2.extension.siddhi.execution.var.models.util.Event; import org.wso2.extension.siddhi.execution.var.models.util.asset.Asset; import org.wso2.extension.siddhi.execution.var.models.util.portfolio.Portfolio; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; public class BacktestRealTime { private static final int BATCH_SIZE = 251; private static final double VAR_CI = 0.95; private static final double BACKTEST_CI = 0.05; private static final int NUMBER_OF_ASSETS = 1; private static final int SAMPLE_SIZE = 1; private static final int VAR_PER_SAMPLE = 130; private static final String PORTFOLIO_KEY = "Portfolio 1"; private ArrayList<Double> calculatedVarList; private ArrayList<Double> actualVarList; private Double previousPortfolioValue; public BacktestRealTime() { calculatedVarList = new ArrayList(); actualVarList = new ArrayList(); previousPortfolioValue = null; } public static void main(String[] args) throws FileNotFoundException { new BacktestRealTime().runBackTest(); } private void runBackTest() throws FileNotFoundException { //VaRCalculator varCalculator = new HistoricalVaRCalculator(BATCH_SIZE, VAR_CI); VaRCalculator varCalculator = new ParametricVaRCalculator(BATCH_SIZE, VAR_CI); //VaRCalculator varCalculator = new MonteCarloVarCalculator(BATCH_SIZE, VAR_CI, 2500,100,0.01); ArrayList<Event> list = readBacktestData(); int i = 0; int totalEvents = (BATCH_SIZE + 1) * NUMBER_OF_ASSETS + VAR_PER_SAMPLE * NUMBER_OF_ASSETS * SAMPLE_SIZE + 1; System.out.println("Read Total Events : " + totalEvents); while (i < totalEvents) { // fill lists if (i < (BATCH_SIZE + 1) * NUMBER_OF_ASSETS) { varCalculator.calculateValueAtRisk(list.get(i)); i++; } else { System.out.print("Event " + (i) + " : "); String jsonString = (String) varCalculator.calculateValueAtRisk(list.get(i)); JSONObject jsonObject = new JSONObject(jsonString); Double calculatedVar = (Double) jsonObject.get(PORTFOLIO_KEY); // hardcoded for portfolio ID 1 System.out.print(String.format("CV : %-15f", calculatedVar)); calculatedVarList.add(calculatedVar); // should filter calculateActualLoss(varCalculator.getPortfolioPool().get("1"), varCalculator.getAssetPool()); System.out.println(); i++; } } runStandardCoverageTest(); } private void runStandardCoverageTest() { BinomialDistribution dist = new BinomialDistribution(VAR_PER_SAMPLE, 1 - VAR_CI); double leftEnd = dist.inverseCumulativeProbability(BACKTEST_CI / 2); double rightEnd = dist.inverseCumulativeProbability(1 - (BACKTEST_CI / 2)); System.out.println("Left End :" + leftEnd); System.out.println("Right End :" + rightEnd); int numberOfExceptions = 0; // int successCount = 0; for (int j = 0; j < SAMPLE_SIZE * NUMBER_OF_ASSETS; j++) { for (int i = j * VAR_PER_SAMPLE; i < (j + 1) * VAR_PER_SAMPLE; i++) { //System.out.println(actualVarList.get(i) + " " + calculatedVarList.get(i)); if (actualVarList.get(i) <= calculatedVarList.get(i)) { numberOfExceptions++; } } System.out.println("Sample Set : " + (j + 1) + " Exceptions : " + numberOfExceptions); // if (rightEnd >= numberOfExceptions && leftEnd <= numberOfExceptions) { // successCount++; // } } System.out.println("Failure Rate : " + (((double) numberOfExceptions) / (VAR_PER_SAMPLE)) * 100); } private void calculateActualLoss(Portfolio portfolio, Map<String, Asset> assetList) { Double currentPortfolioValue = 0.0; Asset temp; Object symbol; Set<String> keys = portfolio.getAssetListKeySet(); Iterator itr = keys.iterator(); while (itr.hasNext()) { symbol = itr.next(); temp = assetList.get(symbol); currentPortfolioValue += temp.getCurrentStockPrice() * portfolio.getCurrentAssetQuantities((String) symbol); } if (previousPortfolioValue != null) { actualVarList.add(currentPortfolioValue - previousPortfolioValue); System.out.print(String.format("AV : %-15f", currentPortfolioValue - previousPortfolioValue)); } previousPortfolioValue = currentPortfolioValue; //System.out.print(String.format("Portfolio Value : %-15f", currentPortfolioValue)); } public ArrayList<Event> readBacktestData() throws FileNotFoundException { ClassLoader classLoader = getClass().getClassLoader(); Scanner scan = new Scanner(new File(classLoader.getResource("BackTestDataNew.csv").getFile())); ArrayList<Event> list = new ArrayList(); Event event; String[] split; while (scan.hasNext()) { event = new Event(); split = scan.nextLine().split(","); if (split.length == 2) { event.setSymbol(split[0]); event.setPrice(Double.parseDouble(split[1])); } else { event.setPortfolioID(split[0]); //portfolio id event.setQuantity(Integer.parseInt(split[1])); //shares event.setSymbol(split[2]); //symbol event.setPrice(Double.parseDouble(split[3])); //price } list.add(event); } return list; } }