-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperl_stocktools.pl
executable file
·1674 lines (1659 loc) · 43.9 KB
/
perl_stocktools.pl
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
#!/usr/bin/perl -w
use strict;
use warnings;
use 5.010;
use LWP;
use DBI;
use Data::Dumper;
require "perl_common.pl";
require "perl_stockcommon.pl";
require "perl_database.pl";
require "perl_database_tools.pl";
require "perl_stocknetwork.pl";
our $StockExDb="StockExchangeDb";
our $StockProfitDb="StockProfitDb";
our $StockInfoDb="StockInfoDb";
our $BuyStockCode="buy_stock_code.txt";
our $StockCodeFile="stock_code.txt";
our $monitor_code="monitor_stock_code.txt";
our $gall_monitor_info={};
our $code_property_separator='@';
our $code_property_assignment=':';
#选择stock代码的技术指标开关
our $g_selectcode_date;
our $g_selectcode_mode;
our $gflag_selectcode_mode=0;#模式
our $gflag_selectcode_level=0;#级别(越低越严格)
our $gflag_selectcode_dig=0;#挖坑
our $gflag_selectcode_macd=0;#macd指数
our $gflag_selectcode_kdj=0;#kdj指数
our $gflag_selectcode_break_surge=0;#突破平台振荡
our $gflag_selectcode_turnover=0;#换手率
$|=1;
# calculate volume moving average
sub _MA_volume{
my ($code,$dhe,$date,$day_count)=@_;
my $total=0;
my @days;
if(@days=DBT_get_earlier_exchange_days($dhe,$code,$date,$day_count)){
foreach my $day(@days){
$total+=DBT_get_volume($code,$dhe,$day);
}
}
return $total/@days;
}
# calculate moving average
sub _MA{
my ($code,$dhe,$date,$day_count)=@_;
my $total=0;
my @days;
if(@days=DBT_get_earlier_exchange_days($dhe,$code,$date,$day_count)){
foreach my $day(@days){
$total+=DBT_get_closing_price($code,$day,$dhe);
}
}
return $total/@days;
}
sub _DIFF{
my $diff_s_day=shift;
my $diff_l_day=shift;
my $code=shift;
my $dhe=shift;
my $day_exchange_start=shift;
my $diff_day=shift;
my $ema_s=_EMA($code,$dhe,$day_exchange_start,$diff_day,$diff_s_day);
my $ema_l=_EMA($code,$dhe,$day_exchange_start,$diff_day,$diff_l_day);
my $diff=$ema_s-$ema_l;
return $diff;
}
sub _DEA{
my $diff_s_day=shift;
my $diff_l_day=shift;
my $code=shift;
my $dhe=shift;
my $day_exchange_start=shift;
my $dea_day=shift;
my $dea_day_cnt=shift;
my $condition="DATE<=\"$dea_day\" ORDER BY DATE DESC LIMIT $dea_day_cnt";
#获取需要计算diff的日期
my @diff_days= DBT_get_earlier_exchange_days($dhe,$code,$dea_day,$dea_day_cnt);
my $sum_diff;
foreach my $diff_date(@diff_days){
$sum_diff+=_DIFF($diff_s_day,$diff_l_day,$code,$dhe,$day_exchange_start,$diff_date);
}
my $dea=$sum_diff/@diff_days;
return $dea;
}
#diff=ema(12)-ema(26)
#dea =ema(9)
#macd=diff-dea;
sub _MACD_DEALITTLETHAN{
my $diff_s_day_cnt=shift;
my $diff_l_day_cnt=shift;
my $dea_day_cnt=shift;
my $code=shift;
my $dhe=shift;
my $day_exchange_start=shift;
my $ema_day=shift;
my $max_dea=shift;
my $diff=_DIFF($diff_s_day_cnt,$diff_l_day_cnt,$code,$dhe,$day_exchange_start,$ema_day);
my $dea=_DEA($diff_s_day_cnt,$diff_l_day_cnt,$code,$dhe,$day_exchange_start,$ema_day,$dea_day_cnt);
print "$code:Diff($diff_s_day_cnt,$diff_l_day_cnt):$diff,DEA($dea_day_cnt):$dea","\n";
if($dea <= $max_dea){
return $diff-$dea;
}
return undef;
}
#diff=ema(12)-ema(26)
#dea =ema(9)
#macd=diff-dea;
sub _MACD_DIFFLITTLETHANZERO{
my $diff_s_day_cnt=shift;
my $diff_l_day_cnt=shift;
my $dea_day_cnt=shift;
my $code=shift;
my $dhe=shift;
my $day_exchange_start=shift;
my $ema_day=shift;
my $diff=_DIFF($diff_s_day_cnt,$diff_l_day_cnt,$code,$dhe,$day_exchange_start,$ema_day);
my $dea=_DEA($diff_s_day_cnt,$diff_l_day_cnt,$code,$dhe,$day_exchange_start,$ema_day,$dea_day_cnt);
print "$code:Diff($diff_s_day_cnt,$diff_l_day_cnt):$diff,DEA($dea_day_cnt):$dea","\n";
if($diff < 0){
return $diff-$dea;
}
return undef;
}
#diff=ema(12)-ema(26)
#dea =ema(9)
#macd=diff-dea;
sub _MACD{
my $diff_s_day_cnt=shift;
my $diff_l_day_cnt=shift;
my $dea_day_cnt=shift;
my $code=shift;
my $dhe=shift;
my $day_exchange_start=shift;
my $ema_day=shift;
my $diff=_DIFF($diff_s_day_cnt,$diff_l_day_cnt,$code,$dhe,$day_exchange_start,$ema_day);
my $dea=_DEA($diff_s_day_cnt,$diff_l_day_cnt,$code,$dhe,$day_exchange_start,$ema_day,$dea_day_cnt);
print "$code:Diff($diff_s_day_cnt,$diff_l_day_cnt):$diff,DEA($dea_day_cnt):$dea","\n";
return $diff-$dea;
}
# calculate exponential moving average
#EMA=P今天*K+EMA昨天*(1-K)
#其中K=2/N+1
#N=EMA的天数(由交易者决定)
#EMA昨天=昨天的EMA
sub _EMA{
my $code=shift;
my $dhe=shift;
my $day_exchange_start=shift;
my $ema_day=shift;
my $day_cnt=shift;
my $v_K=2/($day_cnt+1);
my @P;
#计算开始$date天的平均值
my $first_ema;
my $i=1;
if($first_ema=DBT_get_closing_price($code,$day_exchange_start,$dhe)){
$i=2;
}
for(;$i<$day_cnt+1;$i++){
my @day_price=DBT_get_next_date_closing_price($code,$day_exchange_start,$dhe);
if(!@day_price ){
return $first_ema/$i;
}
$first_ema+=$day_price[1];
$day_exchange_start=$day_price[0];
if( COM_is_same_day($day_price[0],$ema_day)){
return $first_ema/$i;
}
}
$first_ema = $first_ema/$day_cnt;
#计算后续的EMA
while(@P=DBT_get_next_date_closing_price($code,$day_exchange_start,$dhe)){
if(COM_is_earlier_than($P[0],$ema_day)){
$first_ema=$P[1]*$v_K+$first_ema*(1-$v_K);
$day_exchange_start=$P[0];
next;
}
if(COM_is_same_day($P[0],$ema_day)){
return $P[1]*$v_K+$first_ema*(1-$v_K);
}
return $first_ema;
}
return undef;
}
#KDJ 先计算周期(n日,n周等)的RSV值(未成熟随机指标值,然后再计算K值,D值
#J值。以日KDJ数值为例,其计算公式为
#n日RSV=(C-Ln)/(Hn-Ln)×100
#第n日的收盘价,Ln为第n日内的最低收盘价,Hn为n日内最高收盘价。
#RSV值始终在1-100间波动
#其次,计算K值与D值
#当日K值=2/3×前一日K值+1/3×当日RSV
#当日D值=2/3×前一日D值+1/3当日K值
#若无前一日K值与D值则可分别用50代替
#J值=3×当日D值-2×当日K值
#以9日为周期的KD线为例,首先计算出最近9日的RSV值
#9日RSV=(C-L9)/(H9-L9)×100
#公式中C为第9日的收盘价,L9为9日内最低收盘价,H9为9日最高收盘价
#K值=2/3×第8日K值+1/3×第9日RSV
#D值=2/3×第8日D值+1/3×第9日K值
#J值=3×第9日K值-2×第9日D值
#
sub _J_OF_KDJ{
my ($code,$date,$period,$dhe,$day_exchange_start)=@_;
my $J=3*_K_OF_KDJ($code,$date,$period,$dhe,$day_exchange_start)-2*_D_OF_KDJ($code,$date,$period,$dhe,$day_exchange_start);
return $J;
}
sub _D_OF_KDJ{
my ($code,$date,$period,$dhe,$day_exchange_start)=@_;
my $origin_exchange_start=$day_exchange_start;
#第一天的值默认
my $D=2/3*50+1/3*_K_OF_KDJ($code,$date,$period,$dhe,$day_exchange_start);
if(COM_is_same_day($date,$day_exchange_start)){
return $D;
}
#计算前面的值
while(my $day=DBT_get_next_exchange_day($code,$day_exchange_start,$dhe)){
$day_exchange_start=$day;
if(COM_is_earlier_than($day,$date)){
$D=2/3*$D+1/3*_K_OF_KDJ($code,$day,$period,$dhe,$origin_exchange_start);
next;
}
if(COM_is_same_day($day,$date)){
return 2/3*$D+1/3*_K_OF_KDJ($code,$day,$period,$dhe,$origin_exchange_start);
}
return $D;
}
return undef;
}
sub _K_OF_KDJ{
my ($code,$date,$period,$dhe,$day_exchange_start)=@_;
#第一天的k值默认
my $K=2/3*50+1/3*_RSV_OF_KDJ($code,$date,$dhe,$period);
if(COM_is_same_day($date,$day_exchange_start)){
return $K;
}
#计算前面的k值
while(my $day=DBT_get_next_exchange_day($code,$day_exchange_start,$dhe)){
$day_exchange_start=$day;
if(COM_is_earlier_than($day,$date)){
$K=2/3*$K+1/3*_RSV_OF_KDJ($code,$day,$dhe,$period);
next;
}
if(COM_is_same_day($day,$date)){
return 2/3*$K+1/3*_RSV_OF_KDJ($code,$day,$dhe,$period);
}
return $K;
}
return undef;
}
sub _RSV_OF_KDJ{
my ($code,$date,$dhe,$period)=@_;
if(my @days=DBT_get_earlier_exchange_days($dhe,$code,$date,$period)){
my $C=DBT_get_closing_price($code,$days[0],$dhe);
#n日内的最低收盘价
my $Ln=$C;
#n日内的最高收盘价
my $Hn=$C;
foreach my $day(@days){
my $t=DBT_get_closing_price($code,$day,$dhe);
if($Ln>$t){
$Ln=$t;
}
if($Hn<$t){
$Hn=$t;
}
}
if($Hn-$Ln==0){
return 0;
}
return ($C-$Ln)/($Hn-$Ln)*100;
}
return undef;
}
sub _get_turnover{
my $date=shift;
my $code=shift;
my $deh=shift;
my $dih=shift;
my $condition="DATE=\"$date\"";
my @liutogu=MSH_GetValue($dih,$code,"LIUTONGGU");
my @jiaoyigushu=MSH_GetValue($deh,$code,"JIAOYIGUSHU",$condition);
if(defined $jiaoyigushu[0] and defined $liutogu[0]){
return $jiaoyigushu[0]/$liutogu[0];
}
return 0;
}
sub _turnover_get_codes{
my $datefrom=shift;
my $dateto=shift;
my $min=shift;
my $max=shift;
my $daymin=shift;
my $codemax=shift;
my $deh=MSH_OpenDB($StockExDb);
my $dih=MSH_OpenDB($StockInfoDb);
my $condition="DATE>=\"$datefrom\" && DATE<=\"$dateto\" ";
my @code =MSH_GetAllTablesName1($deh);
my @codes;
foreach my $code(@code){
my @date=MSH_GetValue($deh,$code,"DATE",$condition);
my $total=0;
foreach my $date(@date){
my $turnover=_get_turnover($date,$code,$deh,$dih);
if($turnover >= $min && $turnover <= $max){
if(++$total >= $daymin){
push @codes,$code;
}
}
}
}
$deh->disconnect;
$dih->disconnect;
return @codes;
}
sub _get_surge_days{
my ($dhe,$code,$date)=@_;
my $day_count=30;
my $surge_min_day = 3;
my $foreday = DBT_get_fore_exchange_day($code,$date,$dhe);
my @durationdays= DBT_get_earlier_exchange_days($dhe,$code,$foreday,$day_count);
my @days;
if (@durationdays){
foreach my $one(@durationdays){
my $rise = DBT_get_days_rise($code,$dhe,$one,$date);
if(abs($rise)>0.2){
last;
}
push @days,$one;
}
if(scalar(@days)<$surge_min_day){
return undef;
}
return @days;
}
return undef;
}
sub _is_break_surge{
my ($dhe,$code,$date)=@_;
my @days= DBT_get_earlier_exchange_days($dhe,$code,$date,2);
my $rise = DBT_get_rise($code,$dhe,$days[0]);
@days = _get_surge_days($dhe,$code,$days[1]);
if($rise > 0.05 && $days[0]){
my $max_closing_price = DBT_get_max_closing_price($code,$dhe,$days[$#days],$days[0]);
my $closing_price = DBT_get_closing_price($code,$days[0],$dhe);
if ($closing_price && ($closing_price - $max_closing_price)/$closing_price > 0.05){
return 1;
}
}
return 0;
}
sub _is_diging{
my ($dhe,$code,$date)=@_;
my @days= DBT_get_earlier_exchange_days($dhe,$code,$date,2);
if(@days){
my $rise = DBT_get_rise($code,$dhe,$days[1]);
if($rise < -0.04 && $rise > -0.09){
return 1;
}
}
return 0;
}
#拐点日期
sub _get_inflection_date
{
my ($dhe,$code,$date)=@_;
}
#在一定时间里股价持续上升
sub _is_mode1
{
my ($dhe,$code,$date,$level)=@_;
my @days= DBT_get_earlier_exchange_days($dhe,$code,$date,2);
if (not $level){
$level = 0;
}
if(@days){
my $maxrise=0.05;
my $durationrise = 0;
my $day_count=15;
my @durationdays= DBT_get_earlier_exchange_days($dhe,$code,$days[0],$day_count);
my @daysbigvol;
my @dayslittlevol;
my @daysbig;
my @dayslittle;
my @daysother;
my @daysup;
my @daysdown;
my $maxVol = 50000*100;
my $minDurationRise = 0.06;
my $lastupdays=4;
my @lastupdays;
my $lastupdaysmin=0.6;
my $tmp = $lastupdays;
foreach my $one(@durationdays){
my $volume= DBT_get_volume($code,$dhe,$one);
my $rise = DBT_get_rise($code,$dhe,$one);
if ($tmp> 0){
if ($rise > 0){
push @lastupdays,$rise;
}
$tmp--;
}
if ($rise > 0){
push @daysup,$rise;
}else{
push @daysdown,$rise;
}
if (abs($rise) < 0.03){
push @dayslittle,$rise;
}elsif (abs($rise < 0.055)){
push @daysbig,$rise;
}else{
push @daysother,$rise;
}
if ($volume > $maxVol){
push @daysbigvol,$volume;
}else{
push @dayslittlevol,$volume;
}
}
if(($#daysup+1) < 3 * ($#daysdown+1)){
return 0;
}
if (@durationdays && scalar(@durationdays) > 1){
$durationrise = DBT_get_days_rise($code,$dhe,$durationdays[$#durationdays],$durationdays[0]);
}
if ($level <=0){
if (scalar(@daysbig) > $day_count/4 ){
return 0;
}
if (scalar(@daysother) < 1){
return 0;
}
if (scalar(@daysbigvol) > $day_count/7){
return 0;
}
}
if ($level <= 1){
if(abs($durationrise) < $minDurationRise){
return 0;
}
if ( scalar(@daysother) >= $day_count/6 ){
return 0;
}
if (scalar(@lastupdays) < $lastupdays * $lastupdaysmin){
return 0;
}
}
return 1;
}
return 0;
}
sub _is_mode3
{
my ($dhe,$code,$date,$level)=@_;
my $dayscount = 5;
my @days= DBT_get_earlier_exchange_days($dhe,$code,$date,$dayscount+1);
if (@days){
my $last = $days[0];
my $foredayrise = DBT_get_rise($code,$dhe,$days[$#days]);
if ($level < 1){
if($foredayrise > 0){
return 0;
}
}
foreach my $day(@days){
my $dayrise = DBT_get_rise($code,$dhe,$day);
my $rise = DBT_get_days_rise($code,$dhe,$day,$last);
if ($dayrise < 0){
return 0;
}
if ($rise < 0){
return 0;
}
}
if(!_is_valid_KDJ_cross($code,$dhe,$date,$dayscount)){
return 0;
}
return 1;
}
return 0;
}
#底部放量
sub _is_mode4
{
my ($dhe,$code,$date,$level)=@_;
#底部判断
my $dayscount = 20;
my $dayupcount = $dayscount/10;
my $tmp = $dayupcount;
my @days= DBT_get_earlier_exchange_days($dhe,$code,$date,$dayscount+1);
if (@days){
my @tmp;
foreach my $day(@days){
my $volume = DBT_get_volume($code,$dhe,$day);
push @tmp,$volume;
}
my $last = $tmp[0];
my $last1 = $tmp[1];
my $lastday=$days[0];
my $lastrise = DBT_get_rise($code,$dhe,$lastday);
if ($lastrise < 0.03 || $lastrise > 0.08){
return 0;
}
foreach my $tmp(@tmp){
if ($last > $tmp*10 && $last1 > $tmp*6){
return 1;
}
}
}
return 0;
}
sub _is_MA_UP{
my ($dhe,$code,$last_day,$MA_day_count,$duration)=@_;
if(my @days = DBT_get_earlier_exchange_days($dhe,$code,$last_day,$duration)){
my $max_val=10000000;
foreach my $day(@days){
my $tmp = _MA($code,$dhe,$day,$MA_day_count);
if($tmp > $max_val){
return 0;
}
$max_val = $tmp;
}
return 1;
}
return 0;
}
sub _is_MA_volume_cross{
my ($dhe,$code,$last_day,$min_MA_day_count,$max_MA_day_count,$duration)=@_;
if(my @days = DBT_get_earlier_exchange_days($dhe,$code,$last_day,$duration)){
my $last_exchange_day = $days[0];
my $first_exchange_day = $days[$#days];
my $last_min_MA = _MA_volume($code,$dhe,$last_exchange_day,$min_MA_day_count);
my $last_max_MA = _MA_volume($code,$dhe,$last_exchange_day,$max_MA_day_count);
my $first_min_MA = _MA_volume($code,$dhe,$first_exchange_day,$min_MA_day_count);
my $first_max_MA = _MA_volume($code,$dhe,$first_exchange_day,$max_MA_day_count);
#有正斜角
if(($last_min_MA - $last_max_MA)> ($first_min_MA - $first_max_MA)){
return 1;
}
if($last_min_MA >= $last_max_MA && $first_min_MA <= $first_max_MA){
return 1;
}
if($last_min_MA <= $last_max_MA && $first_min_MA >= $first_max_MA){
return 1;
}
}
return 0;
}
sub acos { atan2( sqrt(1 - $_[0] * $_[0]), $_[0] ) }
#夹角 = acos( (dx1*dx2+dy1*dy2)/sqrt((dx1*dx1++dy1*dy1)*(dx2*dx2+dy2*dy2)) )
#还没有找正确的计算方法
sub _angle{
my ($dx1,$dy1,$dx2,$dy2)=@_;
#my $angle = acos(($dx1*$dx2+$dy1*$dy2)/sqrt(($dx1*$dx1+$dy1*$dy1)*($dx2*$dx2+$dy2*$dy2)));
#$angle = ($dy1-$dy2)/($dx1-$dx2);
#return $angle;
}
sub _is_MA_cross{
my ($dhe,$code,$last_day,$min_MA_day_count,$max_MA_day_count,$duration)=@_;
if(my @days = DBT_get_earlier_exchange_days($dhe,$code,$last_day,$duration)){
my $last_exchange_day = $days[0];
my $first_exchange_day = $days[$#days];
my $last_min_MA = _MA($code,$dhe,$last_exchange_day,$min_MA_day_count);
my $last_max_MA = _MA($code,$dhe,$last_exchange_day,$max_MA_day_count);
my $first_min_MA = _MA($code,$dhe,$first_exchange_day,$min_MA_day_count);
my $first_max_MA = _MA($code,$dhe,$first_exchange_day,$max_MA_day_count);
#有正斜角
if(($last_min_MA - $last_max_MA)> ($first_min_MA - $first_max_MA)){
return 1;
}
if($last_min_MA >= $last_max_MA && $first_min_MA <= $first_max_MA){
return 1;
}
if($last_min_MA <= $last_max_MA && $first_min_MA >= $first_max_MA){
return 1;
}
}
return 0;
}
sub _day_J_OF_KDJ{
my ($code,$dhe,$date,$KDJ_duration)=@_;
my @tmp_day = DBT_get_earlier_exchange_days($dhe,$code,$date,$KDJ_duration);
my $kdj_start_day = $tmp_day[$#tmp_day];
my $period=9;
return _J_OF_KDJ($code,$date,$period,$dhe,$kdj_start_day);
}
sub _day_D_OF_KDJ{
my ($code,$dhe,$date,$KDJ_duration)=@_;
my @tmp_day = DBT_get_earlier_exchange_days($dhe,$code,$date,$KDJ_duration);
my $kdj_start_day = $tmp_day[$#tmp_day];
my $period=9;
return _D_OF_KDJ($code,$date,$period,$dhe,$kdj_start_day);
}
sub _day_K_OF_KDJ{
my ($code,$dhe,$date,$KDJ_duration)=@_;
my @tmp_day = DBT_get_earlier_exchange_days($dhe,$code,$date,$KDJ_duration);
my $kdj_start_day = $tmp_day[$#tmp_day];
my $period=9;
return _K_OF_KDJ($code,$date,$period,$dhe,$kdj_start_day);
}
sub _is_valid_KDJ_cross{
my ($code,$dhe,$date,$duration)=@_;
my $KDJ_duration = 30;
my @tmp_day = DBT_get_earlier_exchange_days($dhe,$code,$date,$duration);
if(@tmp_day){
my $start_day = $tmp_day[$#tmp_day];
my $D_start = _day_D_OF_KDJ($code,$dhe,$start_day,$KDJ_duration);
my $D_last = _day_D_OF_KDJ($code,$dhe,$date,$KDJ_duration);
if($D_last <= $D_start){
return 0;
}
#确定J线是否与D线交叉
my $J_start = _day_J_OF_KDJ($code,$dhe,$start_day,$KDJ_duration);
my $J_last = _day_J_OF_KDJ($code,$dhe,$date,$KDJ_duration);
if($J_last <= $J_start){
return 0;
}
if(($J_last - $D_last)< ($J_start - $D_start)){
return 0;
}
if($J_last <= $D_last || $J_start > $D_start){
return 0;
}
#确定K线是否与D线交叉
my $K_start = _day_K_OF_KDJ($code,$dhe,$start_day,$KDJ_duration);
my $K_last = _day_K_OF_KDJ($code,$dhe,$date,$KDJ_duration);
if($K_last <= $K_start){
return 0;
}
if(($K_last - $D_last)< ($K_start - $D_start)){
return 0;
}
if($K_last <= $D_last || $K_start > $D_start){
return 0;
}
return 1;
}
return 0;
}
#金叉
sub _is_mode2
{
my ($dhe,$code,$date,$level)=@_;
my $min_day_count=5;
my $max_day_count=10;
my $duration=5;
#离当前最多天中必有交叉
my $max_cross_day=3;
my @date = DBT_get_earlier_exchange_days($dhe,$code,$date,1);
if(!@date){
return 0;
}
$date = $date[0];
#检测是平均值否一直在上升状态
if(!_is_MA_UP($dhe,$code,$date,$min_day_count,$duration)){
return 0;
}
if(!_is_MA_UP($dhe,$code,$date,$max_day_count,$duration)){
return 0;
}
#检测小均值是否大于大均值
my $tmp_min = _MA($code,$dhe,$date,$min_day_count);
my $tmp_max = _MA($code,$dhe,$date,$max_day_count);
if($tmp_max >= $tmp_min){
return 0;
}
#检测平均值是否有交叉
if(!_is_MA_cross($dhe,$code,$date,$min_day_count,$max_day_count,$max_cross_day)){
return 0;
}
#检测交易量是否有交叉
if(!_is_MA_volume_cross($dhe,$code,$date,$min_day_count,$max_day_count,$max_cross_day)){
return 0;
}
#检测是否KDJ有效交叉
if(!_is_valid_KDJ_cross($code,$dhe,$date,$max_cross_day)){
return 0;
}
return 1;
}
sub _select_codes
{
my $stockcodefile=shift;
my $stock_cnt=shift;
my @codes;
my $code;
my $dhe=MSH_OpenDB($StockExDb);
my $dhi=MSH_OpenDB($StockInfoDb);
#流通市值限制
my $circulation_value_limit=1;
my $codeiterator;
SCOM_start_code_iterate(\$codeiterator,COM_get_fromcode());
while($code = SCOM_iterator_get_code(\$codeiterator)){
my $code_info=();
chomp $code;
my $date="2052-12-31";
my $data_start_day="2012-01-01";
if (defined $g_selectcode_date){
$date = $g_selectcode_date;
}
my @last_exchange_data_day=DBT_get_earlier_exchange_days($dhe,$code,$date,3);
$date=$last_exchange_data_day[0];
if(not $date){
COM_WARN("skip $code ,date is not suitable!","\r\n");
next;
}
my $yesterday=$last_exchange_data_day[1];
my $last_exchange_day = DBT_get_last_exchange_day($dhe,$code);
next if(!$last_exchange_day);
$code_info=join(':',$code,$date);
if($circulation_value_limit){
#对流通市值做限制
my $cur_price=DBT_get_closing_price($code,$last_exchange_day,$dhe);
my $liutongshizhi=$cur_price*DBT_get_exchange_stockts($code,$dhi);
my $billion=1000000000 ;
my $million=1000000 ;
if($liutongshizhi>18*$billion or $liutongshizhi <40*$million){
my $mb=sprintf("%.3f",$liutongshizhi/$billion);
COM_WARN("Skip $code:market value : $mb billion\n");
next;
}
}
if($gflag_selectcode_macd){
#my $macd=_MACD(12,26,9,$code,$dhe,"2011-01-01",$date);
my $macd= _MACD_DEALITTLETHAN(12,26,9,$code,$dhe,$data_start_day,$date,1);
next if(!$macd);
my $macd1=_MACD(12,26,9,$code,$dhe,$data_start_day,$last_exchange_data_day[1]);
#next if($macd < 0.03 || $macd <$macd1 );
next if($macd <$macd1 );
my $macd2=_MACD(12,26,9,$code,$dhe,$data_start_day,$last_exchange_data_day[2]);
next if($macd1<$macd2);
COM_WARN($code,":$date:MACD:$macd","\n");
#push @codes,join(":",$code,$date,"MACD",$macd);
}
if($gflag_selectcode_kdj){
my $period=9;
my @days=DBT_get_earlier_exchange_days($dhe,$code,$date,30);
@days=reverse @days;
#$date="2012-03-15";
my $kdj_start_day=$days[0];
my $K=_K_OF_KDJ($code,$date,$period,$dhe,$kdj_start_day);
my $YK=_K_OF_KDJ($code,$yesterday,$period,$dhe,$kdj_start_day);
my $D=_D_OF_KDJ($code,$date,$period,$dhe,$kdj_start_day);
my $YD=_D_OF_KDJ($code,$yesterday,$period,$dhe,$kdj_start_day);
my $J=_J_OF_KDJ($code,$date,$period,$dhe,$kdj_start_day);
print join(":",$code,$date,"K:",$K,"D:",$D,"J:",$J),"\n";
if($YK - $YD < $K - $D){
print join(":",$code,$date,"YK:",$YK,"YD:",$YD,"J:",$J),"\n";
$code_info=join(":",$code_info,"K",$K,"D",$D,"J",$J);
}else{
next;
}
}
if($gflag_selectcode_dig){
if(!_is_diging($dhe,$code,$date)){
next;
}
}
if($gflag_selectcode_break_surge){
if(!_is_break_surge($dhe,$code,$date)){
next;
}
}
if($gflag_selectcode_mode){
if($g_selectcode_mode == 1 && !_is_mode1($dhe,$code,$date,$gflag_selectcode_level)){
next;
}elsif ($g_selectcode_mode == 2 && !_is_mode2($dhe,$code,$date,$gflag_selectcode_level)){
next;
}elsif ($g_selectcode_mode == 3 && !_is_mode3($dhe,$code,$date,$gflag_selectcode_level)){
next;
}elsif ($g_selectcode_mode == 4 && !_is_mode4($dhe,$code,$date,$gflag_selectcode_level)){
next;
}
}
push @codes,$code_info;
last if(@codes >= $stock_cnt);
}
$dhe->disconnect;
$dhi->disconnect;
return @codes;
}
sub _get_all_bought_stocks{
my @codes;
#读取信息文件
open IN,"<",$BuyStockCode;
while(<IN>){
my @codeinfo=split($code_property_separator,$_);
my $code;
if(@codeinfo and COM_get_property(\@codeinfo,'code',\$code) and SCOM_is_valid_code($code)){
push @codes,$code;
}
}
close IN;
return @codes;
}
sub _delete_buy_code{
my ($code)=@_;
my @buycodes;
#读取信息文件
if(open IN,"<",$BuyStockCode){
while(<IN>){
chomp $_;
my @info=split($code_property_separator,$_);
my $value;
if(@info && COM_get_property(\@info,'code',\$value) && index($code,$value)!=0){
push @buycodes,$_;
}
}
close IN;
}
#保存到文件
open OUT,">",$BuyStockCode;
syswrite(OUT,join("\n",@buycodes));
close OUT;
}
sub _add_buy_code_info{
my (@codeinfo)=@_;
my $order=join($code_property_separator,@codeinfo);
#保存到文件
open OUT,">>",$BuyStockCode;
syswrite(OUT,"\n");
syswrite(OUT,$order);
close OUT;
return 1;
}
sub _get_buy_code_info{
my ($code,$flag)=@_;
my @info;
#读取信息文件
open IN,"<",$BuyStockCode;
while(<IN>){
@info=split($code_property_separator,$_);
my $value;
if(@info && COM_get_property(\@info,'code',\$value) && index($code,$value)==0){
last;
}
}
close IN;
if(@info and !$flag){
return @info;
}
my $value;
if(COM_get_property(\@info,$flag,\$value)){
return $value;
}
return undef;
}
sub _add_property{
my ($ref_info,$key,$value)=@_;
push @{$ref_info},join($code_property_assignment,$key,$value);
}
sub _remove_property{
my ($ref_info,$key,$value)=@_;
if($ref_info and $key){
COM_get_command_line_property(\@{$ref_info},$key);
}
}
sub _buy{
my ($code,$price,$total,$stoploss,$importantprice)=@_;
if(!$importantprice){
$importantprice=$price*1.05;
}
if(!defined $stoploss){
$stoploss=$price*0.98;#将止损点设在98%
}
_delete_buy_code($code);
my @codeinfo;
#push @codeinfo,$code;
_add_property(\@codeinfo,'code',$code);
#push @codeinfo,$price;
_add_property(\@codeinfo,'price',$price);
#push @codeinfo,$total;
_add_property(\@codeinfo,'total',$total);
#push @codeinfo,$stoploss;
_add_property(\@codeinfo,'stoploss',$stoploss);
#push @codeinfo,$importantprice;
_add_property(\@codeinfo,'importantprice',$importantprice);
_AMI($code);
return _add_buy_code_info(@codeinfo);
}
sub _get_code_monitor_info_file{
my ($code,$flag)=@_;
return SCOM_code_get_file_name($code,$flag);
}
sub _log{
my ($logfile,$msg)=@_;
open OUT,">>",$logfile;
syswrite(OUT,"\n");
syswrite(OUT,$msg);
close OUT;
}
sub _sms{
my ($flag0,$flag1,$flag2,$flag3)=@_;
#cliofetion
#if($flag3){
# system("cliofetion -f $flag0 -p $flag1 -t $flag2 -d \"$flag3\"");
#}else{
# system("cliofetion -f $flag0 -p $flag1 -d \"$flag2\"");
#}
#python
if($flag3){
system("python2 pywapfetion/bobfetion.py $flag0 $flag1 $flag2 \"$flag3\"");
}else{
system("python2 pywapfetion/bobfetion.py $flag0 $flag1 \"$flag2\"");
}
}
sub _report_code{
my ($code,$msg)=@_;
printf $msg."\n";
my $flag0=COM_get_flag(0,"flag");
my $flag1=COM_get_flag(1,"flag");
my $flag2=COM_get_flag(2,"flag");
_sms($flag0,$flag1,$msg);
#_sms($flag0,$flag1,'13823510132',$msg);
_log( _get_code_monitor_info_file($code,'log'),$msg);
}
sub _construct_code_day_header{
my ($code,$type)=@_;
if(SCOM_is_valid_code($code)){
my $day=COM_today(0);
return "$day:$code:$type";
}
return undef;
}
sub _construct_code_header{
my ($code,$type)=@_;
if(SCOM_is_valid_code($code)){
return "$code:$type";
}
return undef;
}
sub _is_exchange_info_loged{
my ($code,$logflag)=@_;
if(open (IN,'<', _get_code_monitor_info_file($code,'log'))){
foreach my $line(<IN>){
if(index($line,$logflag)!=-1){
close IN;
return 1;
}
}
}
close IN;
return 0;
}
sub _is_today_loged{
my ($logflag)=@_;
if(open (IN,'<',COM_today(1))){
foreach my $line(<IN>){
if(index($line,$logflag)!=-1){
close IN;
return 1;
}
}
}
close IN;
return 0;
}
sub _monitor_bought_stock{
my ($code,$ref_monitor_info)=@_;
if($code){
if (!SCOM_today_is_exchange_day()){
return;
}
my $tip_percent_average_diff=0.005;
my $tip_percent_reported_diff=0.005;
my $tip_percent_fore_diff=0.007;
my $cur_price=SN_get_stock_cur_price($code);
my $buyprice= _get_buy_code_info($code,'price');
my $stoploss = _get_buy_code_info($code,'stoploss');
my $total = _get_buy_code_info($code,'total');
my $importantprice= _get_buy_code_info($code,'importantprice');
my $percent=($importantprice-$buyprice)/$buyprice;
my $K=int((($cur_price-$buyprice)/$buyprice)/$percent);
my $hour=COM_get_cur_time('hour');
my $minute=COM_get_cur_time('minute');
my $income= SCOM_calc_income($code,$buyprice,$cur_price,$total);
my $average=\$ref_monitor_info->{average_price};
my $fore_price=\$ref_monitor_info->{fore_price};
my $reported_price=\$ref_monitor_info->{reported_price};
$income=sprintf("%.2f",$income);
chomp $stoploss;
#交易期间检测
if (SCOM_is_exchange_duration($hour,$minute)){
#为了减少SCOM_is_suspension函数的联网先判断$cur_price是否为0
if($cur_price==0){
if( SCOM_is_suspension($code)){
my $last_close_price=SN_get_stock_last_close_price($code);
$income= SCOM_calc_income($code,$buyprice,$last_close_price,$total);
$income=sprintf("%.2f",$income);
if( !_is_exchange_info_loged($code,_construct_code_day_header($code,'suspension'))){
my $reportstr=_construct_code_day_header($code,'suspension').":($buyprice:$last_close_price:$income)";
_report_code($code,$reportstr);