├── .DS_Store
├── .idea
├── financialstocklibrary.iml
├── misc.xml
├── modules.xml
└── workspace.xml
├── README.md
└── packagetool
├── Generatedealday.py
├── Pkltool.py
├── Read.py
├── Tools.py
└── __init__.py
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mysticbinary/FinancialStockLibrary/19a41007d284d27146b5118cec8a9fe582b826d1/.DS_Store
--------------------------------------------------------------------------------
/.idea/financialstocklibrary.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | true
79 | DEFINITION_ORDER
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 | 1515670740631
151 |
152 |
153 | 1515670740631
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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 工具库使用说明
2 | ---
3 | - Generatedealday.py 是生成中国股市的交易日期
4 | - Pkltool.py 是读写pkl的标准格式函数
5 | - Read.py 是读写普通excel的函数
6 | - Tools.py 是综合性的工具库
7 |
--------------------------------------------------------------------------------
/packagetool/Generatedealday.py:
--------------------------------------------------------------------------------
1 | import tushare as ts
2 | import datetime
3 | import xlsxwriter
4 |
5 | path = '../outdata/'
6 | lists = []
7 | deal_list = []
8 |
9 |
10 | def main():
11 | """
12 | 生成交易日输出 excel
13 | """
14 | # 需要手工修改的 开始时间、结束时间
15 | begin = datetime.date(2015, 1, 6)
16 | end = datetime.date(2015, 1, 31)
17 |
18 | # get all date
19 | for i in range((end - begin).days + 1):
20 | day = begin + datetime.timedelta(days=i)
21 | lists.append(day)
22 |
23 | # get deal date
24 | for j in lists:
25 | a = ts.is_holiday(str(j))
26 | if a == False:
27 | deal_list.append(j)
28 |
29 | # out excel date
30 | workbook = xlsxwriter.Workbook(path + "交易日s.xlsx")
31 | worksheet = workbook.add_worksheet("Sheet1")
32 | for k in range(len(deal_list)):
33 | worksheet.write(0, k, deal_list[k])
34 | workbook.close()
35 |
36 |
37 | if __name__ == '__main__':
38 | main()
39 |
--------------------------------------------------------------------------------
/packagetool/Pkltool.py:
--------------------------------------------------------------------------------
1 | import pickle
2 |
3 |
4 | def read(path):
5 | with open(path, "rb") as f:
6 | dicts = pickle.load(f)
7 | return dicts
8 |
9 |
10 | def write(path, pkl):
11 | with open(path, "wb") as f:
12 | pickle.dump(pkl, f)
13 |
--------------------------------------------------------------------------------
/packagetool/Read.py:
--------------------------------------------------------------------------------
1 | import pandas as pd
2 | import xlrd
3 | from datetime import datetime
4 |
5 |
6 | def read_excel_create_array(data_source2):
7 | """
8 | 获取建仓日的矩阵
9 | :return: array
10 | """
11 | return pd.read_excel(data_source2, 'Sheet1', index_col=None)
12 |
13 |
14 | def get_excel_date(data_source1):
15 | """
16 | 获取全部交易日期
17 | :return: list
18 | """
19 | date_list = []
20 | book = xlrd.open_workbook(data_source1)
21 | sheet_1 = book.sheet_by_name('Sheet1')
22 | for cell in sheet_1.row(0):
23 | date_list.append(datetime(*xlrd.xldate_as_tuple(cell.value, 0)).strftime('%Y-%m-%d'))
24 | return date_list
25 |
26 |
27 | def get_create_day(data_source2):
28 | """
29 | 获取建仓日
30 | :return: list
31 | """
32 | date_list = []
33 | book = xlrd.open_workbook(data_source2)
34 | sheet_1 = book.sheet_by_name('Sheet1')
35 | for cell in sheet_1.row(0):
36 | date_list.append(datetime(*xlrd.xldate_as_tuple(cell.value, 0)).strftime('%Y-%m-%d'))
37 | return date_list
38 |
--------------------------------------------------------------------------------
/packagetool/Tools.py:
--------------------------------------------------------------------------------
1 | from settings import settingfile as sets
2 | import pandas as pd
3 |
4 |
5 | def get_not_ten(standard):
6 | """四舍五入十位 返回 0.2*标仓 """
7 | return round(sets.stock_position_number * standard / 100) * 100
8 |
9 |
10 | def gerenate_candealcode(element, connect, today):
11 | """生成标准的交易类型"""
12 | df = callsql_price(connect=connect, date=today, code_str=element[0])
13 | if 0 != len(df):
14 | # 获取该股票的标仓
15 | stander = math_stander(open=df["open"][0])
16 | else:
17 | return []
18 | # 生成列表 [0-code,1-该code的标仓,2-历史仓位]
19 | return [element[0], stander, 1]
20 |
21 |
22 | def get_everdaycode(connect, today, code, everdaycode_list):
23 | """
24 | 在建仓日这一天,生成
25 | 获取每日需要交易的 code_list
26 | 一个code如果建仓之后,在新的信号日再次出现,就把名字转换成 1@2016-02-02
27 | :return: [['839', 37800, 143000.0],...]
28 | """
29 | code_str = code
30 | # 先判断和之前code 是否相同
31 | if len(everdaycode_list) != 0: # 第一天的list为空
32 | for i in range(len(everdaycode_list)):
33 | if str(code) == str(everdaycode_list[i][0]):
34 | code_str = str(code_str) + "@" + str(today)
35 | # 先判断 返回值是否等于 “sql1”
36 | df = callsql_price(connect=connect, date=today, code_str=code)
37 | if 0 != len(df):
38 | # 获取该股票的标仓
39 | stander = math_stander(open=df["open"][0])
40 | # 获取该股票的收盘价
41 | close = df["close"][len(df["close"]) - 1]
42 | else:
43 | return []
44 | # 生成列表 [0-code,1-该code的标仓,2-建仓的收盘价]
45 | return [code_str, stander, close]
46 |
47 |
48 | def math_stander(open):
49 | """
50 | 根据open,计算出标准仓位
51 | :param openlist:
52 | :return:
53 | """
54 | frist_open = int(open)
55 | frist_open = frist_open / 10000
56 | frist_open = sets.one_code_money / frist_open
57 |
58 | baiwei = int(frist_open)
59 | if baiwei > 99:
60 | baiwei = baiwei // 100 # 整除
61 |
62 | gewei = baiwei % 10
63 | # print("个位是:",gewei)
64 | if gewei == 1 or gewei == 3 or gewei == 5 or gewei == 7 or gewei == 9:
65 | baiwei = baiwei - 1
66 | stander = baiwei * 100
67 | else:
68 | stander = baiwei * 100
69 | return stander
70 |
71 |
72 | def get_sixcode(code):
73 | """
74 | 返回一个标准6位的编码 带后缀
75 | :param code:
76 | :return:
77 | """
78 | code = str(code)
79 | # 补齐code 并且判断是.SZ or .SH
80 | if len(code) == 1 or len(code) == 2 or len(code) == 3 or len(code) == 3 or len(code) == 4 or len(code) == 5:
81 | code = code.zfill(6)
82 | if int(code[0]) == 6:
83 | code = code + ".SH"
84 | else:
85 | code = code + ".SZ"
86 | return code
87 |
88 |
89 | def get_hist_postion(everdayinfor_dict, yestday, code):
90 | """
91 | 获得一个code 上一天的历史仓位
92 | """
93 | elementlist = everdayinfor_dict[str(yestday)]
94 | for i in elementlist:
95 | if str(i[0]) == str(code):
96 | return i[2]
97 |
98 |
99 | def if_newcode(connect, all_code, today, stop_dicts):
100 | """
101 | 判断建仓日的code 是否是一字板、停牌,是就不加入list
102 | :param newcode_lists:
103 | :param today:
104 | :return: list
105 | """
106 | del_one_list = []
107 | # find 一字板
108 | tmp = []
109 | for index in range(len(all_code)):
110 | df = callsql_price(connect, today, all_code[index])
111 | if len(df) == 0: # 等于0有多种情况 1.一字板 2.停牌 3.数据库没有这个数据
112 | tmp.append(all_code[index])
113 | else:
114 | if df['open'][0] == df['close'][0] and df['open'][0] == df['high'][0] and df['open'][0] == df['low'][0]:
115 | tmp.append(all_code[index])
116 |
117 | del_one_list = [x for x in all_code if x not in tmp]
118 | # find 停牌
119 | addsuffix_list = add_suffix(del_one_list)
120 | dicts = stop_dicts
121 | today_stop_list = dicts[str(today)]
122 | return [x[0:6] for x in addsuffix_list if x not in today_stop_list]
123 |
124 |
125 | def money_regulate(history_buy_dicts, yestday):
126 | """
127 | 求出一只股票可以分的钱
128 | :param history_buy_dicts:
129 | :param yestday:
130 | :return:
131 | """
132 | # {"2016-1-1":[50,98000],"2016-1-2":[60,8800],...}
133 | lists = history_buy_dicts[yestday]
134 | # 1.先得到份数
135 | par_number = can_buy_number(lists[0])
136 | # 2.在得到昨天的可用资金
137 | # lists[1]
138 | # 一份的钱
139 | if par_number != 0:
140 | one_par_money = lists[1] / par_number
141 | # 一只股票的钱
142 | one_code_money = one_par_money / sets.firstday_code
143 | else:
144 | one_code_money = 0
145 | print("今天有多少只:{0},有多少份:{1},一只能分多少钱:{2}".format(lists[0], par_number, one_code_money))
146 | return one_code_money
147 |
148 |
149 | def can_buy_number(x):
150 | """
151 | 算法,根据昨天的购买支数,返回今天可以购买的份数
152 | :param x:
153 | :return:
154 | """
155 | # x = 199
156 | print("您输入的值为:{0}".format(x))
157 | CONSTANT = sets.how_many_part # 份数
158 | # print("CONSTANT", CONSTANT)
159 | SUM = sets.firstday_code * CONSTANT # 总支数
160 | # print("SUM", SUM)
161 | if x == 0:
162 | print("可以购买的份数{0}".format(sets.how_many_part))
163 | return sets.how_many_part
164 | elif x == SUM:
165 | print("可以购买的份数{0}".format(0))
166 | return 0
167 | else:
168 | x2 = SUM - x
169 | # 总量 % 还剩能购买的量
170 | gety = SUM % x2
171 | # print(gety)
172 | if gety != 0:
173 | s = x2 / sets.firstday_code
174 | print("!=0 可以购买的份数:", int(s))
175 | return int(s)
176 | else:
177 | if SUM - x < sets.firstday_code:
178 | print("< 可以 购买的份数:{0}".format(0))
179 | return 0
180 | else:
181 | sy = SUM / x
182 | print("/ 可以 购买的份数:", int(sy))
183 | return int(sy)
184 |
185 |
186 | def if_newcode(newcode_lists, today, stop_dicts, create_stop_one_dict, w):
187 | """
188 | 判断建仓日的code 是否是一字板、停牌,是就不加入list
189 | :param newcode_lists:
190 | :param today:
191 | :return: list
192 | """
193 | # 新code 如果是 一字板 和 停牌不加入 list
194 | # find 一字板
195 | df = rd.get_code4price(newcode_lists, today, w)
196 | new_list = []
197 | one_seven_list = []
198 | for onecode in newcode_lists:
199 | count = 0
200 | for i in range(len(df[onecode])):
201 | if df[onecode][0] == df[onecode][i]:
202 | count += 1
203 | if count != len(df[onecode]):
204 | new_list.append(onecode)
205 | else:
206 | one_seven_list.append("{0}=1".format(onecode))
207 | # print("在{0}建仓日发现一字板:{1}".format(today, onecode))
208 | # find 停牌
209 | addsuffix_list = add_suffix(new_list)
210 | dicts = stop_dicts
211 | today_stop_list = dicts[str(today)]
212 |
213 | for j in new_list:
214 | for k in today_stop_list:
215 | if str(k) == str(j):
216 | one_seven_list.append(str("{0}=7".format(j[0:6])))
217 | create_stop_one_dict[str(today)] = one_seven_list
218 | return [x[0:6] for x in addsuffix_list if x not in today_stop_list]
219 |
220 |
221 | def only_code(lists):
222 | """
223 | 返回每天唯一的code
224 | :param lists:
225 | :return: list
226 | """
227 | return list({}.fromkeys([x[0:6] for x in lists]).keys())
228 |
229 |
230 | def get_addcode(code, everdaycode_list, today):
231 | """
232 | 在建仓日这一天,生成
233 | 获取每日需要交易的 code_list
234 | 一个code如果建仓之后,在新的信号日再次出现,就把名字转换成 1@2016-02-02
235 | :return: ['839',"840",...]
236 | """
237 | code_str = code
238 | # 先判断和之前code 是否相同
239 | if len(everdaycode_list) != 0: # 第一天的list为空
240 | for i in range(len(everdaycode_list)):
241 | if str(code) == str(everdaycode_list[i]):
242 | code_str = str(code_str) + "@" + str(today)
243 | return code_str
244 |
245 |
246 | def add_suffix(lists):
247 | """
248 | "给code添加后缀"
249 | :param lists:
250 | :return: list
251 | """
252 | list1 = map(get_sixcode, lists)
253 | return list(list1)
254 |
255 |
256 | def del_suffix(lists):
257 | """
258 | "给code添加后缀"
259 | :param lists:
260 | :return: list
261 | """
262 | list1 = map(get_sixcode, lists)
263 | return list(list1)
264 |
265 |
266 | def get_sixcode(code):
267 | """
268 | 返回一个标准6位的编码 带后缀
269 | :param code:
270 | :return:
271 | """
272 | code = str(code)
273 | # 补齐code 并且判断是.SZ or .SH
274 | if len(code) == 1 or len(code) == 2 or len(code) == 3 or len(code) == 3 or len(code) == 4 or len(code) == 5:
275 | code = code.zfill(6)
276 | if int(code[0]) == 6:
277 | code = code + ".SH"
278 | else:
279 | code = code + ".SZ"
280 | return code
281 |
282 |
283 | def mergenoeday(everdayinfor_dict, today):
284 | """
285 | 获取一天内所有code ,list , 然后合并成一个矩阵
286 | :param everdayinfor_dict:
287 | :param today:
288 | :return: df
289 | """
290 | oneday_allcode = everdayinfor_dict[today]
291 | df = pd.DataFrame(oneday_allcode, index=range(0, len(oneday_allcode)),
292 | columns=["code", "open", "现仓位", "特殊备注1or7", "开仓用的钱", "开仓剩余钱", "平仓得的钱", "卖出价", "持有or卖出备注:1or0",
293 | "最高收盘价", "股票市值"])
294 | return df
295 |
--------------------------------------------------------------------------------
/packagetool/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mysticbinary/FinancialStockLibrary/19a41007d284d27146b5118cec8a9fe582b826d1/packagetool/__init__.py
--------------------------------------------------------------------------------