跳转到内容

枢轴点计算器

js
/**
 * 枢轴点计算器
 * 最后一个收盘的最高价,最低价,收盘价和今日开盘价
 * @param {number} highest 昨日最高价
 * @param {number} lowest 昨日最低价
 * @param {number} close 昨日收盘价
 * @param {number} open 今日开盘价
 */
function pivot_calc(highest, lowest, close, open) {
  let camarilla = {}; //卡玛利拉(Camarilla)枢轴点
  let woodie = {}; //伍迪(Woodie)枢轴点
  let classic = {}; //经典枢轴点
  let fibonacci = {}; //斐波纳契枢轴点
  let demark = {}; //汤姆.丹麦枢轴点(Tom DeMark's pivot points)
  let sum = 0;
  let hprice = parseFloat(highest); //昨日最高价
  let lprice = parseFloat(lowest); //昨日最低价
  let cprice = parseFloat(close); //昨日收盘价
  let oprice = parseFloat(open); //昨日开盘价
  //校验数字
  if (hprice >= lprice && hprice >= cprice && hprice >= oprice && lprice <= cprice) {
    /**
     * 卡玛利拉(Camarilla)枢轴点
     * 卡玛利拉枢轴点,是由前一时期的最高价(H).最低价(L)和收盘价(C)推算出来.
     * 以下是通常用来计算卡玛利拉枢轴点的方法:
     * 枢轴点(P) = (hprice + lprice + cprice) / 3
     * 阻力位 1 (P1) = (hprice - lprice) * 1.1 /12 + cprice
     * 阻力位 2 (R2) = (hprice - lprice) * 1.1 / 6 + cprice
     * 阻力位 3 (R3) = (hprice - lprice) * 1.1 / 4 + cprice
     * 阻力位 (R4) = (hprice - lprice) * 1.1 / 2 + cprice
     * 支撑位 1 (S1) = cprice - (hprice - lprice) * 1.1 / 12
     * 支撑位 2 (S2) = cprice - (hprice - lprice) * 1.1 /6
     * 支撑位 3 (S3) = cprice - (hprice - lprice) * 1.1 / 4
     * 支撑位 4 (S4) = cprice - (hprice - lprice) * 1.1 / 2
     */
    camarilla['name'] = '卡玛利拉';
    camarilla['P'] = Math.round(((1 * hprice + 1 * lprice + 1 * cprice) / 3) * 10000) / 10000;
    camarilla['R1'] = Math.round((1 * cprice + ((1 * hprice - 1 * lprice) * 1.1) / 12) * 10000) / 10000;
    camarilla['R2'] = Math.round((1 * cprice + ((1 * hprice - 1 * lprice) * 1.1) / 6) * 10000) / 10000;
    camarilla['R3'] = Math.round((1 * cprice + ((1 * hprice - 1 * lprice) * 1.1) / 4) * 10000) / 10000;
    camarilla['R4'] = Math.round((1 * cprice + ((1 * hprice - 1 * lprice) * 1.1) / 2) * 10000) / 10000;
    camarilla['S1'] = Math.round((1 * cprice - ((1 * hprice - 1 * lprice) * 1.1) / 12) * 10000) / 10000;
    camarilla['S2'] = Math.round((1 * cprice - ((1 * hprice - 1 * lprice) * 1.1) / 6) * 10000) / 10000;
    camarilla['S3'] = Math.round((1 * cprice - ((1 * hprice - 1 * lprice) * 1.1) / 4) * 10000) / 10000;
    camarilla['S4'] = Math.round((1 * cprice - ((1 * hprice - 1 * lprice) * 1.1) / 2) * 10000) / 10000;
    /**
     * 伍迪(Woodie)枢轴点
     * 伍迪枢轴点与经典枢轴点类似,但给予收盘价更大的权重.
     * 伍迪枢轴点也是由前一时期的最高价(H).最低价(L)和收盘价(C)推算出当期的枢轴点.
     * 计算伍迪枢轴点的规则如下:
     * 枢轴点(P) = (H + L + 2 * C) / 4
     * 阻力位 1 (P1) = (2 * P) - L
     * 阻力位 2 (R2) = P + H - L
     * 支撑位 1 (S1) = (2 * P) - H
     * 支撑位 2 (S2) = P - H + L
     */
    woodie['name'] = '伍迪';
    let wP = (1 * hprice + 1 * lprice + 2 * cprice) / 4;
    woodie['P'] = Math.round(wP * 10000) / 10000;
    woodie['R1'] = Math.round((2 * wP - 1 * lprice) * 10000) / 10000;
    woodie['R2'] = Math.round((1 * wP + 1 * hprice - 1 * lprice) * 10000) / 10000;
    woodie['S1'] = Math.round((2 * wP - 1 * hprice) * 10000) / 10000;
    woodie['S2'] = Math.round((1 * wP - 1 * hprice + 1 * lprice) * 10000) / 10000;
    /**
     * 经典枢轴点是外汇交易中最常用的一类枢轴点.
     * 经典枢轴点的计算方法,是由前一时期的最高价(H).最低价(L)和收盘价(C),算出后一时期的经典枢轴点.
     * 经典枢轴点的计算公式如下:
     * 枢轴点:  (P) = (hprice + lprice + cprice) / 3
     * 阻力位1: (R1) = (2 * P) - lprice
     * 阻力位2: (R2) = P + hprice - lprice
     * 阻力位3: (R3) = hprice + 2 * (P - lprice)
     * 支撑位1: (S1) = (2 * P) - hprice
     * 支撑位2: (S2) = P - hprice + lprice
     * 支撑位3: (S3) = lprice - 2 * (hprice - P)
     */
    classic['name'] = '经典';
    let bP = (1 * hprice + 1 * lprice + 1 * cprice) / 3;
    classic['P'] = Math.round(bP * 10000) / 10000;
    classic['R1'] = Math.round((2 * bP - 1 * lprice) * 10000) / 10000;
    classic['R2'] = Math.round((1 * bP + 1 * hprice - 1 * lprice) * 10000) / 10000;
    classic['R3'] = Math.round((1 * hprice + 2 * (1 * bP - 1 * lprice)) * 10000) / 10000;
    classic['S1'] = Math.round((2 * bP - 1 * hprice) * 10000) / 10000;
    classic['S2'] = Math.round((1 * bP + 1 * lprice - 1 * hprice) * 10000) / 10000;
    classic['S3'] = Math.round((1 * lprice - 2 * (1 * hprice - 1 * bP)) * 10000) / 10000;
    /**
     * 计算斐波纳契枢轴点:
     * 斐波纳契枢轴点是经典枢轴点与斐波纳契比率的结合.
     * 首先,由前一时期的最高价(H).最低价(L)和收盘价(C)之和除以3,算出后一时期的枢轴点.
     * 然后将斐波纳契比率分别加上枢轴点,推算出后一时期的支撑位和阻力位:
     * 枢轴点:  (P) = (hprice + lprice + cprice) / 3
     * 阻力位1: (R1) = P + (hprice + lprice) * 0.382
     * 阻力位2: (R2) = P + (hprice + lprice) * 0.618
     * 阻力位3: (R3) = P + (hprice + lprice) * 1.0
     * 支撑位1: (S1) = P + (hprice - lprice) * 0.382
     * 支撑位2: (S2) = P + (hprice - lprice) * 0.618
     * 支撑位3: (S3) = P + (hprice - lprice) * 1.0
     */
    fibonacci['name'] = '斐波纳契';
    let pivP = (1 * hprice + 1 * lprice + 1 * cprice) / 3;
    fibonacci['P'] = Math.round(pivP * 10000) / 10000;
    fibonacci['R1'] = Math.round((1 * pivP + (1 * hprice - 1 * lprice) * 0.382) * 10000) / 10000;
    fibonacci['R2'] = Math.round((1 * pivP + (1 * hprice - 1 * lprice) * 0.618) * 10000) / 10000;
    fibonacci['R3'] = Math.round((1 * pivP + (1 * hprice - 1 * lprice) * 1.0) * 10000) / 10000;
    fibonacci['S1'] = Math.round((1 * pivP - (1 * hprice - 1 * lprice) * 0.382) * 10000) / 10000;
    fibonacci['S2'] = Math.round((1 * pivP - (1 * hprice - 1 * lprice) * 0.618) * 10000) / 10000;
    fibonacci['S3'] = Math.round((1 * pivP - (1 * hprice - 1 * lprice) * 1.0) * 10000) / 10000;
    /**
     * 汤姆.丹麦枢轴点(Tom DeMark's pivot points)
     * 汤姆.丹麦枢轴点是对当期的新最高价和新最低价的预测.根据汤姆.丹麦的方法,
     * 当期的价格波动幅度可由前一时期的最高价(H).最低价(L)和收盘价(C)以及当期的开盘价 推算出来.
     * 以下是计算汤姆.丹麦枢轴点的规则:
     * 如果前一时期的收盘价小于当期的开盘价 : X = H + 2 * L + C;
     * 如果前一时期的收盘价大于当期的开盘价 : X = 2 * H + L + C;
     * 如果前一时期的收盘价等于当期的开盘价: X = H + L + 2 * C;
     * 当期的新最高价 = X / 2 - L;
     * 当期的新最低价 = X / 2 - H
     */
    if (oprice > cprice) {
      sum = 2 * hprice + 1 * lprice + 1 * cprice;
    } else if (oprice == cprice) {
      sum = 1 * hprice + 1 * lprice + 2 * cprice;
    } else if (oprice < cprice) {
      sum = 1 * hprice + 2 * lprice + 1 * cprice;
    }
    demark['name'] = '汤姆.丹麦';
    demark['R1'] = Math.round((sum / 2 - 1 * lprice) * 10000) / 10000;
    demark['S1'] = Math.round((sum / 2 - 1 * hprice) * 10000) / 10000;
  } else {
    if (hprice < lprice) {
      console.log('昨日最高价不能小于昨日最低价');
    }
    if (hprice < cprice) {
      console.log('昨日最高价不能小于昨日收盘价');
    }
    if (hprice < oprice) {
      console.log('昨日最高价不能小于昨日开盘价');
    }
    if (lprice > cprice) {
      console.log('昨日最低价不能大于昨日收盘价');
    }
  }
  //R[idx]:阻力位,S[idx]:支撑位,P:轴心点
  let stock = [camarilla, woodie, classic, fibonacci, demark];
  return stock;
}
// //{date: '2022/10/20', h: 39.28, l: 37.42, c: 37.64};
// console.log(pivot_calc(40.01, 36.08, 39.49, 38.21));
// //{date: '2022/10/20', h: 9.95, l: 9.65, c: 9.76};
// console.log(pivot_calc(10.04, 9.72, 9.76, 9.66));