Exemplo n.º 1
0
 def test_with_a_holiday_1(self):
     # 20210104 monday / 20210109 saturday
     df = stock.get_market_fundamental_by_date("20210104", "20210109", "005930")
     self.assertIsInstance(df, pd.DataFrame)
     temp = np.isclose(df.iloc[0], [37528, 26.218750,  2.210938, 3166, 1.709961, 1416])
     self.assertEqual(temp.sum(), 6)
     self.assertEqual(len(df), 5)
Exemplo n.º 2
0
 def test_with_holidays(self):
     # 20210103 sunday / 20210110 sunday
     df = stock.get_market_fundamental_by_date("20210103", "20210110",
                                               "005930")
     self.assertIsInstance(df, pd.DataFrame)
     temp = np.isclose(df.iloc[0], [37528, 26.22, 2.21, 3166, 1.71, 1416])
     self.assertEqual(temp.sum(), 6)
     self.assertEqual(len(df), 5)
Exemplo n.º 3
0
 def test_io(self):
     df = stock.get_market_fundamental_by_date("20180212", "20180216", "006800")
     self.assertIsInstance(df.index[0], pandas._libs.tslibs.timestamps.Timestamp)
     self.assertIsInstance(df['DIV'].iloc[0], np.float64)
     self.assertIsInstance(df['BPS'].iloc[0], np.int32)
     self.assertIsInstance(df['PER'].iloc[0], np.float64)
     self.assertIsInstance(df['EPS'].iloc[0], np.int32)
     self.assertIsInstance(df['PBR'].iloc[0], np.float64)
Exemplo n.º 4
0
 def test_market_fundamental_2_format(self):
     df = stock.get_market_fundamental_by_date("20180212", "20180216",
                                               "006800")
     self.assertEqual(type(df.index[0]), str)
     self.assertEqual(type(df['DIV'].iloc[0]), np.float64)
     self.assertEqual(type(df['BPS'].iloc[0]), np.int32)
     self.assertEqual(type(df['PER'].iloc[0]), np.float64)
     self.assertEqual(type(df['EPS'].iloc[0]), np.int32)
     self.assertEqual(type(df['PBR'].iloc[0]), np.float64)
Exemplo n.º 5
0
 def test_with_freq(self):
     df = stock.get_market_fundamental_by_date("20200101",
                                               "20200430",
                                               "005930",
                                               freq="m")
     self.assertIsInstance(df, pd.DataFrame)
     temp = np.isclose(df.iloc[0], [35342, 8.54, 1.56, 6461, 2.57, 1416])
     self.assertEqual(temp.sum(), 6)
     self.assertEqual(len(df), 4)
Exemplo n.º 6
0
def Daily_Crawling(start_date: str, end_date: str, code: str):
    # 위 순서대로
    df_market_cap = stock.get_market_cap_by_date(start_date, end_date, code)
    df_ohlcv = stock.get_market_ohlcv_by_date(start_date, end_date, code)
    df_fundamental = stock.get_market_fundamental_by_date(
        start_date, end_date, code)
    df = pd.concat([df_market_cap.iloc[:, 0], df_ohlcv, df_fundamental],
                   axis=1)
    return df
Exemplo n.º 7
0
 def test_in_kosdaq(self):
     df = stock.get_market_fundamental_by_date("20200101", "20200430",
                                               "263720")
     #              BPS    PER   PBR  EPS  DIV  DPS
     # 날짜
     # 2020-01-02  3033  48.78  6.59  410  0.0    0
     # 2020-01-03  3033  45.24  6.12  410  0.0    0
     # 2020-01-06  3033  43.90  5.93  410  0.0    0
     # 2020-01-07  3033  45.73  6.18  410  0.0    0
     # 2020-01-08  3033  44.15  5.97  410  0.0    0
     self.assertIsInstance(df, pd.DataFrame)
     temp = np.isclose(df.iloc[0], [3033, 48.78, 6.59, 410, 0.0, 0])
     self.assertEqual(temp.sum(), 6)
Exemplo n.º 8
0
 def test_with_valid_business_days(self):
     df = stock.get_market_fundamental_by_date("20210104", "20210108", "005930")
     #               BPS        PER       PBR   EPS       DIV   DPS
     # 날짜
     # 2021-01-04  37528  26.218750  2.210938  3166  1.709961  1416
     # 2021-01-05  37528  26.500000  2.240234  3166  1.690430  1416
     # 2021-01-06  37528  25.953125  2.189453  3166  1.719727  1416
     # 2021-01-07  37528  26.187500  2.210938  3166  1.709961  1416
     # 2021-01-08  37528  28.046875  2.369141  3166  1.589844  1416
     self.assertIsInstance(df, pd.DataFrame)
     temp = np.isclose(df.iloc[0], [37528, 26.218750,  2.210938, 3166, 1.709961, 1416])
     self.assertEqual(temp.sum(), 6)
     self.assertEqual(len(df), 5)
