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

[#6] 3.02 주식차트 정보 기능 구현 수정 #36

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { User } from './auth/user.entity';
import { StockIndexModule } from './stock/index/stock.index.module';
import { SocketService } from './websocket/socket.service';
import { SocketGateway } from './websocket/socket.gateway';
import { TopfiveModule } from './stock/topfive/topfive.module';
import { StockTopfiveModule } from './stock/topfive/stock.topfive.module';
import { KoreaInvestmentModule } from './koreaInvestment/korea.investment.module';

@Module({
imports: [
Expand All @@ -25,9 +26,10 @@ import { TopfiveModule } from './stock/topfive/topfive.module';
entities: [User],
synchronize: true,
}),
KoreaInvestmentModule,
AuthModule,
StockIndexModule,
TopfiveModule,
StockTopfiveModule,
],
controllers: [AppController],
providers: [AppService, SocketService, SocketGateway],
Expand Down
10 changes: 10 additions & 0 deletions BE/src/koreaInvestment/korea.investment.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { KoreaInvestmentService } from './korea.investment.service';

@Module({
imports: [],
controllers: [],
providers: [KoreaInvestmentService],
exports: [KoreaInvestmentService],
})
export class KoreaInvestmentModule {}
28 changes: 28 additions & 0 deletions BE/src/koreaInvestment/korea.investment.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import axios from 'axios';

export class KoreaInvestmentService {
private accessToken: string;
private tokenExpireTime: Date;

async getAccessToken() {
// accessToken이 유효한 경우
if (this.accessToken && this.tokenExpireTime > new Date()) {
return this.accessToken;
}
const response = await axios.post(
`${process.env.KOREA_INVESTMENT_BASE_URL}/oauth2/tokenP`,
{
grant_type: 'client_credentials',
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
appsecret: process.env.KOREA_INVESTMENT_APP_SECRET,
},
);

const { data } = response;

this.accessToken = data.access_token;
this.tokenExpireTime = new Date(Date.now() + +data.expires_in);

return this.accessToken;
}
}
6 changes: 6 additions & 0 deletions BE/src/stock/enum/MarketType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum MarketType {
ALL = 'ALL',
KOSPI = 'KOSPI',
KOSDAQ = 'KOSDAQ',
KOSPI200 = 'KOSPI200',
}
68 changes: 68 additions & 0 deletions BE/src/stock/index/interface/stock.index.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export interface AccessTokenInterface {
access_token: string;
access_token_token_expired: string;
token_type: string;
expires_in: number;
}

export interface StockIndexChartInterface {
output: StockIndexChartElementInterface[];
rt_cd: string;
msg_cd: string;
msg1: string;
}

interface StockIndexChartElementInterface {
bsop_hour: string;
bstp_nmix_prpr: string;
bstp_nmix_prdy_vrss: string;
prdy_vrss_sign: string;
bstp_nmix_prdy_ctrt: string;
acml_tr_pbmn: string;
acml_vol: string;
cntg_vol: string;
}

export interface StockIndexValueInterface {
output: {
bstp_nmix_prpr: string;
bstp_nmix_prdy_vrss: string;
prdy_vrss_sign: string;
bstp_nmix_prdy_ctrt: string;
acml_vol: string;
prdy_vol: string;
acml_tr_pbmn: string;
prdy_tr_pbmn: string;
bstp_nmix_oprc: string;
prdy_nmix_vrss_nmix_oprc: string;
oprc_vrss_prpr_sign: string;
bstp_nmix_oprc_prdy_ctrt: string;
bstp_nmix_hgpr: string;
prdy_nmix_vrss_nmix_hgpr: string;
hgpr_vrss_prpr_sign: string;
bstp_nmix_hgpr_prdy_ctrt: string;
bstp_nmix_lwpr: string;
prdy_clpr_vrss_lwpr: string;
lwpr_vrss_prpr_sign: string;
prdy_clpr_vrss_lwpr_rate: string;
ascn_issu_cnt: string;
uplm_issu_cnt: string;
stnr_issu_cnt: string;
down_issu_cnt: string;
lslm_issu_cnt: string;
dryy_bstp_nmix_hgpr: string;
dryy_hgpr_vrss_prpr_rate: string;
dryy_bstp_nmix_hgpr_date: string;
dryy_bstp_nmix_lwpr: string;
dryy_lwpr_vrss_prpr_rate: string;
dryy_bstp_nmix_lwpr_date: string;
total_askp_rsqn: string;
total_bidp_rsqn: string;
seln_rsqn_rate: string;
shnu_rsqn_rate: string;
ntby_rsqn: string;
};
rt_cd: string;
msg_cd: string;
msg1: string;
}
48 changes: 39 additions & 9 deletions BE/src/stock/index/stock.index.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { Controller, Get } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { StockIndexService } from './stock.index.service';
import { StockIndexResponseDto } from './dto/stock.index.response.dto';
import { KoreaInvestmentService } from '../../koreaInvestment/korea.investment.service';

@Controller('/api/stocks/index')
@ApiTags('주가 지수 API')
export class StockIndexController {
constructor(private readonly stockIndexService: StockIndexService) {}
constructor(
private readonly stockIndexService: StockIndexService,
private readonly koreaInvestmentService: KoreaInvestmentService,
) {}

@Get()
@ApiOperation({
Expand All @@ -19,18 +23,44 @@ export class StockIndexController {
type: StockIndexResponseDto,
})
async getStockIndex() {
const accessToken = await this.koreaInvestmentService.getAccessToken();

const stockLists = await Promise.all([
this.stockIndexService.getDomesticStockIndexListByCode('0001'), // 코스피
this.stockIndexService.getDomesticStockIndexListByCode('1001'), // 코스닥
this.stockIndexService.getDomesticStockIndexListByCode('2001'), // 코스피200
this.stockIndexService.getDomesticStockIndexListByCode('3003'), // KSQ150
this.stockIndexService.getDomesticStockIndexListByCode(
'0001',
accessToken,
), // 코스피
this.stockIndexService.getDomesticStockIndexListByCode(
'1001',
accessToken,
), // 코스닥
this.stockIndexService.getDomesticStockIndexListByCode(
'2001',
accessToken,
), // 코스피200
this.stockIndexService.getDomesticStockIndexListByCode(
'3003',
accessToken,
), // KSQ150
]);

const stockValues = await Promise.all([
this.stockIndexService.getDomesticStockIndexValueByCode('0001'), // 코스피
this.stockIndexService.getDomesticStockIndexValueByCode('1001'), // 코스닥
this.stockIndexService.getDomesticStockIndexValueByCode('2001'), // 코스피200
this.stockIndexService.getDomesticStockIndexValueByCode('3003'), // KSQ150
this.stockIndexService.getDomesticStockIndexValueByCode(
'0001',
accessToken,
), // 코스피
this.stockIndexService.getDomesticStockIndexValueByCode(
'1001',
accessToken,
), // 코스닥
this.stockIndexService.getDomesticStockIndexValueByCode(
'2001',
accessToken,
), // 코스피200
this.stockIndexService.getDomesticStockIndexValueByCode(
'3003',
accessToken,
), // KSQ150
]);

return new StockIndexResponseDto(stockLists, stockValues);
Expand Down
3 changes: 2 additions & 1 deletion BE/src/stock/index/stock.index.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { StockIndexController } from './stock.index.controller';
import { StockIndexService } from './stock.index.service';
import { KoreaInvestmentModule } from '../../koreaInvestment/korea.investment.module';

@Module({
imports: [],
imports: [KoreaInvestmentModule],
controllers: [StockIndexController],
providers: [StockIndexService],
exports: [StockIndexService],
Expand Down
111 changes: 6 additions & 105 deletions BE/src/stock/index/stock.index.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Injectable } from '@nestjs/common';
import { StockIndexListChartElementDto } from './dto/stock.index.list.chart.element.dto';
import { StockIndexListElementDto } from './dto/stock.index.list.element.dto';
import { StockIndexValueElementDto } from './dto/stock.index.value.element.dto';
import {
StockIndexChartInterface,
StockIndexValueInterface,
} from './interface/stock.index.interface';

@Injectable()
export class StockIndexService {
private accessToken: string;
private expireDateTime: number;

async getDomesticStockIndexListByCode(code: string) {
const accessToken = await this.getAccessToken();

async getDomesticStockIndexListByCode(code: string, accessToken: string) {
const url = `${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-timeprice`;
const queryParams = `?FID_INPUT_HOUR_1=300&FID_COND_MRKT_DIV_CODE=U&FID_INPUT_ISCD=${code}`;

Expand Down Expand Up @@ -40,9 +39,7 @@ export class StockIndexService {
);
}

async getDomesticStockIndexValueByCode(code: string) {
const accessToken = await this.getAccessToken();

async getDomesticStockIndexValueByCode(code: string, accessToken: string) {
const url = `${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-price`;
const queryParams = `?FID_COND_MRKT_DIV_CODE=U&FID_INPUT_ISCD=${code}`;

Expand All @@ -67,100 +64,4 @@ export class StockIndexService {
result.output.prdy_vrss_sign,
);
}

private async getAccessToken() {
if (!this.accessToken || this.expireDateTime <= Date.now()) {
const url = 'https://openapivts.koreainvestment.com:29443/oauth2/tokenP';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({
grant_type: 'client_credentials',
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
appsecret: process.env.KOREA_INVESTMENT_APP_SECRET,
}),
});
const result: AccessTokenInterface = await response.json();
this.accessToken = result.access_token;
this.expireDateTime = new Date(
result.access_token_token_expired,
).getTime();
return result.access_token;
}

return this.accessToken;
}
}

// interfaces

interface AccessTokenInterface {
access_token: string;
access_token_token_expired: string;
token_type: string;
expires_in: number;
}

interface StockIndexChartInterface {
output: StockIndexChartElementInterface[];
rt_cd: string;
msg_cd: string;
msg1: string;
}

interface StockIndexChartElementInterface {
bsop_hour: string;
bstp_nmix_prpr: string;
bstp_nmix_prdy_vrss: string;
prdy_vrss_sign: string;
bstp_nmix_prdy_ctrt: string;
acml_tr_pbmn: string;
acml_vol: string;
cntg_vol: string;
}

interface StockIndexValueInterface {
output: {
bstp_nmix_prpr: string;
bstp_nmix_prdy_vrss: string;
prdy_vrss_sign: string;
bstp_nmix_prdy_ctrt: string;
acml_vol: string;
prdy_vol: string;
acml_tr_pbmn: string;
prdy_tr_pbmn: string;
bstp_nmix_oprc: string;
prdy_nmix_vrss_nmix_oprc: string;
oprc_vrss_prpr_sign: string;
bstp_nmix_oprc_prdy_ctrt: string;
bstp_nmix_hgpr: string;
prdy_nmix_vrss_nmix_hgpr: string;
hgpr_vrss_prpr_sign: string;
bstp_nmix_hgpr_prdy_ctrt: string;
bstp_nmix_lwpr: string;
prdy_clpr_vrss_lwpr: string;
lwpr_vrss_prpr_sign: string;
prdy_clpr_vrss_lwpr_rate: string;
ascn_issu_cnt: string;
uplm_issu_cnt: string;
stnr_issu_cnt: string;
down_issu_cnt: string;
lslm_issu_cnt: string;
dryy_bstp_nmix_hgpr: string;
dryy_hgpr_vrss_prpr_rate: string;
dryy_bstp_nmix_hgpr_date: string;
dryy_bstp_nmix_lwpr: string;
dryy_lwpr_vrss_prpr_rate: string;
dryy_bstp_nmix_lwpr_date: string;
total_askp_rsqn: string;
total_bidp_rsqn: string;
seln_rsqn_rate: string;
shnu_rsqn_rate: string;
ntby_rsqn: string;
};
rt_cd: string;
msg_cd: string;
msg1: string;
}
33 changes: 33 additions & 0 deletions BE/src/stock/topfive/interface/stock.topfive.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export interface StockApiOutputData {
stck_shrn_iscd: string;
data_rank: string;
hts_kor_isnm: string;
stck_prpr: string;
prdy_vrss: string;
prdy_vrss_sign: string;
prdy_ctrt: string;
acml_vol: string;
stck_hgpr: string;
hgpr_hour: string;
acml_hgpr_data: string;
stck_lwpr: string;
lwpr_hour: string;
acml_lwpr_date: string;
lwpr_vrss_prpr_rate: string;
dsgt_date_clpr_vrss_prpr_rate: string;
cnnt_ascn_dynu: string;
hgpr_vrss_prpr_rate: string;
cnnt_down_dynu: string;
oprc_vrss_prpr_sign: string;
oprc_vrss_prpr: string;
oprc_vrss_prpr_rate: string;
prd_rsfl: string;
prd_rsfl_rate: string;
}

export interface StockApiResponse {
output: StockApiOutputData[];
rt_cd: string;
msg_cd: string;
msg1: string;
}
Loading