package com.smppw.core.reta.calc;


import com.smppw.common.pojo.dto.CompoundRet;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.core.reta.Rate;
import com.smppw.utils.DataUtil;
import com.smppw.utils.Statistics;

import java.util.ArrayList;
import java.util.List;

public class SharpeRatio extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		List<Double> rets = new ArrayList<>();
		double sharp = 0.0;

		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null || value.getRetOfRf() == null)
				continue;
			double unit=value.getRetOfFund() - value.getRetOfRf();
			sharp += unit;
			rets.add(value.getRetOfFund());
		}
		
		sharp /= (list.size() - 1);
		
		Double stdDev = Statistics.stdDevUnbiased(rets);
		
//		if (stdDev == null) {
//			return null;
//		} else if (stdDev == 0) {
//			return DataUtil.handleZeroValue(sharp);
//		}
//		sharp /= stdDev;
		Double sharpDou = DataUtil.divNearZeroValue(sharp, stdDev);
		if (sharpDou == null) {
			return null;
		}
		sharp = sharpDou;

		Double result = ifAnnualized ? Rate.AnnualizedBySqrt(sharp, frequency) : sharp;
		return DataUtil.handleOverflowData(result);
	}

}
