Skip to content
Snippets Groups Projects
Commit 9c4962ab authored by benjamin.anthonio's avatar benjamin.anthonio
Browse files

feat : affichage du prix interpolé

parent a5ceafaf
No related branches found
No related tags found
No related merge requests found
Showing
with 133 additions and 56 deletions
File deleted
testApi/media/image1.jpg

14.9 KiB

testApi/media/image10.png

16.5 KiB

File added
testApi/media/image3.png

28.9 KiB

testApi/media/image4.png

25.3 KiB

testApi/media/image5.png

27.3 KiB

testApi/media/image6.png

20.4 KiB

testApi/media/image7.png

33.2 KiB

testApi/media/image8.png

24.3 KiB

testApi/media/image9.png

24.4 KiB

......@@ -44,6 +44,7 @@ public class ApiController {
@PostMapping("/sendDates")
public Map<String, Object> sendDates(@RequestBody DateRange json) throws InterruptedException, com.fasterxml.jackson.core.JsonProcessingException {
System.out.println("POST /sendDates");
System.out.println("Dates received: " + json.getStartDate() + " - " + json.getEndDate());
this.dateRange = new DateRange(json.getStartDate(), json.getEndDate());
return Collections.emptyMap();
......
......@@ -7,8 +7,7 @@ public class Candle {
private final double high;
private final double low;
private final double close;
// private final long closeTime;
private final long closeTime;
// private double volume;
// private double assetVolume;
// private int nbTrades;
......@@ -22,7 +21,7 @@ public class Candle {
this.high = highPrice;
this.low = lowPrice;
this.close = closePrice;
// this.closeTime = closeTime;
this.closeTime = closeTime/1000;
}
public long getTime() {
......@@ -45,7 +44,19 @@ public class Candle {
return close;
}
// public long getCloseTime() {
// return closeTime;
// }
public long getCloseTime() {
return closeTime;
}
public double checkprice(double price) {
if (price > this.getHigh()) {
return this.getHigh();
}
else if (price < this.getLow()) {
return this.getLow();
}
else {
return price;
}
}
}
......@@ -30,6 +30,7 @@ public class DateRange {
this.endDate = endDate;
}
public long getTimestampStartDate() {
return this.dateToTimestamp(this.startDate);
}
......@@ -38,8 +39,17 @@ public class DateRange {
return this.dateToTimestamp(this.endDate);
}
public long dateToTimestamp(String date) {
public long getTimestampEndDateInSeconds() {
return this.dateToTimestampInSec(this.endDate);
}
private long dateToTimestamp(String date) {
LocalDate localDate = LocalDate.parse(date);
return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
private long dateToTimestampInSec(String date) {
return this.dateToTimestamp(date) / 1000;
}
}
......@@ -21,19 +21,9 @@ public class Interval {
}
public void nextSecond() {
this.deltaTime += 1000;
this.deltaTime += 1;
}
public double checkprice(double price) {
if (price > this.currentCandle.getHigh()) {
return this.currentCandle.getHigh();
}
else if (price < this.currentCandle.getLow()) {
return this.currentCandle.getLow();
}
else {
return price;
}
}
}
......@@ -48,7 +48,9 @@ public class ApiService {
System.out.println("La date de fin ne peut pas être avant la date de début");
return Optional.empty();
}
System.out.println("in");
try {
System.out.println("in try");
List<List<Object>> jsonCandles = null;
while (startTime < endTime)
{
......@@ -63,6 +65,7 @@ public class ApiService {
.queryParam("limit", 1000)
.toUriString();
String response = restTemplate.getForObject(url, String.class);
System.out.println("reponse binance : " + response);
ObjectMapper objectMapper = new ObjectMapper();
jsonCandles = objectMapper.readValue(response, new TypeReference<List<List<Object>>>() {});
......@@ -107,8 +110,10 @@ public class ApiService {
List<List<Object>> jsonCandles = optJsonCandles.orElse(null);
if (jsonCandles != null) {
this.simulateurService.setCandles(deserializeCandles(jsonCandles));
System.out.println("init list candles sim");
}
else {
System.out.println("null json candles");
return null;
}
}
......@@ -118,6 +123,7 @@ public class ApiService {
// }
// this.initCandles(startDate, endDate);
// this.candlesJson = objectMapper.writeValueAsString(this.simulateurService.getCandles());
System.out.println("return list");
return this.simulateurService.getCandles();
}
......
......@@ -75,6 +75,11 @@ public class SimulateurService {
value += tmp;
}
System.out.println("x : " + x);
for (int i = 0; i < x_tab.length; i++) {
System.out.print(i + " : " + x_tab[i]);
}
System.out.println();
// System.out.println("x tab : { " + x_tab[0] + ", " + x_tab[1] + ", " + x_tab[2] + ", " + x_tab[3] + "}");
System.out.println("res approx : " + value);
return new Price(x, value);
}
......@@ -86,7 +91,7 @@ public class SimulateurService {
* priceOHLC valeur de OHLC
* @param interval
*/
public void taskEachSec(Interval interval) {
public void taskEachSec(Interval interval, long[] timestamps, double[] priceOHLC) {
// long x = interval.getCurrentCandle().getTime() + this.interval.getDeltaTime();
// long[] x_tab = {};
// float[] y = {};
......@@ -95,15 +100,25 @@ public class SimulateurService {
// System.out.println("delta time : " + x);
Candle currentCandle = interval.getCurrentCandle();
long currentTimestamp = currentCandle.getTime() + interval.getDeltaTime();
long[] timestamps = generateRandomTimstamps(4, currentCandle.getTime(), currentCandle.getTime() + 15 * 60 * 1000 - 1);
double[] priceOHLC = randomPositionsOHLC(currentCandle);
this.prices.add(this.approximatePrice(timestamps, priceOHLC, currentTimestamp));
Price tmpPrice = this.approximatePrice(timestamps, priceOHLC, currentTimestamp);
this.prices.add(new Price(tmpPrice.getTime(), interval.getCurrentCandle().checkprice(tmpPrice.getValue())));
interval.nextSecond();
System.out.println("delta time : " + currentTimestamp);
// System.out.println("delta time : " + currentTimestamp);
}
public double[] randomPositionsOHLC(Candle currentCandle) {
double[] priceOHLC = new double[4];
double[] priceOHLC;
if (currentCandle.getLow() == currentCandle.getOpen() && currentCandle.getHigh() == currentCandle.getClose() || currentCandle.getHigh() == currentCandle.getOpen() && currentCandle.getLow() == currentCandle.getClose()) {
priceOHLC = new double[] {currentCandle.getOpen(), currentCandle.getClose()};
}
else if (currentCandle.getLow() == currentCandle.getOpen() || currentCandle.getLow() == currentCandle.getClose()) {
priceOHLC = new double[] {currentCandle.getOpen(), currentCandle.getHigh(), currentCandle.getClose()};
}
else if (currentCandle.getHigh() == currentCandle.getOpen() || currentCandle.getHigh() == currentCandle.getClose()) {
priceOHLC = new double[] {currentCandle.getOpen(), currentCandle.getLow(), currentCandle.getClose()};
}
else {
priceOHLC = new double[4];
priceOHLC[0] = currentCandle.getOpen();
if (this.rnd.nextBoolean()) {
priceOHLC[1] = currentCandle.getHigh();
......@@ -114,13 +129,35 @@ public class SimulateurService {
priceOHLC[2] = currentCandle.getHigh();
}
priceOHLC[3] = currentCandle.getClose();
}
return priceOHLC;
}
public long[] generateRandomTimstamps(int nbTimestampToGenerate, long startTimestamp, long endTimestamp) {
long[] timestamps = new long[nbTimestampToGenerate];
for (int i = 0; i < nbTimestampToGenerate; i++) {
timestamps[i] = generateRandomTimstamp(startTimestamp, endTimestamp);
public long[] generateRandomTimstamps(long startTimestamp, long endTimestamp, Candle currentCandle) {
int nbTimestamps;
if (currentCandle.getLow() == currentCandle.getOpen() && currentCandle.getHigh() == currentCandle.getClose() || currentCandle.getHigh() == currentCandle.getOpen() && currentCandle.getLow() == currentCandle.getClose()) {
nbTimestamps = 2;
}
else if (currentCandle.getLow() == currentCandle.getOpen() || currentCandle.getHigh() == currentCandle.getOpen() || currentCandle.getHigh() == currentCandle.getClose() || currentCandle.getLow() == currentCandle.getClose()) {
nbTimestamps = 3;
}
else {
nbTimestamps = 4;
}
long rndTimestamp1 = generateRandomTimstamp(startTimestamp, endTimestamp-2);
long rndTimestamp2 = generateRandomTimstamp(rndTimestamp1, endTimestamp-1);
long[] timestamps;
if (nbTimestamps == 2) {
timestamps = new long[]{startTimestamp, endTimestamp};
}
else if (nbTimestamps == 3) {
timestamps = new long[]{startTimestamp, rndTimestamp1, endTimestamp};
}
else if (rndTimestamp2 == endTimestamp) {
timestamps = new long[]{startTimestamp, rndTimestamp1, endTimestamp};
}
else {
timestamps = new long[]{startTimestamp, rndTimestamp1, rndTimestamp2, endTimestamp};
}
return timestamps;
}
......@@ -131,19 +168,30 @@ public class SimulateurService {
public List<Price> getPricesForPeriod(DateRange period) {
final int nbTimestamp15Min = 15 * 60;
long endTime = period.getTimestampEndDate();
long endTime = period.getTimestampEndDateInSeconds();
int indexCurrentCandle = 0;
Candle currentCandle = candles.get(indexCurrentCandle);
Interval interval = new Interval(currentCandle);
long[] timestamps = generateRandomTimstamps(currentCandle.getTime(), currentCandle.getCloseTime(), currentCandle);
double[] priceOHLC = randomPositionsOHLC(currentCandle);
while (interval.getCurrentCandle().getTime() < endTime) {
// Boucle qui simule 15 min
for (int i = 0; i < nbTimestamp15Min; i++) {
taskEachSec(interval);
taskEachSec(interval, timestamps, priceOHLC);
}
indexCurrentCandle++;
// System.out.println("indexCurrentCandle : " + indexCurrentCandle);
// System.out.println("endTime : " + endTime);
// System.out.println("current time : " + interval.getCurrentCandle().getTime());
// System.out.println("len candles : "+ candles.size());
// System.out.println();
currentCandle = candles.get(indexCurrentCandle);
interval = new Interval(currentCandle);
timestamps = generateRandomTimstamps(currentCandle.getTime(), currentCandle.getCloseTime(), currentCandle);
priceOHLC = randomPositionsOHLC(currentCandle);
}
// System.out.println("len prices : " + prices.size());
// System.out.println("return prices");
return this.prices;
}
}
......@@ -4,3 +4,4 @@ spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSou
simulateur.ApproximationPeriod=1
server.tomcat.max-http-form-post-size=10000000
\ No newline at end of file
......@@ -27,11 +27,14 @@ export function callApi(route, method = "GET", data = null)
// });
console.log("ok");
// console.log(data);
displayChart(data);
break;
case "/prices":
console.log("ok prices");
console.log(data);
console.log("data affichées");
displayPriceChart(data);
break;
......
......@@ -7,7 +7,7 @@ import { callApi } from './api.js';
window.onload = async function() {
await callApi("/btcusdt");
// await callApi("/prices");
await callApi("/prices");
};
export function displayChart(data) {
......@@ -23,30 +23,37 @@ export function displayChart(data) {
});
console.log(data);
candlestickSeries.setData(data);
chart.timeScale().applyOptions({
timeVisible: true,
secondsVisible: true,
fixLeftEdge: true,
fixRightEdge: true,
});
chart.timeScale().fitContent();
}
export function displayPriceChart() {
export function displayPriceChart(data) {
const chartOptions = { layout: { textColor: 'black', background: { type: 'solid', color: 'white' } } };
console.log(data);
// console.log(data);
const chart = createChart(document.getElementById('container'), chartOptions);
const areaSeries = chart.addSeries(AreaSeries, {
upColor: '#26a69a', downColor: '#ef5350', borderVisible: false,
wickUpColor: '#26a69a', wickDownColor: '#ef5350',
});
areaSeries.setData([
{ time: '2024-12-22', value: 94000 },
{ time: '2024-12-23', value: 95000 },
{ time: '2024-12-24', value: 96000 },
{ time: '2024-12-25', value: 97000 },
{ time: '2024-12-26', value: 98000 },
{ time: '2024-12-27', value: 99000 },
{ time: '2024-12-28', value: 100000 },
{ time: '2024-12-29', value: 102000 },
{ time: '2024-12-30', value: 101000 },
{ time: '2024-12-31', value: 110000 },
]);
areaSeries.setData(data);
chart.timeScale().applyOptions({
timeVisible: true,
secondsVisible: true,
fixLeftEdge: true,
fixRightEdge: true,
});
chart.timeScale().fitContent();
}
function initChart() {
const chartOptions = {
// width: 800,
// height: 400,
layout: { textColor: 'black', background: { type: 'solid', color: 'white' } } };
chart = createChart(document.getElementById('container'), chartOptions);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment