package com.smppw.core.reta.calc;


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

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

/**
 * @author zhengsongliang
 * @datetime 2017年7月25日 下午3:55:29
 */
public class M2 extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		List<Double> bmkList = new ArrayList<>();
		double rfSum = 0;
		for (CompoundRet value : list) {
			if (value.getRetOfBmk() == null || value.getRetOfRf() == null)
				continue;
			rfSum += value.getRetOfRf();
			bmkList.add(value.getRetOfBmk());
		}
		double rfAvg = rfSum / list.size();
		Double SharpeRatio = IndicatorFactory.getInstance().get(Indicator.SharpeRatio).calc(list, frequency, false);
		if(SharpeRatio==null) {
			return null;
		}
		while (bmkList.contains(null)) {
			bmkList.remove(null);
		}

		double bmkStd = Statistics.stdDevUnbiased(bmkList);
		double M2 = rfAvg + SharpeRatio * bmkStd;

		if (ifAnnualized) {
			M2 *= frequency.getNum();
		}
		return DataUtil.handleOverflowData(M2);
	}

}
