Skip to content

Commit

Permalink
Merge pull request #72 from CSID-DGU/feature/#33/trading
Browse files Browse the repository at this point in the history
[feat] : 업비트 자동매매 매수 기능까지 구현 완료
  • Loading branch information
bbbang105 authored Jun 16, 2024
2 parents 4d29c38 + 7fc44f3 commit 24dd197
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 89 deletions.
2 changes: 2 additions & 0 deletions backend/src/main/java/org/dgu/backend/BackendApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class BackendApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.dgu.backend.common.constant.SuccessStatus;
import org.dgu.backend.dto.TradingDto;
import org.dgu.backend.service.TradingService;
import org.dgu.backend.service.UpbitAutoTrader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -13,6 +14,7 @@
@RequiredArgsConstructor
public class TradingController {
private final TradingService tradingService;
private final UpbitAutoTrader upbitAutoTrader;

// 자동매매 등록 API
@PostMapping
Expand All @@ -33,4 +35,10 @@ public ResponseEntity<ApiResponse<Object>> removeAutoTrading(
tradingService.removeAutoTrading(authorizationHeader, portfolioId);
return ApiResponse.onSuccess(SuccessStatus.SUCCESS_DELETE_TRADING);
}

// 자동매매 수동 테스트 API
@GetMapping("/test")
public void test() {
upbitAutoTrader.performAutoTrading();
}
}
5 changes: 3 additions & 2 deletions backend/src/main/java/org/dgu/backend/domain/TradingLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;
import org.dgu.backend.common.BaseEntity;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Entity
Expand All @@ -33,7 +34,7 @@ public class TradingLog extends BaseEntity {
private Long capital;

@Column(name = "coin", nullable = false)
private Double coin;
private BigDecimal coin;

@Column(name = "coin_price", nullable = false)
private Long coinPrice;
Expand All @@ -42,7 +43,7 @@ public class TradingLog extends BaseEntity {
private Double rate;

@Builder
public TradingLog(Portfolio portfolio, String type, LocalDateTime date, Long capital, Double coin, Long coinPrice, Double rate){
public TradingLog(Portfolio portfolio, String type, LocalDateTime date, Long capital, BigDecimal coin, Long coinPrice, Double rate){
this.portfolio = portfolio;
this.type = type;
this.date = date;
Expand Down
36 changes: 26 additions & 10 deletions backend/src/main/java/org/dgu/backend/domain/TradingOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ public class TradingOption extends BaseEntity {
@JoinColumn(name = "portfolios_id", foreignKey = @ForeignKey(name = "trading_options_fk_portfolios_id"))
private Portfolio portfolio;

@Column(name = "start_capital", nullable = false)
private BigDecimal startCapital;
@Column(name = "initial_capital", nullable = false)
private Long initialCapital;

@Column(name = "current_capital", nullable = false)
private Long currentCapital;

@Column(name = "avg_price")
private BigDecimal avgPrice;
private Double avgPrice;

@Column(name = "trading_unit_price", nullable = false)
private BigDecimal tradingUnitPrice;
private Long tradingUnitPrice;

@Column(name = "coin_count", nullable = false, scale = 10)
private BigDecimal coinCount;

@Column(name = "trading_count", nullable = false)
private int tradingCount;

@Column(name = "remained_buying_count", nullable = false)
private int remainedBuyingCount;
@Column(name = "buying_count", nullable = false)
private int buyingCount;

@Column(name = "start_date", nullable = false)
private LocalDateTime startDate;
Expand All @@ -47,17 +56,24 @@ public class TradingOption extends BaseEntity {
private LocalDateTime endDate;

@Builder
public TradingOption(User user, Portfolio portfolio, BigDecimal startCapital, BigDecimal tradingUnitPrice, int remainedBuyingCount, LocalDateTime startDate, LocalDateTime endDate) {
public TradingOption(User user, Portfolio portfolio, Long initialCapital, Long currentCapital, Long tradingUnitPrice, int tradingCount, int buyingCount, LocalDateTime startDate, LocalDateTime endDate) {
this.user = user;
this.portfolio = portfolio;
this.startCapital = startCapital;
this.initialCapital = initialCapital;
this.currentCapital = currentCapital;
this.tradingUnitPrice = tradingUnitPrice;
this.remainedBuyingCount = remainedBuyingCount;
this.coinCount = BigDecimal.ZERO;
this.tradingCount = tradingCount;
this.buyingCount = buyingCount;
this.startDate = startDate;
this.endDate = endDate;
}

public void updateAvgPrice(BigDecimal avgPrice) {
public void updateAvgPrice(Double avgPrice) {
this.avgPrice = avgPrice;
}

public void updateCoinCount(BigDecimal coinCount) {
this.coinCount = coinCount;
}
}
10 changes: 6 additions & 4 deletions backend/src/main/java/org/dgu/backend/dto/BackTestingDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.Getter;
import org.dgu.backend.domain.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -69,7 +71,7 @@ public static class BackTestingResult {
private LocalDateTime date;
private String action;
private Double coinPrice;
private Double coin;
private BigDecimal coin;
private Long capital;
private Double rate;
private Long income;
Expand All @@ -79,12 +81,12 @@ public void updateRate(Double rate) {
this.rate = rate;
}

public static BackTestingDto.BackTestingResult of(LocalDateTime date, String action, Double coinPrice, Double coin, Long capital, Double rate, Long income, Integer tradingPeriod) {
public static BackTestingDto.BackTestingResult of(LocalDateTime date, String action, Double coinPrice, BigDecimal coin, Long capital, Double rate, Long income, Integer tradingPeriod) {
return BackTestingDto.BackTestingResult.builder()
.date(date)
.action(action)
.coinPrice(coinPrice)
.coin(coin)
.coin(coin.setScale(3, RoundingMode.HALF_UP))
.capital(capital)
.rate(rate)
.income(income)
Expand Down Expand Up @@ -222,7 +224,7 @@ public static class TradingLog {
private String type;
private LocalDateTime date;
private Long capital;
private Double coin;
private BigDecimal coin;
private Long coinPrice;
private Double rate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static DashBoardDto.RepresentativeCoinResponse of(UpbitDto.Ticker ticker,
.marketName(ticker.getMarket())
.koreanName(koreanName)
.englishName(englishName)
.changePrice(BigDecimal.valueOf(ticker.getPrice()))
.changePrice(ticker.getPrice())
.changeRate(BigDecimal.valueOf(ticker.getChangeRate()).setScale(5, RoundingMode.HALF_UP))
.isIncrease(ticker.getChange().equals("RISE"))
.build();
Expand Down
9 changes: 5 additions & 4 deletions backend/src/main/java/org/dgu/backend/dto/TradingDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.dgu.backend.domain.TradingOption;
import org.dgu.backend.domain.User;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

Expand All @@ -31,9 +30,11 @@ public TradingOption to(User user, Portfolio portfolio, PortfolioOption portfoli
return TradingOption.builder()
.user(user)
.portfolio(portfolio)
.startCapital(BigDecimal.valueOf(fund))
.tradingUnitPrice(BigDecimal.valueOf(fund / portfolioOption.getTradingUnit()))
.remainedBuyingCount(portfolioOption.getTradingUnit())
.initialCapital(fund)
.currentCapital(fund)
.tradingUnitPrice(fund / portfolioOption.getTradingUnit())
.tradingCount(portfolioOption.getTradingUnit())
.buyingCount(0)
.startDate(startDate)
.endDate(endDate)
.build();
Expand Down
58 changes: 55 additions & 3 deletions backend/src/main/java/org/dgu/backend/dto/UpbitDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.dgu.backend.domain.Market;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public class UpbitDto {
@Builder
Expand Down Expand Up @@ -86,8 +87,8 @@ public static class Account {
private String currency;
@JsonProperty("balance")
private BigDecimal coinCount;
private BigDecimal locked;
private BigDecimal avgBuyPrice;
private Double locked;
private Double avgBuyPrice;
private String unitCurrency;
}

Expand All @@ -99,11 +100,62 @@ public static class Account {
public static class Ticker {
private String market;
@JsonProperty("trade_price")
private Double price;
private BigDecimal price;
private String change;
@JsonProperty("signed_change_rate")
private Double changeRate;
@JsonProperty("signed_change_price")
private Double changePrice;
}

@Getter
@Builder
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class OrderRequest {
private String market;
private String side;
private BigDecimal volume;
private Double price;
private String ordType;

public static OrderRequest of(String market, String side, BigDecimal volume, Double price, String ordType) {
return OrderRequest.builder()
.market(market)
.side(side)
.volume(volume)
.price(price)
.ordType(ordType)
.build();
}
}

@Getter
@Builder
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class OrderResponse {
private String uuid;
private String side;
private String ordType;
private String price;
private String state;
private String market;
@JsonProperty("created_at")
private LocalDateTime createdAt;
private String volume;
@JsonProperty("remaining_volume")
private String remainingVolume;
@JsonProperty("reserved_fee")
private String reservedFee;
@JsonProperty("remaining_fee")
private String remainingFee;
@JsonProperty("paid_fee")
private String paidFee;
private String locked;
@JsonProperty("executed_volume")
private String executedVolume;
@JsonProperty("trades_count")
private Integer tradesCount;
}
}
Loading

0 comments on commit 24dd197

Please sign in to comment.