Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
11 kB
4
Indexable
Never
IMGroupDefinitionRepository imGroupDefinitionRepositoryInstance = new IMGroupDefinitionRepository();
        IMGroupTimeBucketRepository imGroupTimeBucketRepository = new IMGroupTimeBucketRepository();
        List<IMGroupDefinitionEntity> imGroupDefinitionList = imGroupDefinitionRepositoryInstance.getAllFields(groupNameList);

        List<ImGroupTimeBucketEntity> timeBucketList = imGroupTimeBucketRepository.getTimeBuckets(groupNameList);

        VolatilitySer volatilityInstance = new VolatilitySer();
        List<Double> sonucTablosuPFEDegerleri = new ArrayList<>();
        boolean assetAndCurrency = false;
        boolean yieldCurveAndDtm = false;
        boolean assetCurrencyANDyieldCurveDtm = false;

        for (int i = 0; i <imGroupDefinitionList.size() ; i++) {
            VolatilityDto dto = new VolatilityDto();
            //String startDate = DateUtils.convert_yyyyMMdd_to_ddMMyyyy(DateUtils.gunGeriGitIsGunuIcin(date,imGroupDefinitionList.get(i).getObservationPeriod()));
            String startDate = DateUtils.convert_yyyyMMdd_to_ddMMyyyy(DateUtils.gunGeriGitIsGunuIcin(date,imGroupDefinitionList.get(i).getObservationPeriod(),"T0"));
            dto.setStartDate(DateUtils.date2Str(startDate));
            date.replace(".", "");
            dto.setEndDate(date);
            dto.setVolatilityModel("EWMA");
            dto.setAssetCurrency(imGroupDefinitionList.get(i).getAsset()+"/"+imGroupDefinitionList.get(i).getCurrency());
            dto.setCmbRetCalc("Logarithmic");
            dto.setType("hard");
            EwmaServ emwainstance = new EwmaServ();
            HesaplamaParam hp = new HesaplamaParam();
            hp.setVolatiliteTipi("EWMA");
            Stat stat = new Stat(hp);

            hp.setVolatiliteTipi("EWMA");
//            FiyatHesapla fiyatHesaplaInstance = new FiyatHesapla(hp);
//            fiyatHesaplaInstance.setBaslangicTarihi(DateUtils.date2Str(startDate));
//            fiyatHesaplaInstance.setVarlikAdi(imGroupDefinitionList.get(i).getAsset());
//            fiyatHesaplaInstance.setBitisTarihi(date);
//            fiyatHesaplaInstance.setYuzdelikLogaritmik("logaritmik");
//
//            fiyatHesaplaInstance.getirileriAl(true);20220301 20210302

            ZamanSerisi zs = null;
            ZamanSerisi zsForAssetAndCurrency = null;
            ZamanSerisi zsForYieldCurveAndDtm = null;

            double volatility = 0;
            double finalvolatilityForAssetAndCurrency = 0;
            double finalvolatilityForYieldCurveAndDtm = 0;
            double totalYield = 0;
            double averageYield = 0;
            double correl = 0;
            double fxirVolatility = 0;

            SeriMaske smTum = new SeriMaske();
            //smTum.ekleVeri("20200303", "20220103","T0");
            smTum.ekleVeri(dto.getStartDate(), dto.getEndDate(),"T0");
            smTum.olusturMaske();


            DegerlemeControl dc = new DegerlemeControl(hp, hp.getSession());

            if(imGroupDefinitionList.get(i).getAsset() != null && imGroupDefinitionList.get(i).getCurrency() != null && imGroupDefinitionList.get(i).getYieldCurve() == null) {
                assetAndCurrency = true;
                zs = new ZamanSerisi(imGroupDefinitionList.get(i).getAsset(),  dto.getStartDate(), dto.getEndDate(), hp, imGroupDefinitionList.get(i).getCurrency());
            }

            else if(imGroupDefinitionList.get(i).getAsset() == null && imGroupDefinitionList.get(i).getCurrency() == null && imGroupDefinitionList.get(i).getYieldCurve() != null) {
                yieldCurveAndDtm = true;
                zs = new ZamanSerisi(imGroupDefinitionList.get(i).getYieldCurve(), 365, dc, smTum, false, hp,true);
            }

            else if (imGroupDefinitionList.get(i).getAsset() != null && imGroupDefinitionList.get(i).getCurrency() != null && imGroupDefinitionList.get(i).getYieldCurve() != null){
                assetCurrencyANDyieldCurveDtm = true;
                zsForAssetAndCurrency = new ZamanSerisi(imGroupDefinitionList.get(i).getAsset(),  dto.getStartDate(), dto.getEndDate(), hp, imGroupDefinitionList.get(i).getCurrency());
                zsForYieldCurveAndDtm = new ZamanSerisi(imGroupDefinitionList.get(i).getYieldCurve(), 360, dc, smTum, false, hp,true);
            }

            if(assetCurrencyANDyieldCurveDtm == true){

                zsForAssetAndCurrency.uygulaMaske(smTum);
                zsForYieldCurveAndDtm.uygulaMaske(smTum);

                for (int k = 0; k < zsForYieldCurveAndDtm.getSeri().length; k++) {
                        //totalYield += ((PiyasaVeriData) zs.seriArrayIslenmemis.get(k)).yield;
                        totalYield += zsForYieldCurveAndDtm.getSeri()[k];
                        // newmap.put(k,((PiyasaVeriData) zs.seriArrayIslenmemis.get(k)).tarih);
                }

                averageYield = totalYield / zsForYieldCurveAndDtm.getSeri().length;

                correl = stat.getCorrel(zsForAssetAndCurrency, zsForYieldCurveAndDtm);

                zsForAssetAndCurrency.convertSeriYuzdelikGetiri(smTum.getMaskeArray());
                zsForYieldCurveAndDtm.convertSeriYuzdelikGetiri(smTum.getMaskeArray());

                double varianceForassetCurrency = stat.getVariance(zsForAssetAndCurrency);
                finalvolatilityForAssetAndCurrency = Math.sqrt(varianceForassetCurrency);

                double varianceForYieldCurveAndDtm = stat.getVariance(zsForYieldCurveAndDtm);
                finalvolatilityForYieldCurveAndDtm =  Math.sqrt(varianceForYieldCurveAndDtm);

                double correlationPopulation = Stat.findCorrelationPopulation(zsForAssetAndCurrency.getSeri(), zsForYieldCurveAndDtm.getSeri());

                if(imGroupDefinitionList.get(i).getMethod() != null) {
                    ZamanSerisi zamanSeriAfterImposingMethodForAssetAndCurrency = this.getSeriAfterImposingMethod(zsForAssetAndCurrency, imGroupDefinitionList.get(i).getMethod());
                    ZamanSerisi zamanSeriAfterImposingMethodForYieldCurveAndDtm = this.getSeriAfterImposingMethod(zsForYieldCurveAndDtm, imGroupDefinitionList.get(i).getMethod());

                    zamanSeriAfterImposingMethodForAssetAndCurrency.volatilite = null;
                    zamanSeriAfterImposingMethodForAssetAndCurrency.mean = null;
                    double varForAssetAndCurrency = stat.getVariance(zamanSeriAfterImposingMethodForAssetAndCurrency);
                    finalvolatilityForAssetAndCurrency = Math.sqrt(varForAssetAndCurrency);

                    zamanSeriAfterImposingMethodForYieldCurveAndDtm.volatilite = null;
                    zamanSeriAfterImposingMethodForYieldCurveAndDtm.mean = null;
                    double varForYieldCurveAndDtm = stat.getVariance(zamanSeriAfterImposingMethodForYieldCurveAndDtm);
                    finalvolatilityForYieldCurveAndDtm = Math.sqrt(varForYieldCurveAndDtm);

                }

                //fxirVolatility =  Math.pow(0.5, 2) * Math.pow(finalvolatilityForAssetAndCurrency,2)  +  Math.pow(0.5, 2) * Math.pow((finalvolatilityForYieldCurveAndDtm * averageYield),(finalvolatilityForYieldCurveAndDtm * averageYield)) * finalvolatilityForYieldCurveAndDtm * averageYield + (2* Math.pow(0.5,2)* correlationPopulation * finalvolatilityForAssetAndCurrency * finalvolatilityForYieldCurveAndDtm * averageYield));
                fxirVolatility =  Math.pow(0.5, 2) * Math.pow(finalvolatilityForAssetAndCurrency,2)  +  Math.pow(0.5, 2) * Math.pow((finalvolatilityForYieldCurveAndDtm * averageYield),2) + 2* Math.pow(0.5,2) * correlationPopulation * finalvolatilityForAssetAndCurrency * finalvolatilityForYieldCurveAndDtm * averageYield;
                volatility = Math.sqrt(fxirVolatility);

            }

            else {

                zs.uygulaMaske(smTum);

                if (yieldCurveAndDtm == true) {

                    for (int k = 0; k < zs.getSeri().length; k++) {
                        //totalYield += ((PiyasaVeriData) zs.seriArrayIslenmemis.get(k)).yield;
                        totalYield += zs.getSeri()[k];
                        // newmap.put(k,((PiyasaVeriData) zs.seriArrayIslenmemis.get(k)).tarih);
                    }

                    averageYield = totalYield / zs.getSeri().length;

                }

                zs.convertSeriYuzdelikGetiri(smTum.getMaskeArray());

                double var = stat.getVariance(zs);
                volatility = Math.sqrt(var);

                if(imGroupDefinitionList.get(i).getMethod() != null) {
                    zs = this.getSeriAfterImposingMethod(zs,imGroupDefinitionList.get(i).getMethod());
                    zs.volatilite = null;
                    zs.mean = null;
                    var = stat.getVariance(zs);
                    volatility = Math.sqrt(var);
                }
            }


            double capital_adequacy_coefficient = Double.parseDouble(EngineConf.getConfDegeri("CAPITAL_ADEQUACY_COEFFICIENT"));
            double pfe = 0;
            double formulaA = 0;
            double formulaB = 0;
            double cva = 0;
            List<CvaDefaultRiskWeightEntity> cvaDefaultRiskWeightEntityList = new CVADefaultRiskWeightRepository().list();

            for (int j = 0; j < timeBucketList.size(); j++) {
                if(imGroupDefinitionList.get(i).getGroupName().equalsIgnoreCase(timeBucketList.get(j).getGroupName())) {

                    double multiplier = Double.parseDouble(imGroupDefinitionList.get(i).getMultiplier());
                    double dtmSqrt = Math.sqrt(Double.parseDouble(timeBucketList.get(j).getDtm()));
                    pfe = this.calculatePFE(assetAndCurrency,yieldCurveAndDtm,volatility,multiplier,dtmSqrt,averageYield,assetCurrencyANDyieldCurveDtm);

                    for (int k = 0; k < cvaDefaultRiskWeightEntityList.size() ; k++) {

                        for (int l = 0; l < Double.parseDouble(imGroupDefinitionList.get(i).getTrdDiversification()); l++) {

                            formulaA += cvaDefaultRiskWeightEntityList.get(k).getDefaultRiskWeight() * (Double.parseDouble(timeBucketList.get(j).getDtm()) / 365) * pfe;

                            formulaB += Math.pow((cvaDefaultRiskWeightEntityList.get(k).getDefaultRiskWeight() * (Double.parseDouble(timeBucketList.get(j).getDtm()) / 365) * pfe),2);

                        }

                      //cva =  capital_adequacy_coefficient * 2.328065920326599 / Double.parseDouble(imGroupDefinitionList.get(i).getTrdDiversification())  *  Math.sqrt(  (0.5 * Math.pow(formulaA,2)) + (0.75*formulaB));
                        cva =  capital_adequacy_coefficient * 2.328065920326599 / Double.parseDouble(imGroupDefinitionList.get(i).getTrdDiversification())  *  Math.sqrt( 0.5 * Math.pow(0.0001239224217,2) + 3.839191649998158e-9);

                        formulaA = 0;
                        formulaB = 0;


                    }


                }

            }

        }

        return null;
    }