public static FxRate calculateCross ( final CurrencyPair targetPair , final FxRate fx1 , final FxRate fx2 , final int precision , final int precisionForInverseFxRate , final MajorCurrencyRanking ranking , final int bidRounding , final int askRounding , CurrencyProvider currencyProvider ) { final Optional < String > crossCcy = fx1 . getCurrencyPair ( ) . findCommonCcy ( fx2 . getCurrencyPair ( ) ) ; final String xCcy = crossCcy . orElseThrow ( ( ) -> new IllegalArgumentException ( "The 2 FXRates do not share a ccy " + fx1 . getCurrencyPair ( ) + " " + fx2 . getCurrencyPair ( ) ) ) ; if ( crossCcy . isPresent ( ) && targetPair . containsCcy ( crossCcy . get ( ) ) ) { throw new IllegalArgumentException ( "The target currency pair " + targetPair + " contains the common ccy " + crossCcy . get ( ) ) ; } final String fx1Ccy1 = fx1 . getCurrencyPair ( ) . getCcy1 ( ) ; final String fx2Ccy1 = fx2 . getCurrencyPair ( ) . getCcy1 ( ) ; final String fx1Ccy2 = fx1 . getCurrencyPair ( ) . getCcy2 ( ) ; final String fx2Ccy2 = fx2 . getCurrencyPair ( ) . getCcy2 ( ) ; // what if it is both ccy2? final boolean shouldDivide = fx1Ccy1 . equals ( xCcy ) && fx2Ccy1 . equals ( xCcy ) || fx1Ccy2 . equals ( xCcy ) && fx2Ccy2 . equals ( xCcy ) ; FxRateImpl crossRate = null ; if ( shouldDivide ) { final FxRate numeratorFx = targetPair . getCcy1 ( ) . equals ( fx2Ccy2 ) || targetPair . getCcy1 ( ) . equals ( fx1Ccy1 ) ? fx1 : fx2 ; final FxRate denominatorFx = numeratorFx == fx1 ? fx2 : fx1 ; LOG . debug ( "CALC {} / {}" , numeratorFx , denominatorFx ) ; BigDecimal bid = BigDecimalUtil . divide ( precision , numeratorFx . getBid ( ) , denominatorFx . getAsk ( ) , bidRounding ) ; BigDecimal ask = BigDecimalUtil . divide ( precision , numeratorFx . getAsk ( ) , denominatorFx . getBid ( ) , askRounding ) ; crossRate = new FxRateImpl ( targetPair , xCcy , ranking . isMarketConvention ( targetPair ) , bid , ask , currencyProvider ) ; } else { crossRate = calculateWithDivide ( targetPair , fx1 , fx2 , precision , precisionForInverseFxRate , ranking , bidRounding , askRounding , currencyProvider , xCcy , fx1Ccy2 , fx2Ccy2 ) ; } LOG . debug ( "X RATE {}" , crossRate ) ; LOG . debug ( crossRate . getDescription ( ) ) ; return crossRate ; }
Calculate the cross rate use this only if required .