fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <limits>
  6. #include <chrono>
  7. #include <thread>
  8. #include <future>
  9. #include <omp.h>
  10.  
  11. using namespace std;
  12.  
  13. double round_to(double value, double precision = 1.0)
  14. {
  15. return round(value / precision) * precision;
  16. }
  17.  
  18. // Forward fill function to fill missing data points with the last valid value
  19. void forward_fill(vector<double>& data) {
  20. for (size_t i = 1; i < data.size(); ++i) {
  21. // If the current value is zero (or NaN), replace it with the last valid value
  22. if (data[i] == 0 || std::isnan(data[i])) {
  23. data[i] = data[i - 1];
  24. }
  25. }
  26. }
  27.  
  28. // Replace infinity values with a specified fallback value (e.g., 0 or last valid value)
  29. void replace_infinity(vector<double>& data, double fallback_value = 90) {
  30. for (size_t i = 1; i < data.size(); ++i) {
  31. if (std::isinf(data[i])) {
  32. // If the value is +∞ or -∞, replace it with fallback_value
  33. if(data[i]<std::numeric_limits<int>::lowest())
  34. data[i] = -fallback_value;
  35. else
  36. data[i] = fallback_value;
  37. }
  38. }
  39. }
  40. // Function to calculate Exponential Moving Average (EMA)
  41. vector<double> ema(const vector<double>& data, size_t n) {
  42. vector<double> result;
  43. double multiplier = 2.0 / (n + 1);
  44.  
  45. // Initialize the first EMA value as the first data point
  46. result.push_back(data[0]);
  47. for (size_t i = 1; i < data.size(); ++i) {
  48. result.push_back((data[i] - result[i - 1]) * multiplier + result[i - 1]);
  49. }
  50.  
  51. return result;
  52. }
  53.  
  54. // Function to calculate the rolling standard deviation (std)
  55. vector<double> rolling_std(const vector<double>& data, size_t n) {
  56. vector<double> result;
  57.  
  58. for (size_t i = 0; i < data.size(); ++i) {
  59. if (i >= n - 1) {
  60. double sum = 0, mean = 0, sq_sum = 0;
  61. for (size_t j = i; j > i - n; --j) {
  62.  
  63. sum += data[j];
  64. sq_sum += data[j] * data[j];
  65.  
  66. }
  67. mean = sum / n;
  68. result.push_back(sqrt(sq_sum / n - mean * mean));
  69.  
  70. }
  71. }
  72.  
  73. return result;
  74. }
  75.  
  76. // Function to calculate the Bollinger Bands and return the result as a vector
  77. vector<double> bollinger(const vector<double>& data, const vector<double>& ema_data, const vector<double>& std_data, size_t n) {
  78.  
  79. vector<double> result(data.size(), 0);
  80. vector<double> result_diff(data.size(), 0);
  81. for (size_t i = 0; i < data.size(); ++i) {
  82. if (i >= n - 1) {
  83. result[i] = (data[i] - ema_data[i]) / std_data[i]; // Standardized difference
  84. }
  85. }
  86. for (size_t i = 1; i < data.size(); ++i) {
  87.  
  88. result_diff[i] = round_to(result[i]-result[i-1], 0.000001); // Standardized difference
  89. }
  90. return result_diff;
  91. }
  92.  
  93.  
  94. void bollinger_exec(const vector<double>& data, const vector<double>& ema_data, const vector<double>& std_data, size_t n, vector<double>& result_diff) {
  95. vector<double> result(data.size(), 0);
  96.  
  97. #pragma omp parallel num_threads(10)
  98. {
  99. #pragma omp parallel for num_threads(10)
  100. for (size_t i = 0; i < data.size(); ++i) {
  101. if (i >= n - 1) {
  102. result[i] = (data[i] - ema_data[i]) / std_data[i]; // Standardized difference
  103. }
  104. }
  105. }
  106.  
  107. result_diff.emplace_back(0);
  108. for (size_t i = 1; i < data.size(); ++i)
  109. {
  110. //result_diff[i] = round_to(result[i]-result[i-1], 0.000001); // Standardized difference
  111. result_diff.emplace_back(round_to(result[i]-result[i-1], 0.000001));
  112. }
  113.  
  114. }
  115.  
  116. // Function to transform data (calculate EMA, std, Bollinger) and apply forward fill and infinity replacement
  117. vector<vector<double>> transform(const vector<double>& data, size_t n) {
  118. // Calculate EMA
  119. vector<double> bollinger_data;
  120. vector<double> ema_data;
  121. vector<double> std_data;
  122. ema_data = ema(data, n);
  123.  
  124. // Calculate rolling standard deviation
  125. std_data = rolling_std(data, n);
  126.  
  127. // Calculate Bollinger Bands
  128. //future<vector<double>> ema_async = async(launch::deferred, ema, ref(data), n);
  129. //future<vector<double>> std_async = async(launch::deferred, rolling_std, ref(data), n);
  130.  
  131. //vector<double> ema_data = ema_async.get();
  132. //vector<double> std_data = std_async.get();
  133. //vector<double> bollinger_data = bollinger(data, ema_data, std_data, n);
  134. bollinger_exec(data, ema_data, std_data, n, bollinger_data);
  135.  
  136. //future<void> boll_async = async(launch::deferred,bollinger_exec, ref(data), ref(ema_data), ref(std_data), n,ref(bollinger_data));
  137. //boll_async.wait();
  138. //thread t = thread(bollinger_exec, ref(data), ref(ema_data), ref(std_data), n, ref(bollinger_data));
  139. //t.join();
  140. // Forward fill the Bollinger Bands, EMA, and std data to handle any missing values
  141. //forward_fill(ema_data);
  142. //forward_fill(std_data);
  143. forward_fill(bollinger_data);
  144. //thread t_forward_fill = thread(forward_fill, ref(bollinger_data));
  145.  
  146. //thread t_replace_infinity = thread(replace_infinity, ref(bollinger_data), n);
  147.  
  148. // Replace infinity values with fallback_value (0 for simplicity)
  149. //replace_infinity(ema_data);
  150. //replace_infinity(std_data);
  151. replace_infinity(bollinger_data);
  152.  
  153. //t_forward_fill.join();
  154. //t_replace_infinity.join();
  155.  
  156. //future<void> t_forward_fill = async(launch::deferred,forward_fill, ref(bollinger_data));
  157. //t_forward_fill.wait();
  158. //future<void> t_replace_infinity = async(launch::deferred, replace_infinity, ref(bollinger_data), n);
  159. //t_replace_infinity.wait();
  160. // Combine the results into a 2D vector (for simplicity)
  161. //vector<vector<double>> result;
  162. vector<vector<double>> result(data.size(), vector<double>(4, 0));
  163.  
  164. for (size_t i = 0; i < data.size(); ++i) {
  165. result[i][0] = data[i]; // Original Data
  166. result[i][1] = ema_data[i]; // EMA
  167. result[i][2] = std_data[i]; // Rolling STD
  168. result[i][3] = bollinger_data[i]; // Bollinger Bands
  169. }
  170. return result;
  171.  
  172. }
  173.  
  174. int main() {
  175. // Sample data (example: closing prices)
  176. //vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
  177.  
  178.  
  179.  
  180. // Introduce some infinity values for testing
  181. //data[3] = std::numeric_limits<double>::infinity(); // +∞ at index 3
  182. //data[5] = -std::numeric_limits<double>::infinity(); // -∞ at index 5
  183.  
  184. for (size_t i = 0; i < 100; i++)
  185. {
  186. size_t n = 3; // Period for EMA and Bollinger
  187. //replace_infinity(data);
  188. // Call the transform function
  189. auto t1 = chrono::high_resolution_clock::now();
  190. vector<double> data;
  191.  
  192. for (size_t i = 1; i <= 50; i++)
  193. {
  194. double ii(i);
  195. data.emplace_back(ii);
  196. }
  197.  
  198. vector<vector<double>> result = transform(data, n);
  199. auto t2 = chrono::high_resolution_clock::now();
  200.  
  201. chrono::duration<double, std::milli> ms_double = t2 - t1;
  202. std::cout << ms_double.count() << "ms\n"<<endl;
  203. std::cout << result.size() <<"n"<<endl;
  204.  
  205. // Output the results
  206.  
  207. /*for (size_t i = n+1; i < result.size(); ++i) {
  208.   cout << "Data: " << result[i][0]
  209.   << " EMA: " << result[i][1]
  210.   << " Std: " << result[i][2]
  211.   << " Bollinger: " << result[i][3] << endl;
  212.   }*/
  213. }
  214.  
  215. system("pause");
  216. return 0;
  217. }
  218.  
