-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ง fix: stock-order-socket๊ณผ stock-order ์๋น์ค๊ฐ ์ํ์ฐธ์กฐ ํ์ง ์๊ฒ ์์ #53
- ๊ฐ socket service๋ฅผ ๊ฐ ๊ธฐ๋ฅ ๋ชจ๋์ ์ข ์๋๋๋ก ์์ ํจ.
- Loading branch information
Showing
7 changed files
with
136 additions
and
157 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
...stock/index/stock-index-socket.service.ts โ ...stock/index/stock-index-socket.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { | ||
Injectable, | ||
InternalServerErrorException, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { LessThanOrEqual, MoreThanOrEqual } from 'typeorm'; | ||
import { BaseSocketService } from '../../websocket/base-socket.service'; | ||
import { SocketGateway } from '../../websocket/socket.gateway'; | ||
import { Order } from './stock-order.entity'; | ||
import { TradeType } from './enum/trade-type'; | ||
import { StatusType } from './enum/status-type'; | ||
import { StockOrderRepository } from './stock-order.repository'; | ||
|
||
@Injectable() | ||
export class StockOrderSocketService { | ||
private TR_ID = 'H0STCNT0'; | ||
|
||
private readonly logger = new Logger(); | ||
|
||
constructor( | ||
private readonly socketGateway: SocketGateway, | ||
private readonly baseSocketService: BaseSocketService, | ||
private readonly stockOrderRepository: StockOrderRepository, | ||
) { | ||
baseSocketService.registerSocketOpenHandler(async () => { | ||
const orders: Order[] = | ||
await this.stockOrderRepository.findAllCodeByStatus(); | ||
orders.forEach((order) => { | ||
baseSocketService.registerCode(this.TR_ID, order.stock_code); | ||
}); | ||
}); | ||
|
||
baseSocketService.registerSocketDataHandler( | ||
this.TR_ID, | ||
(data: string[]) => { | ||
this.checkExecutableOrder( | ||
data[0], // ์ฃผ์ ์ฝ๋ | ||
data[2], // ์ฃผ์ ์ฒด๊ฒฐ๊ฐ | ||
).catch(() => { | ||
throw new InternalServerErrorException(); | ||
}); | ||
}, | ||
); | ||
} | ||
|
||
subscribeByCode(trKey: string) { | ||
this.baseSocketService.registerCode(this.TR_ID, trKey); | ||
} | ||
|
||
unsubscribeByCode(trKey: string) { | ||
this.baseSocketService.unregisterCode(this.TR_ID, trKey); | ||
} | ||
|
||
private async checkExecutableOrder(stockCode: string, value) { | ||
const buyOrders = await this.stockOrderRepository.find({ | ||
where: { | ||
stock_code: stockCode, | ||
trade_type: TradeType.BUY, | ||
status: StatusType.PENDING, | ||
price: MoreThanOrEqual(value), | ||
}, | ||
}); | ||
|
||
const sellOrders = await this.stockOrderRepository.find({ | ||
where: { | ||
stock_code: stockCode, | ||
trade_type: TradeType.SELL, | ||
status: StatusType.PENDING, | ||
price: LessThanOrEqual(value), | ||
}, | ||
}); | ||
|
||
await Promise.all(buyOrders.map((buyOrder) => this.executeBuy(buyOrder))); | ||
await Promise.all( | ||
sellOrders.map((sellOrder) => this.executeSell(sellOrder)), | ||
); | ||
|
||
if ( | ||
!(await this.stockOrderRepository.existsBy({ | ||
stock_code: stockCode, | ||
status: StatusType.PENDING, | ||
})) | ||
) | ||
this.unsubscribeByCode(stockCode); | ||
} | ||
|
||
private async executeBuy(order) { | ||
this.logger.log(`${order.id}๋ฒ ๋งค์ ์์ฝ์ด ์ฒด๊ฒฐ๋์์ต๋๋ค.`, 'BUY'); | ||
|
||
const totalPrice = order.price * order.amount; | ||
const fee = this.calculateFee(totalPrice); | ||
await this.stockOrderRepository.updateOrderAndAssetWhenBuy( | ||
order, | ||
totalPrice + fee, | ||
); | ||
} | ||
|
||
private async executeSell(order) { | ||
this.logger.log(`${order.id}๋ฒ ๋งค๋ ์์ฝ์ด ์ฒด๊ฒฐ๋์์ต๋๋ค.`, 'SELL'); | ||
|
||
const totalPrice = order.price * order.amount; | ||
const fee = this.calculateFee(totalPrice); | ||
await this.stockOrderRepository.updateOrderAndAssetWhenSell( | ||
order, | ||
totalPrice - fee, | ||
); | ||
} | ||
|
||
private calculateFee(totalPrice: number) { | ||
if (totalPrice <= 10000000) return totalPrice * 0.16; | ||
if (totalPrice > 10000000 && totalPrice <= 50000000) | ||
return totalPrice * 0.14; | ||
if (totalPrice > 50000000 && totalPrice <= 100000000) | ||
return totalPrice * 0.12; | ||
if (totalPrice > 100000000 && totalPrice <= 300000000) | ||
return totalPrice * 0.1; | ||
return totalPrice * 0.08; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
import { forwardRef, Module } from '@nestjs/common'; | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { StockOrderController } from './stock-order.controller'; | ||
import { StockOrderService } from './stock-order.service'; | ||
import { Order } from './stock-order.entity'; | ||
import { StockOrderRepository } from './stock-order.repository'; | ||
import { SocketModule } from '../../websocket/socket.module'; | ||
import { AssetModule } from '../../asset/asset.module'; | ||
import { StockOrderSocketService } from './stock-order-socket.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([Order]), | ||
forwardRef(() => SocketModule), | ||
AssetModule, | ||
], | ||
imports: [TypeOrmModule.forFeature([Order]), SocketModule, AssetModule], | ||
controllers: [StockOrderController], | ||
providers: [StockOrderService, StockOrderRepository], | ||
exports: [StockOrderService], | ||
providers: [StockOrderService, StockOrderRepository, StockOrderSocketService], | ||
}) | ||
export class StockOrderModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,10 @@ | ||
import { forwardRef, Module } from '@nestjs/common'; | ||
import { StockIndexSocketService } from './stock/index/stock-index-socket.service'; | ||
import { Module } from '@nestjs/common'; | ||
import { SocketGateway } from './socket.gateway'; | ||
import { SocketTokenService } from './socket-token.service'; | ||
import { StockPriceSocketService } from './stock/price/stock-price-socket.service'; | ||
import { BaseSocketService } from './base-socket.service'; | ||
import { StockOrderModule } from '../stock/order/stock-order.module'; | ||
|
||
@Module({ | ||
imports: [forwardRef(() => StockOrderModule)], | ||
controllers: [], | ||
providers: [ | ||
SocketTokenService, | ||
StockIndexSocketService, | ||
SocketGateway, | ||
StockPriceSocketService, | ||
BaseSocketService, | ||
], | ||
exports: [SocketGateway, StockPriceSocketService], | ||
providers: [SocketTokenService, SocketGateway, BaseSocketService], | ||
exports: [SocketGateway, BaseSocketService], | ||
}) | ||
export class SocketModule {} |
51 changes: 0 additions & 51 deletions
51
BE/src/websocket/stock/price/stock-price-socket.service.ts
This file was deleted.
Oops, something went wrong.