-
Notifications
You must be signed in to change notification settings - Fork 47
/
DarvasBoxSTUDY.ts
112 lines (104 loc) · 3.02 KB
/
DarvasBoxSTUDY.ts
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
# DARVASBOX
# DREWGRIFFITH15 (C) 2014
def state = {default state_1, state_2, state_3, state_4, state_5};
def upper;
def lower;
def prevLower = CompoundValue(1, lower[1], low);
def prevUpper = CompoundValue(1, upper[1], high);
switch (state[1]) {
case state_1:
lower = low;
if (prevUpper >= high) {
upper = high[1];
state = state.state_2;
} else {
upper = high;
state = state.state_1;
}
case state_2:
if (prevUpper >= high) {
lower = low;
upper = prevUpper;
state = state.state_3;
} else {
lower = low;
upper = high;
state = state.state_1;
}
case state_3:
if (prevUpper < high) {
lower = low;
upper = high;
state = state.state_1;
} else if (prevLower > low) {
lower = low;
upper = prevUpper;
state = state.state_3;
} else {
lower = prevLower;
upper = prevUpper;
state = state.state_4;
}
case state_4:
if (prevUpper < high) {
lower = low;
upper = high;
state = state.state_1;
} else if (prevLower > low) {
lower = low;
upper = prevUpper;
state = state.state_3;
} else {
lower = prevLower;
upper = prevUpper;
state = state.state_5;
}
case state_5:
if (prevUpper < high) {
lower = low;
upper = high;
state = state.state_1;
} else if (prevLower > low) {
lower = low;
upper = high;
state = state.state_1;
} else {
lower = prevLower;
upper = prevUpper;
state = state.state_5;
}
}
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, barNumber));
def boxNum;
def boxUpperIndex;
plot "Upper Band";
plot "Lower Band";
plot "Buy Signal" = CompoundValue(1, state[1] == state.state_5 and prevUpper < close, no);
plot "Sell Signal" = CompoundValue(1, state[1] == state.state_5 and prevLower > close, no);
plot "Rating" = if CompoundValue(1, state[1] == state.state_5 and prevUpper < close, no) then 2
else if CompoundValue(1, state[1] == state.state_5 and prevLower > close, no) then 1
else 0;
if (IsNaN(close)) {
boxNum = boxNum[1] + 1;
boxUpperIndex = 0;
"Upper Band" = Double.NaN;
"Lower Band" = Double.NaN;
} else {
boxNum = TotalSum("Buy Signal" or "Sell Signal");
boxUpperIndex = fold indx = 0 to barCount - barNumber + 2 with valInd = Double.NaN
while IsNaN(valInd)
do if (GetValue(boxNum, -indx) != boxNum)
then indx
else Double.NaN;
"Upper Band" = GetValue(upper, -boxUpperIndex + 1);
"Lower Band" = GetValue(lower, -boxUpperIndex + 1);
}
"Upper Band".SetDefaultColor(Color.GREEN);
"Lower Band".SetDefaultColor(Color.RED);
"Sell Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
"Sell Signal".SetDefaultColor(Color.RED);
"Buy Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
"Buy Signal".SetDefaultColor(Color.GREEN);
"Rating".hide();
"Rating".HideBubble();