src/main/java/com/billybyte/dse/debundles/RunGreeks.java
static final void example5_DseResultsAsCsv(String portfolioPath){
		List<DerivativeSensitivityTypeInterface> senses = getSenses();
		DerivativeSetEngine dse = DerivativeSetEngineBuilder.dseForStocksUsingYahoo();
		Map<String, Integer> portfolioMap = getPortfolio(portfolioPath);
		if(portfolioMap == null || portfolioMap.size()<=0){
			Utils.prtObErrMess(RunGreeks.class, "Portfolio Map is null or can't be found");
			return;
		}
		
		
		Map<String,Map<DerivativeSensitivityTypeInterface,DerivativeReturn>> secToSenseToDrArrMap = 
				new HashMap<String, Map<DerivativeSensitivityTypeInterface,DerivativeReturn>>();

		// create inner map for each shortName which holds the sensitivities
		for(String sn : portfolioMap.keySet()){
			secToSenseToDrArrMap.put(sn, new HashMap<DerivativeSensitivityTypeInterface, DerivativeReturn>());
		}
		
		for(DerivativeSensitivityTypeInterface sense : senses){
			Map<String, DerivativeReturn[]> dseResults = 
					dse.getSensitivity(sense, portfolioMap.keySet());
			for(String shortName : dseResults.keySet()){
				DerivativeReturn dr = dseResults.get(shortName)[0];
				 Map<DerivativeSensitivityTypeInterface,DerivativeReturn> senseMap = 
						 secToSenseToDrArrMap.get(shortName);
				senseMap.put(sense, dr);
			}
		}
		
		

		// Create csv output
		TreeMap<String,String[]> snToCsvMap = 
				new TreeMap<String, String[]>();
		List<String> exceptionList = new ArrayList<String>();
		
		for(String sn : secToSenseToDrArrMap.keySet()){
			Map<DerivativeSensitivityTypeInterface,DerivativeReturn> senseToDrArrMap = 
					secToSenseToDrArrMap.get(sn);
			BigDecimal size = new BigDecimal(portfolioMap.get(sn));
			String[] line = new String[senses.size()+2];
			line[0] = sn;
			line[1] = size.toString();
			for(int i = 0;i<senses.size();i++){
				line[i+2]="null";
				DerivativeSensitivityTypeInterface sense = senses.get(i);
				DerivativeReturn dr = senseToDrArrMap.get(sense);
				if(dr==null){
					exceptionList.add(sense.getString()+ " No return from DSE");
					continue;
				}
				if(!dr.isValidReturn()){
					exceptionList.add(sn + " " + sense.getString()+ " " + dr.getException().getMessage());
					continue;
				}
				line[i+2] = new BigDecimal(dr.getValue().doubleValue()).multiply(size).setScale(5,RoundingMode.HALF_EVEN).toString();
			}
			
			snToCsvMap.put(sn,line);
		}
		

		System.out.flush();
		String header = "shortName,size,price,delta,gamma,vega,theta,rho";
		Utils.prt(header);
		for(String key : snToCsvMap.keySet()){
			String[] lineTokens = snToCsvMap.get(key);
			String line="";
			for(String lineToken:lineTokens){
				line+=lineToken+",";
			}
			line = line.substring(0,line.length()-1);
			Utils.prt(line);
		}

		if(exceptionList.size()>0){
			Utils.prt("");
			Utils.prt("exceptions below:");
			CollectionsStaticMethods.prtListItems(exceptionList);
			
		}

	}
/**
	 * Get a collection of basic greeks from a csv portfolio.  Print the results
	 *   as csv.
	 * @param portfolioPath
	 */
Get a collection of basic greeks from a csv portfolio.  Print the results
as csv.
@param portfolioPath
Get a collection of basic greeks from a csv portfolio . Print the results as csv . @param portfolioPath
static final void example5_DseResultsAsCsv(String portfolioPath){
		List<DerivativeSensitivityTypeInterface> senses = getSenses();
		DerivativeSetEngine dse = DerivativeSetEngineBuilder.dseForStocksUsingYahoo();
		Map<String, Integer> portfolioMap = getPortfolio(portfolioPath);
		if(portfolioMap == null || portfolioMap.size()<=0){
			Utils.prtObErrMess(RunGreeks.class, "Portfolio Map is null or can't be found");
			return;
		}
		Map<String,Map<DerivativeSensitivityTypeInterface,DerivativeReturn>> secToSenseToDrArrMap = 
				new HashMap<String, Map<DerivativeSensitivityTypeInterface,DerivativeReturn>>();
		for(String sn : portfolioMap.keySet()){
			secToSenseToDrArrMap.put(sn, new HashMap<DerivativeSensitivityTypeInterface, DerivativeReturn>());
		}
		for(DerivativeSensitivityTypeInterface sense : senses){
			Map<String, DerivativeReturn[]> dseResults = 
					dse.getSensitivity(sense, portfolioMap.keySet());
			for(String shortName : dseResults.keySet()){
				DerivativeReturn dr = dseResults.get(shortName)[0];
				 Map<DerivativeSensitivityTypeInterface,DerivativeReturn> senseMap = 
						 secToSenseToDrArrMap.get(shortName);
				senseMap.put(sense, dr);
			}
		}
		TreeMap<String,String[]> snToCsvMap = 
				new TreeMap<String, String[]>();
		List<String> exceptionList = new ArrayList<String>();
		for(String sn : secToSenseToDrArrMap.keySet()){
			Map<DerivativeSensitivityTypeInterface,DerivativeReturn> senseToDrArrMap = 
					secToSenseToDrArrMap.get(sn);
			BigDecimal size = new BigDecimal(portfolioMap.get(sn));
			String[] line = new String[senses.size()+2];
			line[0] = sn;
			line[1] = size.toString();
			for(int i = 0;i<senses.size();i++){
				line[i+2]="null";
				DerivativeSensitivityTypeInterface sense = senses.get(i);
				DerivativeReturn dr = senseToDrArrMap.get(sense);
				if(dr==null){
					exceptionList.add(sense.getString()+ " No return from DSE");
					continue;
				}
				if(!dr.isValidReturn()){
					exceptionList.add(sn + " " + sense.getString()+ " " + dr.getException().getMessage());
					continue;
				}
				line[i+2] = new BigDecimal(dr.getValue().doubleValue()).multiply(size).setScale(5,RoundingMode.HALF_EVEN).toString();
			}
			snToCsvMap.put(sn,line);
		}
		System.out.flush();
		String header = "shortName,size,price,delta,gamma,vega,theta,rho";
		Utils.prt(header);
		for(String key : snToCsvMap.keySet()){
			String[] lineTokens = snToCsvMap.get(key);
			String line="";
			for(String lineToken:lineTokens){
				line+=lineToken+",";
			}
			line = line.substring(0,line.length()-1);
			Utils.prt(line);
		}
		if(exceptionList.size()>0){
			Utils.prt("");
			Utils.prt("exceptions below:");
			CollectionsStaticMethods.prtListItems(exceptionList);
		}
	}
