package br.com.sl.core;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import br.com.kronus.core.Timeframe;
import br.com.sl.domain.dto.robo.ProfitTick;
import br.com.sl.domain.model.Candle;
import br.com.sl.domain.model.tipos.TipoPeriodo;
import br.com.sl.domain.model.tipos.TipoPeriodoCandle;
import br.com.sl.shared.ExcelDataUtils;
public class ExcelProfitHistoricoDataProvider
implements ProfitDataProvider
{
private final Path excelFile
;
private final String sheetName
;
private static final DateTimeFormatter STRING_DATA_FORMATTER = DateTimeFormatter.
ofPattern("dd/MM/yyyy HH:mm:ss");
private static final DateTimeFormatter STRING_DATE_TIME_FORMATTER = DateTimeFormatter.
ofPattern("dd-MM-yyyy HH:mm:ss");
public ExcelProfitHistoricoDataProvider
(Path excelFile,
String sheetName
) {
this.
excelFile = excelFile
;
this.
sheetName = sheetName
;
}
@
Override
public Map<String, ProfitTick
> readCurrentTicks
() {
// TODO Auto-generated method stub
return null;
}
/**
* Lê o arquivo Excel dentro do resources.
* Exemplo de uso:
* lerCandles("/dados/Dados Trade 20251117.xlsx");
* @throws IOException
*/
public List<Candle
> lerCandles
() throws IOException {
List<Candle
> candles =
new ArrayList<>();
try (FileInputStream fis =
new FileInputStream(excelFile.
toFile()); Workbook workbook =
new XSSFWorkbook
(fis
)) {
int numberOfSheets = workbook.
getNumberOfSheets();
for (int i =
0; i
< numberOfSheets
; i++
) {
Sheet sheet = workbook.
getSheetAt(i
);
if (sheet.
getSheetName().
equals(sheetName
)) {
for (Row row : sheet
) {
// 0 = Ativo
// 1 = Dia
// 2 = Hora
// 3 = Abertura
// 4 = Máxima
// 5 = Mínima
// 6 = Fechamento
// 7 = Volume
// 8 = ...
Cell ativoCell = row.
getCell(0);
Cell dataCell = row.
getCell(1);
Cell horaCell = row.
getCell(2);
Cell aberturaCell = row.
getCell(3);
Cell maximaCell = row.
getCell(4);
Cell minimaCell = row.
getCell(5);
Cell fechamentoCell = row.
getCell(6);
if (!ExcelDataUtils.
isNumeric(aberturaCell
) ||
!ExcelDataUtils.
isNumeric(maximaCell
)
||
!ExcelDataUtils.
isNumeric(minimaCell
) ||
!ExcelDataUtils.
isNumeric(fechamentoCell
)) {
continue;
}
LocalDate data = ExcelDataUtils.
lerData(dataCell
);
LocalTime hora = ExcelDataUtils.
lerHora(horaCell
);
hora = hora.
plusMinutes(6).
plusSeconds(28);
LocalDateTime dataHora = LocalDateTime.
of(data, hora
);
String ativoDescricao = ativoCell.
getStringCellValue();
BigDecimal abertura =
BigDecimal.
valueOf(aberturaCell.
getNumericCellValue());
BigDecimal topo =
BigDecimal.
valueOf(maximaCell.
getNumericCellValue());
BigDecimal fundo =
BigDecimal.
valueOf(minimaCell.
getNumericCellValue());
BigDecimal fechamento =
BigDecimal.
valueOf(fechamentoCell.
getNumericCellValue());
Candle candle =
new Candle
(null, ativoDescricao, dataHora, abertura, topo, fundo, fechamento, TipoPeriodoCandle.
M1.
getValor());
candles.
add(candle
);
}
}
}
} catch (EncryptedDocumentException e
) {
// TODO Auto-generated catch block
e.
printStackTrace();
}
return adicionarContadores
(inverterLista
(candles
));
}
public static List<Candle
> inverterLista
(List<Candle
> candles
) {
List<Candle
> invertida =
new ArrayList<>(candles
);
Collections.
reverse(invertida
);
return invertida
;
}
public static List<Candle
> adicionarContadores
(List<Candle
> candles
) {
Integer contador =
1;
List<Candle
> comContadores =
new ArrayList<>();
for (Candle candle : candles
) {
candle.
setContadorCandle(contador
);
comContadores.
add(candle
);
contador++
;
}
return comContadores
;
}
/**
* 1 minuto = 1, 5 minutos = 2, 15 minutos = 3, 1 dia = 4
*/
private Timeframe resolveTipoTemporizador
(String sheetName
) {
if (sheetName ==
null) return null;
String name = sheetName.
toUpperCase(Locale.
ROOT);
if (name.
startsWith("1 MIN")) return Timeframe.
M1;
if (name.
startsWith("5 MIN")) return Timeframe.
M5;
if (name.
startsWith("15 MIN")) return Timeframe.
M15;
if (name.
startsWith("1 DIA")) return Timeframe.
D1;
return null;
}
}