Skip to content

Commit

Permalink
[Feat] 전날 종가대비 매수/매도호가 주가변동률 로직 단순화
Browse files Browse the repository at this point in the history
- 전날 종가 대비 현재 매수/매도호가 주가 변동률 계산하는 로직 단순화 (기존에 활용하던 API가 아닌, 다른 API 활용 시 훨씬 간단하게 계산이 가능하여 로직 단순화)

Issues #17
  • Loading branch information
novice1993 committed Sep 14, 2023
1 parent 30013dd commit 4897b92
Showing 1 changed file with 6 additions and 42 deletions.
48 changes: 6 additions & 42 deletions client/src/components/StockOrderSection/StockPrice.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import useGetStockData from "../../hooks/useGetStockData";
import { StockProps } from "../../models/stockProps";

import useGetStockInfo from "../../hooks/useGetStockInfo";
import { useState, useEffect, useRef } from "react";
import { useSelector, useDispatch } from "react-redux";
import { isHoliday } from "@hyunbinseo/holidays-kr";
import { styled } from "styled-components";
import { setStockOrderPrice } from "../../reducer/StockOrderPrice-Reducer";
import { StateProps } from "../../models/stateProps";
Expand All @@ -15,6 +13,7 @@ const StockPrice = (props: StockPriceProps) => {

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

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

// 전날 종가 데이터 계산 -> 1) 화~토 : 바로 전날 2) 월요일 및 공휴일 : 영업일 기준 전날
let previousDayStockClosingPrice: number = 0;

const today = new Date();
const getToday = today.getDay();
const daysOfWeek = ["일", "월", "화", "수", "목", "금", "토"];
const nowInKoreanTime = new Date(today.getTime() + 9 * 60 * 60 * 1000); // 날짜 계산 -> UTC 시간에 9시간 더해서 한국 시간대로 변환

const nonBusinessDay = isHoliday(today, { include: { sunday: true } }); // 일요일, 공휴일 (임시 공휴일 포함) 체크
const isMonday = daysOfWeek[getToday] === "월"; // 월요일인지 체크

if (nonBusinessDay || isMonday) {
const standardDay = new Date(nowInKoreanTime);
const todayYymmdd = standardDay.toISOString().slice(0, 10);

const yesterdayStockInfo = stockPrice.filter((stockInfo: StockProps) => {
const dayInfo = stockInfo.stockTradeTime.slice(0, 10);
return dayInfo !== todayYymmdd && stockInfo;
});

if (yesterdayStockInfo.length !== 0) {
previousDayStockClosingPrice = parseInt(yesterdayStockInfo[yesterdayStockInfo.length - 1].stck_prpr);
}
} else {
const standardDay = new Date(nowInKoreanTime);
standardDay.setDate(nowInKoreanTime.getDate() - 1);
const yesterdayYymmdd = standardDay.toISOString().slice(0, 10);

const yesterdayStockInfo = stockPrice.filter((stockInfo: StockProps) => {
const dayInfo = stockInfo.stockTradeTime.slice(0, 10);
return dayInfo === yesterdayYymmdd && stockInfo;
});

if (yesterdayStockInfo.length !== 0) {
previousDayStockClosingPrice = parseInt(yesterdayStockInfo[yesterdayStockInfo.length - 1].stck_prpr);
}
}

// 전날 종가대비 매도/매수호가 변동률
// 전날 종가대비 매도/매수호가 변동률 계산
const presentStockPrice = parseInt(stockInfo.stockInfResponseDto.stck_prpr, 10);
const priceChageAmountComparedYesterday = Math.abs(parseInt(stockInfo.stockInfResponseDto.prdy_vrss, 10));
const previousDayStockClosingPrice = presentStockPrice - priceChageAmountComparedYesterday;
const changeRate = (((price - previousDayStockClosingPrice) / previousDayStockClosingPrice) * 100).toFixed(2);

return (
Expand Down

0 comments on commit 4897b92

Please sign in to comment.