fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define MAX_DAYS 1000
  6.  
  7. // Fungsi untuk menghitung Moving Average secara iteratif
  8. void moving_average_iterative(double prices[], double ma[], int days, int period) {
  9. for (int i = 0; i <= days - period; i++) {
  10. double sum = 0.0;
  11. for (int j = 0; j < period; j++) {
  12. sum += prices[i + j];
  13. }
  14. ma[i] = sum / period;
  15. }
  16. }
  17.  
  18. // Fungsi untuk menghitung Moving Average secara rekursif
  19. void moving_average_recursive(double prices[], double ma[], int days, int period, int index) {
  20. if (index > days - period) return; // Basis kasus
  21. double sum = 0.0;
  22. for (int j = 0; j < period; j++) {
  23. sum += prices[index + j];
  24. }
  25. ma[index] = sum / period;
  26. moving_average_recursive(prices, ma, days, period, index + 1); // Rekursi
  27. }
  28.  
  29. // Fungsi untuk menghitung profit dari strategi trading
  30. double calculate_profit(double prices[], double ma[], int days) {
  31. double profit = 0.0;
  32. for (int i = 1; i < days; i++) {
  33. if (prices[i] > ma[i - 1]) {
  34. profit += prices[i] - prices[i - 1]; // Buy signal
  35. } else if (prices[i] < ma[i - 1]) {
  36. profit -= prices[i - 1] - prices[i]; // Sell signal
  37. }
  38. }
  39. return profit;
  40. }
  41.  
  42. int main() {
  43. double prices[MAX_DAYS];
  44. int days = MAX_DAYS;
  45. int period = 5; // Periode MA
  46.  
  47. // Menghasilkan data harga acak
  48. srand(time(NULL));
  49. for (int i = 0; i < days; i++) {
  50. prices[i] = 100 + (rand() % 100); // Harga antara 100 dan 199
  51. }
  52.  
  53. double ma_iterative[MAX_DAYS] = {0};
  54. double ma_recursive[MAX_DAYS] = {0};
  55.  
  56. // Mengukur waktu untuk fungsi iteratif
  57. clock_t start_iterative = clock();
  58. moving_average_iterative(prices, ma_iterative, days, period);
  59. clock_t end_iterative = clock();
  60. double time_iterative = (double)(end_iterative - start_iterative) / CLOCKS_PER_SEC;
  61.  
  62. // Mengukur waktu untuk fungsi rekursif
  63. clock_t start_recursive = clock();
  64. moving_average_recursive(prices, ma_recursive, days, period, 0);
  65. clock_t end_recursive = clock();
  66. double time_recursive = (double)(end_recursive - start_recursive) / CLOCKS_PER_SEC;
  67.  
  68. // Menghitung profit
  69. double profit_iterative = calculate_profit(prices, ma_iterative, days);
  70. double profit_recursive = calculate_profit(prices, ma_recursive, days);
  71.  
  72. // Menampilkan beberapa nilai harga
  73. printf("Prices (first 100 days):\n");
  74. for (int i = 0; i < 100; i++) {
  75. printf("Day %d: %.2f\n", i + 1, prices[i]);
  76. }
  77.  
  78. // Menampilkan hasil
  79. printf("\nProfit (Iterative): %.2f\n", profit_iterative);
  80. printf("Profit (Recursive): %.2f\n", profit_recursive);
  81.  
  82. // Menampilkan waktu eksekusi
  83. printf("\nExecution Time (Iterative): %.6f seconds\n", time_iterative);
  84. printf("Execution Time (Recursive): %.6f seconds\n", time_recursive);
  85.  
  86. FILE *gnuplotPipe = popen("gnuplot -persistent", "w");
  87.  
  88. if (gnuplotPipe) {
  89.  
  90. fprintf(gnuplotPipe, "set title 'Prices and Moving Average'\n");
  91.  
  92. fprintf(gnuplotPipe, "set xlabel 'Days'\n");
  93.  
  94. fprintf(gnuplotPipe, "set ylabel 'Price'\n");
  95.  
  96. fprintf(gnuplotPipe, "plot 'data.txt' using 1:2 with lines title 'Prices', '' using 1:3 with lines title 'Moving Average'\n");
  97.  
  98. pclose(gnuplotPipe);
  99.  
  100. } else {
  101.  
  102. fprintf(stderr, "Could not open gnuplot.\n");
  103.  
  104. }
  105. return 0;
  106. }
Success #stdin #stdout #stderr 0.01s 5284KB
stdin
Standard input is empty
stdout
Prices (first 100 days):
Day 1: 153.00
Day 2: 175.00
Day 3: 111.00
Day 4: 173.00
Day 5: 118.00
Day 6: 175.00
Day 7: 148.00
Day 8: 150.00
Day 9: 125.00
Day 10: 123.00
Day 11: 118.00
Day 12: 158.00
Day 13: 109.00
Day 14: 110.00
Day 15: 127.00
Day 16: 191.00
Day 17: 149.00
Day 18: 163.00
Day 19: 123.00
Day 20: 156.00
Day 21: 196.00
Day 22: 191.00
Day 23: 174.00
Day 24: 113.00
Day 25: 151.00
Day 26: 111.00
Day 27: 125.00
Day 28: 161.00
Day 29: 114.00
Day 30: 145.00
Day 31: 109.00
Day 32: 119.00
Day 33: 172.00
Day 34: 120.00
Day 35: 192.00
Day 36: 191.00
Day 37: 148.00
Day 38: 140.00
Day 39: 193.00
Day 40: 173.00
Day 41: 116.00
Day 42: 112.00
Day 43: 132.00
Day 44: 125.00
Day 45: 174.00
Day 46: 111.00
Day 47: 116.00
Day 48: 123.00
Day 49: 174.00
Day 50: 191.00
Day 51: 131.00
Day 52: 122.00
Day 53: 135.00
Day 54: 105.00
Day 55: 136.00
Day 56: 186.00
Day 57: 168.00
Day 58: 161.00
Day 59: 148.00
Day 60: 134.00
Day 61: 158.00
Day 62: 157.00
Day 63: 105.00
Day 64: 131.00
Day 65: 130.00
Day 66: 198.00
Day 67: 174.00
Day 68: 178.00
Day 69: 190.00
Day 70: 167.00
Day 71: 151.00
Day 72: 106.00
Day 73: 131.00
Day 74: 135.00
Day 75: 183.00
Day 76: 105.00
Day 77: 198.00
Day 78: 151.00
Day 79: 128.00
Day 80: 124.00
Day 81: 143.00
Day 82: 111.00
Day 83: 147.00
Day 84: 178.00
Day 85: 116.00
Day 86: 135.00
Day 87: 164.00
Day 88: 137.00
Day 89: 196.00
Day 90: 164.00
Day 91: 123.00
Day 92: 106.00
Day 93: 174.00
Day 94: 129.00
Day 95: 189.00
Day 96: 104.00
Day 97: 179.00
Day 98: 163.00
Day 99: 182.00
Day 100: 121.00

Profit (Iterative): 18.00
Profit (Recursive): 18.00

Execution Time (Iterative): 0.000000 seconds
Execution Time (Recursive): 0.000000 seconds
stderr
sh: 1: gnuplot: not found