Success #stdin #stdout #stderr 0.01s 5280KB
stdin
Standard input is empty
stdout
0.010652ms

50n
0.005646ms

50n
0.00453ms

50n
0.004265ms

50n
0.004138ms

50n
0.004164ms

50n
0.004092ms

50n
0.004059ms

50n
0.00408ms

50n
0.004064ms

50n
0.004076ms

50n
0.004047ms

50n
0.004063ms

50n
0.004051ms

50n
0.004112ms

50n
0.00406ms

50n
0.004039ms

50n
0.004106ms

50n
0.004084ms

50n
0.004026ms

50n
0.004054ms

50n
0.004085ms

50n
0.00404ms

50n
0.004044ms

50n
0.004083ms

50n
0.004052ms

50n
0.004039ms

50n
0.004063ms

50n
0.004039ms

50n
0.004052ms

50n
0.004075ms

50n
0.004058ms

50n
0.00406ms

50n
0.00404ms

50n
0.004082ms

50n
0.004043ms

50n
0.00408ms

50n
0.004045ms

50n
0.004051ms

50n
0.00407ms

50n
0.004045ms

50n
0.004043ms

50n
0.004083ms

50n
0.00405ms

50n
0.004074ms

50n
0.004073ms

50n
0.004048ms

50n
0.004057ms

50n
0.00405ms

50n
0.004047ms

50n
0.004083ms

50n
0.004113ms

50n
0.004048ms

50n
0.004146ms

50n
0.00406ms

50n
0.004026ms

50n
0.004037ms

50n
0.004037ms

50n
0.004066ms

50n
0.004102ms

50n
0.004067ms

50n
0.004087ms

50n
0.004084ms

50n
0.004068ms

50n
0.004022ms

50n
0.004073ms

50n
0.004075ms

50n
0.004044ms

50n
0.004095ms

50n
0.00406ms

50n
0.004067ms

50n
0.00405ms

50n
0.004078ms

50n
0.004062ms

50n
0.004043ms

50n
0.004043ms

50n
0.004038ms

50n
0.00404ms

50n
0.004041ms

50n
0.004061ms

50n
0.004037ms

50n
0.00405ms

50n
0.004059ms

50n
0.00405ms

50n
0.004048ms

50n
0.004071ms

50n
0.004066ms

50n
0.004069ms

50n
0.004023ms

50n
0.004048ms

50n
0.004078ms

50n
0.004053ms

50n
0.004021ms

50n
0.004071ms

50n
0.004053ms

50n
0.004065ms

50n
0.004058ms

50n
0.004035ms

50n
0.004043ms

50n
0.004055ms

50n
stderr
sh: 1: pause: not found