-
Notifications
You must be signed in to change notification settings - Fork 1
/
HP_lib.py
1208 lines (1089 loc) · 33.5 KB
/
HP_lib.py
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
"""
#功能:通通股票分析软件框架函数库 Ver1.00
#版本:Ver1.00
#设计人:独狼荷蒲
#电话:18578755056
#QQ:2775205
#百度:荷蒲指标
#开始设计日期: 2018-07-08
#公众号:独狼股票分析
#使用者请同意最后<版权声明>
#最后修改日期:2018年9月14日
#主程序:HP_main.py
###################基本函数库##############################
使用说明
df 指标序列
tp 指标字段,例如close
n 周期数
al 字段别名,可以省略
----------------------------------------------------
import hplibx as mylib
平均移动线函数
def MA(df,tp, n) #移动平均,Moving Average
使用 df=(df,'close',5)
def EMA(df,tp, n) #指数移动平均.Exponential Moving Average
上穿函数
def CROSS(df,tp1,tp2)
#取前n周期数值函数
def REF(df,tp,n)
#取后n周期数值函数
def REFX(df,tp, n)
#Standard Deviation#标准偏差
def STDDEV(df,tp,n):
#取前n周期数值的最高价
def HHV(df,tp, n,al=''):
#取前n周期数值的最低价
def LLV(df,tp, n,al=''):
#取前n周期数值大于0的次数
def COUNT(df,tp, n,al=''):
#求前n周期数值和
def SUM(df,tp, n,al=''):
#Winner当前价格获利率
def WINNER(df,price, tp1,al=''):
#求动态移动平均。
#DMA(X,A),求X的A日动态移动平均。
def DMA(df,tp1,tp2,al=''):
#大于等于函数
def EGT(df,tp1,tp2,al=''):
#小于等于函数
def ELT(df,tp1,tp2,al=''):
#等于函数
def EQUAL(df,tp1,tp2,al=''):
#大于函数
def GT(df,tp1,tp2,al=''):
#小于函数
def LT(df,tp1,tp2,al=''):
#并且函数
def AND(df,tp1,tp2,al=''):
#或者函数
def OR(df,tp1,tp2,al=''):
###################指标库########################
def ACCDIST(df, n): #积累/分配,Accumulation/Distribution
def ADX(df, n, n_ADX): #定向运动平均指数,Average Directional Movement Index
def ATR(df, n): #平均真实范围.Average True Range
def BBANDS(df, n): #布林带.Bollinger Bands
def CCI(df, n): #商品通道指数,Commodity Channel Index
def COPP(df, n): #COPPOCK曲线,Coppock Curve
def Chaikin(df): #蔡金振荡器,Chaikin Oscillator
def DONCH(df, n): #奇安通道,Donchian Channel
def EOM(df, n): #缓解运动,Ease of Movement
def FORCE(df, n): #力指数,Force Index
def KELCH(df, n): #Keltner通道,Keltner Channel
def KST(df, r1, r2, r3, r4, n1, n2, n3, n4): # KST振荡器,KST Oscillator
def MACD(df, n_fast, n_slow):
#MACD指标信号和MACD的区别, MACD Signal and MACD difference
def MFI(df, n): #资金流量指标和比率,Money Flow Index and Ratio
def MOM(df, n): #动量.Momentum
def MassI(df): #质量指数,Mass Index
def OBV(df, n): #平衡量,On-balance volume
def PPSR(df): #支点,支撑和阻力.Pivot Points, Supports and Resistances
def ROC(df, n): #变化率.Rate of Change
def RSI(df, n): #相对强弱指标,Relative Strength Index
def STDDEV(df, n): #标准偏差,#Standard Deviation
def STO(df, n): #随机指标D,Stochastic oscillator %D
def STOK(df): #随机指标K,Stochastic oscillator %K
def TRIX(df, n): #矩阵,#Trix
def TSI(df, r, s): #真实强度指数,True Strength Index
def ULTOSC(df): #最终振荡器,Ultimate Oscillator
def Vortex(df, n): #涡指标,#Vortex Indicator
"""
import platform
import pandas as pd
import numpy
import math as m
from HP_global import *
#版本号
def VER():
return 1.00
#版本号
def Ver():
return 1.00
#聚宽股票代码转换
def jqsn(s):
if (len(s)<6 and len(s)>0):
s=s.zfill(6)+'.XSHE'
if len(s)==6:
if s[0:1]=='0':
s=s+'.XSHE'
else:
s=s+'.XSHG'
return s
##############内部函数库########################
def ema(c_list,n=12):
y_list=[]
_n = 1
for c in c_list:
if c == c_list[0]:
y = c
elif _n<n:
y= c*2/(_n+1) + (1- 2/(_n+1))*y_list[-1]
else:
y=c*2/(n+1)+(1-2/(n+1))*y_list[-1]
y_list.append(y)
_n = _n+1
return y_list
##############基本函数库#########################
def G_MA(Series,n):
G_pyver=int(platform.python_version()[0:1])
G_ma=None
if G_pyver==2:
G_MAstr='pd.rolling_mean(Series,n)'
G_ma=eval(G_MAstr)
else :
G_MAstr='Series.rolling(window=n,center=False).mean()'
G_ma=eval(G_MAstr)
return G_ma
#复制函数
def COPY(df,tp1,al=''):
if (al.strip()==''):
na=tp1+'_2'
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
y=df.get_value(i, tp1)
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#替换函数
def REPLACE(df,tp1,x):
df[tp1]=x
return df
#ONLYONE函数
def ONLYONE(df,tp1):
i=0
while i < len(df):
if df[tp1][i]>0 :
df[tp1]=0
df[tp1][i]=1
break
i=i+1
return df
#大于等于函数
def EGTN(df,tp1,x,al=''):
if (al.strip()==''):
na=tp1+'_EGTN_' + str(x)
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) >= x) :
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#小于等于函数
def ELTN(df,tp1,x,al=''):
if (al.strip()==''):
na=tp1+'_ELTN' + str(x)
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) <= x) :
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#大于等于函数
def EGT(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_EGT_' + tp2
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) >= df.get_value(i, tp2)):
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#小于等于函数
def ELT(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_ELT_' + tp2
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) <= df.get_value(i, tp2)):
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#等于函数
def EQUAL(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_EQUAL_' + tp2
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) == df.get_value(i, tp2)):
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#大于函数
def GT(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_GT_' + tp2
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) > df.get_value(i, tp2)):
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#小于函数
def LT(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_LT_' + tp2
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) < df.get_value(i, tp2)):
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#并且函数
def AND(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_AND_' + tp2
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, tp1) >0 and df.get_value(i, tp2)>0):
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#或者函数
def OR(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_AND_' + tp2
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df)-1:
if (df.get_value(i, tp1) >0 or df.get_value(i, tp2)>0):
y=1
else:
y=0
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#WW:= IF(L>CC, 0, IF(H<CC, 1, (CC-L+0.01)/(H-L+0.01))); { 每日获利盘 }
#Winner2:DMA(ww, VOL/CAPITAL)*100; { 获利盘 };
#Winner当前价格获利率
def WINNER(df,price, tp1,al=''):
if price==0.0:
price=df.get_value(len(df)-1, 'close')
if (al.strip()==''):
na='WINNER_' + tp1
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df):
if (df.get_value(i, 'low') <price):
y=0.0
elif (df.get_value(i, 'high') >price):
y=1.00
else:
y=(price-df.get_value(i, 'low')+0.01)/(df.get_value(i, 'high')-df.get_value(i, 'low')+0.01)
yy=y
ZB_l.append(yy)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#Moving Average 平均线
def MA(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_MA_' + str(n)
else:
na=al
#MA = pd.Series(pd.rolling_mean(df[tp1],window=n,center=False), name =na)
MA = pd.Series(df[tp1].rolling(window=n,center=False).mean(), name =na )
df = df.join(MA)
return df
#Exponential Moving Average
def EMA(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_EMA_' + str(n)
else:
na=al
#EMA = pd.Series(pd.ewma(df[tp1], span = n, min_periods = n - 1), name = na)
EMA = pd.Series(df[tp1].ewm(span = n, min_periods = n - 1,adjust=True,ignore_na=False).mean(), name = na)
df = df.join(EMA)
return df
#求动态移动平均。
#DMA(X,A),求X的A日动态移动平均。
#算法: 若Y=DMA(X,A)
#则 Y=A*X+(1-A)*Y',其中Y'表示上一周期Y值,A必须小于1。
#例如:DMA(CLOSE,VOL/CAPITAL)表示求以换手率作平滑因子的平均价
def DMA(df,tp1,tp2,al=''):
if (al.strip()==''):
na='DMA_'+tp1+'_' + tp2
else:
na=al
i = 1
ZB_l = [0]
y=df.get_value(i-1, tp1)*df.get_value(i-1, tp2)
i=i+1
while i < df.index[-1]:
y=df.get_value(i-1, tp1)*df.get_value(i-1, tp2)+(1-df.get_value(i-1, tp2))*y
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#上穿函数
def CROSS(df,tp1,tp2,al=''):
if (al.strip()==''):
na=tp1+'_CROSS_' + tp2
else:
na=al
i = 1
CR_l = [0]
y=0
while i < len(df):
if ((df.get_value(i-1, tp1) <df.get_value(i-1, tp2)) and (df.get_value(i, tp1) >=df.get_value(i, tp2))):
y=1
else:
y=0
CR_l.append(y)
i = i + 1
CR_s = pd.Series(CR_l)
CR = pd.Series(CR_s, name = na)
df = df.join(CR)
return df
#取前n周期数值函数
def REF(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_REF_' + str(n)
else:
na=al
i = 0
ZB_l = []
y = 0
while i < n:
y=df.get_value(i, tp1)
ZB_l.append(y)
i=i+1
while i < len(df):
y=df.get_value(i-n, tp1)
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#取后n周期数值函数
def REFX(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_REFX_' + str(n)
else:
na=al
i = 0
ZB_l = []
y=0
while i < len(df)-n:
y=df.get_value(i+n, tp1)
ZB_l.append(y)
i=i+1
while i < len(df):
y=df.get_value(i, tp1)
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#Standard Deviation#标准偏差
def STDDEV(df,tp,n):
df = df.join(pd.Series(pd.rolling_std(df[tp], n), name = tp+'_STD_' + str(n)))
return df
#取前n周期数值的最高价
def HHV(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_HHV_' + str(n)
else:
na=al
i = 0
ZB_l = []
y=df.get_value(i, tp1)
while i < n:
if y<df.get_value(i, tp1):
y=df.get_value(i, tp1)
ZB_l.append(y)
i=i+1
while i < len(df):
j=1
y=df.get_value(i, tp1)
while j < n:
if y<df.get_value(i-j, tp1) :
y=df.get_value(i-j, tp1)
j=j+1
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#取前n周期数值的最低价
def LLV(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_HHV_' + str(n)
else:
na=al
i = 0
ZB_l = []
y=df.get_value(0, tp1)
while i < n:
if y>df.get_value(i, tp1):
y=df.get_value(i, tp1)
ZB_l.append(y)
i=i+1
while i < len(df):
j=1
y=df.get_value(i, tp1)
while j < n:
if y>df.get_value(i-j, tp1) :
y=df.get_value(i-j, tp1)
j=j+1
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#取前n周期数值大于0的次数
def COUNT(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_COUNT_' + str(n)
else:
na=al
i = 0
ZB_l = []
y=0
while i < n:
if df.get_value(i, tp1)>0:
y=y+1
ZB_l.append(y)
i=i+1
while i < len(df)+1:
j=1
y=0
while j < n:
if df.get_value(i-j, tp1)>0 :
y=y=y+1
j=j+1
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#求前n周期数值和
def SUM(df,tp, n,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_SUM_' + str(n)
else:
na=al
i = 0
ZB_l = []
y=0
while i < n:
y=y+ df.get_value(i, tp1)
ZB_l.append(y)
i=i+1
while i < len(df):
j=1
y=0
while j < n:
y=y+ df.get_value(i-j, tp1)
j=j+1
ZB_l.append(y)
i = i + 1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
#SMA(X,N,M),求X的N日移动平均,M为权重。算法:若Y=SMA(X,N,M) 则 Y=(M*X+(N-M)*Y')/N,其中Y'表示上一周期Y值,N必须大于M。
def SMA(df,tp,n,m,al=''):
if (tp.strip()==''):
tp1='close'
else:
tp1=tp
if (al.strip()==''):
na=tp1+'_SMA_' + str(n)
else:
na=al
i = 0
ZB_l = []
y=1
while i < len(df):
y=(df.get_value(i, tp1)*m+(n-m)*y)/n
ZB_l.append(y)
i=i+1
ZB_s = pd.Series(ZB_l)
ZB = pd.Series(ZB_s, name = na)
df = df.join(ZB)
return df
################指标库#########################
#RSV:=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100;
#K:SMA(RSV,M1,1);
#D:SMA(K,M2,1);
#J:3*K-2*D;
def KDJ(df,n,m1,m2):
i = 0
RSV=0.0000
ZB_l = []
yl= df.get_value(0, 'low')
yh= df.get_value(0, 'high')
while i < n:
if yl>df.get_value(i, 'low') :
yl=df.get_value(i, 'low')
if yh<df.get_value(i, 'high') :
yh=df.get_value(i, 'high')
i=i+1
RSV= (df.get_value(i, 'close')-yl)/(yh-yl)*100.0000
ZB_l.append(RSV)
while i < len(df):
j=0
yl= df.get_value(i, 'low')
yh= df.get_value(i, 'high')
while j < n:
if yl>df.get_value(i-j, 'low') :
yl=df.get_value(i-j, 'low')
if yh<df.get_value(i-j, 'high') :
yh=df.get_value(i-j, 'high')
j=j+1
if yh !=yl :
RSV= (df.get_value(i, 'close')-yl)/(yh-yl)*100.0000
else:
RSV=50
ZB_l.append(RSV)
i = i + 1
ZB_s = pd.Series(ZB_l)
rsv=ZB_s
ZB = pd.Series(ZB_s, name = 'RSV')
df = df.join(ZB)
i = 0
ZB_l = []
y=1
while i < len(df):
y=(rsv[i]*1+(m1-1)*y)/m1
ZB_l.append(y)
i=i+1
ZB_s = pd.Series(ZB_l)
k=ZB_s
ZB = pd.Series(ZB_s, name = 'K')
df = df.join(ZB)
i = 0
ZB_l = []
y=1
while i < len(df):
y=(k[i]*1+(m2-1)*y)/m2
ZB_l.append(y)
i=i+1
ZB_s = pd.Series(ZB_l)
d=ZB_s
ZB = pd.Series(ZB_s, name = 'D')
df = df.join(ZB)
j=3*k-2*d
ZB = pd.Series(j, name = 'J')
df = df.join(ZB)
return df
def OBVX(df, n,m):
i = 0
OBV = [0]
while i < df.index[-1]:
if df.get_value(i + 1, 'close') - df.get_value(i, 'close') > 0:
OBV.append(df.get_value(i + 1, 'volume'))
if df.get_value(i + 1, 'close') - df.get_value(i, 'close') == 0:
OBV.append(0)
if df.get_value(i + 1, 'close') - df.get_value(i, 'close') < 0:
OBV.append(-df.get_value(i + 1, 'volume'))
i = i + 1
OBV = pd.Series(OBV,name = 'OBV')
df=df.join(OBV)
OBV_ma = pd.Series(pd.rolling_mean(OBV, n), name = 'OBV_' + str(n))
df = df.join(OBV_ma)
OBV_ma = pd.Series(pd.rolling_mean(OBV, m), name = 'OBV_' + str(m))
df = df.join(OBV_ma)
return df
#Relative Strength Index
def RSIX(df, n,al=''):
if (al.strip()==''):
na=tp1+'RSI_' + str(n)
else:
na=al
i = 0
UpI = [0]
DoI = [0]
while i + 1 <= df.index[-1]:
UpMove = df.get_value(i + 1, 'high') - df.get_value(i, 'high')
DoMove = df.get_value(i, 'low') - df.get_value(i + 1, 'low')
if UpMove > DoMove and UpMove > 0:
UpD = UpMove
else: UpD = 0
UpI.append(UpD)
if DoMove > UpMove and DoMove > 0:
DoD = DoMove
else: DoD = 0
DoI.append(DoD)
i = i + 1
UpI = pd.Series(UpI)
DoI = pd.Series(DoI)
PosDI = pd.Series(pd.ewma(UpI, span = n, min_periods = n - 1))
NegDI = pd.Series(pd.ewma(DoI, span = n, min_periods = n - 1))
RSI = pd.Series(PosDI*100.00 / (PosDI + NegDI), name = na)
df = df.join(RSI)
return df
def MACDX(df, n_long, n_short,m):
d1=pd.Series(pd.ewma(df['close'], span = n_long, min_periods = n_long - 1))
#d1= pd.Series(df['close'].ewm(span = n_long, min_periods = n_long - 1,adjust=True,ignore_na=False).mean())
d2=pd.Series(pd.ewma(df['close'], span = n_short, min_periods = n_short - 1))
#d2= pd.Series(df['close'].ewm(span = n_short, min_periods = n_short - 1,adjust=True,ignore_na=False).mean())
diff = pd.Series(d1 - d2)
#dea=pd.Series(pd.ewma(diff, span = m, min_periods = m - 1))
dea= pd.Series(diff.ewm(span = m, min_periods = m - 1,adjust=True,ignore_na=False).mean())
DIFF= pd.Series(diff,name='DIFF')
DEA= pd.Series(dea,name='DEA')
MACD = pd.Series(2*(diff-dea), name = 'MACD')
df = df.join(DIFF)
df = df.join(DEA)
df = df.join(MACD)
return df
#MACD, MACD Signal and MACD difference
def MACD(df, n_fast, n_slow):
EMAfast = pd.Series(pd.ewma(df['close'], span = n_fast, min_periods = n_slow - 1))
EMAslow = pd.Series(pd.ewma(df['close'], span = n_slow, min_periods = n_slow - 1))
MACD = pd.Series(EMAfast - EMAslow, name = 'MACD_' + str(n_fast) + '_' + str(n_slow))
MACDsign = pd.Series(pd.ewma(MACD, span = 9, min_periods = 8), name = 'MACDsign_' + str(n_fast) + '_' + str(n_slow))
MACDdiff = pd.Series(MACD - MACDsign, name = 'MACDdiff_' + str(n_fast) + '_' + str(n_slow))
df = df.join(MACD)
df = df.join(MACDsign)
df = df.join(MACDdiff)
return df
#Momentum
def MOM(df, n):
M = pd.Series(df['close'].diff(n), name = 'Momentum_' + str(n))
df = df.join(M)
return df
#Rate of Change
def ROC(df, n):
M = df['close'].diff(n - 1)
N = df['close'].shift(n - 1)
ROC = pd.Series(M / N, name = 'ROC_' + str(n))
df = df.join(ROC)
return df
#Average True Range
def ATR(df, n):
i = 0
TR_l = [0]
while i < len(df.index):
TR = max(df.get_value(i + 1, 'high'), df.get_value(i, 'close')) - min(df.get_value(i + 1, 'low'), df.get_value(i, 'close'))
TR_l.append(TR)
i = i + 1
TR_s = pd.Series(TR_l)
ATR = pd.Series(pd.ewma(TR_s, span = n, min_periods = n), name = 'ATR_' + str(n))
df = df.join(ATR)
return df
#Bollinger Bands
def BBANDS(df, n):
MA = pd.Series(pd.rolling_mean(df['close'], n))
MSD = pd.Series(pd.rolling_std(df['close'], n))
b1 = 4 * MSD / MA
B1 = pd.Series(b1, name = 'BollingerB_' + str(n))
df = df.join(B1)
b2 = (df['close'] - MA + 2 * MSD) / (4 * MSD)
B2 = pd.Series(b2, name = 'Bollinger%b_' + str(n))
df = df.join(B2)
return df
#Pivot Points, Supports and Resistances
def PPSR(df):
PP = pd.Series((df['high'] + df['low'] + df['close']) / 3)
R1 = pd.Series(2 * PP - df['low'])
S1 = pd.Series(2 * PP - df['high'])
R2 = pd.Series(PP + df['high'] - df['low'])
S2 = pd.Series(PP - df['high'] + df['low'])
R3 = pd.Series(df['high'] + 2 * (PP - df['low']))
S3 = pd.Series(df['low'] - 2 * (df['high'] - PP))
psr = {'PP':PP, 'R1':R1, 'S1':S1, 'R2':R2, 'S2':S2, 'R3':R3, 'S3':S3}
PSR = pd.DataFrame(psr)
df = df.join(PSR)
return df
#Stochastic oscillator %K
def STOK(df):
SOk = pd.Series((df['close'] - df['low']) / (df['high'] - df['low']), name = 'SO%k')
df = df.join(SOk)
return df
#Stochastic oscillator %D
def STO(df, n):
SOk = pd.Series((df['close'] - df['low']) / (df['high'] - df['low']), name = 'SO%k')
SOd = pd.Series(pd.ewma(SOk, span = n, min_periods = n - 1), name = 'SO%d_' + str(n))
df = df.join(SOd)
return df
#Trix
def TRIX(df, n):
EX1 = pd.ewma(df['close'], span = n, min_periods = n - 1)
EX2 = pd.ewma(EX1, span = n, min_periods = n - 1)
EX3 = pd.ewma(EX2, span = n, min_periods = n - 1)
i = 0
ROC_l = [0]
while i + 1 <= df.index[-1]:
ROC = (EX3[i + 1] - EX3[i]) / EX3[i]
ROC_l.append(ROC)
i = i + 1
Trix = pd.Series(ROC_l, name = 'Trix_' + str(n))
df = df.join(Trix)
return df
#Average Directional Movement Index
def ADX(df, n, n_ADX):
i = 0
UpI = []
DoI = []
while i + 1 <= df.index[-1]:
UpMove = df.get_value(i + 1, 'high') - df.get_value(i, 'high')
DoMove = df.get_value(i, 'low') - df.get_value(i + 1, 'low')
if UpMove > DoMove and UpMove > 0:
UpD = UpMove
else: UpD = 0
UpI.append(UpD)
if DoMove > UpMove and DoMove > 0:
DoD = DoMove
else: DoD = 0
DoI.append(DoD)
i = i + 1
i = 0
TR_l = [0]
while i < df.index[-1]:
TR = max(df.get_value(i + 1, 'high'), df.get_value(i, 'close')) - min(df.get_value(i + 1, 'low'), df.get_value(i, 'close'))
TR_l.append(TR)
i = i + 1
TR_s = pd.Series(TR_l)
ATR = pd.Series(pd.ewma(TR_s, span = n, min_periods = n))
UpI = pd.Series(UpI)
DoI = pd.Series(DoI)
PosDI = pd.Series(pd.ewma(UpI, span = n, min_periods = n - 1) / ATR)
NegDI = pd.Series(pd.ewma(DoI, span = n, min_periods = n - 1) / ATR)
ADX = pd.Series(pd.ewma(abs(PosDI - NegDI) / (PosDI + NegDI), span = n_ADX, min_periods = n_ADX - 1), name = 'ADX_' + str(n) + '_' + str(n_ADX))
df = df.join(ADX)
return df
#Mass Index
def MassI(df):
Range = df['high'] - df['low']
EX1 = pd.ewma(Range, span = 9, min_periods = 8)
EX2 = pd.ewma(EX1, span = 9, min_periods = 8)
Mass = EX1 / EX2
MassI = pd.Series(pd.rolling_sum(Mass, 25), name = 'Mass Index')
df = df.join(MassI)
return df
#Vortex Indicator: http://www.vortexindicator.com/VFX_VORTEX.PDF
def Vortex(df, n):
i = 0
TR = [0]
while i < df.index[-1]:
Range = max(df.get_value(i + 1, 'high'), df.get_value(i, 'close')) - min(df.get_value(i + 1, 'low'), df.get_value(i, 'close'))
TR.append(Range)
i = i + 1
i = 0
VM = [0]