MCPcopy
hub / github.com/tradytics/surpriver / get_data

Method get_data

data_loader.py:77–148  ·  view source on GitHub ↗

Get stock data.

(self, symbol)

Source from the content-addressed store, hash-verified

75 return frequent_key
76
77 def get_data(self, symbol):
78 """
79 Get stock data.
80 """
81
82 # Find period
83 if self.DATA_GRANULARITY_MINUTES == 1:
84 period = "7d"
85 else:
86 period = "30d"
87
88 try:
89 # get crytpo price from Binance
90 if(self.DATA_SOURCE == 'binance'):
91 # Binance clients doesn't like 60m as an interval
92 if(self.DATA_GRANULARITY_MINUTES == 60):
93 interval = '1h'
94 else:
95 interval = str(self.DATA_GRANULARITY_MINUTES) + "m"
96 stock_prices = self.binance_client.get_klines(symbol=symbol, interval = interval)
97 # ensure that stock prices contains some data, otherwise the pandas operations below could fail
98 if len(stock_prices) == 0:
99 return [], [], True
100 # convert list to pandas dataframe
101 stock_prices = pd.DataFrame(stock_prices, columns=['Datetime', 'Open', 'High', 'Low', 'Close',
102 'Volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore'])
103 stock_prices['Datetime'] = stock_prices['Datetime'].astype(float)
104 stock_prices['Open'] = stock_prices['Open'].astype(float)
105 stock_prices['High'] = stock_prices['High'].astype(float)
106 stock_prices['Low'] = stock_prices['Low'].astype(float)
107 stock_prices['Close'] = stock_prices['Close'].astype(float)
108 stock_prices['Volume'] = stock_prices['Volume'].astype(float)
109 # get stock prices from yahoo finance
110 else:
111 stock_prices = yf.download(
112 tickers = symbol,
113 period = period,
114 interval = str(self.DATA_GRANULARITY_MINUTES) + "m",
115 auto_adjust = False,
116 progress=False)
117 stock_prices = stock_prices.reset_index()
118 stock_prices = stock_prices[['Datetime','Open', 'High', 'Low', 'Close', 'Volume']]
119 data_length = len(stock_prices.values.tolist())
120 self.stock_data_length.append(data_length)
121
122 # After getting some data, ignore partial data based on number of data samples
123 if len(self.stock_data_length) > 5:
124 most_frequent_key = self.get_most_frequent_key(self.stock_data_length)
125 if data_length != most_frequent_key:
126 return [], [], True
127
128 if self.IS_TEST == 1:
129 stock_prices_list = stock_prices.values.tolist()
130 stock_prices_list = stock_prices_list[1:] # For some reason, yfinance gives some 0 values in the first index
131 future_prices_list = stock_prices_list[-(self.FUTURE_FOR_TESTING + 1):]
132 historical_prices = stock_prices_list[:-self.FUTURE_FOR_TESTING]
133 historical_prices = pd.DataFrame(historical_prices)
134 historical_prices.columns = ['Datetime','Open', 'High', 'Low', 'Close', 'Volume']

Callers 1

Calls 1

get_most_frequent_keyMethod · 0.95

Tested by

no test coverage detected