跳转到内容

股票涨幅计算器

js
//ST股5%,沪深10%,科创版/创业板20%,北交所30%
const MARKET = {
  "st": {lable: "ST股票", value: 0.05},
  "sh": {lable: "沪深板块", value: 0.1},
  "kc": {lable: "科创版/创业版", value: 0.2},
  "bj": {lable: "北交所", value: 0.3},
};

/**
 * 计算A股涨停和跌停的价格
 * 涨幅=(现价-上一个交易日收盘价)/上一个交易日收盘价*100
 * @param {Number} price 当前价格
 * @param {Number} number 几个板
 * @param {String} limit_type 涨停或跌停,默认涨停
 * @param {String} market_type 股票类型 sh:沪深,kc:科创、创业板,bj:北交所,st:ST股
 * @returns 计算后的结果
 */
let stock_limit = (price, number = 1, limit_type, market_type = "sh") => {
  if (price == "" || price.length < 1) return console.log("请输入股票价格");
  let current_price = price;
  let price_array = [price];
  const flag = limit_type === "down";
  const text = flag ? "跌" : "涨";
  const lable = MARKET[market_type].lable;
  const value = MARKET[market_type].value;
  console.log("\x1B[1m\x1B[36m%s\x1B[0m", `当前价格:${price}  总连板数:${number}  类型:${text}  市场:${lable}`);
  for (let i = 1; i <= number; i++) {
    let fee = flag ? 1 - value : 1 + value;
    current_price = (current_price * fee).toFixed(2);
    price_array[i] = current_price;
    let last_price = price_array[i - 1];
    console.log("\x1B[1m\x1B[36m%s\x1B[0m", `${i}${text}停价:${current_price}  ${text}幅:${(((current_price - last_price) / last_price) * 100).toFixed(2)}%`);
  }
  let tobal = (current_price - price).toFixed(2);
  let total_gain = ((tobal / price) * 100).toFixed(2);
  console.log("\x1B[1m\x1B[36m%s\x1B[0m", `总${text}:${tobal}${text}幅是:${total_gain}%`);
  console.log("\x1B[1m\x1B[30m%s\x1B[0m", "----------------------------------------------");
};

/**
 * 计算当前价到目标价的涨跌幅
 * 涨幅计算器的英文名(Percentage Increase Calculator)
 * 涨幅=(现价-上一个交易日收盘价)/上一个交易日收盘价*100
 * @param {*} num_before
 * @param {*} num_now
 */
let current_target = (num_before, num_now) => {
  let percent = (((num_now - num_before) / num_before) * 100).toFixed(2);
  console.log("\x1B[1m\x1B[36m%s\x1B[0m", `当前价格:${num_before}  目标标价:${num_now}  ${percent > 0 ? "涨" : "跌"}幅:${percent}%`);
  console.log("\x1B[1m\x1B[30m%s\x1B[0m", "----------------------------------------------");
};
/**
 * 计算当前价格涨跌 N% 之后的价格
 * @param {*} price 当前价格 10
 * @param {*} percent 涨幅 10%
 * @returns
 */
let calc_custom_price = (price, percent) => {
  let price_up = ((price * (100 + percent)) / 100).toFixed(2);
  let price_down = ((price * (100 - percent)) / 100).toFixed(2);
  console.log("\x1B[1m\x1B[36m%s\x1B[0m", `当前价格:${price}${percent}% ⇒ ${price_up}${percent}% ⇒ ${price_down}`);
  console.log("\x1B[1m\x1B[30m%s\x1B[0m", "----------------------------------------------");
};
//计算连板
stock_limit(10, 10, null, "sh"); //sh 总涨:15.93,总涨幅是:159.30%
stock_limit(10, 10, "down", "sh"); //sh 总跌:-6.52,总跌幅是:-65.20%
//计算当前价到目标价的涨跌幅;
current_target(6, 10);
current_target(10, 6);
//计算当前价格涨跌 N% 之后的价格
calc_custom_price(100, 10);