Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.joining;
- import static java.util.stream.Collectors.toList;
- class ETF {
- private List<EtfComponent> components;
- private Map<String, Float> componentWeight;
- private String name;
- private double bestBid;
- private double bestAsk;
- private double ourAsk;
- private double ourBid;
- private String currency;
- public String getName() {
- return name;
- }
- public String getCurrency() {
- return currency;
- }
- public Map<String, Float> getComponentWeight() {
- return componentWeight;
- }
- public void setComponentWeight(Map<String, Float> componentWeight) {
- this.componentWeight = componentWeight;
- }
- public void setBestBid(double bestBid) {
- this.bestBid = bestBid;
- }
- public void setBestAsk(double bestAsk) {
- this.bestAsk = bestAsk;
- }
- public void setOurAsk(double ourAsk) {
- this.ourAsk = ourAsk;
- }
- public void setOurBid(double ourBid) {
- this.ourBid = ourBid;
- }
- public double getOurAsk() {
- return ourAsk;
- }
- public double getOurBid() {
- return ourBid;
- }
- public double getBestBid() {
- return bestBid;
- }
- public double getBestAsk() {
- return bestAsk;
- }
- public List<EtfComponent> getComponents() {
- return components;
- }
- public ETF(List<EtfComponent> components, String name, String currency){
- this.components = components;
- this.name = name;
- this.currency = currency;
- }
- }
- class EtfComponent {
- double bid;
- double ask;
- String name;
- String currency;
- public String getName() {
- return name;
- }
- public double getBid() {
- return bid;
- }
- public double getAsk() {
- return ask;
- }
- public String getCurrency() {
- return currency;
- }
- public void setBid(double bid) {
- this.bid = bid;
- }
- public void setAsk(double ask) {
- this.ask = ask;
- }
- public EtfComponent(String name, String currency) {
- this.name = name;
- this.currency = currency;
- }
- }
- class Market{
- private static Market market_instance = null;
- private List<ETF> etfs;
- private List<EtfComponent> components;
- private Map<String, Double> exchangeRatesCurrencies;
- private Map<String, ETF> nameToETF;
- private Map<String, EtfComponent> nameToEtfComponent;
- private Map<String, Double> marketETFAsks;
- private Map<String, Double> marketETFBids;
- private Map<String, Double> ETFsAO;
- private Map<String, Double> ETFsBO;
- private Market(){
- etfs = new ArrayList<>();
- components = new ArrayList<>();
- exchangeRatesCurrencies = new HashMap<>();
- nameToETF = new HashMap<>();
- nameToEtfComponent = new HashMap<>();
- marketETFAsks = new HashMap<>();
- marketETFBids = new HashMap<>();
- ETFsAO = new HashMap<>();
- ETFsBO = new HashMap<>();
- }
- public Map<String, EtfComponent> getNameToEtfComponent() {
- return nameToEtfComponent;
- }
- public static Market getMarket(){
- if (market_instance == null){
- market_instance = new Market();
- }
- market_instance.exchangeRatesCurrencies.put("EUREUR", 1.0);
- return market_instance;
- }
- public List<ETF> getEtfs() {
- return etfs;
- }
- public static void addEtf(ETF etf){
- market_instance.nameToETF.put(etf.getName(), etf);
- market_instance.etfs.add(etf);
- }
- public static void addEtfComponent(EtfComponent component){
- market_instance.nameToEtfComponent.put(component.getName(), component);
- market_instance.components.add(component);
- }
- public void updateComponent(String name, double price, String side){
- if (side.equals("A")){
- market_instance.nameToEtfComponent.get(name).setAsk(price);
- }else if(side.equals("B")){
- market_instance.nameToEtfComponent.get(name).setBid(price);
- }
- // TODO: else throw an exception
- }
- public double calculateTheo(ETF etf, String side){
- double theo = 0;
- try{
- for (EtfComponent component: etf.getComponents()
- ) {
- double bidAskprice = (side.equals("A")) ? component.getAsk() : component.getBid();
- System.out.println(component.getCurrency() + etf.getCurrency());
- theo += bidAskprice *
- etf.getComponentWeight().get(component.getName()) *
- exchangeRatesCurrencies.get(component.getCurrency() + etf.getCurrency());
- }
- theo /= etf.getComponents().size();
- theo = (double) Math.round(theo*100) / 100; //round to 2 decimals
- if (side.equals("A"))
- etf.setOurAsk(theo);
- else if (side.equals("B"))
- etf.setOurBid(theo);
- return theo;
- } catch (Exception e){
- System.out.println("Exception when calculating theo for: " + etf.getName() + " on side " + side);
- etf.setOurBid(-1);
- etf.setOurAsk(-1);
- return -1;
- }
- }
- public void updateCurrencyExchange(String name, double price){
- exchangeRatesCurrencies.put(name.substring(0,3)+name.substring(0,3),1.0); // optimize
- exchangeRatesCurrencies.put(name.substring(3,6)+name.substring(6,6),1.0); // optimize
- exchangeRatesCurrencies.put(name, price);
- exchangeRatesCurrencies.put(flipCurrency(name), 1/price);
- }
- public void updateEtf(String name, double price, String side){
- if (side.equals("A"))
- market_instance.nameToETF.get(name).setBestAsk(price);
- else if (side.equals("B"))
- market_instance.nameToETF.get(name).setBestBid(price);
- // TODO: else throw an exception
- }
- public List<List<String>> updateOffers() {
- List<List<String>> output = new ArrayList<>();
- for(ETF etf: etfs) {
- String currentETF = etf.getName();
- double currentAO = etf.getOurAsk();
- double currentBO = etf.getOurBid();
- double marketAsk = etf.getBestAsk();
- double marketBid = etf.getBestBid();
- System.out.println("ETF: " + currentETF);
- System.out.println("Our Ask: " + currentAO);
- System.out.println("Our Bid: " + currentBO);
- System.out.println("Market Ask: " + marketAsk);
- System.out.println("Market Bid: " + marketBid);
- if (marketAsk != 0 || marketBid != 0) {
- if (currentAO > marketAsk) {
- currentAO = -1;
- } else {
- currentAO = marketAsk - 0.01;
- }
- if (currentBO < marketBid) {
- currentBO = -1;
- } else {
- currentBO = marketBid + 0.01;
- }
- }
- if (ETFsAO.containsKey(currentETF) && ETFsBO.containsKey(currentETF)) {
- if (currentAO != -1 || currentBO != -1) {
- // add to print list quantity 0
- ETFsAO.put(currentETF, -1.0);
- ETFsBO.put(currentETF, -1.0);
- output.add(getUpdateOutput("AO", currentETF, ETFsAO.get(currentETF), "0"));
- output.add(getUpdateOutput("BO", currentETF, ETFsBO.get(currentETF), "0"));
- } else if (currentAO != ETFsAO.get(currentETF) || currentBO != ETFsBO.get(currentETF)) {
- // print new price
- ETFsAO.put(currentETF, currentAO);
- ETFsBO.put(currentETF, currentBO);
- output.add(getUpdateOutput("AO", currentETF, currentAO, "10"));
- output.add(getUpdateOutput("BO", currentETF, currentBO, "10"));
- }
- // first time we set the price
- } else if (currentAO != -1 && currentBO != -1) {
- System.out.println("update output");
- ETFsAO.put(currentETF, currentAO);
- ETFsBO.put(currentETF, currentBO);
- output.add(getUpdateOutput("AO", currentETF, currentAO, "10"));
- output.add(getUpdateOutput("BO", currentETF, currentBO, "10"));
- }
- }
- return output;
- }
- private List<String> getUpdateOutput(String Operation, String currentETF, Double currentAO, String Quantity) {
- ArrayList<String> ETFUpdateOutput = new ArrayList<>();
- ETFUpdateOutput.add(Operation);
- ETFUpdateOutput.add(currentETF);
- ETFUpdateOutput.add(Double.toString(currentAO));
- ETFUpdateOutput.add(Quantity);
- return ETFUpdateOutput;
- }
- private String flipCurrency(String name){
- return name.substring(name.length()/2).concat(name.substring(0, name.length()/2));
- }
- }
- class Result {
- /*
- * Complete the 'addEtf' function below.
- *
- * The function accepts following parameters:
- * 1. STRING name
- * 2. STRING currency
- * 3. 2D_STRING_ARRAY components
- */
- public static Market market = Market.getMarket();
- public static void addEtf(String name, String currency, List<List<String>> components) {
- List<EtfComponent> etfComponents = new ArrayList<>();
- Map<String, Float> weights = new HashMap<>();
- for (List<String> component: components) {
- String componentName = component.get(0);
- float componentWeight = Float.parseFloat(component.get(1));
- String componentCurrency = component.get(2);
- EtfComponent etfComponent = new EtfComponent(componentName, componentCurrency);
- etfComponents.add(etfComponent);
- weights.put(componentName, componentWeight);
- market.getNameToEtfComponent().put(componentName,etfComponent);
- }
- ETF etf = new ETF(etfComponents, name, currency);
- etf.setComponentWeight(weights);
- market.addEtf(etf);
- }
- /*
- * Complete the 'playback' function below.
- *
- * The function accepts 2D_STRING_ARRAY messages as parameter.
- */
- public static void playback(List<List<String>> messages) {
- List<List<String>> offers = null;
- for (List<String> message:messages
- ) {
- String type = message.get(0);
- String symbol = message.get(1);
- String exchange_name = message.get(2);
- double price = Double.parseDouble(message.get(3));
- int quantity = Integer.parseInt(message.get(4));
- // TODO: Add enums or somthing
- if (exchange_name.equals("Stock")){
- market.updateComponent(symbol, price, type);
- } else if (exchange_name.equals("Currency")){
- market.updateCurrencyExchange(symbol, price);
- }else{
- market.updateEtf(symbol, price, type);
- }
- for (ETF etf: market.getEtfs()){
- market.calculateTheo(etf, "A"); // TODO: Optimize to only calculate when there is a change
- market.calculateTheo(etf, "B"); // TODO: Optimize to only calculate when there is a change
- }
- offers = market.updateOffers();
- makeOffers(offers);
- }
- }
- private static void makeOffers(List<List<String>> offers){
- for (List<String> offer: offers
- ) {
- System.out.println(String.join(" ", offer));
- }
- }
- }
- public class test2 {
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- int NumberOfETF = Integer.parseInt(bufferedReader.readLine().trim());
- IntStream.range(0, NumberOfETF).forEach(NumberOfETFItr -> {
- try {
- String ETFSymbol = bufferedReader.readLine();
- String ETFCurrency = bufferedReader.readLine();
- int NumberOfListingInETF = Integer.parseInt(bufferedReader.readLine().trim());
- List<List<String>> listings = new ArrayList<>();
- IntStream.range(0, NumberOfListingInETF).forEach(i -> {
- try {
- listings.add(
- Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .collect(toList())
- );
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- });
- Result.addEtf(ETFSymbol, ETFCurrency, listings);
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- });
- int NumberOfMessages = Integer.parseInt(bufferedReader.readLine().trim());
- List<List<String>> messages = new ArrayList<>();
- IntStream.range(0, NumberOfMessages).forEach(i -> {
- try {
- messages.add(
- Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .collect(toList())
- );
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- });
- Result.playback(messages);
- bufferedReader.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement