Skip to content

Commit

Permalink
feat: candle 정보 수집 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungGwan123 committed Nov 21, 2024
1 parent 67eb989 commit 103b705
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/trade/trade-ask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class AskService implements OnModuleInit {
queryRunner,
);

if (!asset && tradeData.price > buyData.price) {
if (asset && tradeData.price > buyData.price) {
asset.price = Math.floor(asset.price + (tradeData.price - buyData.price) * buyData.quantity);

await this.assetRepository.updateAssetPrice(asset, queryRunner);
Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/upbit/chart.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ export class ChartRepository {
console.error("DB Searching Error : "+error)
}
}
async getSimpleChartData(key){
const data = await this.chartRedis.get(key);
if(!data){
return false;
}else return JSON.parse(data)
}
}
74 changes: 70 additions & 4 deletions packages/server/src/upbit/chart.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export class ChartService implements OnModuleInit{
this.cleanQueue()
}
async upbitApiDoor(type,coin,to, minute){
console.log("type : "+type)
console.log("market : "+coin)
console.log("minute : "+minute)
console.log("to : "+to)
const validMinutes = ["1", "3", "5", "10", "15", "30", "60", "240"];
if (type === 'minutes') {
if (!minute || !validMinutes.includes(minute)) {
Expand Down Expand Up @@ -201,4 +197,74 @@ export class ChartService implements OnModuleInit{
}
setTimeout(()=>this.cleanQueue(),100)
}
makeCandle(coinData){
const name = coinData.code;
// date와 time을 각각 파싱
const year = coinData.trade_date.slice(0, 4);
const month = coinData.trade_date.slice(4, 6);
const day = coinData.trade_date.slice(6, 8);

const hour = coinData.trade_time.slice(0, 2);
const minute = coinData.trade_time.slice(2, 4);
const second = coinData.trade_time.slice(4, 6);

const tradeDate = new Date(`${year}-${month}-${day}T${hour}:${minute}:${second}`);
const kstDate = new Date(tradeDate.getTime() + 9 * 60 * 60 * 1000 * 2);

const price = coinData.trade_price;
const timestamp = coinData.trade_timestamp;
const candle_acc_trade_volume = coinData.trade_volume
const candle_acc_trade_price = price * candle_acc_trade_volume;
const candle = {
market : name,
candle_date_time_kst : kstDate.toISOString().slice(0,19),
opening_price : price,
high_price : price,
low_price : price,
trade_price : price,
timestamp : timestamp,
candle_acc_trade_price : candle_acc_trade_price,
candle_acc_trade_volume : candle_acc_trade_volume
}
const type = ['years','months','weeks','days','minutes','seconds'];
const minute_type = ["1", "3", "5", "10", "15", "30", "60", "240"];
type.forEach(async (key)=>{
if(key === 'minutes'){
const keys = [];
minute_type.forEach((min)=>{
keys.push(this.formatDate(kstDate, key, name, min));
})
keys.forEach(async (min)=>{
const candleData = await this.chartRepository.getSimpleChartData(min);
if(!candleData){
this.chartRepository.setChartData(min,JSON.stringify(candle))
}else{
candleData.trade_price = price;
candleData.high_price = candleData.high_price < price ? price : candleData.high_price;
candleData.low_price = candleData.low_price > price ? price : candleData.low_price;
candleData.timestamp = timestamp;
candleData.candle_acc_trade_price = candle_acc_trade_price;
candleData.candle_acc_trade_volume += candle_acc_trade_volume;

this.chartRepository.setChartData(min,JSON.stringify(candleData))
}
})
}else{
const redisKey = this.formatDate(kstDate, key, name, null);
const candleData = await this.chartRepository.getSimpleChartData(redisKey);
if(!candleData){
this.chartRepository.setChartData(redisKey,JSON.stringify(candle))
}else{
candleData.trade_price = price;
candleData.high_price = candleData.high_price < price ? price : candleData.high_price;
candleData.low_price = candleData.low_price > price ? price : candleData.low_price;
candleData.timestamp = timestamp;
candleData.candle_acc_trade_price = candle_acc_trade_price;
candleData.candle_acc_trade_volume += candle_acc_trade_volume;

this.chartRepository.setChartData(redisKey,JSON.stringify(candleData))
}
}
})
}
}
3 changes: 3 additions & 0 deletions packages/server/src/upbit/coin-ticker-websocket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UPBIT_WEBSOCKET_CONNECTION_TIME,
UPBIT_WEBSOCKET_URL,
} from 'common/upbit';
import { ChartService } from './chart.service';

@Injectable()
export class CoinTickerService implements OnModuleInit {
Expand All @@ -16,6 +17,7 @@ export class CoinTickerService implements OnModuleInit {
constructor(
private readonly coinListService: CoinListService,
private readonly sseService: SseService,
private readonly chartService: ChartService
) {}

onModuleInit() {
Expand All @@ -38,6 +40,7 @@ export class CoinTickerService implements OnModuleInit {
const message = JSON.parse(data.toString());
if (message.error) throw new Error(JSON.stringify(message));
this.sseService.coinTickerSendEvent(message);
this.chartService.makeCandle(message);
} catch (error) {
console.error('CoinTickerWebSocket 오류:', error);
}
Expand Down

0 comments on commit 103b705

Please sign in to comment.