전략 시간봉 오류 수정

This commit is contained in:
Macbook
2026-05-27 15:49:56 +09:00
parent 63693d47c0
commit e86142dad5
5 changed files with 139 additions and 11 deletions
@@ -71,11 +71,21 @@ public class StrategyConditionTimeframeService {
private void collectFromNode(JsonNode node, Set<String> out) {
if (node == null || node.isNull()) return;
if (collectTimeframesFromNode(node, out)) return;
// TIMEFRAME 래핑 없는 레거시 트리 — 기본 1m
out.add("1m");
}
/**
* 트리에서 TIMEFRAME 노드를 수집. 발견 시 true.
*/
private boolean collectTimeframesFromNode(JsonNode node, Set<String> out) {
if (node == null || node.isNull()) return false;
String type = node.path("type").asText("");
if ("TIMEFRAME".equals(type)) {
out.add(LiveStrategyTimeframeService.normalize(node.path("candleType").asText("1m")));
return;
return true;
}
if ("AND".equals(type) || "OR".equals(type)) {
@@ -85,23 +95,22 @@ public class StrategyConditionTimeframeService {
for (JsonNode tf : children) {
out.add(LiveStrategyTimeframeService.normalize(tf.path("candleType").asText("1m")));
}
return;
return true;
}
boolean found = false;
for (JsonNode child : children) {
collectFromNode(child, out);
found |= collectTimeframesFromNode(child, out);
}
return;
return found;
}
}
JsonNode child = node.path("child");
if (!child.isMissingNode() && !child.isNull()) {
collectFromNode(child, out);
return;
return collectTimeframesFromNode(child, out);
}
// 단일 START(기본 1m) 또는 TIMEFRAME 래핑 없는 트리
out.add("1m");
return false;
}
private static boolean allTimeframeChildren(JsonNode children) {
@@ -100,9 +100,35 @@ public class StrategyTriggerBranchEvaluator {
}
}
JsonNode wrapped = findTimeframeWrapper(node);
if (wrapped != null) {
String ct = LiveStrategyTimeframeService.normalize(wrapped.path("candleType").asText("1m"));
JsonNode sub = firstChild(wrapped);
return new BranchScope("SINGLE", List.of(new BranchDef(ct, sub != null && !sub.isNull() ? sub : node)));
}
return new BranchScope("SINGLE", List.of(new BranchDef("1m", node)));
}
/** 레거시 DSL — 루트가 TIMEFRAME이 아닐 때 하위 TIMEFRAME 래퍼 탐색 */
private static JsonNode findTimeframeWrapper(JsonNode node) {
if (node == null || node.isNull()) return null;
if ("TIMEFRAME".equals(node.path("type").asText(""))) return node;
JsonNode children = node.path("children");
if (children.isArray()) {
for (JsonNode child : children) {
JsonNode found = findTimeframeWrapper(child);
if (found != null) return found;
}
}
JsonNode child = node.path("child");
if (!child.isMissingNode() && !child.isNull()) {
return findTimeframeWrapper(child);
}
return null;
}
private static JsonNode firstChild(JsonNode timeframeNode) {
JsonNode children = timeframeNode.path("children");
if (children.isArray() && !children.isEmpty()) return children.get(0);
@@ -0,0 +1,75 @@
package com.goldenchart.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.goldenchart.entity.GcStrategy;
import com.goldenchart.repository.GcStrategyRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class StrategyConditionTimeframeServiceTest {
@Mock
private GcStrategyRepository strategyRepo;
@Spy
private ObjectMapper objectMapper = new ObjectMapper();
@InjectMocks
private StrategyConditionTimeframeService service;
@BeforeEach
void stubStrategy() {
GcStrategy strategy = new GcStrategy();
strategy.setId(1L);
strategy.setBuyConditionJson("""
{
"type": "TIMEFRAME",
"candleType": "5m",
"children": [{
"type": "CONDITION",
"condition": { "indicatorType": "RSI", "period": 14 }
}]
}
""");
strategy.setSellConditionJson(null);
when(strategyRepo.findById(1L)).thenReturn(Optional.of(strategy));
}
@Test
void collectForStrategy_readsTimeframeFromDsl() {
Set<String> tfs = service.collectForStrategy(1L);
assertEquals(Set.of("5m"), tfs);
assertTrue(service.usesTimeframe(1L, "5m"));
assertFalse(service.usesTimeframe(1L, "1m"));
}
@Test
void collectForStrategy_legacyTreeDefaultsTo1m() {
GcStrategy legacy = new GcStrategy();
legacy.setId(2L);
legacy.setBuyConditionJson("""
{
"type": "AND",
"children": [{
"type": "CONDITION",
"condition": { "indicatorType": "RSI", "period": 14 }
}]
}
""");
when(strategyRepo.findById(2L)).thenReturn(Optional.of(legacy));
assertEquals(Set.of("1m"), service.collectForStrategy(2L));
}
}