-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade.py
61 lines (54 loc) · 1.43 KB
/
trade.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
#########################################
################ 策略1 ##################
#########################################
估价高于20日均线买,低于20日均线卖
import tushare as ts
import matplotlib.pyplot as plt
def parse(STOCK):
is_buy = 0
buy_val = []
buy_date=[]
sell_val=[]
sell_date=[]
df = ts.get_hist_data(STOCK)
ma20=df['ma20']
close=df['close']
rate=1.0
rate_list=[]
idx=len(ma20)
while idx > 0:
idx -= 1
close_val = close[idx]
ma20_val = ma20[idx]
if close_val > ma20_val:
if is_buy == 0:
is_buy = 1
buy_val.append(close_val)
buy_date.append(close.keys()[idx])
elif close_val < ma20_val:
if is_buy == 1:
is_buy=0
sell_val.append(close_val)
sell_date.append(close.keys()[idx])
rate=rate*(sell_val[-1]*(1-0.002)/buy_val[-1])
rate_list.append(rate)
continue
rate_list.append(rate)
print "stock number: %s" %STOCK
print "buy count: %d" %len(buy_val)
print "sell count: %d" %len(sell_val)
# rate_list=[1]
for i in range(len(sell_val)):
# rate=rate*(sell_val[i]*(1-0.002)/buy_val[i])
# rate_list.append(rate)
print "buy date: %s, buy price: %.2f" %(buy_date[i], buy_val[i])
print "sell date: %s, sell price: %.2f" %(sell_date[i], sell_val[i])
print "rate: %.2f" % rate_list[-1]
# print rate_list
x_axis=range(1, len(ma20)+1)
label=list(df.index)[::-1]
plt.plot(x_axis, rate_list)
plt.show()
if __name__=='__main__':
STOCK='600000'
parse(STOCK)