static final void example5_DseResultsAsCsv ( String portfolioPath ) { List < DerivativeSensitivityTypeInterface > senses = getSenses ( ) ; DerivativeSetEngine dse = DerivativeSetEngineBuilder . dseForStocksUsingYahoo ( ) ; Map < String , Integer > portfolioMap = getPortfolio ( portfolioPath ) ; if ( portfolioMap == null || portfolioMap . size ( ) <= 0 ) { Utils . prtObErrMess ( RunGreeks . class , "Portfolio Map is null or can't be found" ) ; return ; } Map < String , Map < DerivativeSensitivityTypeInterface , DerivativeReturn > > secToSenseToDrArrMap = new HashMap < String , Map < DerivativeSensitivityTypeInterface , DerivativeReturn > > ( ) ; for ( String sn : portfolioMap . keySet ( ) ) { secToSenseToDrArrMap . put ( sn , new HashMap < DerivativeSensitivityTypeInterface , DerivativeReturn > ( ) ) ; } for ( DerivativeSensitivityTypeInterface sense : senses ) { Map < String , DerivativeReturn [ ] > dseResults = dse . getSensitivity ( sense , portfolioMap . keySet ( ) ) ; for ( String shortName : dseResults . keySet ( ) ) { DerivativeReturn dr = dseResults . get ( shortName ) [ 0 ] ; Map < DerivativeSensitivityTypeInterface , DerivativeReturn > senseMap = secToSenseToDrArrMap . get ( shortName ) ; senseMap . put ( sense , dr ) ; } } TreeMap < String , String [ ] > snToCsvMap = new TreeMap < String , String [ ] > ( ) ; List < String > exceptionList = new ArrayList < String > ( ) ; for ( String sn : secToSenseToDrArrMap . keySet ( ) ) { Map < DerivativeSensitivityTypeInterface , DerivativeReturn > senseToDrArrMap = secToSenseToDrArrMap . get ( sn ) ; BigDecimal size = new BigDecimal ( portfolioMap . get ( sn ) ) ; String [ ] line = new String [ senses . size ( ) + 2 ] ; line [ 0 ] = sn ; line [ 1 ] = size . toString ( ) ; for ( int i = 0 ; i < senses . size ( ) ; i ++ ) { line [ i + 2 ] = "null" ; DerivativeSensitivityTypeInterface sense = senses . get ( i ) ; DerivativeReturn dr = senseToDrArrMap . get ( sense ) ; if ( dr == null ) { exceptionList . add ( sense . getString ( ) + " No return from DSE" ) ; continue ; } if ( ! dr . isValidReturn ( ) ) { exceptionList . add ( sn + " " + sense . getString ( ) + " " + dr . getException ( ) . getMessage ( ) ) ; continue ; } line [ i + 2 ] = new BigDecimal ( dr . getValue ( ) . doubleValue ( ) ) . multiply ( size ) . setScale ( 5 , RoundingMode . HALF_EVEN ) . toString ( ) ; } snToCsvMap . put ( sn , line ) ; } System . out . flush ( ) ; String header = "shortName,size,price,delta,gamma,vega,theta,rho" ; Utils . prt ( header ) ; for ( String key : snToCsvMap . keySet ( ) ) { String [ ] lineTokens = snToCsvMap . get ( key ) ; String line = "" ; for ( String lineToken : lineTokens ) { line += lineToken + "," ; } line = line . substring ( 0 , line . length ( ) - 1 ) ; Utils . prt ( line ) ; } if ( exceptionList . size ( ) > 0 ) { Utils . prt ( "" ) ; Utils . prt ( "exceptions below:" ) ; CollectionsStaticMethods . prtListItems ( exceptionList ) ; } }
Get a collection of basic greeks from a csv portfolio.
Get a collection of basic greeks from a csv portfolio .
// create inner map for each shortName which holds the sensitivities // Create csv output