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 java.util.List;

/**
 * @author lizhuang
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/29 10:15
 */
public class ProfitLossRatio extends Rate {

    @Override
    public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {

        if(list == null || list.isEmpty()){
            return null;
        }
        double profitNum = 0;
        double lossNum = 0;
        for (CompoundRet compoundRet : list) {
            if(compoundRet.getRetOfFund() == null ){
                continue;
            }
            if(compoundRet.getRetOfFund() < 0){
                lossNum += compoundRet.getRetOfFund();
                continue;
            }
            if(compoundRet.getRetOfFund() > 0){
                profitNum += compoundRet.getRetOfFund();
            }
        }
        return lossNum == 0 ? null : -((double) profitNum /lossNum);
    }
}