Exemplo n.º 9
0
    def test_not_empty_result(self):
        df = stock.get_market_ohlcv_by_date("20190225", "20190228", "000660")
        self.assertNotEqual(df.empty, True)

        df = stock.get_market_price_change_by_ticker("20180301", "20180320")
        self.assertNotEqual(df.empty, True)

        df = stock.get_market_fundamental_by_ticker("20180305", "KOSPI")
        self.assertNotEqual(df.empty, True)

        df = stock.get_market_fundamental_by_date("20180301", "20180320",
                                                  '005930')
        self.assertNotEqual(df.empty, True)
Exemplo n.º 10
0
def make_band_data(option):
    # st.write(option)
    #오늘날짜까지
    now = datetime.now()
    today = '%s%s%s' % (now.year, now.month, now.day)
    fun_df = stock.get_market_fundamental_by_date("20080101", today, option,
                                                  "m")

    #Price 추가 => 이것의 문제는 수정주가가 아님
    # fun_df['Close'] = fun_df['PER'] * fun_df['EPS']

    #Price 수정 주가로 변경
    p_df = stock.get_market_ohlcv_by_date("20080101", today, option, "m")
    fun_df['Close'] = p_df['종가']

    #PER Max/Min/half/3/1
    e_max = round(fun_df['PER'].max(), 1)
    if (e_max >= 30.00):
        e_max = 30.00
    e_min = round(fun_df['PER'].min(), 1)
    e_half = round((e_max + e_min) / 2, 1)
    e_3 = round((e_max - e_half) / 2 + e_half, 1)
    e_1 = round((e_half - e_min) / 2 + e_min, 1)

    #가격 데이터 만들기
    fun_df[str(e_max) + "X"] = (fun_df['EPS'] * e_max).round(2)
    fun_df[str(e_3) + "X"] = (fun_df['EPS'] * e_3).round(2)
    fun_df[str(e_half) + "X"] = (fun_df['EPS'] * e_half).round(2)
    fun_df[str(e_1) + "X"] = (fun_df['EPS'] * e_1).round(2)
    fun_df[str(e_min) + "X"] = (fun_df['EPS'] * e_min).round(2)

    #PBR Max/Min/half/3/1
    b_max = round(fun_df['PBR'].max(), 1)
    if (b_max >= 20.00):
        b_max = 20.00
    b_min = round(fun_df['PBR'].min(), 1)
    b_half = round((b_max + b_min) / 2, 1)
    b_3 = round((b_max - b_half) / 2 + b_half, 1)
    b_1 = round((b_half - b_min) / 2 + b_min, 1)

    #가격 데이터 만들기
    fun_df[str(b_max) + "X"] = fun_df['BPS'] * b_max
    fun_df[str(b_3) + "X"] = (fun_df['BPS'] * b_3).round(2)
    fun_df[str(b_half) + "X"] = (fun_df['BPS'] * b_half).round(2)
    fun_df[str(b_1) + "X"] = (fun_df['BPS'] * b_1).round(2)
    fun_df[str(b_min) + "bX"] = (fun_df['BPS'] * b_min).round(2)

    fun_df.round(decimals=2)

    return fun_df
Exemplo n.º 11
0
 def test_with_valid_business_days(self):
     df = stock.get_market_fundamental_by_date("20210104", "20210108",
                                               "005930")
     #               BPS    PER   PBR   EPS   DIV   DPS
     # 날짜
     # 2021-01-04  37528  26.22  2.21  3166  1.71  1416
     # 2021-01-05  37528  26.50  2.24  3166  1.69  1416
     # 2021-01-06  37528  25.96  2.19  3166  1.72  1416
     # 2021-01-07  37528  26.18  2.21  3166  1.71  1416
     # 2021-01-08  37528  28.05  2.37  3166  1.59  1416
     self.assertIsInstance(df, pd.DataFrame)
     temp = np.isclose(df.iloc[0], [37528, 26.22, 2.21, 3166, 1.71, 1416])
     self.assertEqual(temp.sum(), 6)
     self.assertEqual(len(df), 5)
Exemplo n.º 12
0
def get_market_data(nowDate, companies, codes):
    DIVs, BPSs, PERs, EPSs, PBRs=[],[],[],[],[]
    DIV,BPS,PER,EPS,PBR = "","","","",""
    for i in range(len(companies)):
        df = stock.get_market_fundamental_by_date(nowDate,nowDate,codes[i])
        L = list(df.iloc[0])
        DIV,BPS,PER,EPS,PBR=L[0],L[1],L[2],L[3],L[4]
        print(nowDate+" || ",companies[i],DIV,BPS,PER,EPS,PBR)
        DIVs.append(DIV)
        BPSs.append(BPS)
        PERs.append(PER)
        EPSs.append(EPS)
        PBRs.append(PBR)
    return DIVs, BPSs, PERs, EPSs, PBRs
