-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] : 자동매매 기능 구현
- Loading branch information
Showing
16 changed files
with
429 additions
and
149 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
backend/src/main/java/org/dgu/backend/domain/UserTradingLog.java
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,50 @@ | ||
package org.dgu.backend.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.dgu.backend.common.BaseEntity; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@Table(name = "users_trading_logs") | ||
public class UserTradingLog extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "trading_logs_id") | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "users_id", foreignKey = @ForeignKey(name = "users_trading_logs_fk_portfolios_id")) | ||
private User user; | ||
|
||
@Column(name = "type", nullable = false) | ||
private String type; | ||
|
||
@Column(name = "capital", nullable = false) | ||
private Long capital; | ||
|
||
@Column(name = "coin", nullable = false, precision = 30, scale = 15) | ||
private BigDecimal coin; | ||
|
||
@Column(name = "coin_price", nullable = false) | ||
private Double coinPrice; | ||
|
||
@Column(name = "rate", nullable = false) | ||
private Double rate; | ||
|
||
@Builder | ||
public UserTradingLog(User user, String type, Long capital, BigDecimal coin, Double coinPrice, Double rate){ | ||
this.user = user; | ||
this.type = type; | ||
this.capital = capital; | ||
this.coin = coin; | ||
this.coinPrice = coinPrice; | ||
this.rate = rate; | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/org/dgu/backend/repository/UserTradingLogRepository.java
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,15 @@ | ||
package org.dgu.backend.repository; | ||
|
||
import io.lettuce.core.dynamic.annotation.Param; | ||
import org.dgu.backend.domain.User; | ||
import org.dgu.backend.domain.UserTradingLog; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
|
||
public interface UserTradingLogRepository extends JpaRepository<UserTradingLog,Long> { | ||
@Query("SELECT utl FROM UserTradingLog utl WHERE utl.user = :user " + | ||
"AND utl.createdAt > (SELECT MAX(utl2.createdAt) FROM UserTradingLog utl2 WHERE utl2.user = :user AND utl2.type = 'SELL')") | ||
List<UserTradingLog> findRecentLogsAfterLastSell(@Param("user") User user); | ||
} |
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
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/org/dgu/backend/service/OrderService.java
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,9 @@ | ||
package org.dgu.backend.service; | ||
|
||
import org.dgu.backend.domain.UpbitKey; | ||
|
||
import java.util.Map; | ||
|
||
public interface OrderService { | ||
void executeOrder(Map<String, String> params, UpbitKey upbitKey); | ||
} |
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/org/dgu/backend/service/OrderServiceImpl.java
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,31 @@ | ||
package org.dgu.backend.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.dgu.backend.domain.UpbitKey; | ||
import org.dgu.backend.util.HashUtil; | ||
import org.dgu.backend.util.JwtUtil; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OrderServiceImpl implements OrderService { | ||
@Value("${upbit.url.order}") | ||
private String UPBIT_URL_ORDER; | ||
private final JwtUtil jwtUtil; | ||
private final UpbitApiClient upbitApiClient; | ||
|
||
// 주문을 진행하는 메서드 | ||
@Override | ||
@Transactional | ||
public void executeOrder(Map<String, String> params, UpbitKey upbitKey) { | ||
String queryString = HashUtil.buildQueryString(params); | ||
String queryHash = HashUtil.generateQueryHash(queryString); | ||
String authenticationToken = jwtUtil.generateUpbitOrderToken(upbitKey, queryHash); | ||
upbitApiClient.createNewOrder(UPBIT_URL_ORDER, authenticationToken, params); | ||
System.out.println("자동매매에 성공했습니다."); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/org/dgu/backend/service/TradingService.java
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,8 +1,12 @@ | ||
package org.dgu.backend.service; | ||
|
||
import org.dgu.backend.domain.PortfolioOption; | ||
import org.dgu.backend.domain.TradingOption; | ||
import org.dgu.backend.domain.User; | ||
import org.dgu.backend.dto.TradingDto; | ||
|
||
public interface TradingService { | ||
void registerAutoTrading(String authorizationHeader, TradingDto.AutoTradingRequest autoTradingRequest); | ||
void removeAutoTrading(String authorizationHeader, String portfolioId); | ||
void executeTrade(User user, PortfolioOption portfolioOption, TradingOption tradingOption, Double curPrice); | ||
} |
Oops, something went wrong.