-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPATS.java
More file actions
226 lines (189 loc) · 6.75 KB
/
PATS.java
File metadata and controls
226 lines (189 loc) · 6.75 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package TraderOracle;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import java.io.*;
import java.net.*;
import com.motivewave.platform.sdk.common.*;
import com.motivewave.platform.sdk.common.desc.*;
import com.motivewave.platform.sdk.common.menu.*;
import com.motivewave.platform.sdk.study.*;
import com.motivewave.platform.sdk.draw.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@StudyHeader(
namespace="com.DickInTheSpleen",
id="PATS",
rb="TraderOracle.nls.strings", // locale specific strings are loaded from here
name="PATS Trading System",
label="PATS Trading System",
desc="PATS Trading System",
menu="TraderOracle",
overlay=true,
studyOverlay=true,
signals=true)
public class PATS extends Study
{
//region PRIVATE VARS
private static final Color RED = new Color(255, 0, 0, 255);
private static final Color GREEN = new Color(0, 255, 0, 255);
private static final Color WHITE = new Color(255, 255, 255, 255);
private static final Color YELLOW = new Color(255, 255, 0, 255);
private static final Color BLUE = new Color(120, 170, 250, 255);
enum Signals { WICK, TOUCH }
//endregion
//region ON BAR CLOSE
@Override
public void onBarClose(DataContext ctx)
{
var s = ctx.getDataSeries();
int index = s.getEndIndex();
//region CANDLE CALCULATIONS
double close = s.getClose(index);
double open = s.getOpen(index);
double pclose = s.getClose(index - 1);
double popen = s.getOpen(index - 1);
double ppclose = s.getClose(index - 2);
double ppopen = s.getOpen(index - 2);
double low = s.getLow(index);
double plow = s.getLow(index - 1);
double high = s.getHigh(index);
double phigh = s.getHigh(index - 1);
boolean c0G = close > s.getOpen(index);
boolean c1G = s.getClose(index - 1) > s.getOpen(index - 1);
boolean c2G = s.getClose(index - 2) > s.getOpen(index - 2);
boolean c0R = close < open;
boolean c1R = s.getClose(index - 1) < s.getOpen(index - 1);
boolean c2R = s.getClose(index - 2) < s.getOpen(index - 2);
double body = Math.abs(open - close);
double pbody = Math.abs(s.getOpen(index - 1) - s.getClose(index - 1));
boolean bGDoji = c1G && pbody < phigh - pclose && pbody < popen - plow;
boolean bRDoji = c1R && pbody < phigh - popen && pbody < pclose - plow;
boolean bDoji = bGDoji || bRDoji;
//endregion
}
//endregion
//region INITIALIZE
@Override
public void initialize(Defaults defaults)
{
var sd = createSD();
var tab = sd.addTab("Lines");
var grp = tab.addGroup("Alert on These");
grp.addRow(new BooleanDescriptor("TOUCH21", "Alert EMA 21 touches", true));
var grp2 = tab.addGroup("Markers");
grp2.addRow(new MarkerDescriptor("UP", "Up Marker", Enums.MarkerType.TRIANGLE, Enums.Size.SMALL, defaults.getGreen(), defaults.getLineColor(), true, true));
grp2.addRow(new MarkerDescriptor("DOWN", "Down Marker", Enums.MarkerType.TRIANGLE, Enums.Size.SMALL, defaults.getRed(), defaults.getLineColor(), true, true));
RuntimeDescriptor desc = new RuntimeDescriptor();
setRuntimeDescriptor(desc);
desc.declareSignal(Signals.WICK, "Line Wick");
desc.declareSignal(Signals.TOUCH, "Line Touch");
}
@Override
public void onSettingsUpdated(DataContext ctx)
{
}
//endregion
//region CANDLE COLORS
private int FindLastRed(DataSeries s, int index, double ema)
{
for (int i = index - 1; i > 0; i--){
Color ch = GetCandleColor(s, i);
double ema21 = s.ema(i, 21, Enums.BarInput.CLOSE);
if (ch == RED && s.getOpen(index) > s.getClose(i) && ema > ema21)
return i;
}
return 0;
}
private Color GetCandleColor(DataSeries s, int index)
{
//region CANDLE CALCULATIONS
double close = s.getClose(index);
double open = s.getOpen(index);
double pclose = s.getClose(index - 1);
double popen = s.getOpen(index - 1);
double ppclose = s.getClose(index - 2);
double ppopen = s.getOpen(index - 2);
double low = s.getLow(index);
double plow = s.getLow(index - 1);
double high = s.getHigh(index);
double phigh = s.getHigh(index - 1);
//endregion
if (high < phigh && low > plow){ // INSIDE BAR
if (close > pclose && open > popen)
return GREEN;
else if (close < pclose && open < popen)
return RED;
else
return YELLOW;
}
else if (high > phigh && low < plow) { // OUTSIDE BAR
if (close > pclose && open > popen)
return GREEN;
else if (close > phigh || open > phigh)
return GREEN;
else if (close < pclose && open < popen)
return RED;
else if (close < plow || open < plow)
return RED;
else
return BLUE;
}
else if (high > phigh){ // HIGHER
return GREEN;
}
else if (low < plow){ // LOWER
return RED;
}
return WHITE;
}
//endregion
@Override
protected void calculate(int index, DataContext ctx)
{
var s = ctx.getDataSeries();
int last = s.size() - 1;
if (s == null || index < 202 || !s.isBarComplete(index))
return;
//region CANDLE CALCULATIONS
double close = s.getClose(index);
double open = s.getOpen(index);
double pclose = s.getClose(index - 1);
double popen = s.getOpen(index - 1);
double ppclose = s.getClose(index - 2);
double ppopen = s.getOpen(index - 2);
double low = s.getLow(index);
double plow = s.getLow(index - 1);
double high = s.getHigh(index);
double phigh = s.getHigh(index - 1);
boolean c0G = close > s.getOpen(index);
boolean c1G = s.getClose(index - 1) > s.getOpen(index - 1);
boolean c2G = s.getClose(index - 2) > s.getOpen(index - 2);
boolean c0R = close < open;
boolean c1R = s.getClose(index - 1) < s.getOpen(index - 1);
boolean c2R = s.getClose(index - 2) < s.getOpen(index - 2);
double body = Math.abs(open - close);
double pbody = Math.abs(s.getOpen(index - 1) - s.getClose(index - 1));
boolean bGDoji = c1G && pbody < phigh - pclose && pbody < popen - plow;
boolean bRDoji = c1R && pbody < phigh - popen && pbody < pclose - plow;
boolean bDoji = bGDoji || bRDoji;
//endregion
Color cc = GetCandleColor(s, index);
s.setPriceBarColor(index, cc);
if (cc == RED) {
double ema21 = s.ema(index, 21, Enums.BarInput.CLOSE);
int ix = FindLastRed(s, index, ema21);
debug("ix " + ix + " index " + index);
if (ix > 0) {
var marker = getSettings().getMarker("UP");
Coordinate coords = new Coordinate(s.getStartTime(ix), (double) s.getLow(ix)-0.6);
this.addFigure(new Marker(coords, Enums.Position.BOTTOM, marker, "ENGULFING CANDLE"));
}
}
}
}