-
Notifications
You must be signed in to change notification settings - Fork 16
/
Predictor.java
executable file
·65 lines (50 loc) · 1.72 KB
/
Predictor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// camel-k: language=java
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.BindToRegistry;
import org.apache.camel.PropertyInject;
import org.apache.camel.builder.RouteBuilder;
public class Predictor extends RouteBuilder {
@Override
public void configure() throws Exception {
from("knative:event/market.btc.usdt")
.unmarshal().json()
.transform().simple("${body[price]}")
.log("Latest value for BTC/USDT is: ${body}")
.to("seda:evaluate?waitForTaskToComplete=Never")
.setBody().constant("");
from("seda:evaluate")
.bean("algorithm")
.choice()
.when(body().isNotNull())
.log("Predicted action: ${body}")
.to("direct:publish");
from("direct:publish")
.marshal().json()
.removeHeaders("*")
.setHeader("CE-Type", constant("predictor.{{predictor.name}}"))
.to("knative:event");
}
@BindToRegistry("algorithm")
public static class SimpleAlgorithm {
@PropertyInject(value="algorithm.sensitivity", defaultValue = "0.0001")
private double sensitivity;
private Double previous;
public Map<String, Object> predict(double value) {
Double reference = previous;
this.previous = value;
if (reference != null && value < reference * (1 - sensitivity)) {
Map<String, Object> res = new HashMap<>();
res.put("value", value);
res.put("operation", "buy");
return res;
} else if (reference != null && value > reference * (1 + sensitivity)) {
Map<String, Object> res = new HashMap<>();
res.put("value", value);
res.put("operation", "sell");
return res;
}
return null;
}
}
}