package link.at17.mid.tushare.data.models;

import link.at17.mid.tushare.enums.PivotPointType;
import lombok.Getter;

/**
 * 中心点、压力位和支撑位
 * @author Barry
 *
 */
@Getter
public class PivotPoint {
    private Double resistance4;
    private Double resistance3;
    private Double resistance2;
    private Double resistance1;
    private Double pivotPoint;
    private Double support1;
    private Double support2;
    private Double support3;
    private Double support4;
    private PivotPointType pivotPointType;
    
    public PivotPoint(StockValue stockValue, PivotPointType pivotPointType) {
        Double high  = stockValue.getHigh(),  // c
               low   = stockValue.getLow(),   // f
               open  = stockValue.getOpen(),  // e
               close = stockValue.getClose(); // h
        if (pivotPointType.equals(PivotPointType.Classic)) {
            Double k = (high + low + close) / 3, g = 2 * k - high, bres1 = 2 * k - low;
            resistance3 = high + 2 * (k - low);
            resistance2 = k + (bres1 - g);
            resistance1 = bres1;
            pivotPoint = k;
            support1 = g;
            support2 = k - (bres1 - g);
            support3 = low - 2 * (high - k);
        }
        else if (pivotPointType.equals(PivotPointType.Woodies)) {
            Double d = (high + low + 2 * close) / 4 ;
            resistance1 = 2 * d - low;
            resistance2 = d + high - low;
            pivotPoint = d;
            support1 = 2 * d - high;
            support2 = d - high + low;
        }
        else if (pivotPointType.equals(PivotPointType.Camarilla)) {
            resistance1 = close + ((high - low) * (1.1 / 12));
            resistance2 = close + ((high - low) * (1.1 / 6));
            resistance3 = close + ((high - low) * (1.1 / 4));
            resistance4 = close + ((high - low) * (1.1 / 2));
            support1 = close - ((high - low) * (1.1 / 12));
            support2 = close - ((high - low) * (1.1 / 6));
            support3 = close - ((high - low) * (1.1 / 4));
            support4 = close - ((high - low) * (1.1 / 2));
        }
        else {
            Double b;
            if (close < open) {
                b = high + low * 2 + close;
            }
            else if (close > open) {
                b = high * 2 + low + close;
            }
            else {
                b = high + low + close * 2;
            }
            resistance1 = b / 2 - low;
            support1 = b / 2 - high;
            pivotPoint = b / 4;
        }
        this.pivotPointType = pivotPointType;
    }
}
