-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunme.py
211 lines (200 loc) · 10.6 KB
/
runme.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 6 09:37:37 2017
双均线策略, double MA
reference https://www.joinquant.com/post/1398?f=study&m=algorithm
@author: Jeff
"""
import pandas as pd
import numpy as np
import time
import os
import pickle
import math
start_time = time.time()
def MovingAverage(number,closing_price):
MA_n = np.array(closing_price)
for stock_id in range(len(closing_price.columns)):
this_stock_data = closing_price[closing_price.columns[stock_id]]
for i in range(len(this_stock_data.index)):
if type(this_stock_data[i]) == str:
if this_stock_data[i] != '--':
continue
else:
MA_n[i,stock_id] = '--'
continue
else:
if i < 2 + (number-1): # assume the head 2 lines are stock code and name
MA_n[i,stock_id] = '--'
else:
tmp = list(this_stock_data[i-number+1:i+1])
tmp_cntr = 0
for j in range(number):
if type(tmp[j]) == str:
tmp_cntr = 1
break
if tmp_cntr == 1:
MA_n[i,stock_id] = '--'
continue
MA_n[i,stock_id] = sum(tmp)/number # this_stock_data[i-number:i].mean()
MA = pd.DataFrame(MA_n,index=closing_price.index,columns=closing_price.columns)
return MA
def ma_cross(ma_small,ma_big,stock_list):
'''
是否长线MA大于短线MA的时间序列
'''
compare_array = np.array(ma_big[stock_list])
starting_index = 0
for i in range(len(ma_big.index)):
if type(ma_big.iloc[i,0]) == str:
continue
else:
starting_index = i
break
for stock in range(len(stock_list)):
tmp_compare_bit = []
tmp_compare_bit = list(np.sign(ma_big.ix[starting_index:,stock_list[stock]]-ma_small.ix[starting_index:,stock_list[stock]]))
compare_array[starting_index:,stock] = tmp_compare_bit
return pd.DataFrame(compare_array,index=ma_big.index,columns=stock_list)
def buy_sell_based_on_ma_cross(ma_cross_results):
'''
根据长短期MA的交叉决定是否买进
'''
buy_sell_action = np.array(ma_cross_results)
stock_list = ma_cross_results.columns
for stock in range(len(stock_list)):
previous_bit = None
for i in range(len(ma_cross_results.index)):
if type(ma_cross_results.ix[i,stock]) == str: #停牌不清仓
continue
if previous_bit == None:
buy_sell_action[i,stock] = '--'
previous_bit = ma_cross_results.ix[i,stock]
continue
if previous_bit == ma_cross_results.ix[i,stock]:
if buy_sell_action[i-1,stock] == 'buy' or buy_sell_action[i-1,stock] == 'hold':
buy_sell_action[i,stock] = 'hold'
else:
buy_sell_action[i,stock] = '--'
elif previous_bit - ma_cross_results.ix[i,stock] == 2: # golden cross
buy_sell_action[i,stock] = 'buy'
previous_bit = ma_cross_results.ix[i,stock]
elif previous_bit - ma_cross_results.ix[i,stock] == -2: # dead cross
buy_sell_action[i,stock] = 'sell'
previous_bit = ma_cross_results.ix[i,stock]
elif previous_bit - ma_cross_results.ix[i,stock] == 1 and ma_cross_results.ix[i,stock] == -1:
j = 2
while j<i:
if ma_cross_results.ix[i-j,stock] - ma_cross_results.ix[i,stock] == 2:
buy_sell_action[i,stock] = 'buy'
break
elif ma_cross_results.ix[i-j,stock] - ma_cross_results.ix[i,stock] == 2:
continue
else: # ma_cross_results.ix[i-j,stock] 和 ma_cross_results.ix[i,stock] 一样都是-1
if buy_sell_action[i-j,stock] == 'buy' or buy_sell_action[i-j,stock] == 'hold':
buy_sell_action[i,stock] = 'hold'
break
else:
buy_sell_action[i,stock] = '--'
break
j += 1
previous_bit = ma_cross_results.ix[i,stock]
elif previous_bit - ma_cross_results.ix[i,stock] == 1 and ma_cross_results.ix[i,stock] == 0:
if buy_sell_action[i-1,stock] == 'buy' or buy_sell_action[i-1,stock] == 'hold':
buy_sell_action[i,stock] = 'hold'
else:
buy_sell_action[i,stock] = '--'
# 这时候因为ma_cross_results.ix[i,stock]情况特殊,所以previous_bit不改变
elif previous_bit - ma_cross_results.ix[i,stock] == -1 and ma_cross_results.ix[i,stock] == 1:
j = 2
while j<i:
if ma_cross_results.ix[i-j,stock] - ma_cross_results.ix[i,stock] == -2:
buy_sell_action[i,stock] = 'sell'
break
elif ma_cross_results.ix[i-j,stock] - ma_cross_results.ix[i,stock] == 0:
continue
else: # 都是1
if buy_sell_action[i-j,stock] == 'buy' or buy_sell_action[i-j,stock] == 'hold':
buy_sell_action[i,stock] = 'hold'
break
else:
buy_sell_action[i,stock] = '--'
break
j += 1
previous_bit = ma_cross_results.ix[i,stock]
elif previous_bit - ma_cross_results.ix[i,stock] == -1 and ma_cross_results.ix[i,stock] == 0:
if buy_sell_action[i-1,stock] ==
return pd.DataFrame(buy_sell_action,index=ma_cross_results.index,columns=ma_cross_results.columns)
def gain_and_loss(buy_sell_action,opening_price):
# 根据buy_sell_based_on_ma_cross的结果和开盘价来计算损失和收益
position_status = np.array(buy_sell_action)
stock_list = buy_sell_action.columns
money_allocation = 100000
remaining_cap = np.array(buy_sell_action)
starting_index = None
for i in range(len(buy_sell_action.index)):
if type(buy_sell_action.index[i]) != str:
starting_index = i
break
remaining_cap[starting_index:,:] = np.zeros((len(buy_sell_action.index)-starting_index,len(buy_sell_action.columns))) + money_allocation
remaining_cap = pd.DataFrame(remaining_cap,index=buy_sell_action.index,columns=buy_sell_action.columns)
for stock in range(len(stock_list)):
hands = None
buying_cost = None
for i in range(2,len(buy_sell_action.index)):
if buy_sell_action.iloc[i,stock] == 'buy':
if buy_sell_action.iloc[i-1,stock] == '--':
position_status[i,stock] = 0
hands = None
buying_cost = None
elif buy_sell_action.iloc[i-1,stock] == 'sell':
remaining_cap.iloc[i:,stock] = remaining_cap.iloc[i:,stock] + hands * opening_price.ix[i,stock_list[stock]] * 100
position_status[i,stock] = 0
hands = None
buying_cost = None
elif buy_sell_action.iloc[i,stock] == 'hold':
if buy_sell_action.iloc[i-1,stock] == 'buy':
hands = math.trunc(remaining_cap.iloc[i,stock]/(opening_price.ix[i,stock_list[stock]]*100))
position_status[i,stock] = hands * opening_price.ix[i,stock_list[stock]] *100
buying_cost = hands * opening_price.ix[i,stock_list[stock]] *100
remaining_cap.iloc[i:,stock] = remaining_cap.iloc[i:,stock] - buying_cost
else: # hold yesterday, hold today
position_status[i,stock] = hands * opening_price.iloc[i,stock] * 100
# remaining_cap.iloc[i:,stock] = remaining_cap.iloc[i:,stock] - buying_cost
elif buy_sell_action.iloc[i,stock] == 'sell':
if buy_sell_action.iloc[i-1,stock] == 'buy':
hands = math.trunc(remaining_cap.iloc[i,stock]/(opening_price.ix[i,stock_list[stock]]*100))
position_status[i,stock] = hands * opening_price.ix[i,stock_list[stock]] *100
buying_cost = hands * opening_price.ix[i,stock_list[stock]] *100
remaining_cap.iloc[i:,stock] = remaining_cap.iloc[i:,stock] - buying_cost
elif buy_sell_action.iloc[i-1,stock] == 'hold':
position_status[i,stock] = hands * opening_price.ix[i,stock_list[stock]] * 100
else:
print('check here')
elif buy_sell_action.iloc[i,stock] == '--':
if buy_sell_action.iloc[i-1,stock] == 'sell':
remaining_cap.iloc[i:,stock] = remaining_cap.iloc[i:,stock] + hands * opening_price.ix[i,stock_list[stock]] * 100
position_status[i,stock] = 0
hands = None
buying_cost = None
elif buy_sell_action.iloc[i-1,stock] == '--':
position_status[i,stock] = 0
hands = None
buying_cost = None
elif buy_sell_action.iloc[i-1,stock] != '--' and type(buy_sell_action.iloc[i-1,stock]) == str:
position_status[i,stock] = 0
hands = None
buying_cost = None
return pd.DataFrame(position_status,index=buy_sell_action.index,columns=buy_sell_action.columns),remaining_cap #两个array在同一天的值相加即为总资产
if __name__ == '__main__':
os.chdir('C:\\Users\\talen_000\\Desktop\\dualMAstrategy')
closing_price = pd.read_excel('个股日收盘价2016till20170606.xlsx')
opening_price = pd.read_excel('个股日开盘价2016till20170606.xlsx')
# ma20 = MovingAverage(20,closing_price)
# ma60 = MovingAverage(60,closing_price)
# ma5 = MovingAverage(5,closing_price)
sector_and_stocks = pickle.load(open('C:/Users/talen_000/Desktop/行业和个股/sectorandstocks.txt','rb'))
ma5 = pickle.load(open('C:/Users/talen_000/Desktop/dualMAstrategy/MA5_2016till20170606.txt','rb'))
ma20 = pickle.load(open('C:/Users/talen_000/Desktop/dualMAstrategy/MA20_2016till20170606.txt','rb'))
ma60 = pickle.load(open('C:/Users/talen_000/Desktop/dualMAstrategy/MA60_2016till20170606.txt','rb'))
print('time collapsed: ',time.time()-start_time,' seconds')