-
Notifications
You must be signed in to change notification settings - Fork 1
/
MACD.py
145 lines (128 loc) · 3.8 KB
/
MACD.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
__author__ = 'Esmidth'
import numpy as np
import talib
import tushare as ts
import os
import IOput
import StringHandler as sh
import pandas as pd
import time
def MACDMethod(DataIn):
DataIn = DataIn.sort_index(ascending=True) # Must Sort Raw Data First Or Calculate in a reserved index
Log = False
"""
:type DataIn: DataFrame
:type Output:DataFrame
"""
buyLog = []
sellLog = []
DataOut = talib.abstract.MACD(DataIn, 12, 26, 9, 1)
# print(output[output > 0][output<0.03]['macdhist'])
i = 0
# print(output)
have = False
for x in DataOut['macdhist']:
if have == False and x > 0:
have = True
buyLog.append(i)
elif have == True and x < 0:
have = False
sellLog.append(i)
i += 1
i = 0
startFund = 100
for x in sellLog:
num = DataIn['close'][sellLog[i]] - DataIn['close'][buyLog[i]]
num = num / DataIn['close'][buyLog[i]]
startFund = startFund * (1 + num)
# print(startFund)
i += 1
# print(startFund)
if Log:
purchaseLog(DataIn, DataOut, buyLog, sellLog)
return startFund / 100
# days = DataIn.index[-1] - DataIn.index[0]
# startFund= startFund/100
# % per day
# return startFund / 100 # / (days.days)
def purchaseLog(inputs, outputs, buyLog, sellLog):
i = 0
for x in sellLog:
print("------------------------------------------------------")
print("Buy: \t", inputs.index[buyLog[i]], inputs['close'][buyLog[i]])
print("Sell: \t", inputs.index[sellLog[i]], inputs['close'][sellLog[i]])
num = (inputs['close'][sellLog[i]] - inputs['close'][buyLog[i]]) * 100 / inputs['close'][buyLog[i]]
print("%.3f%%" % num)
i += 1
def main1():
files = ['300104', '600080', '600081', '600083', '600084', '600085', '600086']
for x in files:
print('----------------------')
print(x)
print(MACDMethod(IOput.load(x + '.xlsx')))
# ori = ori.sort_index(ascending=True)
# print(ori)
# d = MACDMethod(ori)
def testAll(date):
path = 'DataBase' + date.__str__() + '/'
files = os.listdir(path)
dic = {}
profits = []
idd = 1
# lenth = len(sh.DataBase20151106)
length = len(files)
'''
for x in sh.DataBase20151106:
profit = MACDMethod(IO.load(path + x + '.xlsx')) * 100
dic[profit] = x
vals.append(profit)
print("%.2f%% %s Done\t Profit: %s%%" % (100 * i / lenth, x, profit))
i += 1
'''
for file in files:
profit = MACDMethod(IOput.load(path + file)) * 100
dic[profit] = file
profits.append(profit)
print("%.2f%% %s Done\t Profit: %s%%" % (100 * idd / length, file, profit))
idd += 1
profits = sorted(profits)
profits.reverse()
idd = 1
for profit in profits:
print("#%s\t%s:\t%.2f%%" % (idd, dic[profit], profit))
idd += 1
IOput.outputToExcel('2016_04_12', dic, profits)
if __name__ == "__main__":
'''
path = 'DataBase_20151106\\'
# IO.write('600086.xlsx','600086')
# ori = IO.load(path + '600080.xlsx')
ori = ts.get_hist_data('600080')
print(MACDMethod(ori))
'''
testAll(date=20170321)
# path = './DataBase20160510/'
# file = '600080.xlsx'
# ori = IO.load(path + file)
# ori = ts.get_hist_data('600080',start='2016-01-01')
# ori = ori.sort_index(ascending=True)
#print(MACDMethod(ori))
'''
dic = {}
dic['123123'] = 123123
dic['1000'] = 3
dic['2131231'] = 4
dic['123123a'] = -12312
'''
'''
x = sorted(dic.values())
x.reverse()
print(x)
for y in x:
print(dic[y],y)
df = IO.load(path+'600080'+'.xlsx')
print(type(df.index[1]))
print(df.index[0])
dd = time.strftime(df.index[0],'%Y-%m-%d')
print(type(dd))
'''