-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathExampleStrategyMonitor.java
64 lines (56 loc) · 1.66 KB
/
ExampleStrategyMonitor.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
package com.univocity.trader.examples;
import com.univocity.trader.account.*;
import com.univocity.trader.indicators.*;
import com.univocity.trader.indicators.base.*;
import com.univocity.trader.strategy.*;
import java.util.*;
public class ExampleStrategyMonitor extends StrategyMonitor {
private final Set<Indicator> indicators = new HashSet<>();
private final InstantaneousTrendline trend;
private boolean waitForUptrend = false;
private boolean waitForDowntrend = false;
public ExampleStrategyMonitor() {
indicators.add(trend = new InstantaneousTrendline(TimeInterval.minutes(25)));
}
@Override
public boolean discardBuy(Strategy strategy) {
if (trend.getSignal(trader.latestCandle()) == Signal.BUY) {
waitForUptrend = false;
}
return waitForUptrend;
}
@Override
public boolean discardShortSell(Strategy strategy) {
if (trend.getSignal(trader.latestCandle()) == Signal.SELL) {
waitForDowntrend = false;
}
return waitForDowntrend;
}
@Override
protected Set<Indicator> getAllIndicators() {
return indicators;
}
@Override
public String handleStop(Trade trade) {
final double currentReturns = trade.priceChangePct();
final double bestReturns = trade.maxChange();
if(trade.isLong()) {
if ((currentReturns - bestReturns) < -2.0) {
if (currentReturns < 0.0) {
waitForUptrend = true;
return "stop loss on long position";
}
return "exit long with some profit";
}
} else if(trade.isShort()){
if ((currentReturns - bestReturns) < -2.0) {
if (currentReturns < 0.0) {
waitForDowntrend = true;
return "stop loss on short position";
}
return "exit short with some profit";
}
}
return null;
}
}