-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cs
1450 lines (1208 loc) · 40.7 KB
/
code.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
using System;
using System.Drawing;
using System.Collections.Generic;
using OpenQuant.API;
using OpenQuant.API.Indicators;
public class DecisionUnit
{
Scenario currentScenario;
public DecisionUnit()
{
currentScenario = Scenario.none;
}
public void placeOrder(DecisionData data, OpenQuant.API.OrderSide side, double qty, double limit)
{
double price = data.bar.Close;
String msg = "Placing order: side: " + side.ToString() + " amt: " + price.ToString() + " Limit: " + limit.ToString() + " Qty: " + qty.ToString();
System.Console.WriteLine(msg);
if(side == OpenQuant.API.OrderSide.Buy)
{
data.strategy.buyOrder = data.strategy.LimitOrder(OpenQuant.API.OrderSide.Buy, qty, limit);
data.strategy.buyOrder.Send();
}
else
{
data.strategy.sellOrder = data.strategy.LimitOrder(OpenQuant.API.OrderSide.Sell, qty, limit);
data.strategy.sellOrder.Send();
}
}
public void run(DecisionData data)
{
if (currentScenario == Scenario.none)
{
if (triggerScenario1(data))
{
//Trigger buy action for scenario1
currentScenario = Scenario.scenario1;
double limitPrice = data.bar.Close + .02;
double price = data.bar.Close;
String msg = "Placing buy order: amt: " + price.ToString() + " Limit: " + limitPrice.ToString();
System.Console.WriteLine(msg);
data.strategy.buyOrder = data.strategy.LimitOrder(OpenQuant.API.OrderSide.Buy, data.strategy.buyQty, limitPrice);
data.strategy.buyOrder.Send();
placeOrder(data, OpenQuant.API.OrderSide.Buy, data.strategy.buyQty, limitPrice);
}
else if (triggerScenario2(data))
{
//Trigger buy action for scenario2
currentScenario = Scenario.scenario2;
}
else if (triggerScenario3(data))
{
//Trigger buy action for scenario3
currentScenario = Scenario.scenario3;
}
}
else
{
switch (currentScenario)
{
case Scenario.scenario1:
handleScenario1(data);
break;
case Scenario.scenario2:
break;
}
}
}
//Returns true if current data point is within s standard deviations from the regression line
// false if the current value is less than stDev standardDeviations from the Regression line.
Boolean dataDropsFromRegression(List<double> coords, double currentVal, int stDev = 2)
{
RegressionData regData = calculateRegression(coords);
//Regression line is
// y = a + bx
double nextY = regData.a + (regData.b * coords.Count);
double upperRange = nextY + (regData.standardDev * stDev);
double lowerRange = nextY - (regData.standardDev * stDev);
if (currentVal < lowerRange)
{
return true;
}
return false;
}
class RegressionData
{
public double a;
public double b;
public double standardDev;
public List<double> yVals;
}
RegressionData calculateRegression(List<double> coords)
{
double xySum = 0;
double xSquaredSum = 0;
double ySquaredSum = 0;
double ySum = 0;
double xSum = 0;
double mean;
for (int i = 1; i < coords.Count; i++)
{
double x = i;
double xSquared = x * x;
double y = coords[i - 1];
double ySquared = y * y;
double xy = x * y;
xSum += x;
ySum += y;
xySum += xy;
xSquaredSum += xSquared;
ySquaredSum += ySquared;
}
double a = ( (ySum * xSquaredSum) - (xSum * xySum) ) / ( (coords.Count * xSquaredSum) - (xSum * xSum) );
double b = ((coords.Count * xySum) - (xSum * ySum) ) / ( ( coords.Count * xSquaredSum) - (xSum * xSum) );
//Determine regression line
// y = a + bx
List<double> yData = new List<double>();
for (int i = 1; i < coords.Count; i++)
{
double x = i;
double y = a + (b * x);
yData.Add(y);
}
RegressionData regData = new RegressionData();
regData.yVals = yData;
//Calculate standard deviation of regression line
ySum = 0;
//First calculate sum
for (int i = 1; i < yData.Count; i++)
{
ySum += yData[i - 1];
}
mean = ySum / yData.Count;
//Next calculate deviations
List<double> deviations = new List<double>();
double devSquaredSum = 0;
for (int i = 1; i < yData.Count; i++)
{
double dev = yData[i - 1] - mean;
double devSquared = dev * dev;
devSquaredSum += devSquared;
}
double standardDev = System.Math.Sqrt(devSquaredSum / (yData.Count - 1));
regData.a = a;
regData.b = b;
regData.standardDev = standardDev;
regData.yVals = yData;
return regData;
}
void handleScenario1(DecisionData data)
{
//Sell Trigger1 data
double thirdEma45 = data.ema_45.getLastNEMA(2);
double previousEma45 = data.ema_45.getLastNEMA(1);
double currentEma45 = data.ema_45.getLastNEMA(0);
//Sell Trigger2 data
double thirdSo180BPercentk = data.so_180_b.getLastNPercentK(2);
double previousSo180BPercentk = data.so_180_b.getLastNPercentK(1);
double currentSo180BPercentk = data.so_180_b.getLastNPercentK(0);
double currentSo180BUpperThreshold = data.so_180_b.getHighThreshold();
//Sell Trigger3 data
//Sell Trigger6 data
List<double> ema45RegData = new List<double>();
for (int i = 0; i < 10; i++)
{
ema45RegData.Add(data.ema_45.getLastNEMA(i + 1));
}
Boolean dataDropFromReg = dataDropsFromRegression(ema45RegData, currentEma45);
Boolean sellTrigger1 = false; // [45] EMA is negative for 2 candles
Boolean sellTrigger2 = false; // [180]21,4,5 is Above the 80 threshold and %K slope goes neg.
// && [45] EMA goes negative
Boolean sellTrigger3 = false; //[45] sell if after 10 candles, 40% of profit is lost
Boolean sellTrigger4 = false; // sell if price drops 3% below buy price
Boolean sellTrigger5 = false; // sell half of position at 4% profit
Boolean sellTrigger6 = false; // create regression line after 10 candles and sell if next candle drops out of regression
if (thirdEma45 > previousEma45 &&
previousEma45 > currentEma45)
{
sellTrigger1 = true;
//SELL!
}
if ((currentSo180BPercentk > currentSo180BUpperThreshold &&
previousSo180BPercentk > currentSo180BPercentk)
&&
(previousEma45 > currentEma45))
{
sellTrigger2 = true;
//Sell!
}
if (dataDropFromReg)
{
sellTrigger6 = true;
//sell!
}
}
public enum Scenario
{
none,
scenario1,
scenario2,
scenario3
}
public Boolean triggerScenario1(DecisionData data)
{
//testing data
double curr_180_percent_d = data.so_180_b.getLastNPercentD(0);
//testing data
//Trigger 1 data
double previousEma45 = data.ema_45.getLastNEMA(1);
double currentEma45 = data.ema_45.getLastNEMA(0);
//Trigger 2 data
double currentSo180BPercentk = data.so_180_b.getLastNPercentK(0);
currentSo180BPercentk = currentSo180BPercentk * 100;
double currentSo180BLowerThreshold = data.so_180_b.getLowThreshold();
int lowerThresholdAllowance = 10;
//Trigger 3 data
double previousSo45APercentK = data.so_45_a.getLastNPercentK(1);
double currentSo45APercentK = data.so_45_a.getLastNPercentK(0);
double previousSo45APercentD = data.so_45_a.getLastNPercentD(1);
double currentSo45APercentD = data.so_45_a.getLastNPercentD(0);
double previousSo45BPercentK = data.so_45_b.getLastNPercentK(1);
double currentSo45BPercentK = data.so_45_b.getLastNPercentK(0);
double previousSo45BPercentD = data.so_45_b.getLastNPercentD(1);
double currentSo45BPercentD = data.so_45_b.getLastNPercentD(0);
Boolean trigger1 = false; //EMA[45] has positive slope
Boolean trigger2 = false; // [180] 14,4,5 gives buy signal
Boolean trigger3 = false; //[45] both stochastics are positive
if (previousEma45 < currentEma45)
{
trigger1 = true;
}
if (currentSo180BPercentk > currentSo180BLowerThreshold &&
currentSo180BPercentk < (currentSo180BLowerThreshold + lowerThresholdAllowance))
{
trigger2 = true;
}
if ((previousSo45APercentK < currentSo45APercentK) &&
(previousSo45APercentD < currentSo45APercentD) &&
(previousSo45BPercentK < currentSo45BPercentK) &&
(previousSo45BPercentD < currentSo45BPercentD))
{
trigger3 = true;
}
//TeSTING
System.Console.WriteLine("======");
System.Console.WriteLine("Current %k = " + currentSo180BPercentk.ToString());
System.Console.WriteLine("Crrent %d = " + curr_180_percent_d.ToString());
System.Console.WriteLine("Lowerthresh= " + currentSo180BLowerThreshold.ToString());
System.Console.WriteLine("allowance = " + lowerThresholdAllowance.ToString());
System.Console.WriteLine("Trigger Status [" + trigger1 + ", " + trigger2 + ", " + trigger3);
System.Console.WriteLine("======");
if (trigger1 && trigger2 && trigger3) {
Environment.Exit(0);
}
//TESTING
return (trigger1 && trigger2 && trigger3);
}
public Boolean triggerScenario2(DecisionData data)
{
//Trigger 1 data
double previousEma45 = data.ema_45.getLastNEMA(1);
double currentEma45 = data.ema_45.getLastNEMA(0);
//Trigger 2 data
double currentSo180APercentk = data.so_180_a.getLastNPercentK(0);
double currentSo180ALowerThreshold = data.so_180_a.getLowThreshold();
int lowerThresholdAllowance = 10;
Boolean trigger1 = false; //EMA[45] has positive slope
Boolean trigger2 = false; // [180] 14,3,3 gives buy signal
if (previousEma45 < currentEma45)
{
trigger1 = true;
}
if (currentSo180APercentk > currentSo180ALowerThreshold &&
currentSo180APercentk < (currentSo180ALowerThreshold + lowerThresholdAllowance))
{
trigger2 = true;
}
return (trigger1 && trigger2);
}
public Boolean triggerScenario3(DecisionData data)
{
return false;
}
}
public class DecisionData
{
public MyStrategy strategy;
public Bar bar;
public StochasticOscillator so_180_a;
public StochasticOscillator so_180_b;
public StochasticOscillator so_45_a;
public StochasticOscillator so_45_b;
public ExponentialMovingAverage ema_45;
public ExponentialMovingAverage ema_180;
}
public class Bulker
{
//Inputs:
// Period
public int bulkPeriod; //Will bulk together 'bulkPeriod' periods and treat it as one Period
List<StockDataNode> bulkData = new List<StockDataNode>();
List<StockDataNode> periodData = new List<StockDataNode>();
public Bulker(int bulk)
{
bulkPeriod = bulk;
}
public StockDataNode getLastPeriodData()
{
if (periodData.Count <= 0)
{
return null;
}
return periodData[periodData.Count - 1];
}
//Returns -1 if the bulker has not yet built up one period
//Returns 1 otherwise
public int update(DateTime timestamp,
double openPrice,
double highPrice,
double lowPrice,
double closePrice)
{
//Do nothing, we don't have enough data yet.
bulkData.Add(new StockDataNode(timestamp, 0, openPrice, highPrice, lowPrice, closePrice));
if (bulkData.Count == 1)
{
return -1;
}
if (bulkData.Count % bulkPeriod == 0)
{
//Calculate the high, low, open, and close
double high = getHighPrice(bulkPeriod, bulkData);
double low = getLowPrice(bulkPeriod, bulkData);
double open = bulkData[bulkData.Count - bulkPeriod].openVal;
double close = closePrice;
periodData.Add(new StockDataNode(timestamp, 0, openPrice, highPrice, lowPrice, closePrice));
return 1;
}
else if (bulkData.Count > bulkPeriod)
{
int tempPeriod = bulkData.Count % bulkPeriod;
double high = getHighPrice(tempPeriod, bulkData);
double low = getLowPrice(tempPeriod, bulkData);
double open = bulkData[bulkData.Count - tempPeriod].openVal;
double close = closePrice;
//testing
/*System.Console.WriteLine("----------------------");
System.Console.WriteLine("bulkdata count === " + bulkData.Count.ToString());
System.Console.WriteLine("bulkPeriod === " + bulkPeriod.ToString());
System.Console.WriteLine("tempPeriod === " + tempPeriod.ToString());
System.Console.WriteLine("----------------------");
*///testing
if (periodData.Count > 1){
//testing
//System.Console.WriteLine("Removing data at position: " + (periodData.Count - 1).ToString());
//testing
periodData.RemoveAt(periodData.Count - 1);
periodData.Add(new StockDataNode(timestamp, 0, openPrice, highPrice, lowPrice, closePrice));
}
else
{
periodData.Add(new StockDataNode(timestamp, 0, openPrice, highPrice, lowPrice, closePrice));
}
}
return -1;
}
public Double getHighPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double high = -1;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (high == -1)
{
high = stockData[size].highVal;
}
if (stockData[size].highVal > high)
{
high = stockData[size].highVal;
}
size--;
}
return high;
}
public Double getHighClosingPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double high = -1;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (high == -1)
{
high = stockData[size].closeVal;
}
if (stockData[size].closeVal > high)
{
high = stockData[size].closeVal;
}
size--;
}
return high;
}
public Double getLowPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double low = 9999;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (low == 9999)
{
low = stockData[size].lowVal;
}
if (stockData[size].lowVal < low)
{
low = stockData[size].lowVal;
}
size--;
}
return low;
}
public Double getLowClosingPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double low = 9999;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (low == 9999)
{
low = stockData[size].closeVal;
}
if (stockData[size].closeVal < low)
{
low = stockData[size].closeVal;
}
size--;
}
return low;
}
}
public class SimpleMovingAverage
{
//Inputs:
// Period
int period;
Bulker bulker;
public SimpleMovingAverage(int tickPeriod, int bulkPer)
{
period = tickPeriod;
bulker = new Bulker(bulkPer);
}
public void updateSimpleMovingAverage(DateTime timestamp,
double openPrice,
double highPrice,
double lowPrice,
double closePrice)
{
int evalPeriod = bulker.update(timestamp,
openPrice,
highPrice,
lowPrice,
closePrice);
if (evalPeriod == -1)
{
//We haven't built up a period yet
return;
}
StockDataNode node = bulker.getLastPeriodData();
openPrice = node.openVal;
highPrice = node.highVal;
lowPrice = node.lowVal;
closePrice = node.closeVal;
if (smaData.Count < period)
{
//Do nothing, we don't have enough data yet.
smaData.Add(new StockDataNode(timestamp, 0, openPrice, highPrice, lowPrice, closePrice));
return;
}
//Use the previous periods to calculate the SMA and store it
int size = smaData.Count - 1;
double sum = 0;
for (int i = 0; i < period; i++)
{
sum += smaData[size].closeVal;
size--;
}
double d = sum / period;
smaData.Add(new StockDataNode(timestamp, d, openPrice, highPrice, lowPrice, closePrice));
//String output = "Simple Moving Average: " + d.ToString();
//Console.WriteLine(output);
}
public double getLastSMA()
{
if (smaData.Count == 0)
{
return 0;
}
return smaData[smaData.Count - 1].value;
}
List<StockDataNode> smaData = new List<StockDataNode>();
}
public class ExponentialMovingAverage
{
//Inputs:
// Period
int period;
Bulker bulker;
private String name;
public bool emaActive = false;
public ExponentialMovingAverage(String emaName, int tickPeriod, int bulkPer)
{
name = emaName;
period = tickPeriod;
bulker = new Bulker(bulkPer);
sma = new SimpleMovingAverage(tickPeriod, bulkPer);
}
public void updateExpentialMovingAverage(DateTime timestamp,
double openPrice,
double highPrice,
double lowPrice,
double closePrice)
{
int evalPeriod = bulker.update(timestamp,
openPrice,
highPrice,
lowPrice,
closePrice);
if (evalPeriod == -1)
{
//We haven't built up a period yet
return;
}
emaActive = true;
//System.Console.WriteLine(name + " is active.");
//testing
//System.Console.WriteLine("ema count " + emaData.Count.ToString());
//System.Console.WriteLine("period: " + period.ToString());
//testing
StockDataNode node = bulker.getLastPeriodData();
openPrice = node.openVal;
highPrice = node.highVal;
lowPrice = node.lowVal;
closePrice = node.closeVal;
if (emaData.Count < period)
{
//Do nothing, we don't have enough data yet.
sma.updateSimpleMovingAverage(timestamp, openPrice, highPrice, lowPrice, closePrice);
//Use previous day's SMA as the starting EMA
emaData.Add(new StockDataNode(timestamp, sma.getLastSMA(), openPrice, highPrice, lowPrice, closePrice));
return;
}
sma.updateSimpleMovingAverage(timestamp, openPrice, highPrice, lowPrice, closePrice);
double simpleMovingAvg = sma.getLastSMA();
double multiplier = 2.00 / (period + 1.00);
double ema = (closePrice - this.getLastEMA() ) * multiplier + this.getLastEMA();
emaData.Add(new StockDataNode(timestamp, ema, openPrice, highPrice, lowPrice, closePrice));
//Testing
//String calcStr = "("+closePrice.ToString() + " - " + this.getLastEMA().ToString() + ") * " + multiplier.ToString() + " + " + this.getLastEMA().ToString();
//Console.WriteLine("CALCSTR ==== " + calcStr);
//String output = "EMA: " + ema.ToString();
//Console.WriteLine(output);
//testing
}
//Grabs the EMA value from N periods ago
//i.e. getLastNEMA(2) returns the EMA Stock Data for 2 periods ago:
// per0 per1 per2 per3 [Current Per]
// getLastNEMA(2) would return per2
public double getLastNEMA(int n)
{
period = n;
if( (period <= 0) || (emaData.Count - period) <= 0)
{
period = 0;
}
return emaData[emaData.Count - 1 - period].value;
}
public double getLastEMA()
{
return emaData[emaData.Count - 1].value;
}
SimpleMovingAverage sma;
List<StockDataNode> emaData = new List<StockDataNode>();
}
public class StockDataNode
{
public StockDataNode(DateTime time,
Double val,
Double open,
Double high,
Double low,
Double close)
{
timestamp = time;
value = val;
openVal = open;
highVal = high;
lowVal = low;
closeVal = close;
}
public StockDataNode(DateTime time,
Double val,
Double open,
Double high,
Double low,
Double close,
Double val2)
{
timestamp = time;
value = val;
openVal = open;
highVal = high;
lowVal = low;
closeVal = close;
value2 = val2;
}
public DateTime timestamp;
public Double value;
public Double openVal;
public Double highVal;
public Double lowVal;
public Double closeVal;
public Double value2;
}
public class StochasticOscillator
{
//Inputs:
// Ticker_period(in minutes) - how often we want the S.O. to be calculated. (e.g. to get a new S.O. datapoint every minute, set this to one)
// High_Threshold
// Low_Threshold
// N_period_for_percent_K
// Smoothing_value_for_percent_D
// M_period_for_percent_D
//Calculations:
// Fast_Percent_K
// Fast_Percent_D
public bool saActive = false;
private bool kActive = false;
private bool dActive = false;
private int tickerPeriod;
private double highThreshold;
private double lowThreshold;
private int nPeriodForPercentK;
private double smoothingForPercentD;
private int mPeriodForPercentD;
private String name;
Bulker bulker;
public StochasticOscillator(String saName,
int tickPeriod,
int bulkPer,
double highThres,
double lowThres,
int nPeriod,
double smooth,
int mPeriod)
{
name = saName;
tickerPeriod = tickPeriod;
highThreshold = highThres;
lowThreshold = lowThres;
nPeriodForPercentK = nPeriod;
smoothingForPercentD = smooth;
mPeriodForPercentD = mPeriod;
bulker = new Bulker(bulkPer);
}
public double getLowThreshold()
{
return lowThreshold;
}
public double getHighThreshold()
{
return highThreshold;
}
public Double getHighPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double high = -1;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (high == -1)
{
high = stockData[size].highVal;
}
if (stockData[size].highVal > high)
{
high = stockData[size].highVal;
}
size--;
}
return high;
}
public Double getHighClosingPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double high = -1;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (high == -1)
{
high = stockData[size].closeVal;
}
if (stockData[size].closeVal > high)
{
high = stockData[size].closeVal;
}
size--;
}
return high;
}
public Double getLowPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double low = 9999;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (low == 9999)
{
low = stockData[size].lowVal;
}
if (stockData[size].lowVal < low)
{
low = stockData[size].lowVal;
}
size--;
}
return low;
}
public Double getLowClosingPrice(int N, List<StockDataNode> stockData)
{
if (stockData.Count <= 0)
{
return 0;
}
int size = stockData.Count - 1;
if (N >= stockData.Count)
{
size = stockData.Count - 1;
}
Double low = 9999;
for (int i = 0; i < N; i++)
{
if (size < 0)
{
break;
}
if (low == 9999)
{
low = stockData[size].closeVal;
}
if (stockData[size].closeVal < low)
{
low = stockData[size].closeVal;
}
size--;
}
return low;
}
public void updateStochasticOscillator(DateTime timestamp,
double openPrice,
double highPrice,
double lowPrice,
double closePrice)
{
int evalPeriod = bulker.update(timestamp,
openPrice,
highPrice,
lowPrice,
closePrice);