1
1
import useGetStockData from "../../hooks/useGetStockData" ;
2
- import { StockProps } from "../../models/stockProps" ;
3
-
2
+ import useGetStockInfo from "../../hooks/useGetStockInfo" ;
4
3
import { useState , useEffect , useRef } from "react" ;
5
4
import { useSelector , useDispatch } from "react-redux" ;
6
- import { isHoliday } from "@hyunbinseo/holidays-kr" ;
7
5
import { styled } from "styled-components" ;
8
6
import { setStockOrderPrice } from "../../reducer/StockOrderPrice-Reducer" ;
9
7
import { StateProps } from "../../models/stateProps" ;
@@ -15,6 +13,7 @@ const StockPrice = (props: StockPriceProps) => {
15
13
16
14
const companyId = useSelector ( ( state : StateProps ) => state . companyId ) ;
17
15
const { stockPrice, stockPriceLoading, stockPriceError } = useGetStockData ( companyId ) ;
16
+ const { stockInfo } = useGetStockInfo ( companyId ) ;
18
17
19
18
const dispatch = useDispatch ( ) ;
20
19
const orderPrice = useSelector ( ( state : StateProps ) => state . stockOrderPrice ) ;
@@ -40,45 +39,10 @@ const StockPrice = (props: StockPriceProps) => {
40
39
return ;
41
40
}
42
41
43
- // 전날 종가 데이터 계산 -> 1) 화~토 : 바로 전날 2) 월요일 및 공휴일 : 영업일 기준 전날
44
- let previousDayStockClosingPrice : number = 0 ;
45
-
46
- const today = new Date ( ) ;
47
- const getToday = today . getDay ( ) ;
48
- const daysOfWeek = [ "일" , "월" , "화" , "수" , "목" , "금" , "토" ] ;
49
- const nowInKoreanTime = new Date ( today . getTime ( ) + 9 * 60 * 60 * 1000 ) ; // 날짜 계산 -> UTC 시간에 9시간 더해서 한국 시간대로 변환
50
-
51
- const nonBusinessDay = isHoliday ( today , { include : { sunday : true } } ) ; // 일요일, 공휴일 (임시 공휴일 포함) 체크
52
- const isMonday = daysOfWeek [ getToday ] === "월" ; // 월요일인지 체크
53
-
54
- if ( nonBusinessDay || isMonday ) {
55
- const standardDay = new Date ( nowInKoreanTime ) ;
56
- const todayYymmdd = standardDay . toISOString ( ) . slice ( 0 , 10 ) ;
57
-
58
- const yesterdayStockInfo = stockPrice . filter ( ( stockInfo : StockProps ) => {
59
- const dayInfo = stockInfo . stockTradeTime . slice ( 0 , 10 ) ;
60
- return dayInfo !== todayYymmdd && stockInfo ;
61
- } ) ;
62
-
63
- if ( yesterdayStockInfo . length !== 0 ) {
64
- previousDayStockClosingPrice = parseInt ( yesterdayStockInfo [ yesterdayStockInfo . length - 1 ] . stck_prpr ) ;
65
- }
66
- } else {
67
- const standardDay = new Date ( nowInKoreanTime ) ;
68
- standardDay . setDate ( nowInKoreanTime . getDate ( ) - 1 ) ;
69
- const yesterdayYymmdd = standardDay . toISOString ( ) . slice ( 0 , 10 ) ;
70
-
71
- const yesterdayStockInfo = stockPrice . filter ( ( stockInfo : StockProps ) => {
72
- const dayInfo = stockInfo . stockTradeTime . slice ( 0 , 10 ) ;
73
- return dayInfo === yesterdayYymmdd && stockInfo ;
74
- } ) ;
75
-
76
- if ( yesterdayStockInfo . length !== 0 ) {
77
- previousDayStockClosingPrice = parseInt ( yesterdayStockInfo [ yesterdayStockInfo . length - 1 ] . stck_prpr ) ;
78
- }
79
- }
80
-
81
- // 전날 종가대비 매도/매수호가 변동률
42
+ // 전날 종가대비 매도/매수호가 변동률 계산
43
+ const presentStockPrice = parseInt ( stockInfo . stockInfResponseDto . stck_prpr , 10 ) ;
44
+ const priceChageAmountComparedYesterday = Math . abs ( parseInt ( stockInfo . stockInfResponseDto . prdy_vrss , 10 ) ) ;
45
+ const previousDayStockClosingPrice = presentStockPrice - priceChageAmountComparedYesterday ;
82
46
const changeRate = ( ( ( price - previousDayStockClosingPrice ) / previousDayStockClosingPrice ) * 100 ) . toFixed ( 2 ) ;
83
47
84
48
return (
0 commit comments