forked from tconsky/ChanlunX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duan.cpp
237 lines (232 loc) · 7.33 KB
/
Duan.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <vector>
#include "Duan.h"
void Duan0(int nCount, float *pOut, float *pIn, float *pHigh, float *pLow)
{
int nState = 0;
int nLastD = 0; // 前一个向下线段的底
int nLastG = 0; // 前一个向上线段的顶
float fTop0 = 0;
float fTop1 = 0;
float fTop2 = 0;
float fBot0 = 0;
float fBot1 = 0;
float fBot2 = 0;
for (int i = 0; i < nCount; i++)
{
if (pIn[i] == 1)
{
fTop1 = fTop2;
fTop2 = pHigh[i];
}
else if (pIn[i] == -1) {
fBot1 = fBot2;
fBot2 = pLow[i];
}
if (nState == 0)
{
if (pIn[i] == 1)
{
nState = 1;
nLastG = i;
pOut[nLastG] = 1;
fTop0 = 0;
fBot0 = 0;
}
else if (pIn[i] == -1)
{
nState = -1;
nLastD = i;
pOut[nLastD] = -1;
fTop0 = 0;
fBot0 = 0;
}
}
else if (nState == 1) // 向上线段运行中
{
if (pIn[i] == 1) // 遇到高点
{
if (pHigh[i] > pHigh[nLastG]) // 比线段最高点还高,高点后移
{
pOut[nLastG] = 0;
nLastG = i;
pOut[nLastG] = 1;
fTop0 = 0;
fBot0 = 0;
}
}
else if (pIn[i] == -1) // 遇到低点
{
if (pLow[i] < pLow[nLastD]) // 低点比向上线段最低点还低了,当一段处理,也是终结。
{
nState = -1;
nLastD = i;
pOut[nLastD] = -1;
fTop0 = 0;
fBot0 = 0;
}
else if (fTop1 > 0 && fTop2 > 0 && fBot1 > 0 && fBot2 > 0 && fTop2 < fTop1 && fBot2 < fBot1) // 向上线段终结
{
nState = -1;
nLastD = i;
pOut[nLastD] = -1;
fTop0 = 0;
fBot0 = 0;
}
else
{
if (fBot0 == 0)
{
fBot0 = pLow[i];
}
else if (pLow[i] < fBot0) // 向上线段终结
{
nState = -1;
nLastD = i;
pOut[nLastD] = -1;
fTop0 = 0;
fBot0 = 0;
}
}
}
}
else if (nState == -1) // 向下线段运行中
{
if (pIn[i] == -1) // 遇到低点
{
if (pLow[i] < pLow[nLastD]) // 比线段最低点还低,低点后移
{
pOut[nLastD] = 0;
nLastD = i;
pOut[nLastD] = -1;
fTop0 = 0;
fBot0 = 0;
}
}
else if (pIn[i] == 1) // 遇到高点
{
if (pHigh[i] > pHigh[nLastG]) // 高点比向下线段最高点还高了,当一段处理,也是终结。
{
nState = 1;
nLastG = i;
pOut[nLastG] = 1;
fTop0 = 0;
fBot0 = 0;
}
else if (fTop1 > 0 && fTop2 > 0 && fBot1 > 0 && fBot2 > 0 && fTop2 > fTop1 && fBot2 > fBot1) // 向下线段终结
{
nState = 1;
nLastG = i;
pOut[nLastG] = 1;
fTop0 = 0;
fBot0 = 0;
}
else
{
if (fTop0 == 0)
{
fTop0 = pHigh[i];
}
else if (pHigh[i] > fTop0) // 向下线段终结
{
nState = 1;
nLastG = i;
pOut[nLastG] = 1;
fTop0 = 0;
fBot0 = 0;
}
}
}
}
}
}
void Duan1(int nCount, float *pOut, float *pIn, float *pHigh, float *pLow)
{
int nState = 0;
int nLastD = 0; // 前一个向下线段的底
int nLastG = 0; // 前一个向上线段的顶
float fTop1 = 0;
float fTop2 = 0;
float fBot1 = 0;
float fBot2 = 0;
for (int i = 0; i < nCount; i++)
{
if (pIn[i] == 1)
{
fTop1 = fTop2;
fTop2 = pHigh[i];
}
else if (pIn[i] == -1) {
fBot1 = fBot2;
fBot2 = pLow[i];
}
if (nState == 0)
{
if (pIn[i] == 1)
{
nState = 1;
nLastG = i;
pOut[nLastG] = 1;
}
else if (pIn[i] == -1)
{
nState = -1;
nLastD = i;
pOut[nLastD] = -1;
}
}
else if (nState == 1) // 向上线段运行中
{
if (pIn[i] == 1) // 遇到高点
{
if (pHigh[i] > pHigh[nLastG]) // 比线段最高点还高,高点后移
{
pOut[nLastG] = 0;
nLastG = i;
pOut[nLastG] = 1;
}
}
else if (pIn[i] == -1) // 遇到低点
{
if (pLow[i] < pLow[nLastD]) // 低点比向上线段最低点还低了,当一段处理,也是终结。
{
nState = -1;
nLastD = i;
pOut[nLastD] = -1;
}
else if (fTop1 > 0 && fTop2 > 0 && fBot1 > 0 && fBot2 > 0 && fTop2 < fTop1 && fBot2 < fBot1) // 向上线段终结
{
nState = -1;
nLastD = i;
pOut[nLastD] = -1;
}
}
}
else if (nState == -1) // 向下线段运行中
{
if (pIn[i] == -1) // 遇到低点
{
if (pLow[i] < pLow[nLastD]) // 比线段最低点还低,低点后移
{
pOut[nLastD] = 0;
nLastD = i;
pOut[nLastD] = -1;
}
}
else if (pIn[i] == 1) // 遇到高点
{
if (pHigh[i] > pHigh[nLastG]) // 高点比向下线段最高点还高了,当一段处理,也是终结。
{
nState = 1;
nLastG = i;
pOut[nLastG] = 1;
}
else if (fTop1 > 0 && fTop2 > 0 && fBot1 > 0 && fBot2 > 0 && fTop2 > fTop1 && fBot2 > fBot1) // 向下线段终结
{
nState = 1;
nLastG = i;
pOut[nLastG] = 1;
}
}
}
}
}