-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc_indicators.php
892 lines (690 loc) · 24.5 KB
/
func_indicators.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<?php
/**
* @author Ebo Eppenga
* @copyright 2022
*
* GoldStar Buy and Sell bot based on signals from for example TradeView
* or any other platform using PHP Bybit API from zhouaini528.
*
* func_indicators.php
* Calculate Technical indicators and some helper functions to
* clean up the data generated.
*
**/
/*===========================================================================*/
/* Helper functions */
/*===========================================================================*/
// Function to remap an array (OK)
function remapArray($data, $remap) {
// Remap the array
$new = [];
foreach ($data as $key => $value) {
$new[$key + ($remap - 1)] = $value;
}
return $new;
}
/*===========================================================================*/
// Function to remove empty values (OK)
function removeEmptyValues($array) {
foreach ($array as $key => $value) {
if (empty($value)) {
unset($array[$key]);
} else {
break;
}
}
return $array;
}
/*===========================================================================*/
// Function to remove arrays that have an empty element
function removeEmptyArrays($array) {
foreach ($array as $key => $subArray) {
// Filter the sub-array to remove empty elements
$filteredArray = array_filter($subArray);
// Check if the filtered array has fewer elements than the original sub-array
if (count($filteredArray) < count($subArray)) {
// Unset the sub-array if it contained any empty element
unset($array[$key]);
}
}
return $array;
}
/*===========================================================================*/
// Function to find the index of the first non-null value
function firstvalueArray($data) {
foreach ($data as $key => $value) {
if ($value !== null) {
// Return the key (index) of the first non-null value
return $key;
}
}
// Return null if no non-null value is found
return null;
}
/*===========================================================================*/
// Function to show the values of a list
function showValues($data, $reverse = false) {
if ($reverse) {$data = array_reverse($data);}
foreach ($data as $value) {
echo $value . "\n";
}
}
/*===========================================================================*/
/* Indicators */
/*===========================================================================*/
// Function to calculate Simple Moving Average (OK)
function calcSMA($data, $period) {
// Initialize variables
$smaValues = [];
$dataCount = count($data);
$startIndex = array_key_first($data);
$endLoop = $dataCount + $startIndex;
// Do calculation
for ($i = $startIndex; $i < $endLoop; $i++) {
if ($i >= ($period + $startIndex) - 1) {
$sum = 0;
for ($j = $i; $j > $i - $period; $j--) {
$sum += $data[$j];
}
$sma = $sum / $period;
$smaValues[$i] = $sma;
}
}
return $smaValues;
}
/*===========================================================================*/
// Function to calculate Exponential Moving Average (OK)
function calcEMA($data, $period) {
$smooth = 2;
$dataCount = count($data);
$startIndex = array_key_first($data);
$endLoop = $dataCount + $startIndex;
// Calculate the initial SMA for the first EMA value
$sma = 0;
for ($i = $startIndex; $i < ($period + $startIndex); $i++) {
$sma += $data[$i];
}
$sma /= $period;
$ema[($period + $startIndex) - 1] = $sma;
// Calculate the multiplier for weighting the EMA
$multiplier = $smooth / ($period + 1);
// Calculate EMA for each data point after the initial SMA period
for ($i = ($period + $startIndex); $i < $endLoop; $i++) {
$emaToday = ($data[$i] - $ema[$i - 1]) * $multiplier + $ema[$i - 1];
$ema[] = $emaToday;
}
return $ema;
}
/*===========================================================================*/
function calcWMA($data, $period) {
// Initialize variables
$wma = [];
$dataCount = count($data);
$startIndex = array_key_first($data);
$endLoop = $dataCount + $startIndex - $period;
// Fill in non applicables with null
for ($i = 0; $i <= ($period - 2); $i++) {
$wma[$i] = null;
}
// Do calculation
for ($i = $startIndex; $i <= $endLoop; $i++) {
$weightedSum = 0;
$weightSum = 0;
for ($j = 0; $j < $period; $j++) {
// The weight increases as we get closer to the current date
$weight = $period - $j;
$weightedSum += $data[$i + $j] * $weight;
$weightSum += $weight;
}
$wma[$i + ($period - 1)] = $weightedSum / $weightSum;
}
return $wma;
}
/*===========================================================================*/
// Function to calculate the Volume Weighted Moving Average (VWMA) (startiondex enzo)
function calcVWMA($data, $volume, $period) {
// Initialize variables
$vwma = [];
$dataCount = count($data);
$startIndex = array_key_first($data);
$endLoop = $dataCount + $startIndex;
// Do calculation
for ($i = $startIndex; $i < $endLoop; $i++) {
if ($i >= $period - 1) {
$totalVolume = 0;
$volumeWeightedPrice = 0;
for ($j = 0; $j < $period; $j++) {
$totalVolume += $volume[$i - $j];
$volumeWeightedPrice += $data[$i - $j] * $volume[$i - $j];
}
$vwma[$i] = $volumeWeightedPrice / $totalVolume;
} else {
// Not enough data to calculate VWMA
$vwma[$i] = null;
}
}
return $vwma;
}
/*===========================================================================*/
// Function to calculate the Welles Wilder's Moving Average (WWMA)
function calcWWMA($data, $period) {
// Initialize variables
$wwma = [];
$startIndex = array_key_first($data);
$dataCount = count($data);
$endLoop = $dataCount + $startIndex;
// The first value in the smoothed data is just the average of the first 'period' data points
$sum = 0;
for ($i = $startIndex; $i < ($period + $startIndex); $i++) {
$sum += $data[$i];
}
$wwma[($period + $startIndex) - 1] = $sum / $period;
// Apply Welles Wilder's smoothing method for the rest of the data points
for ($i = ($period + $startIndex); $i < $endLoop; $i++) {
$wwma[$i] = (($wwma[$i - 1] * ($period - 1)) + $data[$i]) / $period;
}
return $wwma;
}
/*===========================================================================*/
// Function to calculate the Average True Range
function calcATR($highs, $lows, $closes, $period) {
// Initialize variables
$trueRanges = [];
$atrValues = [];
$startIndex = array_key_first($highs);
$dataCount = count($highs);
$endLoop = $dataCount + $startIndex;
// Calculate True Range for each day
for ($i = $startIndex; $i < $endLoop; $i++) {
if ($i == $startIndex) {
// For the first period, true range is simply high - low
$trueRanges[] = $highs[$i] - $lows[$i];
} else {
// For subsequent periods, calculate true range as the maximum of the following:
$tr1 = $highs[$i] - $lows[$i];
$tr2 = abs($highs[$i] - $closes[$i - 1]);
$tr3 = abs($lows[$i] - $closes[$i - 1]);
$trueRanges[] = max($tr1, $tr2, $tr3);
}
}
// Initial ATR is the average of the first 'period' true ranges
$initialATR = array_sum(array_slice($trueRanges, 0, $period)) / $period;
$atrValues[] = $initialATR;
// Calculate subsequent ATR values
for ($i = ($period + $startIndex); $i < $endLoop; $i++) {
$currentATR = (($atrValues[$i - $period] * ($period - 1)) + $trueRanges[$i]) / $period;
$atrValues[] = $currentATR;
}
// Remap array
$atrValues = remapArray($atrValues, $period);
return $atrValues;
}
/*===========================================================================*/
function calcNATR($highs, $lows, $closes, $period) {
// Calculate ATR using the provided calcATR function
$atrValues = calcATR($highs, $lows, $closes, $period);
// Initialize variables
$natrValues = [];
$startIndex = array_key_first($atrValues);
$dataCount = count($atrValues);
$endLoop = $dataCount + $startIndex;
// Calculate NATR for each period where ATR is available
for ($i = $startIndex; $i < $endLoop; $i++) {
$natrValues[] = ($atrValues[$i] / $closes[$i]) * 100;
}
// Remap array
$natrValues = remapArray($natrValues, $period);
return $natrValues;
}
/*===========================================================================*/
// Function to calculate Standard Deviation (OK)
function calcStdDev($data) {
$mean = array_sum($data) / count($data);
$sumOfSquares = 0;
foreach ($data as $value) {
$sumOfSquares += pow($value - $mean, 2);
}
// Using count($data) - 1 for Bessel's correction
$variance = $sumOfSquares / (count($data) - 1);
$standardDeviation = sqrt($variance);
return $standardDeviation;
}
/*===========================================================================*/
// Function to calculate Mean Deviation (OK)
function calcMeanDev($data) {
$sum = 0;
$n = count($data);
// Calculate the mean average
foreach ($data as $value) {
$sum += $value;
}
$mean = $sum / $n;
// Calculate the absolute deviations from the mean
$deviations = array_map(function ($value) use ($mean) {
return abs($value - $mean);
}, $data);
// Calculate the mean deviation
$meanDeviation = array_sum($deviations) / $n;
return $meanDeviation;
}
/*===========================================================================*/
// Function to calculate Relative Strength Index (OK)
function calcRSI($data, $period) {
// Set variables
$rsi = array();
$gains = 0;
$losses = 0;
// Do calculation
for ($i = 1; $i < count($data); $i++) {
$change = $data[$i] - $data[$i - 1];
if ($change > 0) {
$gains += $change;
} else {
$losses -= $change;
}
if ($i >= $period) {
// Average gains and losses
$avgGain = $gains / $period;
$avgLoss = $losses / $period;
// Calculate RS
$rs = $avgLoss == 0 ? 100 : $avgGain / $avgLoss;
// Calculate RSI
$rsi[$i] = 100 - (100 / (1 + $rs));
// Adjust gains and losses for next iteration
if ($i + 1 < count($data)) {
$nextChange = $data[$i + 1] - $data[$i];
if ($nextChange > 0) {
$gains = $avgGain * ($period - 1) + $nextChange;
$losses = $avgLoss * ($period - 1);
} else {
$gains = $avgGain * ($period - 1);
$losses = $avgLoss * ($period - 1) - $nextChange;
}
}
}
}
// Remap array
//$rsi = remapArray($rsi, $period);
return $rsi;
}
/*===========================================================================*/
// Function to calculate Momentum (OK)
function calcMomentum($data, $period) {
$momentumValues = [];
foreach ($data as $index => $value) {
if ($index >= $period) {
// Calculate momentum as the difference between current value and the value $period steps ago.
$momentum = $value - $data[$index - $period];
} else {
// If the period exceeds the index, set momentum to null
$momentum = null;
}
$momentumValues[] = $momentum;
}
// Remove empty values
$momentumValues = removeEmptyValues($momentumValues);
return $momentumValues;
}
/*===========================================================================*/
// Function to calculate MACD (OK)
function calcMACD($data, $fastPeriod, $slowPeriod, $signalPeriod) {
// Calculate fast EMA
$fastEMA = calcEMA($data, $fastPeriod);
// Calculate slow EMA
$slowEMA = calcEMA($data, $slowPeriod);
// Initialize MACD array
$macd = [];
// Calculate MACD values
for ($i = 0; $i < count($data); $i++) {
if (!isset($fastEMA[$i]) || !isset($slowEMA[$i])) {
// Ensure both EMAs are set for the current index
$macd[$i] = null;
} else {
$macd[$i] = $fastEMA[$i] - $slowEMA[$i];
}
}
// Calculate Signal line (EMA of MACD)
$signalLine = calcEMA($macd, $signalPeriod);
// Initialize histogram array
$histogram = [];
// Calculate Histogram values
for ($i = 0; $i < count($macd); $i++) {
if (!isset($macd[$i]) || !isset($signalLine[$i])) {
// Ensure both MACD and Signal line are set for the current index
$histogram[$i] = null;
} else {
$histogram[$i] = $macd[$i] - $signalLine[$i];
}
}
// Combine MACD, Signal line, and Histogram into a single array
$result = [];
for ($i = 0; $i < count($data); $i++) {
$result[$i] = [
'macd' => $macd[$i],
'signal' => $signalLine[$i],
'histogram' => $histogram[$i]
];
}
// Unset arrays with an emtpy element
$result = removeEmptyArrays($result);
return $result;
}
/*===========================================================================*/
// Function to calculate CCI (Commodity Channel Index) (OK)
function calcCCI($high, $low, $close, $period) {
$tp = array(); // Array to store Typical Prices
$cci = array(); // Array to store CCI values
// Calculate Typical Price for each day
for ($i = 0; $i < count($high); $i++) {
$tp[$i] = ($high[$i] + $low[$i] + $close[$i]) / 3;
}
// Calculate CCI for each day where enough data is available
for ($i = $period - 1; $i < count($high); $i++) {
// Get the slice of TP for the given period
$tpSlice = array_slice($tp, $i - $period + 1, $period);
// Calculate the SMA for the TP slice
$smaTP = end(calcSMA($tpSlice, $period));
// Calculate the Mean Deviation
$meanDev = calcMeanDev($tpSlice);
// Calculate CCI
$cci[$i] = ($tp[$i] - $smaTP) / (0.015 * $meanDev);
}
return $cci;
}
/*===========================================================================*/
// Function to calculate AO (Awesome Oscillator) (OK)
function calcAO($highs, $lows, $shortPeriod, $longPeriod) {
// Calculate midpoints for each period
$midpoints = array();
for ($i = 0; $i < count($highs); $i++) {
$midpoints[] = ($highs[$i] + $lows[$i]) / 2;
}
// Calculate SMA for the short and long periods
$shortSMA = calcSMA($midpoints, $shortPeriod);
$longSMA = calcSMA($midpoints, $longPeriod);
$aoValues = array();
$startIndex = $longPeriod - 1;
for ($i = $startIndex; $i < count($highs); $i++) {
if (isset($longSMA[$i])) {
$aoValues[] = $shortSMA[$i] - $longSMA[$i];
}
}
return $aoValues;
}
/*===========================================================================*/
// Function to calculate the Williams Percentage Range (OK)
function calcWilliamsR($highs, $lows, $closes, $period) {
// Initialize variable
$williamsR = array();
// Do calculation
for ($i = 0; $i < count($closes); $i++) {
// Check if we have enough data to calculate Williams %R
if ($i >= $period - 1) {
$highPeriod = array_slice($highs, $i - $period + 1, $period);
$lowPeriod = array_slice($lows, $i - $period + 1, $period);
$close = $closes[$i];
$highestHigh = max($highPeriod);
$lowestLow = min($lowPeriod);
// Avoid division by zero
if ($highestHigh != $lowestLow) {
$williamsRValue = (($highestHigh - $close) / ($highestHigh - $lowestLow)) * -100;
} else {
$williamsRValue = 0; // Assign a default or error value
}
$williamsR[] = $williamsRValue;
} else {
// Not enough data to compute Williams %R
$williamsR[] = null;
}
}
// Remove empty values
$williamsR = removeEmptyValues($williamsR);
return $williamsR;
}
/*===========================================================================*/
// Function to calculate the Bull and Bear Power (OK)
function calcBullBear($highs, $lows, $closes, $period) {
// Calculate the EMA of the closing prices.
$ema = calcEMA($closes, $period);
$bullPower = [];
$bearPower = [];
$bullbearPower = [];
// Calculating Bull and Bear Power
for ($i = 0; $i < count($highs); $i++) {
if (isset($ema[$i])) {
$bullPower[$i] = $highs[$i] - $ema[$i];
$bearPower[$i] = $lows[$i] - $ema[$i];
$bullbearPower[$i] = $bullPower[$i] + $bearPower[$i];
} else {
// In case EMA is not available for the initial period
$bullPower[$i] = null;
$bearPower[$i] = null;
$bullbearPower[$i] = null;
}
}
// Combine all powers into a single array
$result = [];
for ($i = 0; $i < count($highs); $i++) {
$result[$i] = [
'bull' => $bullPower[$i],
'bear' => $bearPower[$i],
'bullbear' => $bullbearPower[$i]
];
}
// Unset arrays with an emtpy element
$result = removeEmptyArrays($result);
return $result;
}
/*===========================================================================*/
// Function to calculate the UO (Ultimate Oscillator) (OK)
function calcUO($highs, $lows, $closes, $period1, $period2, $period3) {
// Initialize variables
$bp = []; // Buying Pressure
$tr = []; // True Range
$uoValues = []; // Array to store Ultimate Oscillator values
$len = count($closes);
// Calculate BP and TR for each day
for ($i = 0; $i < $len; $i++) {
$prevClose = $i === 0 ? $closes[$i] : $closes[$i - 1];
$bp[] = $closes[$i] - min($lows[$i], $prevClose);
$tr[] = max($highs[$i], $prevClose) - min($lows[$i], $prevClose);
}
// Function to calculate the sum of N elements up to index i of an array
$sumLastN = function($arr, $n, $i) {
$start = max(0, $i - $n + 1);
return array_sum(array_slice($arr, $start, $n));
};
// Calculate UO for each possible period starting from the largest period
for ($i = max($period1, $period2, $period3) - 1; $i < $len; $i++) {
$avg1 = $sumLastN($bp, $period1, $i) / $sumLastN($tr, $period1, $i);
$avg2 = $sumLastN($bp, $period2, $i) / $sumLastN($tr, $period2, $i);
$avg3 = $sumLastN($bp, $period3, $i) / $sumLastN($tr, $period3, $i);
$uo = 100 * ((4 * $avg1) + (2 * $avg2) + $avg3) / 7;
$uoValues[$i] = $uo;
}
return $uoValues;
}
/*===========================================================================*/
// Function to calculate Stochastic %K Oscillator (OK)
function calcStochKD($highs, $lows, $closes, $periodD, $smoothK, $smoothD) {
// Initialize arrays for %K and %D
$kValues = array();
$dValues = array();
// Ensure input arrays are of the same length
if (count($highs) !== count($lows) || count($lows) !== count($closes)) {
return ['error' => 'Input arrays must be of the same length'];
}
// Calculate %K values
for ($i = 0; $i < count($closes); $i++) {
if ($i >= $periodD - 1) {
$highestHigh = max(array_slice($highs, $i - $periodD + 1, $periodD));
$lowestLow = min(array_slice($lows, $i - $periodD + 1, $periodD));
$k = 100 * ($closes[$i] - $lowestLow) / ($highestHigh - $lowestLow);
$kValues[] = $k;
}
}
// Apply smoothing to %K
$kValuesSmoothed = calcSMA($kValues, $smoothK);
// Calculate %D values
$dValues = calcSMA($kValuesSmoothed, $smoothD);
// Combine all powers into a single array
$result = [];
for ($i = 0; $i < count($highs); $i++) {
$result[$i + ($periodD - 1)] = [
'k' => $kValuesSmoothed[$i],
'd' => $dValues[$i],
];
}
// Unset arrays with an emtpy element
$result = removeEmptyArrays($result);
// Return the %K and %D arrays
return $result;
}
/*===========================================================================*/
// Function to calculate Stochastic RSI (OK)
function calcStochRSI($closes, $periodRSI, $periodStoch, $smoothK, $smoothD) {
// Step 1: Calculate RSI
$rsiValues = calcRSI($closes, $periodRSI);
// Step 2: Calculate Stochastic RSI
$stochRSIValues = [];
for ($i = 0; $i < count($rsiValues); $i++) {
if ($i < $periodStoch - 1) {
// Not enough data to calculate Stochastic RSI
$stochRSIValues[] = null;
continue;
}
$rsiSlice = array_slice($rsiValues, $i - $periodStoch + 1, $periodStoch);
$lowestRSI = min($rsiSlice);
$highestRSI = max($rsiSlice);
$currentRSI = $rsiValues[$i + 14];
if ($highestRSI - $lowestRSI == 0) {
$stochRSIValues[] = 0;
} else {
$stochRSI = ($currentRSI - $lowestRSI) / ($highestRSI - $lowestRSI);
$stochRSIValues[] = $stochRSI;
}
}
// Smooth the StochRSI (K line) and Calculate the D Line
$kValuesSmoothed = calcSMA($stochRSIValues, $smoothK);
$dValuesSmoothed = calcSMA($kValuesSmoothed, $smoothD);
// Combine K and D values into a single array
$counter = 0;
$startin = max($periodRSI, $periodStoch) + max($smoothK, $smoothD) - 1;
$result = [];
foreach ($kValuesSmoothed as $i => $kValue) {
if (isset($dValuesSmoothed[$i])) {
$result[$counter + $startin] = [
'k' => $kValue * 100,
'd' => $dValuesSmoothed[$i] * 100,
];
}
$counter++;
}
// Return the %K and %D arrays
return $result;
}
/*===========================================================================*/
// Function to calculate Average Direction Index ADX (?)
function calcADX($highs, $lows, $closes, $period) {
// Initialize variables
$dataCount = count($closes);
$endLoop = $dataCount;
$plusDM = $minusDM = $TR = $plusDI = $minusDI = $DX = [];
// Calculate +DM, -DM, and TR
for ($i = 1; $i < $endLoop; $i++) {
$plusDM[$i] = $highs[$i] - $highs[$i - 1];
$minusDM[$i] = $lows[$i - 1] - $lows[$i];
if ($plusDM[$i] < 0 || $plusDM[$i] < $minusDM[$i]) $plusDM[$i] = 0;
if ($minusDM[$i] < 0 || $minusDM[$i] < $plusDM[$i]) $minusDM[$i] = 0;
$TR[$i] = max($highs[$i] - $lows[$i], abs($highs[$i] - $closes[$i - 1]), abs($lows[$i] - $closes[$i - 1]));
}
// Calculate Smoothed +DM, -DM, and TR
$smoothedPlusDM = calcWWMA($plusDM, $period);
$smoothedMinusDM = calcWWMA($minusDM, $period);
$smoothedTR = calcWWMA($TR, $period);
// Calculate +DI and -DI
for ($i = $period; $i < $endLoop; $i++) {
$plusDI[$i] = ($smoothedPlusDM[$i] / $smoothedTR[$i]) * 100;
$minusDI[$i] = ($smoothedMinusDM[$i] / $smoothedTR[$i]) * 100;
$DX[$i] = abs($plusDI[$i] - $minusDI[$i]) / ($plusDI[$i] + $minusDI[$i]) * 100;
}
// Calculate ADX
$ADX = calcEMA($DX, $period);
// Combine all powers into a single array
$result = [];
$startIndex = array_key_first($ADX);
for ($i = $startIndex; $i < $dataCount; $i++) {
$result[$i] = [
'ADX' => $ADX[$i],
'plusDI' => $plusDI[$i],
'minusDI' => $minusDI[$i]
];
}
return $result;
}
/*===========================================================================*/
// Function to calculate Hull Moving Average (?)
function calcHull($data, $period) {
$halfPeriod = intval($period / 2);
$sqrtPeriod = intval(sqrt($period));
$WMA_half = calcWMA($data, $halfPeriod);
$WMA_full = calcWMA($data, $period);
// Find first real values
$unsetIndex = max(firstvalueArray($WMA_half), firstvalueArray($WMA_full));
// Balance the arrays
for ($i = 0; $i < ($unsetIndex); $i++) {
unset($WMA_half[$i]);
unset($WMA_full[$i]);
}
//echo "WMA Half\n";
//print_r($WMA_half);
//echo "WMA Full\n";
//print_r($WMA_full);
// Calculate the difference and then the Hull Moving Average
$Difference = array_map(function ($half, $full) {
return 2 * $half - $full;
}, $WMA_half, $WMA_full);
//echo "Difference:\n";
//print_r($Difference);
// Calculate the final HMA
$HMA = calcWMA($Difference, $sqrtPeriod);
// Remove empty values
$HMA = removeEmptyValues($HMA);
// Remap array
$HMA = remapArray($HMA, $period);
return $HMA;
}
/*===========================================================================*/
// Function to check if previous value was lower (default) or higher (OK)
function checkHL($data, $item = "", $invert = false) {
// Initialize variables
$check = false;
// Check for item
if (empty($item)) {
$lastValue = end($data);
$slastValue = end(array_slice($data, 0, -1));
} else {
$lastValue = end($data)[$item];
$slastValue = end(array_slice($data, 0, -1))[$item];
}
// Check
if ($lastValue >= $slastValue) {
$check = true;
} else {
$check = false;
}
// Invert
if ($invert) {
$check = !$check;
}
return $check;
}
/*===========================================================================*/
/* TO DO */
/*===========================================================================*/
/*
* Function to calculate Volatility
* Function to calculate Rate Of Change
* Function to calculate Change percentage
*/
?>