54 lines
1.8 KiB
Java
54 lines
1.8 KiB
Java
package com.goldenchart.service;
|
|
|
|
import com.goldenchart.entity.GcPaperOrder;
|
|
import com.goldenchart.repository.GcPaperOrderRepository;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
@Service
|
|
@Slf4j
|
|
public class PaperOrderMatcherService {
|
|
|
|
private final GcPaperOrderRepository orderRepo;
|
|
private final PaperTradingService paperTradingService;
|
|
|
|
public PaperOrderMatcherService(GcPaperOrderRepository orderRepo,
|
|
@Lazy PaperTradingService paperTradingService) {
|
|
this.orderRepo = orderRepo;
|
|
this.paperTradingService = paperTradingService;
|
|
}
|
|
|
|
public void onPriceTick(String symbol, double price) {
|
|
if (symbol == null || price <= 0) return;
|
|
List<GcPaperOrder> pending = orderRepo.findByStatusAndSymbol("PENDING", symbol);
|
|
for (GcPaperOrder o : pending) {
|
|
try {
|
|
paperTradingService.tryFillPendingOrder(o.getId(), price);
|
|
} catch (Exception e) {
|
|
log.debug("[PaperOrderMatcher] fill skip order={}: {}", o.getId(), e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Scheduled(fixedDelay = 3000)
|
|
public void pollPendingOrders() {
|
|
Set<String> symbols = new HashSet<>();
|
|
for (GcPaperOrder o : orderRepo.findByStatus("PENDING")) {
|
|
symbols.add(o.getSymbol());
|
|
}
|
|
for (String symbol : symbols) {
|
|
try {
|
|
double price = paperTradingService.resolveMarkPrice(symbol);
|
|
if (price > 0) onPriceTick(symbol, price);
|
|
} catch (Exception ignored) {
|
|
}
|
|
}
|
|
}
|
|
}
|