-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Volume.cs
2066 lines (1744 loc) · 80.9 KB
/
Volume.cs
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Ooples Finance Stock Indicator Library
// https://ooples.github.io/OoplesFinance.StockIndicators/
//
// Copyright © Franklin Moormann, 2020-2022
// cheatcountry@gmail.com
//
// This library is free software and it uses the Apache 2.0 license
// so if you are going to re-use or modify my code then I just ask
// that you include my copyright info and my contact info in a comment
namespace OoplesFinance.StockIndicators;
public static partial class Calculations
{
/// <summary>
/// Calculates the index of the force.
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="maType">Type of the ma.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
public static StockData CalculateForceIndex(this StockData stockData, MovingAvgType maType = MovingAvgType.ExponentialMovingAverage, int length = 14)
{
List<double> rawForceList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentValue = inputList[i];
double currentVolume = volumeList[i];
double prevValue = i >= 1 ? inputList[i - 1] : 0;
double rawForce = MinPastValues(i, 1, currentValue - prevValue) * currentVolume;
rawForceList.AddRounded(rawForce);
}
var forceList = GetMovingAverageList(stockData, maType, length, rawForceList);
for (int i = 0; i < stockData.Count; i++)
{
double force = forceList[i];
double prevForce1 = i >= 1 ? forceList[i - 1] : 0;
double prevForce2 = i >= 2 ? forceList[i - 2] : 0;
var signal = GetCompareSignal(force - prevForce1, prevForce1 - prevForce2);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Fi", forceList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = forceList;
stockData.IndicatorName = IndicatorName.ForceIndex;
return stockData;
}
/// <summary>
/// Calculates the Money Flow Index
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="inputName">Name of the input.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
public static StockData CalculateMoneyFlowIndex(this StockData stockData, InputName inputName = InputName.TypicalPrice, int length = 14)
{
List<double> mfiList = new();
List<double> posMoneyFlowList = new();
List<double> negMoneyFlowList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, _, volumeList) = GetInputValuesList(inputName, stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentVolume = volumeList[i];
double typicalPrice = inputList[i];
double prevTypicalPrice = i >= 1 ? inputList[i - 1] : 0;
double prevMfi1 = i >= 1 ? mfiList[i - 1] : 0;
double prevMfi2 = i >= 2 ? mfiList[i - 2] : 0;
double rawMoneyFlow = typicalPrice * currentVolume;
double posMoneyFlow = i >= 1 && typicalPrice > prevTypicalPrice ? rawMoneyFlow : 0;
posMoneyFlowList.AddRounded(posMoneyFlow);
double negMoneyFlow = i >= 1 && typicalPrice < prevTypicalPrice ? rawMoneyFlow : 0;
negMoneyFlowList.AddRounded(negMoneyFlow);
double posMoneyFlowTotal = posMoneyFlowList.TakeLastExt(length).Sum();
double negMoneyFlowTotal = negMoneyFlowList.TakeLastExt(length).Sum();
double mfiRatio = negMoneyFlowTotal != 0 ? posMoneyFlowTotal / negMoneyFlowTotal : 0;
double mfi = negMoneyFlowTotal == 0 ? 100 : posMoneyFlowTotal == 0 ? 0 : MinOrMax(100 - (100 / (1 + mfiRatio)), 100, 0);
mfiList.AddRounded(mfi);
var signal = GetRsiSignal(mfi - prevMfi1, prevMfi1 - prevMfi2, mfi, prevMfi1, 80, 20);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Mfi", mfiList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = mfiList;
stockData.IndicatorName = IndicatorName.MoneyFlowIndex;
return stockData;
}
/// <summary>
/// Calculates the Klinger Volume Oscillator
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="maType">Type of the ma.</param>
/// <param name="fastLength">Length of the fast.</param>
/// <param name="slowLength">Length of the slow.</param>
/// <param name="signalLength">Length of the signal.</param>
/// <returns></returns>
public static StockData CalculateKlingerVolumeOscillator(this StockData stockData, MovingAvgType maType = MovingAvgType.ExponentialMovingAverage,
int fastLength = 34, int slowLength = 55, int signalLength = 13)
{
List<double> kvoList = new();
List<double> trendList = new();
List<double> dmList = new();
List<double> cmList = new();
List<double> vfList = new();
List<double> kvoHistoList = new();
List<Signal> signalsList = new();
var (inputList, highList, lowList, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentHigh = highList[i];
double currentLow = lowList[i];
double currentValue = inputList[i];
double currentVolume = volumeList[i];
double prevValue = i >= 1 ? inputList[i - 1] : 0;
double mom = MinPastValues(i, 1, currentValue - prevValue);
double prevTrend = trendList.LastOrDefault();
double trend = mom > 0 ? 1 : mom < 0 ? -1 : prevTrend;
trendList.AddRounded(trend);
double prevDm = dmList.LastOrDefault();
double dm = currentHigh - currentLow;
dmList.AddRounded(dm);
double prevCm = cmList.LastOrDefault();
double cm = trend == prevTrend ? prevCm + dm : prevDm + dm;
cmList.AddRounded(cm);
double temp = cm != 0 ? Math.Abs((2 * (dm / cm)) - 1) : -1;
double vf = currentVolume * temp * trend * 100;
vfList.AddRounded(vf);
}
var ema34List = GetMovingAverageList(stockData, maType, fastLength, vfList);
var ema55List = GetMovingAverageList(stockData, maType, slowLength, vfList);
for (int i = 0; i < stockData.Count; i++)
{
double ema34 = ema34List[i];
double ema55 = ema55List[i];
double klingerOscillator = ema34 - ema55;
kvoList.AddRounded(klingerOscillator);
}
var kvoSignalList = GetMovingAverageList(stockData, maType, signalLength, kvoList);
for (int i = 0; i < stockData.Count; i++)
{
double klingerOscillator = kvoList[i];
double koSignalLine = kvoSignalList[i];
double prevKlingerOscillatorHistogram = kvoHistoList.LastOrDefault();
double klingerOscillatorHistogram = klingerOscillator - koSignalLine;
kvoHistoList.AddRounded(klingerOscillatorHistogram);
var signal = GetCompareSignal(klingerOscillatorHistogram, prevKlingerOscillatorHistogram);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Kvo", kvoList },
{ "KvoSignal", kvoSignalList },
{ "KvoHistogram", kvoHistoList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = kvoList;
stockData.IndicatorName = IndicatorName.KlingerVolumeOscillator;
return stockData;
}
/// <summary>
/// Calculates the on balance volume.
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="maType">Type of the ma.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
public static StockData CalculateOnBalanceVolume(this StockData stockData, MovingAvgType maType = MovingAvgType.ExponentialMovingAverage, int length = 20)
{
List<double> obvList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentValue = inputList[i];
double currentVolume = volumeList[i];
double prevValue = i >= 1 ? inputList[i - 1] : 0;
double prevObv = obvList.LastOrDefault();
double obv = currentValue > prevValue ? prevObv + currentVolume : currentValue < prevValue ? prevObv - currentVolume : prevObv;
obvList.AddRounded(obv);
}
var obvSignalList = GetMovingAverageList(stockData, maType, length, obvList);
for (int i = 0; i < stockData.Count; i++)
{
double obv = obvList[i];
double prevObv = i >= 1 ? obvList[i - 1] : 0;
double obvSig = obvSignalList[i];
double prevObvSig = i >= 1 ? obvSignalList[i - 1] : 0;
var signal = GetCompareSignal(obv - obvSig, prevObv - prevObvSig);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Obv", obvList },
{ "ObvSignal", obvSignalList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = obvList;
stockData.IndicatorName = IndicatorName.OnBalanceVolume;
return stockData;
}
/// <summary>
/// Calculates the index of the negative volume.
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="maType">Type of the ma.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
public static StockData CalculateNegativeVolumeIndex(this StockData stockData, MovingAvgType maType = MovingAvgType.ExponentialMovingAverage, int length = 255)
{
List<double> nviList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentClose = inputList[i];
double currentVolume = volumeList[i];
double prevClose = i >= 1 ? inputList[i - 1] : 0;
double prevVolume = i >= 1 ? volumeList[i - 1] : 0;
double pctChg = CalculatePercentChange(currentClose, prevClose);
double prevNvi = nviList.LastOrDefault();
double nvi = currentVolume >= prevVolume ? prevNvi : prevNvi + (prevNvi * pctChg);
nviList.AddRounded(nvi);
}
var nviSignalList = GetMovingAverageList(stockData, maType, length, nviList);
for (int i = 0; i < stockData.Count; i++)
{
double nvi = nviList[i];
double prevNvi = i >= 1 ? nviList[i - 1] : 0;
double nviSignal = nviSignalList[i];
double prevNviSignal = i >= 1 ? nviSignalList[i - 1] : 0;
var signal = GetCompareSignal(nvi - nviSignal, prevNvi - prevNviSignal);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Nvi", nviList },
{ "NviSignal", nviSignalList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = nviList;
stockData.IndicatorName = IndicatorName.NegativeVolumeIndex;
return stockData;
}
/// <summary>
/// Calculates the index of the positive volume.
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="maType">Type of the ma.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
public static StockData CalculatePositiveVolumeIndex(this StockData stockData, MovingAvgType maType = MovingAvgType.ExponentialMovingAverage, int length = 255)
{
List<double> pviList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentClose = inputList[i];
double currentVolume = volumeList[i];
double prevClose = i >= 1 ? inputList[i - 1] : 0;
double prevVolume = i >= 1 ? volumeList[i - 1] : 0;
double pctChg = CalculatePercentChange(currentClose, prevClose);
double prevPvi = pviList.LastOrDefault();
double pvi = currentVolume <= prevVolume ? prevPvi : prevPvi + (prevPvi * pctChg);
pviList.AddRounded(pvi);
}
var pviSignalList = GetMovingAverageList(stockData, maType, length, pviList);
for (int i = 0; i < stockData.Count; i++)
{
double pvi = pviList[i];
double prevPvi = i >= 1 ? pviList[i - 1] : 0;
double pviSignal = pviSignalList[i];
double prevPviSignal = i >= 1 ? pviSignalList[i - 1] : 0;
var signal = GetCompareSignal(pvi - pviSignal, prevPvi - prevPviSignal);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Pvi", pviList },
{ "PviSignal", pviSignalList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = pviList;
stockData.IndicatorName = IndicatorName.PositiveVolumeIndex;
return stockData;
}
/// <summary>
/// Calculates the Chaikin Money Flow
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
public static StockData CalculateChaikinMoneyFlow(this StockData stockData, int length = 20)
{
List<double> chaikinMoneyFlowList = new();
List<double> tempVolumeList = new();
List<double> moneyFlowVolumeList = new();
List<Signal> signalsList = new();
var (inputList, highList, lowList, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentLow = lowList[i];
double currentHigh = highList[i];
double currentClose = inputList[i];
double moneyFlowMultiplier = currentHigh - currentLow != 0 ?
(currentClose - currentLow - (currentHigh - currentClose)) / (currentHigh - currentLow) : 0;
double prevCmf1 = i >= 1 ? chaikinMoneyFlowList[i - 1] : 0;
double prevCmf2 = i >= 2 ? chaikinMoneyFlowList[i - 2] : 0;
double currentVolume = volumeList[i];
tempVolumeList.AddRounded(currentVolume);
double moneyFlowVolume = moneyFlowMultiplier * currentVolume;
moneyFlowVolumeList.AddRounded(moneyFlowVolume);
double volumeSum = tempVolumeList.TakeLastExt(length).Sum();
double mfVolumeSum = moneyFlowVolumeList.TakeLastExt(length).Sum();
double cmf = volumeSum != 0 ? mfVolumeSum / volumeSum : 0;
chaikinMoneyFlowList.AddRounded(cmf);
var signal = GetCompareSignal(cmf - prevCmf1, prevCmf1 - prevCmf2);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Cmf", chaikinMoneyFlowList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = chaikinMoneyFlowList;
stockData.IndicatorName = IndicatorName.ChaikinMoneyFlow;
return stockData;
}
/// <summary>
/// Calculates the accumulation distribution line.
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="maType">Type of the ma.</param>
/// <param name="length">The length.</param>
/// <returns></returns>
public static StockData CalculateAccumulationDistributionLine(this StockData stockData, MovingAvgType maType = MovingAvgType.ExponentialMovingAverage,
int length = 14)
{
List<double> adlList = new();
List<Signal> signalsList = new();
var (inputList, highList, lowList, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentLow = lowList[i];
double currentHigh = highList[i];
double currentClose = inputList[i];
double currentVolume = volumeList[i];
double moneyFlowMultiplier = currentHigh - currentLow != 0 ?
(currentClose - currentLow - (currentHigh - currentClose)) / (currentHigh - currentLow) : 0;
double moneyFlowVolume = moneyFlowMultiplier * currentVolume;
double prevAdl = adlList.LastOrDefault();
double adl = prevAdl + moneyFlowVolume;
adlList.AddRounded(adl);
}
var adlSignalList = GetMovingAverageList(stockData, maType, length, adlList);
for (int i = 0; i < stockData.Count; i++)
{
var adl = adlList[i];
var prevAdl = i >= 1 ? adlList[i - 1] : 0;
var adlSignal = adlSignalList[i];
var prevAdlSignal = i >= 1 ? adlSignalList[i - 1] : 0;
var signal = GetCompareSignal(adl - adlSignal, prevAdl - prevAdlSignal);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Adl", adlList },
{ "AdlSignal", adlSignalList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = adlList;
stockData.IndicatorName = IndicatorName.AccumulationDistributionLine;
return stockData;
}
/// <summary>
/// Calculates the average money flow oscillator.
/// </summary>
/// <param name="stockData">The stock data.</param>
/// <param name="maType">Type of the ma.</param>
/// <param name="length">The length.</param>
/// <param name="smoothLength">Length of the smooth.</param>
/// <returns></returns>
public static StockData CalculateAverageMoneyFlowOscillator(this StockData stockData, MovingAvgType maType = MovingAvgType.WeightedMovingAverage,
int length = 5, int smoothLength = 3)
{
List<double> chgList = new();
List<double> rList = new();
List<double> kList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
var avgvList = GetMovingAverageList(stockData, maType, length, volumeList);
for (int i = 0; i < stockData.Count; i++)
{
double currentValue = inputList[i];
double prevValue = i >= 1 ? inputList[i - 1] : 0;
double chg = MinPastValues(i, 1, currentValue - prevValue);
chgList.AddRounded(chg);
}
var avgcList = GetMovingAverageList(stockData, maType, length, chgList);
for (int i = 0; i < stockData.Count; i++)
{
double avgv = avgvList[i];
double avgc = avgcList[i];
double r = Math.Abs(avgv * avgc) > 0 ? Math.Log(Math.Abs(avgv * avgc)) * Math.Sign(avgc) : 0;
rList.AddRounded(r);
var list = rList.TakeLastExt(length).ToList();
double rh = list.Max();
double rl = list.Min();
double rs = rh != rl ? (r - rl) / (rh - rl) * 100 : 0;
double k = (rs * 2) - 100;
kList.AddRounded(k);
}
var ksList = GetMovingAverageList(stockData, maType, smoothLength, kList);
for (int i = 0; i < stockData.Count; i++)
{
double ks = ksList[i];
double prevKs = i >= 1 ? ksList[i - 1] : 0;
var signal = GetCompareSignal(ks, prevKs);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Amfo", ksList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = ksList;
stockData.IndicatorName = IndicatorName.AverageMoneyFlowOscillator;
return stockData;
}
/// <summary>
/// Calculates the Better Volume Indicator
/// </summary>
/// <param name="stockData"></param>
/// <param name="length"></param>
/// <param name="lbLength"></param>
/// <returns></returns>
public static StockData CalculateBetterVolumeIndicator(this StockData stockData, int length = 8, int lbLength = 2)
{
List<double> v1List = new();
List<double> v2List = new();
List<double> v3List = new();
List<double> v4List = new();
List<double> v5List = new();
List<double> v6List = new();
List<double> v7List = new();
List<double> v8List = new();
List<double> v9List = new();
List<double> v10List = new();
List<double> v11List = new();
List<double> v12List = new();
List<double> v13List = new();
List<double> v14List = new();
List<double> v15List = new();
List<double> v16List = new();
List<double> v17List = new();
List<double> v18List = new();
List<double> v19List = new();
List<double> v20List = new();
List<double> v21List = new();
List<double> v22List = new();
List<Signal> signalsList = new();
var (inputList, highList, lowList, openList, volumeList) = GetInputValuesList(stockData);
var (highestList, lowestList) = GetMaxAndMinValuesList(highList, lowList, lbLength);
for (int i = 0; i < stockData.Count; i++)
{
double highest = highestList[i];
double lowest = lowestList[i];
double currentHigh = highList[i];
double currentLow = lowList[i];
double currentVolume = volumeList[i];
double currentOpen = openList[i];
double currentClose = inputList[i];
double highLowRange = highest - lowest;
double prevClose = i >= 1 ? inputList[i - 1] : 0;
double prevOpen = i >= 1 ? openList[i - 1] : 0;
double range = CalculateTrueRange(currentHigh, currentLow, prevClose);
double prevV1 = v1List.LastOrDefault();
double v1 = currentClose > currentOpen ? range / ((2 * range) + currentOpen - currentClose) * currentVolume :
currentClose < currentOpen ? (range + currentClose - currentOpen) / ((2 * range) + currentClose - currentOpen) * currentVolume :
0.5 * currentVolume;
v1List.AddRounded(v1);
double prevV2 = v2List.LastOrDefault();
double v2 = currentVolume - v1;
v2List.AddRounded(v2);
double prevV3 = v3List.LastOrDefault();
double v3 = v1 + v2;
v3List.AddRounded(v3);
double v4 = v1 * range;
v4List.AddRounded(v4);
double v5 = (v1 - v2) * range;
v5List.AddRounded(v5);
double v6 = v2 * range;
v6List.AddRounded(v6);
double v7 = (v2 - v1) * range;
v7List.AddRounded(v7);
double v8 = range != 0 ? v1 / range : 0;
v8List.AddRounded(v8);
double v9 = range != 0 ? (v1 - v2) / range : 0;
v9List.AddRounded(v9);
double v10 = range != 0 ? v2 / range : 0;
v10List.AddRounded(v10);
double v11 = range != 0 ? (v2 - v1) / range : 0;
v11List.AddRounded(v11);
double v12 = range != 0 ? v3 / range : 0;
v12List.AddRounded(v12);
double v13 = v3 + prevV3;
v13List.AddRounded(v13);
double v14 = (v1 + prevV1) * highLowRange;
v14List.AddRounded(v14);
double v15 = (v1 + prevV1 - v2 - prevV2) * highLowRange;
v15List.AddRounded(v15);
double v16 = (v2 + prevV2) * highLowRange;
v16List.AddRounded(v16);
double v17 = (v2 + prevV2 - v1 - prevV1) * highLowRange;
v17List.AddRounded(v17);
double v18 = highLowRange != 0 ? (v1 + prevV1) / highLowRange : 0;
v18List.AddRounded(v18);
double v19 = highLowRange != 0 ? (v1 + prevV1 - v2 - prevV2) / highLowRange : 0;
v19List.AddRounded(v19);
double v20 = highLowRange != 0 ? (v2 + prevV2) / highLowRange : 0;
v20List.AddRounded(v20);
double v21 = highLowRange != 0 ? (v2 + prevV2 - v1 - prevV1) / highLowRange : 0;
v21List.AddRounded(v21);
double v22 = highLowRange != 0 ? v13 / highLowRange : 0;
v22List.AddRounded(v22);
bool c1 = v3 == v3List.TakeLastExt(length).Min();
bool c2 = v4 == v4List.TakeLastExt(length).Max() && currentClose > currentOpen;
bool c3 = v5 == v5List.TakeLastExt(length).Max() && currentClose > currentOpen;
bool c4 = v6 == v6List.TakeLastExt(length).Max() && currentClose < currentOpen;
bool c5 = v7 == v7List.TakeLastExt(length).Max() && currentClose < currentOpen;
bool c6 = v8 == v8List.TakeLastExt(length).Min() && currentClose < currentOpen;
bool c7 = v9 == v9List.TakeLastExt(length).Min() && currentClose < currentOpen;
bool c8 = v10 == v10List.TakeLastExt(length).Min() && currentClose > currentOpen;
bool c9 = v11 == v11List.TakeLastExt(length).Min() && currentClose > currentOpen;
bool c10 = v12 == v12List.TakeLastExt(length).Max();
bool c11 = v13 == v13List.TakeLastExt(length).Min() && currentClose > currentOpen && prevClose > prevOpen;
bool c12 = v14 == v14List.TakeLastExt(length).Max() && currentClose > currentOpen && prevClose > prevOpen;
bool c13 = v15 == v15List.TakeLastExt(length).Max() && currentClose > currentOpen && prevClose < prevOpen;
bool c14 = v16 == v16List.TakeLastExt(length).Min() && currentClose < currentOpen && prevClose < prevOpen;
bool c15 = v17 == v17List.TakeLastExt(length).Min() && currentClose < currentOpen && prevClose < prevOpen;
bool c16 = v18 == v18List.TakeLastExt(length).Min() && currentClose < currentOpen && prevClose < prevOpen;
bool c17 = v19 == v19List.TakeLastExt(length).Min() && currentClose > currentOpen && prevClose < prevOpen;
bool c18 = v20 == v20List.TakeLastExt(length).Min() && currentClose > currentOpen && prevClose > prevOpen;
bool c19 = v21 == v21List.TakeLastExt(length).Min() && currentClose > currentOpen && prevClose > prevOpen;
bool c20 = v22 == v22List.TakeLastExt(length).Min();
bool climaxUp = c2 || c3 || c8 || c9 || c12 || c13 || c18 || c19;
bool climaxDown = c4 || c5 || c6 || c7 || c14 || c15 || c16 || c17;
bool churn = c10 || c20;
bool lowVolue = c1 || c11;
var signal = GetConditionSignal(climaxUp, climaxDown);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Bvi", v1List }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = v1List;
stockData.IndicatorName = IndicatorName.BetterVolumeIndicator;
return stockData;
}
/// <summary>
/// Calculates the Buff Average
/// </summary>
/// <param name="stockData"></param>
/// <param name="fastLength"></param>
/// <param name="slowLength"></param>
/// <returns></returns>
public static StockData CalculateBuffAverage(this StockData stockData, int fastLength = 5, int slowLength = 20)
{
List<double> priceVolList = new();
List<double> fastBuffList = new();
List<double> slowBuffList = new();
List<double> tempVolumeList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentValue = inputList[i];
double currentVolume = volumeList[i];
tempVolumeList.AddRounded(currentVolume);
double priceVol = currentValue * currentVolume;
priceVolList.AddRounded(priceVol);
double fastBuffNum = priceVolList.TakeLastExt(fastLength).Sum();
double fastBuffDenom = tempVolumeList.TakeLastExt(fastLength).Sum();
double prevFastBuff = fastBuffList.LastOrDefault();
double fastBuff = fastBuffDenom != 0 ? fastBuffNum / fastBuffDenom : 0;
fastBuffList.AddRounded(fastBuff);
double slowBuffNum = priceVolList.TakeLastExt(slowLength).Sum();
double slowBuffDenom = tempVolumeList.TakeLastExt(slowLength).Sum();
double prevSlowBuff = slowBuffList.LastOrDefault();
double slowBuff = slowBuffDenom != 0 ? slowBuffNum / slowBuffDenom : 0;
slowBuffList.AddRounded(slowBuff);
var signal = GetCompareSignal(fastBuff - slowBuff, prevFastBuff - prevSlowBuff);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "FastBuff", fastBuffList },
{ "SlowBuff", slowBuffList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = new List<double>();
stockData.IndicatorName = IndicatorName.BuffAverage;
return stockData;
}
/// <summary>
/// Calculates the Upside Downside Volume
/// </summary>
/// <param name="stockData"></param>
/// <param name="length"></param>
/// <returns></returns>
public static StockData CalculateUpsideDownsideVolume(this StockData stockData, int length = 50)
{
List<double> upVolList = new();
List<double> downVolList = new();
List<double> upDownVolumeList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentValue = inputList[i];
double currentVolume = volumeList[i];
double prevValue = i >= 1 ? inputList[i - 1] : 0;
double upVol = currentValue > prevValue ? currentVolume : 0;
upVolList.AddRounded(upVol);
double downVol = currentValue < prevValue ? currentVolume * -1 : 0;
downVolList.AddRounded(downVol);
double upVolSum = upVolList.TakeLastExt(length).Sum();
double downVolSum = downVolList.TakeLastExt(length).Sum();
double prevUpDownVol = upDownVolumeList.LastOrDefault();
double upDownVol = downVolSum != 0 ? upVolSum / downVolSum : 0;
upDownVolumeList.AddRounded(upDownVol);
var signal = GetCompareSignal(upDownVol, prevUpDownVol);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Udv", upDownVolumeList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = upDownVolumeList;
stockData.IndicatorName = IndicatorName.UpsideDownsideVolume;
return stockData;
}
/// <summary>
/// Calculates the Ease Of Movement
/// </summary>
/// <param name="stockData"></param>
/// <param name="maType"></param>
/// <param name="length"></param>
/// <param name="divisor"></param>
/// <returns></returns>
public static StockData CalculateEaseOfMovement(this StockData stockData, MovingAvgType maType = MovingAvgType.SimpleMovingAverage, int length = 14,
double divisor = 1000000)
{
List<double> halfRangeList = new();
List<double> midpointMoveList = new();
List<double> emvList = new();
List<Signal> signalsList = new();
var (_, highList, lowList, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentHigh = highList[i];
double currentLow = lowList[i];
double currentVolume = volumeList[i];
double prevHalfRange = halfRangeList.LastOrDefault();
double halfRange = (currentHigh - currentLow) * 0.5;
double boxRatio = currentHigh - currentLow != 0 ? currentVolume / (currentHigh - currentLow) : 0;
double prevMidpointMove = midpointMoveList.LastOrDefault();
double midpointMove = halfRange - prevHalfRange;
midpointMoveList.AddRounded(midpointMove);
double emv = boxRatio != 0 ? divisor * ((midpointMove - prevMidpointMove) / boxRatio) : 0;
emvList.AddRounded(emv);
}
var emvSmaList = GetMovingAverageList(stockData, maType, length, emvList);
var emvSignalList = GetMovingAverageList(stockData, maType, length, emvSmaList);
for (int i = 0; i < stockData.Count; i++)
{
double emv = emvList[i];
double emvSignal = emvSignalList[i];
double prevEmv = i >= 1 ? emvList[i - 1] : 0;
double prevEmvSignal = i >= 1 ? emvSignalList[i - 1] : 0;
var signal = GetCompareSignal(emv - emvSignal, prevEmv - prevEmvSignal);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Eom", emvList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = emvList;
stockData.IndicatorName = IndicatorName.EaseOfMovement;
return stockData;
}
/// <summary>
/// Calculates the On Balance Volume Modified
/// </summary>
/// <param name="stockData"></param>
/// <param name="maType"></param>
/// <param name="length1"></param>
/// <param name="length2"></param>
/// <returns></returns>
public static StockData CalculateOnBalanceVolumeModified(this StockData stockData, MovingAvgType maType = MovingAvgType.ExponentialMovingAverage,
int length1 = 7, int length2 = 10)
{
List<Signal> signalsList = new();
var obvList = CalculateOnBalanceVolume(stockData, maType, length1).CustomValuesList;
var obvmList = GetMovingAverageList(stockData, maType, length1, obvList);
var sigList = GetMovingAverageList(stockData, maType, length2, obvmList);
for (int i = 0; i < stockData.Count; i++)
{
double obvm = obvmList[i];
double sig = sigList[i];
double prevObvm = i >= 1 ? obvmList[i - 1] : 0;
double prevSig = i >= 1 ? sigList[i - 1] : 0;
var signal = GetCompareSignal(obvm - sig, prevObvm - prevSig);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Obvm", obvmList },
{ "Signal", sigList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = obvmList;
stockData.IndicatorName = IndicatorName.OnBalanceVolumeModified;
return stockData;
}
/// <summary>
/// Calculates the On Balance Volume Reflex
/// </summary>
/// <param name="stockData"></param>
/// <param name="maType"></param>
/// <param name="length"></param>
/// <param name="signalLength"></param>
/// <returns></returns>
public static StockData CalculateOnBalanceVolumeReflex(this StockData stockData, MovingAvgType maType = MovingAvgType.SimpleMovingAverage,
int length = 4, int signalLength = 14)
{
List<double> ovrList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, volumeList) = GetInputValuesList(stockData);
for (int i = 0; i < stockData.Count; i++)
{
double currentValue = inputList[i];
double currentVolume = volumeList[i];
double prevValue = i >= length ? inputList[i - length] : 0;
double prevOvr = ovrList.LastOrDefault();
double ovr = currentValue > prevValue ? prevOvr + currentVolume : currentValue < prevValue ? prevOvr - currentVolume : prevOvr;
ovrList.AddRounded(ovr);
}
var ovrSmaList = GetMovingAverageList(stockData, maType, signalLength, ovrList);
for (int i = 0; i < stockData.Count; i++)
{
double ovr = ovrList[i];
double ovrEma = ovrSmaList[i];
double prevOvr = i >= 1 ? ovrList[i - 1] : 0;
double prevOvrEma = i >= 1 ? ovrSmaList[i - 1] : 0;
var signal = GetCompareSignal(ovr - ovrEma, prevOvr - prevOvrEma);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Obvr", ovrList },
{ "Signal", ovrSmaList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = ovrList;
stockData.IndicatorName = IndicatorName.OnBalanceVolumeReflex;
return stockData;
}
/// <summary>
/// Calculates the On Balance Volume Disparity Indicator
/// </summary>
/// <param name="stockData"></param>
/// <param name="maType"></param>
/// <param name="length"></param>
/// <param name="signalLength"></param>
/// <param name="top"></param>
/// <param name="bottom"></param>
/// <returns></returns>
public static StockData CalculateOnBalanceVolumeDisparityIndicator(this StockData stockData,
MovingAvgType maType = MovingAvgType.SimpleMovingAverage, int length = 33, int signalLength = 4, double top = 1.1, double bottom = 0.9)
{
List<double> obvdiList = new();
List<double> bscList = new();
List<Signal> signalsList = new();
var (inputList, _, _, _, _) = GetInputValuesList(stockData);
var obvList = CalculateOnBalanceVolume(stockData, maType, length).CustomValuesList;
var obvSmaList = GetMovingAverageList(stockData, maType, length, obvList);
var smaList = GetMovingAverageList(stockData, maType, length, inputList);
var stdDevList = CalculateStandardDeviationVolatility(stockData, maType, length).CustomValuesList;
stockData.CustomValuesList = obvList;
var obvStdDevList = CalculateStandardDeviationVolatility(stockData, maType, length).CustomValuesList;
for (int i = 0; i < stockData.Count; i++)
{
double currentValue = inputList[i];
double sma = smaList[i];
double stdDev = stdDevList[i];
double obvSma = obvSmaList[i];
double obvStdDev = obvStdDevList[i];
double aTop = currentValue - (sma - (2 * stdDev));
double aBot = currentValue + (2 * stdDev) - (sma - (2 * stdDev));
double obv = obvList[i];
double a = aBot != 0 ? aTop / aBot : 0;
double bTop = obv - (obvSma - (2 * obvStdDev));
double bBot = obvSma + (2 * obvStdDev) - (obvSma - (2 * obvStdDev));
double b = bBot != 0 ? bTop / bBot : 0;
double obvdi = 1 + b != 0 ? (1 + a) / (1 + b) : 0;
obvdiList.AddRounded(obvdi);
}
var obvdiEmaList = GetMovingAverageList(stockData, maType, signalLength, obvdiList);
for (int i = 0; i < stockData.Count; i++)
{
double obvdi = obvdiList[i];
double obvdiEma = obvdiEmaList[i];
double prevObvdi = i >= 1 ? obvdiList[i - 1] : 0;
double prevBsc = bscList.LastOrDefault();
double bsc = (prevObvdi < bottom && obvdi > bottom) || obvdi > obvdiEma ? 1 : (prevObvdi > top && obvdi < top) ||
obvdi < bottom ? -1 : prevBsc;
bscList.AddRounded(bsc);
var signal = GetCompareSignal(bsc, prevBsc);
signalsList.Add(signal);
}
stockData.OutputValues = new()
{
{ "Obvdi", obvdiList },
{ "Signal", obvdiEmaList }
};
stockData.SignalsList = signalsList;
stockData.CustomValuesList = obvdiList;
stockData.IndicatorName = IndicatorName.OnBalanceVolumeDisparityIndicator;
return stockData;
}
/// <summary>