-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/realitybot #128
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
Merged
Merged
Feat/realitybot #128
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d7d7d5e
feat: 호가 단위 정책 작업중
109an94 4ff7e11
feat: 호가 단위 정책
109an94 28e4826
feat: 주문 강도 조절
109an94 05d944d
refactor: 로그 출력 수정
109an94 11f331c
Merge remote-tracking branch 'origin/feat/realitybot' into feat/reali…
109an94 7e0170b
add: 주석 수정
109an94 20502ab
test: 호가 수집 단위테스트
109an94 5cd8d40
config: junit realitybot만 실행
109an94 cc32af4
refactor: api 요청 중 잘못된 응답 처리
109an94 8376686
config: 호가 수집 시각 수정
109an94 4d87da4
test: 호가 수집 단위테스트
109an94 2d60130
remove: 모듈 결합 전 코드 삭제
109an94 6879f73
refactor: 코드 개선
109an94 142717c
test: 단위테스트 코드 작성
109an94 6ce7966
test: 시세 수집 단위테스트
109an94 7effaf4
refactor: 코드 개선
109an94 3276b33
test: 시세 수집 단위테스트
109an94 e51cb5b
feat: 선형 보간 작업
109an94 d36a61b
test: 거래소 및 보간 단위 테스트
109an94 fdd6d6e
feat: 신규 거래소 추가
109an94 c8aecc1
test: 단위 테스트
109an94 b504a50
refactor: 가격 정책 변경
109an94 ebe6e44
test: 단위 테스트
109an94 04e121f
refactor: exception 수정
109an94 9742b2c
Merge branch 'dev' of https://github.com/CleanEngine/cleanengine-be i…
109an94 a3e8a62
refactor: 오류 수정
109an94 1d9a8ba
refactor: 오류 수정
109an94 60cabd8
refactor: log 수정
109an94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ repositories { | |
| mavenCentral() | ||
| } | ||
|
|
||
|
|
||
| jacoco { //추가함 | ||
| toolVersion = "0.8.13" | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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
60 changes: 60 additions & 0 deletions
60
src/main/java/com/cleanengine/coin/realitybot/api/CoinoneAPIClient.java
This file contains hidden or 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,60 @@ | ||
| package com.cleanengine.coin.realitybot.api; | ||
|
|
||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class CoinoneAPIClient { | ||
| private final OkHttpClient client; | ||
| private String ticker; | ||
|
|
||
|
|
||
| public String get(String ticker){ //API를 responseBody에 담아 반환 | ||
| this.ticker = ticker; | ||
| Request request = new Request.Builder() | ||
| .url("https://api.coinone.co.kr/public/v2/trades/KRW/"+ticker+"?size=10") | ||
| .get() | ||
| .addHeader("accept", "application/json") | ||
| .build(); | ||
| try (Response response = client.newCall(request).execute()){ | ||
| if ((response.code() == 400)){ | ||
| log.warn("잘못된 ticker를 입력하였습니다. 입력된 ticker : {}",ticker); | ||
| } | ||
| String responseBody = response.body().string(); | ||
| // return gson.toJson(response.body().string()); | ||
| log.info("{}의 Bithumb API 응답 : {}",ticker,responseBody); | ||
| return responseBody; | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| /* public String getOpeningPrice(String ticker){ | ||
| this.ticker = ticker; | ||
| Request request = new Request.Builder() | ||
| .url("https://api.bithumb.com/v1/ticker?markets=KRW-"+ticker) | ||
| .get() | ||
| .addHeader("accept", "application/json") | ||
| .build(); | ||
| try (Response response = client.newCall(request).execute()){ | ||
| if ((response.code() == 400)){ | ||
| log.warn("잘못된 ticker를 입력하였습니다. 입력된 ticker : {}",ticker); | ||
| } | ||
| String responseBody = response.body().string(); | ||
| // return gson.toJson(response.body().string()); | ||
| log.debug("{}의 OpeningPirce 응답 : {}",ticker,responseBody); | ||
| return responseBody; | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("API 요청 중 예외 발생",e); | ||
| } | ||
| }*/ | ||
|
|
||
| } |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/cleanengine/coin/realitybot/api/UnitPriceRefresher.java
This file contains hidden or 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,64 @@ | ||
| package com.cleanengine.coin.realitybot.api; | ||
|
|
||
| import com.cleanengine.coin.order.domain.Asset; | ||
| import com.cleanengine.coin.order.infra.AssetRepository; | ||
caniro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import com.cleanengine.coin.realitybot.dto.OpeningPrice; | ||
| import com.cleanengine.coin.realitybot.parser.OpeningPriceParser; | ||
| import com.cleanengine.coin.realitybot.vo.UnitPricePolicy; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.ApplicationArguments; | ||
| import org.springframework.boot.ApplicationRunner; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class UnitPriceRefresher implements ApplicationRunner { | ||
| private final UnitPricePolicy unitPricePolicy; | ||
| private final AssetRepository assetRepository; | ||
| private final BithumbAPIClient bithumbAPIClient; | ||
| private final OpeningPriceParser openingPriceParser; | ||
| private final Map<String ,Double> unitPriceCache = new ConcurrentHashMap<>(); | ||
|
|
||
| @Override | ||
| public void run(ApplicationArguments args){ | ||
| log.info("Running Unit Price Refresher..."); | ||
| initializeUnitPrices(); | ||
| } | ||
|
|
||
| public void initializeUnitPrices() { | ||
| List<Asset> tickers = assetRepository.findAll(); | ||
| for (Asset ticker : tickers){ | ||
| double unitPrice = fetchOpeningPriceFromAPI(ticker.getTicker()); | ||
| unitPriceCache.put(ticker.getTicker(),unitPrice); | ||
| } | ||
| } | ||
|
|
||
| @Scheduled(cron = "${bot-handler.corn}") | ||
| public void refreshUnitPrices() { | ||
| initializeUnitPrices(); | ||
| // List<Asset> tickers = assetRepository.findAll(); | ||
| // for (Asset ticker : tickers){ | ||
| // double unitPrice = fetchOpeningPriceFromAPI(ticker.getTicker()); | ||
| // unitPriceCache.put(ticker.getTicker(),unitPrice); | ||
| // } | ||
|
|
||
| } | ||
|
|
||
| private double fetchOpeningPriceFromAPI(String ticker) { | ||
| String rawJson = bithumbAPIClient.getOpeningPrice(ticker); //api raw데이터 | ||
| OpeningPrice json = openingPriceParser.parseGson(rawJson); //json을 list로 변환 | ||
| double unitprice = unitPricePolicy.getUnitPrice(json.getOpening_price()); | ||
| return unitprice; | ||
| } | ||
|
|
||
| public double getUnitPriceByTicker(String ticker){ | ||
| return unitPriceCache.get(ticker); | ||
| } | ||
| } | ||
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/cleanengine/coin/realitybot/config/SchedulerConfig.java
This file contains hidden or 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,40 @@ | ||
| package com.cleanengine.coin.realitybot.config; | ||
|
|
||
| import com.cleanengine.coin.realitybot.api.ApiScheduler; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.scheduling.annotation.EnableScheduling; | ||
| import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||
| import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| @Configuration | ||
| @EnableScheduling | ||
| //@RequiredArgsConstructor | ||
| public class SchedulerConfig implements SchedulingConfigurer { | ||
|
|
||
| //멀티쓰레드 환경 x | ||
| // @Autowired | ||
| // private TaskScheduler apiScheduler; | ||
| private final ApiScheduler apiScheduler; | ||
| @Value("${bot-handler.fixed-rate}") | ||
| private final Duration fixedRate; | ||
|
|
||
| protected SchedulerConfig(ApiScheduler apiScheduler, @Value("${bot-handler.fixed-rate}") Duration fixedRate) { | ||
| this.apiScheduler = apiScheduler; | ||
| this.fixedRate = fixedRate; | ||
| } | ||
|
|
||
| @Override | ||
| public void configureTasks(ScheduledTaskRegistrar registrar) { | ||
| // registrar.setScheduler(apiScheduler); //멀티 쓰레드 x | ||
| registrar.addFixedRateTask(() -> { | ||
| try { | ||
| apiScheduler.MarketAllRequest(); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }, fixedRate); | ||
| } | ||
| } |
23 changes: 11 additions & 12 deletions
23
src/main/java/com/cleanengine/coin/realitybot/controller/ApiController.java
This file contains hidden or 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,19 @@ | ||
| package com.cleanengine.coin.realitybot.controller; | ||
|
|
||
| import com.cleanengine.coin.realitybot.api.BithumbAPIClient; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api") | ||
| public class ApiController { | ||
| private final BithumbAPIClient bithumbAPIClient; | ||
| public ApiController(BithumbAPIClient bithumbAPIClient) { | ||
| this.bithumbAPIClient = bithumbAPIClient; | ||
| } | ||
| @GetMapping("/test") | ||
| public String getApiData(){ | ||
| // return bithumbAPIClient.get(ticekr); | ||
| return null; | ||
|
|
||
| }} | ||
| //초기 api 작동 확인용 -> realitybot controller로 전환 필요 | ||
| // private final BithumbAPIClient bithumbAPIClient; | ||
| // public ApiController(BithumbAPIClient bithumbAPIClient) { | ||
| // this.bithumbAPIClient = bithumbAPIClient; | ||
| // } | ||
| // @GetMapping("/test/{tickerName}") | ||
| // public String getApiData(@PathVariable String tickerName) { | ||
| // System.out.println("tickername 출력"+tickerName); | ||
| // return bithumbAPIClient.get(tickerName); | ||
| // } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.