myindicator.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. from time import sleep
  2. import os
  3. import pandas as pd
  4. import talib as ta
  5. import numpy as np
  6. from datetime import datetime as dt
  7. class myind:
  8. def __init__(self, name, value):
  9. self.name = name
  10. self.value = value
  11. def __repr__(self):
  12. return self.name
  13. def __call__(self, x):
  14. return self.value(x)
  15. def myself_kdj(df):
  16. low_list = df['low_back'].rolling(9, min_periods=9).min()
  17. low_list.fillna(value=df['low_back'].expanding().min(), inplace=True)
  18. high_list = df['high_back'].rolling(9, min_periods=9).max()
  19. high_list.fillna(value=df['high_back'].expanding().max(), inplace=True)
  20. rsv = (df['close_back'] - low_list) / (high_list - low_list) * 100
  21. df['k'] = pd.DataFrame(rsv).ewm(com=2).mean()
  22. df['d'] = df['k'].ewm(com=2).mean()
  23. df['j'] = 3 * df['k'] - 2 * df['d']
  24. return df
  25. # macd指标
  26. def get_macd_data(data, short=0, long1=0, mid=0):
  27. if short == 0:
  28. short = 12
  29. if long1 == 0:
  30. long1 = 26
  31. if mid == 0:
  32. mid = 9
  33. data['sema'] = pd.Series(data['close_back']).ewm(span=short).mean()
  34. data['lema'] = pd.Series(data['close_back']).ewm(span=long1).mean()
  35. data.fillna(0, inplace=True)
  36. data['dif'] = data['sema'] - data['lema']
  37. data['dea'] = pd.Series(data['dif']).ewm(span=mid).mean()
  38. data['macd'] = 2 * (data['dif'] - data['dea'])
  39. data.fillna(0, inplace=True)
  40. return data[['dif', 'dea', 'macd']]
  41. # rsi指标
  42. def get_ris(data):
  43. data["rsi_6"] = ta.RSI(data['close_back'], timeperiod=6)
  44. data["rsi_12"] = ta.RSI(data['close_back'], timeperiod=12)
  45. data["rsi_24"] = ta.RSI(data['close_back'], timeperiod=24)
  46. # return data
  47. def get_bias(data):
  48. # 计算方法:
  49. # bias指标
  50. # N期BIAS=(当日收盘价-N期平均收盘价)/N期平均收盘价*100%
  51. data['bias_6'] = (data['close_back'] - data['close_back'].rolling(6, min_periods=1).mean()) / \
  52. data['close_back'].rolling(6, min_periods=1).mean() * 100
  53. data['bias_12'] = (data['close_back'] - data['close_back'].rolling(12, min_periods=1).mean()) / \
  54. data['close_back'].rolling(12, min_periods=1).mean() * 100
  55. data['bias_24'] = (data['close_back'] - data['close_back'].rolling(24, min_periods=1).mean()) / \
  56. data['close_back'].rolling(24, min_periods=1).mean() * 100
  57. data['bias_6'] = round(data['bias_6'], 2)
  58. data['bias_12'] = round(data['bias_12'], 2)
  59. data['bias_24'] = round(data['bias_24'], 2)
  60. def get_wilr(data):
  61. # 威廉指标
  62. # 建议用talib库的WILLR方法,亲测有用
  63. data['willr'] = ta.WILLR(data['high_back'], data['low_back'], data['close_back'], timeperiod=14)
  64. def get_hlfx(data):
  65. import pandas as pd
  66. trading_signals = 0
  67. data_temp = data[['time', 'open_back', 'close_back', 'high_back', 'low_back', 'dif', 'dea', 'macd']]
  68. data_temp.columns = ['time', 'open', 'close', 'high', 'low', 'dif', 'dea', 'macd']
  69. df_day = pd.DataFrame(columns=['time', 'open', 'close', 'high', 'low'])
  70. # df_day = pd.DataFrame()
  71. # 先处理去包含
  72. for i in data_temp.index:
  73. if i == 0 or i == 1:
  74. df_day = pd.concat([df_day.copy(), data_temp.loc[i].to_frame().T], ignore_index=True)
  75. # 不包含
  76. elif (df_day.iloc[-1, 3] > data_temp.loc[i, 'high']
  77. and df_day.iloc[-1, 4] > data_temp.loc[i, 'low']) \
  78. or (df_day.iloc[-1, 3] < data_temp.loc[i, 'high']
  79. and df_day.iloc[-1, 4] < data_temp.loc[i, 'low']):
  80. df_day = pd.concat([df_day.copy(), data_temp.loc[i].to_frame().T], ignore_index=True)
  81. # 包含
  82. else:
  83. # 左高,下降
  84. if df_day.iloc[-2, 3] > df_day.iloc[-1, 3]:
  85. df_day.iloc[-1, 3] = min(df_day.iloc[-1, 3], data_temp.loc[i, 'high'])
  86. df_day.iloc[-1, 4] = min(df_day.iloc[-1, 4], data_temp.loc[i, 'low'])
  87. else:
  88. # 右高,上升
  89. df_day.iloc[-1, 3] = max(df_day.iloc[-1, 3], data_temp.loc[i, 'high'])
  90. df_day.iloc[-1, 4] = max(df_day.iloc[-1, 4], data_temp.loc[i, 'low'])
  91. # print('111', df_day, data_temp)
  92. df_day['HL'] = np.nan
  93. if len(df_day.index) > 2:
  94. # 寻找顶底分型
  95. for x in range(2, len(df_day.index)):
  96. m = x - 1
  97. # 底
  98. # 符合底分型形态,且第2、3根k线是阳线
  99. if ((df_day.loc[x, 'high'] > df_day.loc[x - 1, 'high']) and
  100. (df_day.loc[x - 2, 'high'] > df_day.loc[x - 1, 'high'])):
  101. # and df_day.loc[x, 'close'] > df_day.loc[x, 'open'] and \
  102. # df_day.loc[x - 1, 'close'] > df_day.loc[x - 1, 'open']:
  103. df_day.loc[x, 'HL'] = 'L*'
  104. while m:
  105. if df_day.loc[m, 'HL'] in ['H', 'HH', 'H*']:
  106. if (x - m) > 3:
  107. # 成笔——>L
  108. df_day.loc[x, 'HL'] = 'L'
  109. # 产生信号,进入hlfx_pool
  110. if x == len(df_day.index) - 1:
  111. trading_signals = 1
  112. else:
  113. # 不成笔 次级别中枢,保持L* 修订原H为H*
  114. df_day.loc[m, 'HL'] = 'H*'
  115. break
  116. elif df_day.loc[m, 'HL'] in ['L', 'LL', 'L*']:
  117. if df_day.loc[m - 1, 'low'] > df_day.loc[x - 1, 'low']:
  118. # 前一个为底更高,且中间不存在更低的底
  119. df_day.loc[x, 'HL'] = 'L'
  120. df_day.loc[m, 'HL'] = '-'
  121. # 产生信号,进入hlfx_pool
  122. if x == len(df_day.index) - 1:
  123. trading_signals = 1
  124. # 获得MACD,判断MACD判断背驰
  125. x_macd_dif, x_macd_dea, x_macd_macd = data_temp.loc[x, 'dif'], data_temp.loc[x, 'dea'], \
  126. data_temp.loc[x, 'macd']
  127. m_macd_dif, m_macd_dea, m_macd_macd = data_temp.loc[m, 'dif'], data_temp.loc[m, 'dea'], \
  128. data_temp.loc[m, 'macd']
  129. # MACD底背驰
  130. if m_macd_dif < x_macd_dif:
  131. # 次级别背驰底->LL
  132. df_day.loc[x, 'HL'] = 'LL'
  133. break
  134. else:
  135. # 前底更低,本底无效
  136. df_day.loc[x, 'HL'] = '-'
  137. break
  138. m = m - 1
  139. if m == 0:
  140. df_day.loc[x, 'HL'] = 'L'
  141. # 顶
  142. elif ((df_day.loc[x, 'high'] < df_day.loc[x - 1, 'high']) and (
  143. df_day.loc[x - 2, 'high'] < df_day.loc[x - 1, 'high'])):
  144. df_day.loc[x, 'HL'] = 'H*'
  145. while m:
  146. if df_day.loc[m, 'HL'] in ['L', 'LL', 'L*']:
  147. if x - m > 3:
  148. # 成笔->H
  149. df_day.loc[x, 'HL'] = 'H'
  150. # 产生信号,进入hlfx_pool
  151. if x == len(df_day.index) - 1:
  152. trading_signals = 2
  153. else:
  154. # 不成笔 次级别中枢,保持H* 修订原L为L*
  155. df_day.loc[m, 'HL'] = 'L*'
  156. break
  157. elif df_day.loc[m, 'HL'] in ['H', 'HH', 'H*']:
  158. if df_day.loc[x - 1, 'high'] > df_day.loc[m - 1, 'high']:
  159. # 前一个为顶,且中间存在不包含 or 更高的顶
  160. df_day.loc[x, 'HL'] = 'H'
  161. df_day.loc[m, 'HL'] = '-'
  162. # 产生信号,进入hlfx_pool
  163. if x == len(df_day.index) - 1:
  164. trading_signals = 2
  165. # 获得MACD,判断MACD判断背驰
  166. x_macd_dif, x_macd_dea, x_macd_macd = data_temp.loc[x, 'dif'], data_temp.loc[x, 'dea'], \
  167. data_temp.loc[x, 'macd']
  168. m_macd_dif, m_macd_dea, m_macd_macd = data_temp.loc[m, 'dif'], data_temp.loc[m, 'dea'], \
  169. data_temp.loc[m, 'macd']
  170. # MACD顶背驰
  171. if x_macd_dif < m_macd_dif:
  172. # 次级别背驰底->HH
  173. df_day.loc[x, 'HL'] = 'HH'
  174. break
  175. else:
  176. # 前顶更高,本顶无效
  177. df_day.loc[x, 'HL'] = '-'
  178. break
  179. m = m - 1
  180. if m == 0:
  181. df_day.loc[x, 'HL'] = 'H'
  182. else:
  183. df_day.loc[x, 'HL'] = '-'
  184. df_temp = df_day[['time', 'HL']]
  185. return df_temp, trading_signals
  186. # 帮我优化get_hlfx方法
  187. def get_hlfx_optimization(data):
  188. # print(os.getpid(), 'start', len(data))
  189. trading_signals = 0
  190. data_temp = data[['time', 'open_back', 'close_back', 'high_back', 'low_back', 'dif', 'dea', 'macd']]
  191. data_temp.columns = ['time', 'open', 'close', 'high', 'low', 'dif', 'dea', 'macd']
  192. merged_data = []
  193. prev_kline = data_temp.iloc[0].copy()
  194. merged_data.append(prev_kline)
  195. # df_day = pd.DataFrame(columns=['time', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'])
  196. # df_day = pd.DataFrame(columns=['time', 'high', 'low', 'HL'])
  197. df_day = pd.DataFrame()
  198. for i in range(1, len(data_temp)):
  199. current_kline = data_temp.iloc[i].copy()
  200. # 使用处理过的数据进行判断
  201. last_merged_kline = merged_data[-1]
  202. if i == 0 or i == 1 or \
  203. (last_merged_kline['high'] >= current_kline['high'] and last_merged_kline['low'] <= current_kline['low']) or \
  204. (last_merged_kline['high'] <= current_kline['high'] and last_merged_kline['low'] >= current_kline['low']):
  205. # 根据前一根K线的走势合并K线
  206. if last_merged_kline['close'] > last_merged_kline['open']: # 前一根K线是上涨的
  207. last_merged_kline['high'] = max(last_merged_kline['high'], current_kline['high'])
  208. last_merged_kline['low'] = max(last_merged_kline['low'], current_kline['low'])
  209. else: # 前一根K线是下跌的
  210. last_merged_kline['high'] = min(last_merged_kline['high'], current_kline['high'])
  211. last_merged_kline['low'] = min(last_merged_kline['low'], current_kline['low'])
  212. else:
  213. # 保存新的K线
  214. merged_data.append(current_kline)
  215. df_day = pd.DataFrame(merged_data).reset_index(drop=True)
  216. # 顶底分型
  217. df_day['HL'] = np.nan
  218. try:
  219. if len(df_day.index) > 2:
  220. # 寻找顶底分型
  221. for x in range(2, len(df_day.index)):
  222. m = x - 1
  223. # 底
  224. # 符合底分型形态,且第2、3根k线是阳线
  225. if ((df_day.loc[x, 'high'] > df_day.loc[x - 1, 'high']) and
  226. (df_day.loc[x - 2, 'high'] > df_day.loc[x - 1, 'high'])):
  227. # and df_day.loc[x, 'close'] > df_day.loc[x, 'open'] and \
  228. # df_day.loc[x - 1, 'close'] > df_day.loc[x - 1, 'open']:
  229. df_day.loc[x, 'HL'] = 'L*'
  230. while m:
  231. if df_day.loc[m, 'HL'] in ['H', 'HH', 'H*']:
  232. if (x - m) > 3:
  233. # 成笔——>L
  234. df_day.loc[x, 'HL'] = 'L'
  235. # 产生信号,进入hlfx_pool
  236. if x == len(df_day.index) - 1:
  237. trading_signals = 1
  238. else:
  239. # 不成笔 次级别中枢,保持L* 修订原H为H*
  240. df_day.loc[m, 'HL'] = 'H*'
  241. break
  242. elif df_day.loc[m, 'HL'] in ['L', 'LL', 'L*']:
  243. if df_day.loc[m - 1, 'low'] > df_day.loc[x - 1, 'low']:
  244. # 前一个为底更高,且中间不存在更低的底
  245. df_day.loc[x, 'HL'] = 'L'
  246. df_day.loc[m, 'HL'] = '-'
  247. # 产生信号,进入hlfx_pool
  248. if x == len(df_day.index) - 1:
  249. trading_signals = 1
  250. # 获得MACD,判断MACD判断背驰
  251. x_macd_dif, x_macd_dea, x_macd_macd = data_temp.loc[x, 'dif'], data_temp.loc[x, 'dea'], \
  252. data_temp.loc[x, 'macd']
  253. m_macd_dif, m_macd_dea, m_macd_macd = data_temp.loc[m, 'dif'], data_temp.loc[m, 'dea'], \
  254. data_temp.loc[m, 'macd']
  255. # MACD底背驰
  256. if m_macd_dif < x_macd_dif:
  257. # 次级别背驰底->LL
  258. df_day.loc[x, 'HL'] = 'LL'
  259. break
  260. else:
  261. # 前底更低,本底无效
  262. df_day.loc[x, 'HL'] = '-'
  263. break
  264. m = m - 1
  265. if m == 0:
  266. df_day.loc[x, 'HL'] = 'L'
  267. # 顶
  268. elif ((df_day.loc[x, 'high'] < df_day.loc[x - 1, 'high']) and (
  269. df_day.loc[x - 2, 'high'] < df_day.loc[x - 1, 'high'])):
  270. df_day.loc[x, 'HL'] = 'H*'
  271. while m:
  272. if df_day.loc[m, 'HL'] in ['L', 'LL', 'L*']:
  273. if x - m > 3:
  274. # 成笔->H
  275. df_day.loc[x, 'HL'] = 'H'
  276. # 产生信号,进入hlfx_pool
  277. if x == len(df_day.index) - 1:
  278. trading_signals = 2
  279. else:
  280. # 不成笔 次级别中枢,保持H* 修订原L为L*
  281. df_day.loc[m, 'HL'] = 'L*'
  282. break
  283. elif df_day.loc[m, 'HL'] in ['H', 'HH', 'H*']:
  284. if df_day.loc[x - 1, 'high'] > df_day.loc[m - 1, 'high']:
  285. # 前一个为顶,且中间存在不包含 or 更高的顶
  286. df_day.loc[x, 'HL'] = 'H'
  287. df_day.loc[m, 'HL'] = '-'
  288. # 产生信号,进入hlfx_pool
  289. if x == len(df_day.index) - 1:
  290. trading_signals = 2
  291. # 获得MACD,判断MACD判断背驰
  292. x_macd_dif, x_macd_dea, x_macd_macd = data_temp.loc[x, 'dif'], data_temp.loc[x, 'dea'], \
  293. data_temp.loc[x, 'macd']
  294. m_macd_dif, m_macd_dea, m_macd_macd = data_temp.loc[m, 'dif'], data_temp.loc[m, 'dea'], \
  295. data_temp.loc[m, 'macd']
  296. # MACD顶背驰
  297. if x_macd_dif < m_macd_dif:
  298. # 次级别背驰底->HH
  299. df_day.loc[x, 'HL'] = 'HH'
  300. break
  301. else:
  302. # 前顶更高,本顶无效
  303. df_day.loc[x, 'HL'] = '-'
  304. break
  305. m = m - 1
  306. if m == 0:
  307. df_day.loc[x, 'HL'] = 'H'
  308. else:
  309. df_day.loc[x, 'HL'] = '-'
  310. except BaseException as e:
  311. print('errrrr', e)
  312. df_temp = df_day[['time', 'HL']]
  313. return df_temp, trading_signals
  314. def get_ddfx(data, data_temp, u):
  315. df_day = data
  316. if len(df_day.index) > 2:
  317. # 寻找顶底分型
  318. for x in range(2, len(df_day.index)):
  319. m = x - 1
  320. # 底
  321. # 符合底分型形态,且第2、3根k线是阳线
  322. if ((df_day.loc[x, 'high'] > df_day.loc[x - 1, 'high']) and
  323. (df_day.loc[x - 2, 'high'] > df_day.loc[x - 1, 'high'])):
  324. # and df_day.loc[x, 'close'] > df_day.loc[x, 'open'] and \
  325. # df_day.loc[x - 1, 'close'] > df_day.loc[x - 1, 'open']:
  326. df_day.loc[x, 'HL'] = 'L*'
  327. while m:
  328. if df_day.loc[m, 'HL'] in ['H', 'HH', 'H*']:
  329. if (x - m) > 3:
  330. # 成笔——>L
  331. df_day.loc[x, 'HL'] = 'L'
  332. # 产生信号,进入hlfx_pool
  333. if x == len(df_day.index) - 1:
  334. trading_signals = 1
  335. else:
  336. # 不成笔 次级别中枢,保持L* 修订原H为H*
  337. df_day.loc[m, 'HL'] = 'H*'
  338. break
  339. elif df_day.loc[m, 'HL'] in ['L', 'LL', 'L*']:
  340. if df_day.loc[m - 1, 'low'] > df_day.loc[x - 1, 'low']:
  341. # 前一个为底更高,且中间不存在更低的底
  342. df_day.loc[x, 'HL'] = 'L'
  343. df_day.loc[m, 'HL'] = '-'
  344. # 产生信号,进入hlfx_pool
  345. if x == len(df_day.index) - 1:
  346. trading_signals = 1
  347. # 获得MACD,判断MACD判断背驰
  348. x_macd_dif, x_macd_dea, x_macd_macd = data_temp.loc[x, 'dif'], data_temp.loc[x, 'dea'], \
  349. data_temp.loc[x, 'macd']
  350. m_macd_dif, m_macd_dea, m_macd_macd = data_temp.loc[m, 'dif'], data_temp.loc[m, 'dea'], \
  351. data_temp.loc[m, 'macd']
  352. # MACD底背驰
  353. if m_macd_dif < x_macd_dif:
  354. # 次级别背驰底->LL
  355. df_day.loc[x, 'HL'] = 'LL'
  356. break
  357. else:
  358. # 前底更低,本底无效
  359. df_day.loc[x, 'HL'] = '-'
  360. break
  361. m = m - 1
  362. if m == 0:
  363. df_day.loc[x, 'HL'] = 'L'
  364. # 顶
  365. elif ((df_day.loc[x, 'high'] < df_day.loc[x - 1, 'high']) and (
  366. df_day.loc[x - 2, 'high'] < df_day.loc[x - 1, 'high'])):
  367. df_day.loc[x, 'HL'] = 'H*'
  368. while m:
  369. if df_day.loc[m, 'HL'] in ['L', 'LL', 'L*']:
  370. if x - m > 3:
  371. # 成笔->H
  372. df_day.loc[x, 'HL'] = 'H'
  373. # 产生信号,进入hlfx_pool
  374. if x == len(df_day.index) - 1:
  375. trading_signals = 2
  376. else:
  377. # 不成笔 次级别中枢,保持H* 修订原L为L*
  378. df_day.loc[m, 'HL'] = 'L*'
  379. break
  380. elif df_day.loc[m, 'HL'] in ['H', 'HH', 'H*']:
  381. if df_day.loc[x - 1, 'high'] > df_day.loc[m - 1, 'high']:
  382. # 前一个为顶,且中间存在不包含 or 更高的顶
  383. df_day.loc[x, 'HL'] = 'H'
  384. df_day.loc[m, 'HL'] = '-'
  385. # 产生信号,进入hlfx_pool
  386. if x == len(df_day.index) - 1:
  387. trading_signals = 2
  388. # 获得MACD,判断MACD判断背驰
  389. x_macd_dif, x_macd_dea, x_macd_macd = data_temp.loc[x, 'dif'], data_temp.loc[x, 'dea'], \
  390. data_temp.loc[x, 'macd']
  391. m_macd_dif, m_macd_dea, m_macd_macd = data_temp.loc[m, 'dif'], data_temp.loc[m, 'dea'], \
  392. data_temp.loc[m, 'macd']
  393. # MACD顶背驰
  394. if x_macd_dif < m_macd_dif:
  395. # 次级别背驰底->HH
  396. df_day.loc[x, 'HL'] = 'HH'
  397. break
  398. else:
  399. # 前顶更高,本顶无效
  400. df_day.loc[x, 'HL'] = '-'
  401. break
  402. m = m - 1
  403. if m == 0:
  404. df_day.loc[x, 'HL'] = 'H'
  405. else:
  406. df_day.loc[x, 'HL'] = '-'
  407. data = df_day
  408. print('44444444444444444', u)
  409. print(data)