Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 26.08 이동 평균선 계산 로직 추가 (#183) #199

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BE/src/ranking/ranking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class RankingService {

return {
topRank: parsedTopRank,
userRank: userRank,
userRank,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 얘는 린트 오류때문에 수정하신 건가요? 갑자기 왜 수정하신 건지 모를 파일이 끼어있는 느낌이에요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉 시정하겠습니다

};
}

Expand Down
6 changes: 6 additions & 0 deletions BE/src/stock/detail/dto/stock-detail-chart-data.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ export class InquirePriceChartDataDto {

@ApiProperty({ description: '전일 대비 부호' })
prdy_vrss_sign: string;

@ApiProperty({ description: '이동 평균선 데이터(5일)' })
mov_avg_5: string;

@ApiProperty({ description: '이동 평균선 데이터(20일)' })
mov_avg_20: string;
}
42 changes: 38 additions & 4 deletions BE/src/stock/detail/stock-detail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,25 @@ export class StockDetailService {
date2: string,
periodDivCode: string,
) {
let newDate1 = date1;
let newDate2 = date2;

if (date1 === '') {
const today = new Date();
const prevDay = new Date();
if (periodDivCode === 'D') prevDay.setDate(today.getDate() - 60);
if (periodDivCode === 'M') prevDay.setDate(today.getDate() - 1200);
if (periodDivCode === 'Y') prevDay.setDate(today.getDate() - 20000);
prevDay.setDate(today.getDate() - 365);
newDate2 = new Date().toISOString().slice(0, 10).replace(/-/g, '');
newDate1 = prevDay.toISOString().slice(0, 10).replace(/-/g, '');
Comment on lines +95 to +96
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 이 부분 좀 더 간단하게 하는 내장 메소드 없나 찾아봤는데 없네요...ㅋㅋ 고생하셨습니다!!

}

const queryParams = {
fid_cond_mrkt_div_code: 'J',
fid_input_iscd: stockCode,
fid_input_date_1: date1,
fid_input_date_2: date2,
fid_input_date_1: newDate1,
fid_input_date_2: newDate2,
fid_period_div_code: periodDivCode,
fid_org_adj_prc: '0',
};
Expand All @@ -98,7 +112,7 @@ export class StockDetailService {
queryParams,
);

return this.formatStockInquirePriceData(response).slice().reverse();
return this.formatStockInquirePriceData(response).slice(-30);
}

/**
Expand All @@ -111,7 +125,13 @@ export class StockDetailService {
private formatStockInquirePriceData(response: InquirePriceChartApiResponse) {
const { output2 } = response;

return output2.map((info) => {
output2.sort((a, b) => {
if (a.stck_bsop_date > b.stck_bsop_date) return 1;
if (a.stck_bsop_date < b.stck_bsop_date) return -1;
return 0;
});

return output2.map((info, index) => {
const stockData = new InquirePriceChartDataDto();
const {
stck_bsop_date,
Expand All @@ -131,6 +151,20 @@ export class StockDetailService {
stockData.acml_vol = acml_vol;
stockData.prdy_vrss_sign = prdy_vrss_sign;

if (index >= 4) {
const movAvg5 = output2
.slice(index - 4, index + 1)
.reduce((acc, cur) => acc + Number(cur.stck_clpr), 0);
stockData.mov_avg_5 = (movAvg5 / 5).toFixed(2);
}

if (index >= 19) {
const movAvg20 = output2
.slice(index - 19, index + 1)
.reduce((acc, cur) => acc + Number(cur.stck_clpr), 0);
stockData.mov_avg_20 = (movAvg20 / 20).toFixed(2);
}

return stockData;
});
}
Expand Down
Loading