/
cxStockLogData.py
1030 lines (872 loc) · 41 KB
/
cxStockLogData.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
################################################################################
# -*- coding: utf-8 -*-
# author : Jinwon Oh
# file name : cxStockLogData.py
# date : 2012-10-15 14:19:04
# ver :
# desc. :
# tab size : set sw=4, ts=4
# python ver. : 2.7.1 Stackless 3.1b3 060516 (release27-maint, Jan 1 2011, 13:04:37) [MSC v.1500 32 bit (Intel)]
# ADD CODES FROM HERE
from common import testBlockRequest
from common import templateBlockRequest
from common import getResultDibStatus
from common import getResultDibMsg1
from common import getResultContinue
from common import getResultTime
from common import getResultClassName
from common import checkFileExist
from cxCybosPlus import getCybosPlusClassDic
from cxCybosPlus import constants
from cxFile import cxFile
import time
class cxStockLogData :
chartTypeDic = {
'Day' : 'D',
'Week' : 'W',
'Month' : 'M',
'Minute' : 'm',
'Tick' : 'T'
}
cpClsDic = getCybosPlusClassDic()
#failedList = []
def __init__(self) :
self.failedList = []
def __del__(self) :
pass
def loadLogData(self, stockCode, chartType ) :
try :
ct = self.chartTypeDic[chartType]
except KeyError :
print 'ERROR: cxStockLogData.loadLogData : param chartType : "%s" is not valid.\n'%\
(chartType)
return None
cpStockCode = self.cpClsDic['cxCpStockCode']
stockName = cpStockCode.CodeToName(stockCode)
#print '"%s"'%(stockName)
if stockName == u'' :
print 'Can not find stock name for stock code "%s".'%(stockCode)
return None
path = 'log\\%s\\'%(chartType.lower())
fileName = u'%s%s_%s.log'%(path,stockCode,stockName)
print fileName
if checkFileExist(fileName) == False : return None
dataFile = cxFile(fileName)
tmpList = []
dataList = []
i = 0
for lines in dataFile.readlines() :
if i < 5 :
i += 1
continue
tmpList = []
for item in lines[:-1].split() :
tmpList.append(item)
dataList.append(tmpList)
dataFile.close()
del dataFile
#print 'len of dataList', len(dataList)
#print dataList[0]
#print dataList[len(dataList)-1]
return dataList
def makeLogData(self, stockCode, chartType ) :
cpStockCode = self.cpClsDic['cxCpStockCode']
stockName = cpStockCode.CodeToName(stockCode)
#print '"%s"'%(stockName)
if stockName == u'' :
print 'Can not find stock name for stock code "%s".'%(stockCode)
return False
path = 'log\\%s\\'%(chartType.lower())
fileName = u'%s%s_%s.log'%(path,stockCode,stockName)
if checkFileExist(fileName) == True :
return True
try :
ct = self.chartTypeDic[chartType]
except KeyError :
print 'ERROR: cxStockLogData.makeLogData : param chartType : "%s" is not valid.\n'%\
(chartType)
return False
fieldList = [
0, # 날짜
1, # 시간
3, # 고가
4, # 저가
5, # 종가
8, # 거래량
9, # 거래대금
25, # 주식회전율
]
#if ct == 'T' or ct == 'm' :
# fieldList += [1] # 시간 - hhmm
paramList = [
[ 0, stockCode ],
[ 1, ord(u'1') ], # 기간요청
[ 3, 19500101 ],
[ 4, len(fieldList) ],
[ 5 ] + fieldList,
[ 6, ord(ct) ], # 차트종류
[ 9, ord(u'1') ], # 수정주가
[ 10, ord(u'3') ] # 시간외거래량 모두 제외
]
stockChart = getCybosPlusClassDic()[u'cxStockChart']
resultList = templateBlockRequest( stockChart, paramList )
if resultList == None :
print 'templateBlockRequest("cxStockChart"): result is none.\n'
return False
if len(resultList) == 0 :
print 'templateBlockRequest("cxStockChart") : result length is zero.\n'
return False
bFirst = 1
fieldNum = 0
storeList = []
dataNum = 0
for result in resultList :
if getResultDibStatus(result) != 0 :
break
headerList = result[5]
if len(headerList) == 0 :
print 'header result is empty.'
continue
dataNum += headerList[0][3][2]
print 'dataNum', dataNum, type(dataNum)
if bFirst == 1 :
fieldNum = headerList[0][1][2]
fieldNameList = headerList[0][2][2]
print 'fieldNum', fieldNum, type(fieldNum)
print 'fieldNameList'
for fieldName in fieldNameList :
print fieldName,
print
storeList.insert(3,fieldNum)
storeList.insert(4,fieldNameList)
bFirst = 0
dataList = result[6]
print 'len of dataList', len(dataList)
if len(dataList) == 0 :
print 'failed to make data list for %s'%(stockCode)
self.failedList.append([stockCode,chartType])
return True
#return False
tmpList = []
for dataDic in dataList :
tmpList = []
for fieldType in range( 0, fieldNum ) :
key = stockChart.fieldNameDic[fieldNameList[fieldType]]
tmpList.append( dataDic[key][2] )
storeList.append(tmpList)
storeList.insert(0,dataNum)
storeList.insert(1,storeList[len(storeList)-1][0])
storeList.insert(2,storeList[4][0])
print 'len of storeList',len(storeList)
#print storeList
"""
[0] : dataNum
[1] : start date
[2] : end date
[3] : fieldNum
[4] : field Name List
[5] ~ : data
"""
print fileName
dataFile = cxFile(fileName)
dataFile.write('%s\n'%(storeList[0]))
dataFile.write('%s\n'%(storeList[1]))
dataFile.write('%s\n'%(storeList[2]))
dataFile.write('%s\n'%(storeList[3]))
for fieldName in storeList[4] :
dataFile.write('%s\t'%(fieldName))
dataFile.write('\n')
tmpList = storeList[5:]
tmpList.reverse()
for itemList in tmpList :
for item in itemList :
dataFile.write('%s\t'%(item))
dataFile.write('\n')
dataFile.close()
del dataFile
return True
def makeAllStockLogData(self) :
from cxStockMgr import cxStockMgr
from cxCybosPlus import cxCpCybos
cpCybos = cxCpCybos()
stockMgr = cxStockMgr()
stockListLen = stockMgr.update()
if stockListLen == 0 :
print 'cxStockMgr.update : error occured.'
return
print 'stockListLen : ', stockListLen
for stock in stockMgr.stockList :
for option in ['Day','Minute','Tick'] :
if self.makeLogData(stock[0], option) == False :
print '%s\t%s : makeLogData for %s : failed'%(stock[0],stock[1],option)
return
remainCount = cpCybos.GetLimitRemainCount(constants.LT_NONTRADE_REQUEST)
remainTime = cpCybos.LimitRequestRemainTime()
print 'remainCount : %d, remainTime : %d'%(remainCount,remainTime)
if remainCount <= 1 :
print 'time.sleep for %d'%(remainTime)
time.sleep(remainTime)
def testStrategy001(self, dataList ) :
resultFile = cxFile('log\\day\\st01.txt')
dataListLen = len(dataList)
if dataListLen == 0 :
print 'data가 없습니다.'
return
desc = u'현재가가\n20일 고가보다 높을 때 사고,\n20일 저가보다 낮을 때 판다.\n'
flagBuy = False
earningMoney = 0
buyCount = 0
buyedMoney = 0
maxBuyedMoney = 0
for i in range( 20, dataListLen ) :
data = dataList[i]
date = data[0]
currentValue = int(data[4])
avr = 0
total = 0
maxValue = 0
minValue = 10000000000
for j in range( i - 20, i ) :
oldValue = int(dataList[j][3])
total += oldValue
if maxValue < oldValue :
maxValue = oldValue
if minValue > oldValue :
minValue = oldValue
avr = int(total/20)
resultFile.write(u'%s:current:%d,avr:%d,20s min:%d,20s max:%d\n'%(date,
currentValue,
avr,
minValue,
maxValue))
if (currentValue > maxValue) and (flagBuy == False ) :
resultFile.write(u'BUY at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n' \
%(currentValue,
maxValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = True
buyedMoney += currentValue
buyCount += 1
maxBuyedMoney = max(maxBuyedMoney, buyedMoney)
elif flagBuy == True and currentValue < minValue :
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue,
minValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney,
maxBuyedMoney,
float(earningMoney)/float(maxBuyedMoney)*(100.0)))
resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataList[0][0],
dataList[dataListLen-1][0],
int(dataList[0][4]),
int(dataList[dataListLen-1][4]),
int(int(dataList[dataListLen-1][4])/int(dataList[0][4]))))
resultFile.write(desc)
resultFile.close()
print
def testStrategy002(self, dataList ) :
resultFile = cxFile('log\\day\\st02.txt')
dataListLen = len(dataList)
if dataListLen == 0 :
print 'data가 없습니다.'
return
desc = u'현재가가\n20일 고가보다 높을 때 사고,\n20일 평균가보다 낮을 때 판다.\n'
flagBuy = False
earningMoney = 0
buyCount = 0
buyedMoney = 0
maxBuyedMoney = 0
for i in range( 20, dataListLen ) :
data = dataList[i]
date = data[0]
currentValue = int(data[3])
avr = 0
total = 0
maxValue = 0
minValue = 10000000000
for j in range( i - 20, i ) :
oldValue = int(dataList[j][3])
total += oldValue
if maxValue < oldValue :
maxValue = oldValue
if minValue > oldValue :
minValue = oldValue
avr = int(total/20)
resultFile.write(u'%s:current:%d,avr:%d,20s min:%d,20s max:%d\n'%(date,
currentValue,
avr,
minValue,
maxValue))
if (currentValue > maxValue) and (flagBuy == False ) :
resultFile.write(u'BUY at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n' \
%(currentValue,
maxValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = True
buyedMoney += currentValue
buyCount += 1
maxBuyedMoney = max(maxBuyedMoney, buyedMoney)
elif flagBuy == True and currentValue < avr :
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d for av:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue,
avr,
buyedMoney,
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney,
maxBuyedMoney,
float(earningMoney)/float(maxBuyedMoney)*(100.0)))
resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataList[0][0],
dataList[dataListLen-1][0],
int(dataList[0][3]),
int(dataList[dataListLen-1][3]),
int(int(dataList[dataListLen-1][3])/int(dataList[0][3]))))
resultFile.write(desc)
resultFile.close()
print
def testStrategy003(self, dataList ) :
resultFile = cxFile('log\\day\\st03.txt')
dataListLen = len(dataList)
if dataListLen == 0 :
print 'data가 없습니다.'
return
desc = u'현재가가\n20일 평균가보다 높을 때 사고,\n20일 평균가보다 낮을 때 판다.\n'
flagBuy = False
earningMoney = 0
buyCount = 0
buyedMoney = 0
maxBuyedMoney = 0
for i in range( 20, dataListLen ) :
data = dataList[i]
date = data[0]
currentValue = int(data[3])
avr = 0
total = 0
maxValue = 0
minValue = 10000000000
for j in range( i - 20, i ) :
oldValue = int(dataList[j][3])
total += oldValue
if maxValue < oldValue :
maxValue = oldValue
if minValue > oldValue :
minValue = oldValue
avr = int(total/20)
resultFile.write(u'%s:current:%d,avr:%d,20s min:%d,20s max:%d\n'%(date,
currentValue,
avr,
minValue,
maxValue))
if (currentValue > avr) and (flagBuy == False ) :
resultFile.write(u'BUY at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n' \
%(currentValue,
avr,
buyedMoney,
earningMoney,
buyCount))
flagBuy = True
buyedMoney += currentValue
buyCount += 1
maxBuyedMoney = max(maxBuyedMoney, buyedMoney)
elif flagBuy == True and currentValue < avr :
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d for av:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue,
avr,
buyedMoney,
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney,
maxBuyedMoney,
float(earningMoney)/float(maxBuyedMoney)*(100.0)))
resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataList[0][0],
dataList[dataListLen-1][0],
int(dataList[0][3]),
int(dataList[dataListLen-1][3]),
int(int(dataList[dataListLen-1][3])/int(dataList[0][3]))))
resultFile.write(desc)
resultFile.close()
print
def testStrategy004(self, dataList ) :
resultFile = cxFile('log\\day\\st04.txt')
dataListLen = len(dataList)
if dataListLen == 0 :
print 'data가 없습니다.'
return
desc = u'현재가가\n20일 고가보다 높을 때 사고 (누적),\n20일 평균가보다 낮을 때 판다.\n'
flagBuy = False
earningMoney = 0
buyCount = 0
buyedMoney = 0
maxBuyedMoney = 0
for i in range( 20, dataListLen ) :
data = dataList[i]
date = data[0]
currentValue = int(data[3])
avr = 0
total = 0
maxValue = 0
minValue = 10000000000
for j in range( i - 20, i ) :
oldValue = int(dataList[j][3])
total += oldValue
if maxValue < oldValue :
maxValue = oldValue
if minValue > oldValue :
minValue = oldValue
avr = int(total/20)
resultFile.write(u'%s:current:%d,avr:%d,20s min:%d,20s max:%d\n'%(date,
currentValue,
avr,
minValue,
maxValue))
if (currentValue > maxValue) : #and (flagBuy == False ) :
resultFile.write(u'BUY at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n' \
%(currentValue,
maxValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = True
buyedMoney += currentValue
buyCount += 1
maxBuyedMoney = max(maxBuyedMoney, buyedMoney)
elif flagBuy == True and currentValue < avr :
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d for av:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue,
avr,
buyedMoney,
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney,
maxBuyedMoney,
float(earningMoney)/float(maxBuyedMoney)*(100.0)))
resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataList[0][0],
dataList[dataListLen-1][0],
int(dataList[0][3]),
int(dataList[dataListLen-1][3]),
int(int(dataList[dataListLen-1][3])/int(dataList[0][3]))))
resultFile.write(desc)
resultFile.close()
print
def testStrategy005(self, dataList ) :
resultFile = cxFile('log\\day\\st05.txt')
dataListLen = len(dataList)
if dataListLen == 0 :
print 'data가 없습니다.'
return
desc = u'현재가가\n산 가격의 20% 이상 오를 때 이익실현하고\n산 가격의 5% 이하일 때 청산한다.'
flagBuy = False
earningMoney = 0
buyCount = 0
buyedMoney = 0
maxBuyedMoney = 0
for i in range( 20, dataListLen ) :
data = dataList[i]
date = data[0]
currentValue = int(data[3])
avr = 0
total = 0
maxValue = 0
minValue = 10000000000
for j in range( i - 20, i ) :
oldValue = int(dataList[j][3])
total += oldValue
if maxValue < oldValue :
maxValue = oldValue
if minValue > oldValue :
minValue = oldValue
avr = int(total/20)
resultFile.write(u'%s:current:%d,avr:%d,20s min:%d,20s max:%d\n'%(date,
currentValue,
avr,
minValue,
maxValue))
if (currentValue > maxValue) and (flagBuy == False ) :
resultFile.write(u'BUY at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n' \
%(currentValue,
maxValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = True
buyedMoney = currentValue
buyCount += 1
maxBuyedMoney = max(maxBuyedMoney, buyedMoney)
elif flagBuy == True and \
( currentValue > int(float(buyedMoney)*1.2) ) :
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d for av:%d in > bm:%d (em:%d, bc:%d)\n'%
( currentValue,
avr,
int(float(buyedMoney)*1.2),
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
elif flagBuy == True and \
( currentValue < int(float(buyedMoney)*0.95) ):
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d for av:%d in < bm:%d (em:%d, bc:%d)\n'%
( currentValue,
avr,
int(float(buyedMoney)*0.95),
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney,
maxBuyedMoney,
float(earningMoney)/float(maxBuyedMoney)*(100.0)))
resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataList[0][0],
dataList[dataListLen-1][0],
int(dataList[0][3]),
int(dataList[dataListLen-1][3]),
int(int(dataList[dataListLen-1][3])/int(dataList[0][3]))))
resultFile.write(desc)
resultFile.close()
print
def testStrategy006(self, dataList ) :
resultFile = cxFile('log\\day\\st06.txt')
dataListLen = len(dataList)
if dataListLen == 0 :
print 'data가 없습니다.'
return
desc = u'현재가가\n20일 고가의 75%에서 사서, 20일 저가의 125%에서 판다.\n'
flagBuy = False
earningMoney = 0
buyCount = 0
buyedMoney = 0
maxBuyedMoney = 0
for i in range( 20, dataListLen ) :
data = dataList[i]
date = data[0]
currentValue = int(data[3])
avr = 0
total = 0
maxValue = 0
minValue = 10000000000
for j in range( i - 20, i ) :
oldValue = int(dataList[j][3])
total += oldValue
if maxValue < oldValue :
maxValue = oldValue
if minValue > oldValue :
minValue = oldValue
avr = int(total/20)
resultFile.write(u'%s:current:%d,avr:%d,20s min:%d,20s max:%d\n'%(date,
currentValue,
avr,
minValue,
maxValue))
buyValue = int(float(maxValue)*0.75)
sellValue = int(float(minValue)*1.25)
if (currentValue > buyValue )and (flagBuy == False ) :
resultFile.write(u'BUY at cv:%d > bv:%d in bm:%d (em:%d, bc:%d)\n' \
%(currentValue,
buyValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = True
buyedMoney = currentValue
buyCount += 1
maxBuyedMoney = max(maxBuyedMoney, buyedMoney)
elif flagBuy == True and \
( currentValue > sellValue ) :
#(currentValue < sellValue ) :
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d < sv:%d in bm:%d (em:%d, bc:%d)\n'%
( currentValue,
sellValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney,
maxBuyedMoney,
float(earningMoney)/float(maxBuyedMoney)*(100.0)))
resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataList[0][0],
dataList[dataListLen-1][0],
int(dataList[0][3]),
int(dataList[dataListLen-1][3]),
int(int(dataList[dataListLen-1][3])/int(dataList[0][3]))))
resultFile.write(desc)
resultFile.close()
print
def test_getStockDayData() :
from cxCybosPlus import cxCpStockCode
from cxFile import cxFile
#stockCode = u'A000660' #하이닉스
#stockCode = u'A005930' #삼성전자
stockCode = u'A005380' #현대자동차
#stockCode = u'A004990' #롯데제과
cpStockCode = cxCpStockCode()
stockName = cpStockCode.CodeToName(stockCode)
chartType = u'D'
fileName = u'%s_%s_%s.log'%(stockCode, stockName, chartType)
refreshLog = 1
if refreshLog == 1 :
resultFile = cxFile(fileName)
fieldList = [
0, # 날짜
3, # 고가
4, # 저가
5, # 종가
8, # 거래량
9, # 거래대금
25, # 주식회전율
]
paramList = [
[ 0, stockCode ],
[ 1, ord(u'1') ],
[ 3, 19920901 ],
[ 4, len(fieldList) ],
[ 5 ] + fieldList,
[ 6, ord(chartType) ],
[ 9, ord(u'1') ], # 수정주가
[ 10, ord(u'3') ]
]
testBlockRequest(u'cxStockChart', paramList, 0, 0, 1, 1, resultFile )
resultFile.close()
resultFile = cxFile(fileName)
lines = resultFile.readlines()
"""
value = lines[1].split(u'\t')
print value
print value[0], value[3]
"""
dataLog = []
for i in range(0, len(lines)) :
if i == 0 : continue
value = lines[i].split(u'\t')
#print value[0],value[3]
dataLog.append( [ value[0], value[3] ] )
resultFile.close()
dataLogLen = len(dataLog)
print dataLogLen
flagBuy = False
earningMoney = 0
resultFile = cxFile()
buyCount = 0
buyedMoney = 0
maxBuyedMoney = 0
for i in range(dataLogLen -1 -20, -1, -1 ) :
currentValue = int(dataLog[i][1])
avr = 0
total = 0
maxValue = 0
minValue = 10000000000
#resultFile.write(u'%d\t'%(i))
resultFile.write(u'%s\t'%(dataLog[i][0]))
for j in range(i+20,i,-1) :
oldValue = int(dataLog[j][1])
#resultFile.write(u'%d(%s,%d),'%(j,dataLog[i][0],oldValue))
total += oldValue
if maxValue < oldValue :
maxValue = oldValue
if minValue > oldValue :
minValue = oldValue
resultFile.write(u' ')
avr = int(total/20)
resultFile.write(u'current:%d,avr:%d,20s min:%d,20s max:%d\n'%( currentValue,
avr,
minValue,
maxValue))
if (currentValue > maxValue) and (flagBuy == False) :
resultFile.write(u'BUY at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue,
maxValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = True
buyedMoney += currentValue
buyCount += 1
maxBuyedMoney = max(maxBuyedMoney, buyedMoney)
elif flagBuy == True and currentValue < minValue :
earningMoney += (currentValue*buyCount)-buyedMoney
resultFile.write(u'SELL at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue,
minValue,
buyedMoney,
earningMoney,
buyCount))
flagBuy = False
buyCount = 0
buyedMoney = 0
resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney,
maxBuyedMoney,
float(earningMoney)/float(maxBuyedMoney)*(100.0)))
resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataLog[dataLogLen-1][0],
dataLog[0][0],
int(dataLog[dataLogLen-1][1]),
int(dataLog[0][1]),
int(int(dataLog[0][1])/int(dataLog[dataLogLen-1][1]))))
print
def emulateTurtleTrade( dataList ) :
import math
avrNparam = 20 # 평균 N : 20 일 평균 N
totalAccount = 5000000 # 초기 전체 투자금 : 500만원
unitRatio = 0.02 # Unit 비율 : 거래당 최대 손실 한도 비율 : 전체 투자금의 2%
S1_entry = 20 # S1 진입 조건 : 20일 최고가
S1_exit = 10 # S1 탈출 조건 : 10일 최저가
S2_entry = 55 # S2 진입 조건 : 55일 최고가
S2_exit = 20 # S2 탈출 조건 : 20일 최저가
absExitRatio = 2.0 # 절대 exit 조건 : absExitRatio * N : 2N
print 'data length :', len(dataList)
# dataNum = len(dataList)
dataNum = 40
turtleDataList = []
N_List = []
N = 0
for i in range(0, dataNum) :
TR1 = 0
TR2 = 0
TR3 = 0
N = 0
# 오늘의 고가 - 오늘의 저가
TR1 = float(int(dataList[i][2]) - int(dataList[i][3]))
if i >= 1 :
# 어제의 종가 - 오늘의 고가
TR2 = math.fabs(int(dataList[i-1][4]) - int(dataList[i][2]))
# 어제의 종가 - 오늘의 저가
TR3 = math.fabs(int(dataList[i-1][4]) - int(dataList[i][3]))
N_List.append( max(TR1,TR2,TR3) )
if i >= avrNparam :
N = 0
for k in range(i - avrNparam,i) :
N += N_List[k]
N = float(N/float(avrNparam))
#print i,
#for j in range(0,5) :
# print dataList[i][j],
#print TR1,TR2,TR3,N_List[i],N
turtleDataList.append( [ dataList[i][0], # 0 : 년도월일.
int(dataList[i][2]), # 1 : 고가
int(dataList[i][3]), # 2 : 저가
int(dataList[i][4]), # 3 : 종가
TR1, # 4 : 오늘 고가 - 오늘 저가
TR2, # 5 : 어제 종가 - 오늘 고가
TR3, # 6 : 어제 종가 - 오늘 저가
max(TR1,TR2,TR3), # 7 : 오늘의 N
N # 8 : 오늘의 20일 평균 N
] )
#print
i = 0
for item in turtleDataList :
#print i,item
print '%d\t%d\t%d\t%f'%(item[1],item[2],item[3],item[8])
i += 1
print
"""
unit = totalAccount*unitRatio
S1_entry_value = 0
S1_exit_value = totalAccount # very very big money
S2_entry_value = 0
S2_exit_value = totalAccount # very very big money
for today in range(0,len(turtleDataList)) : # 매일
S1_entry_value = 0
S1_exit_value = totalAccount # very very big money
S2_entry_value = 0
S2_exit_value = totalAccount # very very big money
if today >= S1_entry : # 20일 최고가
for i in range( today - S1_entry, today ) :
S1_entry_value = max(S1_entry_value, turtleDataList[i][1])
if today >= S1_exit : # 10일 최저가
for i in range( today - S1_exit, today ) :
S1_exit_value = min(S1_exit_value, turtleDataList[i][2])
if today >= S2_entry : # 55일 최고가
for i in range( today - S2_entry, today ) :
S2_entry_value = max(S2_entry_value, turtleDataList[i][1])
if today >= S2_exit : # 20일 최저가
for i in range( today - S2_exit, today ) :
S2_exit_value = min(S2_exit_value, turtleDataList[i][2])
todayPrice = turtleDataList[today][3]
if flagBuyes == False :
if todayPrice > S1_entry_value :
tradeNum = int(unit/(2*( todayPrice + turtleDataList[i][8])))
else :
if todayPrice > S1_entry_value :
# 20일 최고가 상향 돌파
if flagSkip == True :
# 이전 신호로 이득보았기에 Skip하되 다음 번은 Skip안함
flagSkip = False
else if flagBuyed == False :
# 안 산 상태에서 20일 최고가 상향 돌파하였기에 entry한다.
flagBuyed = True
#
else if todayPrice < S1_exit_value and flagBuyed == True :
# 산 상태에서 10일 최저가 하향 돌파하기에 exit한다.
flagBuyed = False
else if todayPrice > S2_entry_value and flagBuyed == False :
# 안 산 상태에서 55일 최고가 상향 돌파하기에 entry한다.
# big trend filter
flagBuyed = True
else if todayPrice < S2_entry_value and flagBuyed == True :
# 산 상태에서 20일 최저가 하향 돌파하였기에 exit한다.
flagBuyed = False
if flagBuyed == True and todayPrice <= absExitRatio*buyedMoney :
# 산 상태에서 손실액이 2N이 넘으므로 무조건 exit한다.
flagBuyed = False
"""
def test_cxStockLogData() :
emul = cxStockLogData()
#emul.makeAllStockLogData()
#stockCode = u'A000660'
stockCode = u'A005930'
#emul.makeLogData(stockCode,u'Day')
dataList = emul.loadLogData(stockCode,u'Day')
emulateTurtleTrade(dataList)
#emul.testStrategy001(dataList)
#emul.testStrategy002(dataList)
#emul.testStrategy003(dataList)
#emul.testStrategy004(dataList)
#emul.testStrategy005(dataList)
#emul.testStrategy006(dataList)
#emul.makeLogData(u'001',u'Day')
#for failedItem in emul.failedList :
# print '[',failedItem[0],failedItem[1],']'
#print