def test_not_empty_result(self): df = stock.get_index_ohlcv_by_date("20190101", "20190228", "코스피 200") self.assertNotEqual(df.empty, True) df = stock.get_index_ohlcv_by_date("20190101", "20190228", "코스닥 150") self.assertNotEqual(df.empty, True) df = stock.get_index_status_by_group("20190228", "KOSPI") self.assertNotEqual(df.empty, True) df = stock.get_index_status_by_group("20190228", "KOSDAQ") self.assertNotEqual(df.empty, True)
def get_index_ohlcv(ticker: str, fromdate: date, todate: date) -> Dict[date, Ohlcv]: df = stock.get_index_ohlcv_by_date(ticker=ticker, fromdate=date_to_str(fromdate), todate=date_to_str(todate)) # check index changes expected_df_columns = ['시가', '고가', '저가', '종가', '거래량', '거래대금'] for i in range(len(expected_df_columns)): assert df.columns[i] == expected_df_columns[ i], f'Index changed: ({df.index[i]}, {expected_df_columns[i]})' # convert Dataframe to dict df_dict = df.to_dict(orient='index') # marshalling result = {} for timestamp in df_dict.keys(): d = timestamp.date() values = list(df_dict.get(timestamp).values()) result.update({ d: Ohlcv( open=values[0], high=values[1], low=values[2], close=values[3], vol=values[4], transaction=values[5], ) }) return result
def test_ohlcv_with_nan(self): df = stock.get_index_ohlcv_by_date("19800101", "20200831", "1001") # 시가 고가 저가 종가 거래량 거래대금 # 날짜 # 1980-01-04 0.00 0.00 0.00 100.00 95900 602800000 # 1980-01-05 0.00 0.00 0.00 100.15 131100 776400000 # 1980-01-07 0.00 0.00 0.00 102.53 358300 2029700000 # 1980-01-08 0.00 0.00 0.00 105.28 795800 5567200000 temp = df.iloc[0:5, 0] == np.array([0, 0, 0, 0, 0]) self.assertEqual(temp.sum(), 5)
def test_ohlcv_with_freq(self): df = stock.get_index_ohlcv_by_date("20200101", "20200630", "1001", freq="m") # 시가 고가 저가 종가 거래량 # 날짜 # 2020-01-31 2201.21 2277.23 2119.01 2119.01 13096066333 # 2020-02-29 2086.61 2255.49 1980.82 1987.01 13959766513 # 2020-03-31 1997.03 2089.08 1439.43 1754.64 17091025314 # 2020-04-30 1737.28 1957.51 1664.13 1947.56 21045120912 # 2020-05-31 1906.42 2054.52 1894.29 2029.60 16206496902 temp = df.iloc[0:5, 0] == np.array([2201.21, 2086.61, 1997.03, 1737.28, 1906.42]) self.assertEqual(temp.sum(), 5) self.assertIsInstance(df.index , pd.core.indexes.datetimes.DatetimeIndex) self.assertIsInstance(df.index[0], pd._libs.tslibs.timestamps.Timestamp) self.assertTrue(df.index[0] < df.index[-1])
def test_ohlcv_simple(self): df = stock.get_index_ohlcv_by_date("20210101", "20210130", "1001") # 시가 고가 저가 종가 거래량 거래대금 # 날짜 # 2021-01-04 2874.50 2946.54 2869.11 2944.45 1026510465 25011393960858 # 2021-01-05 2943.67 2990.57 2921.84 2990.57 1519911750 26548380179493 # 2021-01-06 2993.34 3027.16 2961.37 2968.21 1793418534 29909396443430 # 2021-01-07 2980.75 3055.28 2980.75 3031.68 1524654500 27182807334912 # 2021-01-08 3040.11 3161.11 3040.11 3152.18 1297903388 40909490005818 temp = df.iloc[0:5, 0] == np.array([2874.50, 2943.67, 2993.34, 2980.75, 3040.11]) self.assertEqual(temp.sum(), 5) self.assertIsInstance(df.index , pd.core.indexes.datetimes.DatetimeIndex) self.assertIsInstance(df.index[0], pd._libs.tslibs.timestamps.Timestamp) self.assertTrue(df.index[0] < df.index[-1])
def getKospi200Chart(self): date = datetime.date(datetime.now()) date1yago = datetime.date(datetime.now() - relativedelta(years=1)) today = date.strftime("%Y%m%d") today_1yago = date1yago.strftime("%Y%m%d") stock_month = stock.get_index_ohlcv_by_date(today_1yago, today, '1028') stock_month.columns = [ "StartPrice", "HighPrice", "LowPrice", "EndPrice", "Volume" ] records = json.loads(stock_month.to_json()) return JsonResponse(records)
def test_ohlcv_in_holiday(self): df = stock.get_index_ohlcv_by_date("20210101", "20210101", "1001") self.assertIsInstance(df, pd.DataFrame) self.assertTrue(df.empty)
def getDayIndex(self): date = datetime.date(datetime.now()) date1wago = datetime.date(datetime.now() - relativedelta(weeks=2)) today = date.strftime("%Y%m%d") today1wago = date1wago.strftime("%Y%m%d") kospi = stock.get_index_ohlcv_by_date(today1wago, today, '1001') # KOSPI kosdaq = stock.get_index_ohlcv_by_date(today1wago, today, '2001') # KOSDAQ kospi200 = stock.get_index_ohlcv_by_date(today1wago, today, '1028') # KOSPI 200 kospi = kospi.tail(2) kosdaq = kosdaq.tail(2) kospi200 = kospi200.tail(2) # kospi kospi_prev = kospi["종가"][0] kospi_curr = kospi["종가"][1] kospi_diff = kospi_curr - kospi_prev kospi_diff = round(kospi_diff, 2) kospi_flucRate = ((kospi_curr - kospi_prev) / kospi_prev) * 100 kospi_flucRate = round(kospi_flucRate, 2) # kosdaq kosdaq_prev = kosdaq["종가"][0] kosdaq_curr = kosdaq["종가"][1] print(kosdaq_prev) print(kosdaq_curr) kosdaq_diff = kosdaq_curr - kosdaq_prev kosdaq_diff = round(kosdaq_diff, 2) kosdaq_flucRate = ((kosdaq_curr - kosdaq_prev) / kosdaq_prev) * 100 kosdaq_flucRate = round(kosdaq_flucRate, 2) # kospi 200 kospi200_prev = kospi200["종가"][0] kospi200_curr = kospi200["종가"][1] kospi200_diff = kospi200_curr - kospi200_prev kospi200_diff = round(kospi200_diff, 2) kospi200_flucRate = ((kospi200_curr - kospi200_prev) / kospi200_prev) * 100 kospi200_flucRate = round(kospi200_flucRate, 2) json_file = OrderedDict() json_file["KOSPI"] = { 'CurrentPrice': kospi_curr, 'FluctuatedAmount': kospi_diff, 'FluctuatedRate': kospi_flucRate } json_file["KOSDAQ"] = { 'CurrentPrice': kosdaq_curr, 'FluctuatedAmount': kosdaq_diff, 'FluctuatedRate': kosdaq_flucRate } json_file["KOSPI200"] = { 'CurrentPrice': kospi200_curr, 'FluctuatedAmount': kospi200_diff, 'FluctuatedRate': kospi200_flucRate } js = json.dumps(json_file) return JsonResponse(js, safe=False)