Skip to content

Commit 4897b92

Browse files
committed
[Feat] 전날 종가대비 매수/매도호가 주가변동률 로직 단순화
- 전날 종가 대비 현재 매수/매도호가 주가 변동률 계산하는 로직 단순화 (기존에 활용하던 API가 아닌, 다른 API 활용 시 훨씬 간단하게 계산이 가능하여 로직 단순화) Issues #17
1 parent 30013dd commit 4897b92

File tree

1 file changed

+6
-42
lines changed

1 file changed

+6
-42
lines changed

client/src/components/StockOrderSection/StockPrice.tsx

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import useGetStockData from "../../hooks/useGetStockData";
2-
import { StockProps } from "../../models/stockProps";
3-
2+
import useGetStockInfo from "../../hooks/useGetStockInfo";
43
import { useState, useEffect, useRef } from "react";
54
import { useSelector, useDispatch } from "react-redux";
6-
import { isHoliday } from "@hyunbinseo/holidays-kr";
75
import { styled } from "styled-components";
86
import { setStockOrderPrice } from "../../reducer/StockOrderPrice-Reducer";
97
import { StateProps } from "../../models/stateProps";
@@ -15,6 +13,7 @@ const StockPrice = (props: StockPriceProps) => {
1513

1614
const companyId = useSelector((state: StateProps) => state.companyId);
1715
const { stockPrice, stockPriceLoading, stockPriceError } = useGetStockData(companyId);
16+
const { stockInfo } = useGetStockInfo(companyId);
1817

1918
const dispatch = useDispatch();
2019
const orderPrice = useSelector((state: StateProps) => state.stockOrderPrice);
@@ -40,45 +39,10 @@ const StockPrice = (props: StockPriceProps) => {
4039
return;
4140
}
4241

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;
8246
const changeRate = (((price - previousDayStockClosingPrice) / previousDayStockClosingPrice) * 100).toFixed(2);
8347

8448
return (

0 commit comments

Comments
 (0)