xmd79

mtf dips/tops finder

Mar 14th, 2023 (edited)
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1. from binance.client import Client
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from datetime import datetime
  5. import talib as ta
  6. import os, sys
  7.  
  8.  
  9.  
  10. class Trader:
  11. def __init__(self, file):
  12. self.connect(file)
  13.  
  14. """ Creates Binance client """
  15. def connect(self,file):
  16. lines = [line.rstrip('\n') for line in open(file)]
  17. key = lines[0]
  18. secret = lines[1]
  19. self.client = Client(key, secret)
  20.  
  21. filename = 'credentials.txt'
  22. trader = Trader(filename)
  23.  
  24.  
  25.  
  26. filtered_dips1 = []
  27. filtered_dips2 = []
  28. filtered_dips3 = []
  29. filtered_dips4 = []
  30. selected_dips = []
  31. selected_dipsCMO = []
  32.  
  33. filtered_tops1 = []
  34. filtered_tops2 = []
  35. filtered_tops3 = []
  36. filtered_tops4 = []
  37. selected_tops = []
  38. selected_topsCMO = []
  39.  
  40. trading_pairs = ['BTCBUSD','BNBBUSD','DOGEBUSD','AVAXBUSD', 'ADABUSD','SOLBUSD']
  41.  
  42. def filter1(pair):
  43.  
  44. interval = '1d'
  45. symbol = pair
  46. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  47. open_time = [int(entry[0]) for entry in klines]
  48. close = [float(entry[4]) for entry in klines]
  49. close_array = np.asarray(close)
  50.  
  51. print("on daily timeframe " + symbol)
  52.  
  53. x = close
  54. y = range(len(x))
  55.  
  56. best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  57. best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  58. best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  59.  
  60. if x[-1] < best_fit_line3[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
  61. filtered_dips1.append(symbol)
  62. print('dip found')
  63.  
  64. #plt.figure(figsize=(8,6))
  65. #plt.grid(True)
  66. #plt.plot(x)
  67. #plt.plot(best_fit_line1, '--', color='r')
  68. #plt.plot(best_fit_line2, '--', color='r')
  69. #plt.plot(best_fit_line3, '--', color='r')
  70. #plt.show(block=False)
  71. #plt.pause(6)
  72. #plt.close()
  73.  
  74. elif x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  75. filtered_dips1.append(symbol)
  76. print('dip found')
  77.  
  78. #plt.figure(figsize=(8,6))
  79. #plt.grid(True)
  80. #plt.plot(x)
  81. #plt.plot(best_fit_line1, '--', color='r')
  82. #plt.plot(best_fit_line2, '--', color='r')
  83. #plt.plot(best_fit_line3, '--', color='r')
  84. #plt.show(block=False)
  85. #plt.pause(6)
  86. #plt.close()
  87.  
  88. elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
  89. filtered_tops1.append(symbol)
  90. print('top found')
  91.  
  92. elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  93. filtered_tops1.append(symbol)
  94. print('top found')
  95.  
  96. def filter2(filtered_dips1):
  97. interval = '4h'
  98. symbol = filtered_dips1
  99. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  100. open_time = [int(entry[0]) for entry in klines]
  101. close = [float(entry[4]) for entry in klines]
  102. close_array = np.asarray(close)
  103.  
  104. print("on 4h " + symbol)
  105.  
  106. x = close
  107. y = range(len(x))
  108.  
  109. best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  110. best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  111. best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  112.  
  113. if x[-1] < best_fit_line3[-1] and best_fit_line1[0] < best_fit_line1[-1]:
  114. filtered_dips2.append(symbol)
  115. print('found')
  116.  
  117. #plt.figure(figsize=(8,6))
  118. #plt.grid(True)
  119. #plt.plot(x)
  120. #plt.plot(best_fit_line1, '--', color='r')
  121. #plt.plot(best_fit_line2, '--', color='r')
  122. #plt.plot(best_fit_line3, '--', color='r')
  123. #plt.show(block=False)
  124. #plt.pause(6)
  125. #plt.close()
  126.  
  127. if x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  128. filtered_dips2.append(symbol)
  129. print('found')
  130.  
  131. #plt.figure(figsize=(8,6))
  132. #plt.grid(True)
  133. #plt.plot(x)
  134. #plt.plot(best_fit_line1, '--', color='r')
  135. #plt.plot(best_fit_line2, '--', color='r')
  136. #plt.plot(best_fit_line3, '--', color='r')
  137. #plt.show(block=False)
  138. #plt.pause(6)
  139. #plt.close()
  140.  
  141. def filter2x(filtered_tops1):
  142. interval = '4h'
  143. symbol = filtered_tops1
  144. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  145. open_time = [int(entry[0]) for entry in klines]
  146. close = [float(entry[4]) for entry in klines]
  147. close_array = np.asarray(close)
  148.  
  149. print("on 4h " + symbol)
  150.  
  151. x = close
  152. y = range(len(x))
  153.  
  154. best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  155. best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  156. best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  157.  
  158. if x[-1] > best_fit_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
  159. filtered_tops2.append(symbol)
  160. print('top found')
  161.  
  162. elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  163. filtered_tops2.append(symbol)
  164. print('top found')
  165.  
  166.  
  167. def filter3(filtered_dips2):
  168. interval = '1h'
  169. symbol = filtered_dips2
  170. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  171. open_time = [int(entry[0]) for entry in klines]
  172. close = [float(entry[4]) for entry in klines]
  173. close_array = np.asarray(close)
  174.  
  175. print("on 1h timeframe " + symbol)
  176.  
  177. #min = ta.MIN(close_array, timeperiod=30)
  178. #max = ta.MAX(close_array, timeperiod=30)
  179.  
  180. #real = ta.HT_TRENDLINE(close_array)
  181. #wcl = ta.WCLPRICE(max, min, close_array)
  182.  
  183. print(close[-1])
  184. print()
  185. #print(min[-1])
  186. #print(max[-1])
  187. #print(real[-1])
  188.  
  189. x = close
  190. y = range(len(x))
  191.  
  192. best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  193. best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  194. best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  195.  
  196. if x[-1] < best_fit_line1[-1]:
  197. filtered_dips3.append(symbol)
  198. print('found')
  199.  
  200. #plt.figure(figsize=(8,6))
  201. #plt.title(symbol)
  202. #plt.grid(True)
  203. #plt.plot(close)
  204. #plt.plot(best_fit_line1, '--', color='r')
  205. #plt.plot(best_fit_line2, '--', color='r')
  206. #plt.plot(best_fit_line3, '--', color='r')
  207. #plt.plot(close)
  208. #plt.plot(min)
  209. #plt.plot(max)
  210. #plt.plot(real)
  211. #plt.show(block=False)
  212. #plt.pause(5)
  213. #plt.close()
  214.  
  215. else:
  216. print('searching')
  217.  
  218. def filter3x(filtered_tops2):
  219. interval = '1h'
  220. symbol = filtered_tops2
  221. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  222. open_time = [int(entry[0]) for entry in klines]
  223. close = [float(entry[4]) for entry in klines]
  224. close_array = np.asarray(close)
  225.  
  226. print("on 1h timeframe " + symbol)
  227.  
  228. x = close
  229. y = range(len(x))
  230.  
  231. best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  232. best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  233. best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  234.  
  235. if x[-1] > best_fit_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
  236. filtered_tops3.append(symbol)
  237. print('top found')
  238.  
  239. elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  240. filtered_tops3.append(symbol)
  241. print('top found')
  242.  
  243.  
  244. def filter4(filtered_dips3):
  245. interval = '15m'
  246. symbol = filtered_dips3
  247. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  248. open_time = [int(entry[0]) for entry in klines]
  249. close = [float(entry[4]) for entry in klines]
  250. close_array = np.asarray(close)
  251.  
  252. print("on 15m timeframe " + symbol)
  253.  
  254. #min = ta.MIN(close_array, timeperiod=30)
  255. #max = ta.MAX(close_array, timeperiod=30)
  256.  
  257. #real = ta.HT_TRENDLINE(close_array)
  258. #wcl = ta.WCLPRICE(max, min, close_array)
  259.  
  260. print(close[-1])
  261. print()
  262. #print(min[-1])
  263. #print(max[-1])
  264. #print(real[-1])
  265.  
  266. x = close
  267. y = range(len(x))
  268.  
  269. best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  270. best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  271. best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  272.  
  273. if x[-1] < best_fit_line1[-1]:
  274. filtered_dips4.append(symbol)
  275. print('found')
  276.  
  277. #plt.figure(figsize=(8,6))
  278. #plt.title(symbol)
  279. #plt.grid(True)
  280. #plt.plot(close)
  281. #plt.plot(best_fit_line1, '--', color='r')
  282. #plt.plot(best_fit_line2, '--', color='r')
  283. #plt.plot(best_fit_line3, '--', color='r')
  284. #plt.plot(close)
  285. #plt.plot(min)
  286. #plt.plot(max)
  287. #plt.plot(real)
  288. #plt.show(block=False)
  289. #plt.pause(5)
  290. #plt.close()
  291.  
  292. else:
  293. print('searching')
  294.  
  295. def filter4x(filtered_tops3):
  296. interval = '15m'
  297. symbol = filtered_tops3
  298. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  299. open_time = [int(entry[0]) for entry in klines]
  300. close = [float(entry[4]) for entry in klines]
  301. close_array = np.asarray(close)
  302.  
  303. print("on 15m timeframe " + symbol)
  304.  
  305. x = close
  306. y = range(len(x))
  307.  
  308. best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  309. best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  310. best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  311.  
  312. if x[-1] > best_fit_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
  313. filtered_tops4.append(symbol)
  314. print('top found')
  315.  
  316. elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  317. filtered_tops4.append(symbol)
  318. print('top found')
  319.  
  320.  
  321. def momentum(filtered_dips4):
  322. interval = '5m'
  323. symbol = filtered_dips4
  324. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  325. open_time = [int(entry[0]) for entry in klines]
  326. close = [float(entry[4]) for entry in klines]
  327. close_array = np.asarray(close)
  328.  
  329. print()
  330. print("on 5m timeframe " + symbol)
  331.  
  332. real = ta.CMO(close_array, timeperiod=14)
  333. #print(real[-1])
  334.  
  335. if real[-1] < -50:
  336. print('oversold dip found')
  337. selected_dips.append(symbol)
  338. selected_dipsCMO.append(real[-1])
  339.  
  340. else:
  341. print('searching')
  342.  
  343. def momentumx(filtered_tops4):
  344. interval = '5m'
  345. symbol = filtered_tops4
  346. klines = trader.client.get_klines(symbol=symbol,interval=interval)
  347. open_time = [int(entry[0]) for entry in klines]
  348. close = [float(entry[4]) for entry in klines]
  349. close_array = np.asarray(close)
  350.  
  351. print()
  352. print("on 5m timeframe " + symbol)
  353.  
  354. real = ta.CMO(close_array, timeperiod=14)
  355. #print(real[-1])
  356.  
  357. if real[-1] > 50:
  358. print('overbrought top found')
  359. selected_tops.append(symbol)
  360. selected_topsCMO.append(real[-1])
  361.  
  362. else:
  363. print('searching')
  364.  
  365.  
  366. for i in trading_pairs:
  367. output = filter1(i)
  368. print(filtered_dips1)
  369.  
  370. for i in trading_pairs:
  371. output = filter1(i)
  372. print(filtered_tops1)
  373.  
  374. for i in filtered_dips1:
  375. output = filter2(i)
  376. print(filtered_dips2)
  377.  
  378. for i in filtered_tops1:
  379. output = filter2x(i)
  380. print(filtered_tops2)
  381.  
  382. for i in filtered_dips2:
  383. output = filter3(i)
  384. print(filtered_dips3)
  385.  
  386. for i in filtered_tops2:
  387. output = filter3x(i)
  388. print(filtered_tops3)
  389.  
  390. for i in filtered_dips3:
  391. output = filter4(i)
  392. print(filtered_dips4)
  393.  
  394. for i in filtered_tops3:
  395. output = filter4x(i)
  396. print(filtered_tops4)
  397.  
  398. for i in filtered_dips4:
  399. output = momentum(i)
  400. print(selected_dips)
  401.  
  402. for i in filtered_tops4:
  403. output = momentumx(i)
  404. print(selected_tops)
  405.  
  406. if len(selected_dips) > 1:
  407. print('dips are more then 1 oversold')
  408. print(selected_dips)
  409. #print(selected_dipsCMO)
  410.  
  411. if min(selected_dipsCMO) in selected_dipsCMO:
  412. position = selected_dipsCMO.index(min(selected_dipsCMO))
  413.  
  414. for id, value in enumerate(selected_dips):
  415. if id == position:
  416. print(selected_dips[id])
  417.  
  418. elif len(selected_dips) == 1:
  419. print('1 dip found')
  420. print(selected_dips)
  421. print()
  422.  
  423. if len(selected_tops) > 1:
  424. print('tops are more then 1 overbrought')
  425. print(selected_tops)
  426.  
  427. if min(selected_topsCMO) in selected_topsCMO:
  428. position = selected_topsCMO.index(max(selected_topsCMO))
  429.  
  430. for id, value in enumerate(selected_tops):
  431. if id == position:
  432. print(selected_tops[id])
  433.  
  434. elif len(selected_tops) == 1:
  435. print('1 top found')
  436. print(selected_tops)
  437. print()
  438.  
  439. sys.exit(0)
  440. sys.exit()
  441. exit()
  442.  
  443.  
Advertisement
Add Comment
Please, Sign In to add comment