Exemplo n.º 13
0
    def test_not_empty_result(self):
        df = stock.get_market_ohlcv_by_date("20190225", "20190228", "000660")
        self.assertFalse(df.empty)

        df = stock.get_market_cap_by_date("20190101", "20190131", "005930")
        self.assertFalse(df.empty)

        df = stock.get_market_cap_by_ticker("20200625")
        self.assertFalse(df.empty)

        df = stock.get_market_price_change_by_ticker("20180301", "20180320")
        self.assertFalse(df.empty)

        df = stock.get_market_fundamental_by_ticker("20180305", "KOSPI")
        self.assertFalse(df.empty)

        df = stock.get_market_fundamental_by_date("20180301", "20180320", '005930')
        self.assertFalse(df.empty)
Exemplo n.º 14
0
def _update_fundamentals(code: str, fromdate: date, todate: date):
    df = pykrx_stock.get_market_fundamental_by_date(
        fromdate=_date_to_str(fromdate),
        todate=_date_to_str(todate),
        ticker=code
    )

    funds = []
    for idx, row in df.iterrows():
        all_nan = True
        for v in row:
            if v:
                all_nan = False
                break

        if not all_nan:
            # noinspection PyUnresolvedReferences
            funds.append(
                Fundamental(
                    date=idx.date(),
                    bps=row.get('BPS'),
                    per=row.get('PER'),
                    pbr=row.get('PBR'),
                    eps=row.get('EPS'),
                    div=row.get('DIV'),
                    dps=row.get('DPS'),
                )
            )

    with FundamentalTable(code=code, create_if_not_exists=True) as fund_table:
        whitelist = []
        exists_rows = fund_table.all()
        for fund in funds:
            if fund.date not in [row.date for row in exists_rows]:
                whitelist.append(fund)

        print(f'Inserting {len(whitelist)} records...')
        fund_table.insert_all(whitelist)
Exemplo n.º 15
0
    def get_stock_by_day(code, start_date):
        start_date_no_bar = collect_data.bar_remover(start_date)
        start_date = start_date

        # end_date는 오늘 날짜로 고정
        today = datetime.datetime.today().strftime(
            '%Y%m%d')  # krx는 YYYYMMDD 형식, 야후는 YYYY-MM-DD 형식
        stock_df = stock.get_market_ohlcv_by_date(
            start_date_no_bar, today, code, adjusted=True)  # 시가 고가 저가 종가 거래량
        stock_df2 = stock.get_market_fundamental_by_date(
            start_date_no_bar, today, code)  # DIV BPS PER EPS PBR
        today_pdr = datetime.datetime.today().strftime('%Y-%m-%d')
        gold = pdr.get_data_yahoo('GC=F', start=start_date,
                                  end=today_pdr)[['Adj Close', 'Volume']]  # 금값
        gold.columns = ['Adj Close_gold', 'Volume_gold']
        nikkei = pdr.DataReader('^N225',
                                'yahoo',
                                start=start_date,
                                end=today_pdr)[['Adj Close',
                                                'Volume']]  # 일본 지수
        nikkei.columns = ['Adj Close_nikkei', 'Volume_nikkei']
        crude_oil = pdr.get_data_yahoo('CL=F', start=start_date,
                                       end=today_pdr)[['Adj Close',
                                                       'Volume']]  # 유가
        crude_oil.columns = ['Adj Close_oil', 'Volume_oil']
        change = pd.Series(
            pdr.get_data_yahoo('KRW=X', start=start_date,
                               end=today_pdr)['Adj Close'])  # 환율
        # 데이터 leftjoin
        data = pd.concat([stock_df, stock_df2], axis=1)
        data = pd.merge(data,
                        gold,
                        how='left',
                        left_index=True,
                        right_index=True)
        data = pd.merge(data,
                        nikkei,
                        how='left',
                        left_index=True,
                        right_index=True)
        data = pd.merge(data,
                        crude_oil,
                        how='left',
                        left_index=True,
                        right_index=True)
        data = pd.merge(data,
                        change,
                        how='left',
                        left_index=True,
                        right_index=True)
        data.columns = [
            '시가', '고가', '저가', '종가', '거래량', 'DIV', 'BPS', 'PER', 'EPS', 'PBR',
            'Adj Close_gold', 'Volume_gold', 'Adj Close_nikkei',
            'Volume_nikkei', 'Adj Close_oil', 'Volume_oil', 'Adj Close_change'
        ]
        # 기존에 사용했던 가장 최근의 값으로 채우는 방법 이거 쓰면 거의 다날라감
        data = data.fillna(method="pad")
        # 주가 자체의 값이 아니라 등락율을 구하기로 해서 다음날과 비교해서 올라가면 1 아니면 0 마지막날은 그대로
        for i in range(len(data['종가'])):
            if i == 0:
                continue
            elif i > 0 and i < (len(data['종가']) - 1):
                if data['종가'][i] < data['종가'][(i + 1)]:
                    data['종가'][i] = 1
                else:
                    data['종가'][i] = 0
            else:
                pass
        return data
Exemplo n.º 16
0
 def test_io_with_frequency(self):
     df = stock.get_market_fundamental_by_date("20180101", "20180331",
                                               "006800", "m")
     self.assertIsNotNone(df)
     self.assertEqual(len(df), 3)