Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import yfinance as yf
- import datetime
- import matplotlib.pylab as pl
- import numpy as np
- from scipy import stats
- def get_ticker(symbol, startDate = datetime.datetime(2022, 2, 1), endDate = datetime.datetime(2022, 2, 8)):
- ticker = yf.Ticker(symbol)
- data = ticker.history(start=startDate, end=endDate, interval="1m")
- return data
- def get_gap_p(name):
- delta = datetime.timedelta(days=1)
- num_days = 40
- start_week = datetime.datetime(2022, 2, 7)
- date_list = [start_week - datetime.timedelta(days=x) for x in reversed(range(num_days))]
- start_date_list = []
- gap_p = []
- for date in date_list:
- try:
- ticker_data = get_ticker(name, startDate=date, endDate=date+delta)
- high = np.array(ticker_data['High'])
- low = np.array(ticker_data['Low'])
- count_gap = 0
- count_not_gap = 0
- for i in range(high.shape[0])[:-1]:
- gap_found = False
- if high[i+1] < low[i] and low[i+1] < low[i]:
- count_gap += 1
- gap_found = True
- if high[i+1] > high[i] and low[i+1] > high[i]:
- count_gap += 1
- gap_found = True
- if not gap_found:
- count_not_gap += 1
- if count_gap+count_not_gap > 0:
- gap_p.append(count_gap/(count_gap+count_not_gap)*100)
- start_date_list.append(date)
- except:
- pass
- return [start_date_list, gap_p]
- fig, ax1 = pl.subplots()
- ax1.set_title('Percentage of gaps in 1 minute candles')
- ax1.set_xlabel('date')
- ax1.set_ylabel('Percentage of gaps (%)')
- color = 'tab:red'
- start_date_list, gap_p = get_gap_p('GME')
- ax1.plot(start_date_list, gap_p,'x-', color=color, label='GME')
- color = 'tab:orange'
- start_date_list, gap_p = get_gap_p('TSLA')
- ax1.plot(start_date_list, gap_p,'o-', color=color, label='TSLA')
- color = 'tab:blue'
- start_date_list, gap_p = get_gap_p('AAPL')
- ax1.plot(start_date_list, gap_p,'o-', color=color, label='AAPL')
- ax1.legend()
- fig.tight_layout()
- pl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement