C/C++ KDJ指標 範例

C/C++ KDJ指標 範例

C/C++ KDJ指標 範例


資料來源: Bard for Chrome


code

#include <stdio.h>
#include <stdlib.h>

// KDJ 指標計算公式
double K(double *high, double *low, double *close, int N) {
  double sum = 0.0;
  for (int i = 0; i < N; i++) {
    sum += (close[i] - low[i]) / (high[i] - low[i]);
  }
  return 2 / N * sum;
}

double D(double *K, int N) {
  double sum = 0.0;
  for (int i = 0; i < N; i++) {
    sum += K[i];
  }
  return 3 / N * sum;
}

double J(double *K, double *D, int N) {
  return 3 * K[0] - 3 * D[0] + 2 * D[1];
}

// 程式主體
int main() {
  // 設定參數
  int N = 9;
  double high[] = {100, 105, 110, 115, 120, 125, 130, 135, 140};
  double low[] = {95, 90, 85, 80, 75, 70, 65, 60, 55};
  double close[] = {102.5, 107.5, 112.5, 117.5, 122.5, 127.5, 132.5, 137.5, 142.5};

  // 計算 KDJ 指標
  double K_value = K(close, low, high, N);
  double D_value = D(K, N);
  double J_value = J(K, D, N);

  // 輸出 KDJ 指標
  printf("K_value: %.2f\n", K_value);
  printf("D_value: %.2f\n", D_value);
  printf("J_value: %.2f\n", J_value);

  return 0;
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *