Advertisement
Guest User

Untitled

a guest
Feb 8th, 2022
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import yfinance as yf
  2. import datetime
  3. import matplotlib.pylab as pl
  4. import numpy as np
  5. from scipy import stats
  6.  
  7. def get_ticker(symbol, startDate = datetime.datetime(2022, 2, 1), endDate = datetime.datetime(2022, 2, 8)):
  8.  
  9. ticker = yf.Ticker(symbol)
  10. data = ticker.history(start=startDate, end=endDate, interval="1m")
  11. return data
  12.  
  13. def get_gap_p(name):
  14.  
  15. delta = datetime.timedelta(days=1)
  16. num_days = 40
  17. start_week = datetime.datetime(2022, 2, 7)
  18.  
  19. date_list = [start_week - datetime.timedelta(days=x) for x in reversed(range(num_days))]
  20.  
  21. start_date_list = []
  22. gap_p = []
  23.  
  24. for date in date_list:
  25. try:
  26. ticker_data = get_ticker(name, startDate=date, endDate=date+delta)
  27.  
  28. high = np.array(ticker_data['High'])
  29. low = np.array(ticker_data['Low'])
  30.  
  31. count_gap = 0
  32. count_not_gap = 0
  33. for i in range(high.shape[0])[:-1]:
  34.  
  35. gap_found = False
  36. if high[i+1] < low[i] and low[i+1] < low[i]:
  37.  
  38. count_gap += 1
  39. gap_found = True
  40.  
  41. if high[i+1] > high[i] and low[i+1] > high[i]:
  42.  
  43. count_gap += 1
  44. gap_found = True
  45.  
  46. if not gap_found:
  47.  
  48. count_not_gap += 1
  49.  
  50. if count_gap+count_not_gap > 0:
  51. gap_p.append(count_gap/(count_gap+count_not_gap)*100)
  52. start_date_list.append(date)
  53. except:
  54. pass
  55.  
  56. return [start_date_list, gap_p]
  57.  
  58.  
  59.  
  60. fig, ax1 = pl.subplots()
  61.  
  62.  
  63.  
  64.  
  65. ax1.set_title('Percentage of gaps in 1 minute candles')
  66. ax1.set_xlabel('date')
  67. ax1.set_ylabel('Percentage of gaps (%)')
  68.  
  69. color = 'tab:red'
  70. start_date_list, gap_p = get_gap_p('GME')
  71. ax1.plot(start_date_list, gap_p,'x-', color=color, label='GME')
  72.  
  73. color = 'tab:orange'
  74. start_date_list, gap_p = get_gap_p('TSLA')
  75. ax1.plot(start_date_list, gap_p,'o-', color=color, label='TSLA')
  76.  
  77. color = 'tab:blue'
  78. start_date_list, gap_p = get_gap_p('AAPL')
  79. ax1.plot(start_date_list, gap_p,'o-', color=color, label='AAPL')
  80.  
  81.  
  82. ax1.legend()
  83. fig.tight_layout()
  84. pl.show()
  85.  
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement