/
IchimokuKinkoHyo.cs
493 lines (454 loc) · 23.8 KB
/
IchimokuKinkoHyo.cs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//==============================================================
// Forex Strategy Builder
// Copyright © Miroslav Popov. All rights reserved.
//==============================================================
// THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
//==============================================================
using System;
using System.Drawing;
using ForexStrategyBuilder.Infrastructure.Entities;
using ForexStrategyBuilder.Infrastructure.Enums;
using ForexStrategyBuilder.Infrastructure.Interfaces;
namespace ForexStrategyBuilder.Indicators.Store
{
public class IchimokuKinkoHyo : Indicator
{
public IchimokuKinkoHyo()
{
IndicatorName = "Ichimoku Kinko Hyo";
PossibleSlots = SlotTypes.Open | SlotTypes.OpenFilter | SlotTypes.Close;
SeparatedChart = false;
IndicatorAuthor = "Miroslav Popov";
IndicatorVersion = "2.0";
IndicatorDescription = "Bundled in FSB distribution.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
// ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
if (SlotType == SlotTypes.Open)
IndParam.ListParam[0].ItemList = new[]
{
"Enter the market at Tenkan Sen",
"Enter the market at Kijun Sen"
};
else if (SlotType == SlotTypes.OpenFilter)
IndParam.ListParam[0].ItemList = new[]
{
"Tenkan Sen rises",
"Kijun Sen rises",
"Tenkan Sen is higher than Kijun Sen",
"Tenkan Sen crosses Kijun Sen upward",
"The bar opens above Tenkan Sen",
"The bar opens above Kijun Sen",
"Chikou Span is above closing price",
"The position opens above Kumo",
"The position opens inside or above Kumo",
"Tenkan Sen is above Kumo",
"Tenkan Sen is inside or above Kumo",
"Kijun Sen is above Kumo",
"Kijun Sen is inside or above Kumo",
"Senkou Span A is higher than Senkou Span B",
"Senkou Span A crosses Senkou Span B upward"
};
else if (SlotType == SlotTypes.Close)
IndParam.ListParam[0].ItemList = new[]
{
"Exit the market at Tenkan Sen",
"Exit the market at Kijun Sen"
};
else
IndParam.ListParam[0].ItemList = new[]
{
"Not Defined"
};
IndParam.ListParam[0].Index = 0;
IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index];
IndParam.ListParam[0].Enabled = true;
IndParam.ListParam[0].ToolTip = "Application of Ichimoku Kinko Hyo.";
// NumericUpDown parameters
IndParam.NumParam[0].Caption = "Tenkan";
IndParam.NumParam[0].Value = 9;
IndParam.NumParam[0].Min = 6;
IndParam.NumParam[0].Max = 12;
IndParam.NumParam[0].Point = 0;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "Tenkan Sen period.";
IndParam.NumParam[2].Caption = "Kijun";
IndParam.NumParam[2].Value = 26;
IndParam.NumParam[2].Min = 18;
IndParam.NumParam[2].Max = 34;
IndParam.NumParam[2].Point = 0;
IndParam.NumParam[2].Enabled = true;
IndParam.NumParam[2].ToolTip = "Kijun Sen period, Chikou Span period and Senkou Span shift.";
IndParam.NumParam[4].Caption = "Senkou Span B";
IndParam.NumParam[4].Value = 52;
IndParam.NumParam[4].Min = 36;
IndParam.NumParam[4].Max = 84;
IndParam.NumParam[4].Point = 0;
IndParam.NumParam[4].Enabled = true;
IndParam.NumParam[4].ToolTip = "Senkou Span B period.";
// CheckBox parameters
IndParam.CheckParam[0].Caption = "Use previous bar value";
IndParam.CheckParam[0].Enabled = true;
IndParam.CheckParam[0].ToolTip = "Use indicator value from previous bar.";
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
// Reading the parameters
var tenkan = (int)IndParam.NumParam[0].Value;
var kijun = (int)IndParam.NumParam[2].Value;
var senkou = (int)IndParam.NumParam[4].Value;
int previous = IndParam.CheckParam[0].Checked ? 1 : 0;
int firstBar = Math.Max(Math.Max(tenkan, kijun), senkou) + previous + 2;
double sigma = Sigma();
var tenkanSen = new double[Bars];
for (int bar = tenkan - 1; bar < Bars; bar++)
{
double highestHigh = double.MinValue;
double lowestLow = double.MaxValue;
for (int i = 0; i < tenkan; i++)
{
if (High[bar - i] > highestHigh)
highestHigh = High[bar - i];
if (Low[bar - i] < lowestLow)
lowestLow = Low[bar - i];
}
tenkanSen[bar] = (highestHigh + lowestLow) / 2;
}
var kijunSen = new double[Bars];
for (int bar = kijun - 1; bar < Bars; bar++)
{
double highestHigh = double.MinValue;
double lowestLow = double.MaxValue;
for (int i = 0; i < kijun; i++)
{
if (High[bar - i] > highestHigh)
highestHigh = High[bar - i];
if (Low[bar - i] < lowestLow)
lowestLow = Low[bar - i];
}
kijunSen[bar] = (highestHigh + lowestLow) / 2;
}
var chikouSpan = new double[Bars];
for (int bar = 0; bar < Bars - kijun; bar++)
{
chikouSpan[bar] = Close[bar + kijun];
}
var senkouSpanA = new double[Bars];
for (int bar = firstBar; bar < Bars - kijun; bar++)
{
senkouSpanA[bar + kijun] = (tenkanSen[bar] + kijunSen[bar]) / 2;
}
var senkouSpanB = new double[Bars];
for (int bar = senkou - 1; bar < Bars - kijun; bar++)
{
double highestHigh = double.MinValue;
double lowestLow = double.MaxValue;
for (int i = 0; i < senkou; i++)
{
if (High[bar - i] > highestHigh)
highestHigh = High[bar - i];
if (Low[bar - i] < lowestLow)
lowestLow = Low[bar - i];
}
senkouSpanB[bar + kijun] = (highestHigh + lowestLow) / 2;
}
// Saving components
Component = SlotType == SlotTypes.OpenFilter ? new IndicatorComp[7] : new IndicatorComp[6];
Component[0] = new IndicatorComp
{
CompName = "Tenkan Sen",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.Red,
FirstBar = firstBar,
Value = tenkanSen
};
Component[1] = new IndicatorComp
{
CompName = "Kijun Sen",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.Blue,
FirstBar = firstBar,
Value = kijunSen
};
Component[2] = new IndicatorComp
{
CompName = "Chikou Span",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
ChartColor = Color.Green,
FirstBar = firstBar,
Value = chikouSpan
};
Component[3] = new IndicatorComp
{
CompName = "Senkou Span A",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.CloudUp,
ChartColor = Color.SandyBrown,
FirstBar = firstBar,
Value = senkouSpanA
};
Component[4] = new IndicatorComp
{
CompName = "Senkou Span B",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.CloudDown,
ChartColor = Color.Thistle,
FirstBar = firstBar,
Value = senkouSpanB
};
Component[5] = new IndicatorComp
{
FirstBar = firstBar,
Value = new double[Bars],
DataType = IndComponentType.Other
};
if (SlotType == SlotTypes.OpenFilter)
{
Component[5].CompName = "Is long entry allowed";
Component[5].DataType = IndComponentType.AllowOpenLong;
Component[6] = new IndicatorComp
{
FirstBar = firstBar,
Value = new double[Bars],
CompName = "Is short entry allowed",
DataType = IndComponentType.AllowOpenShort
};
}
switch (IndParam.ListParam[0].Text)
{
case "Enter the market at Tenkan Sen":
Component[5].CompName = "Tenkan Sen entry price";
Component[5].DataType = IndComponentType.OpenPrice;
Component[5].ChartType = IndChartType.NoChart;
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = tenkanSen[bar - previous];
}
break;
case "Enter the market at Kijun Sen":
Component[5].CompName = "Kijun Sen entry price";
Component[5].DataType = IndComponentType.OpenPrice;
Component[5].ChartType = IndChartType.NoChart;
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = kijunSen[bar - previous];
}
break;
case "Exit the market at Tenkan Sen":
Component[5].CompName = "Tenkan Sen exit price";
Component[5].DataType = IndComponentType.ClosePrice;
Component[5].ChartType = IndChartType.NoChart;
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = tenkanSen[bar - previous];
}
break;
case "Exit the market at Kijun Sen":
Component[5].CompName = "Kijun Sen exit price";
Component[5].DataType = IndComponentType.ClosePrice;
Component[5].ChartType = IndChartType.NoChart;
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = kijunSen[bar - previous];
}
break;
case "Tenkan Sen rises":
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = tenkanSen[bar - previous] > tenkanSen[bar - previous - 1] + sigma ? 1 : 0;
Component[6].Value[bar] = tenkanSen[bar - previous] < tenkanSen[bar - previous - 1] - sigma ? 1 : 0;
}
break;
case "Kijun Sen rises":
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = kijunSen[bar - previous] > kijunSen[bar - previous - 1] + sigma ? 1 : 0;
Component[6].Value[bar] = kijunSen[bar - previous] < kijunSen[bar - previous - 1] - sigma ? 1 : 0;
}
break;
case "Tenkan Sen is higher than Kijun Sen":
IndicatorIsHigherThanAnotherIndicatorLogic(firstBar, previous, tenkanSen, kijunSen, ref Component[5], ref Component[6]);
break;
case "Tenkan Sen crosses Kijun Sen upward":
IndicatorCrossesAnotherIndicatorUpwardLogic(firstBar, previous, tenkanSen, kijunSen, ref Component[5], ref Component[6]);
break;
case "The bar opens above Tenkan Sen":
BarOpensAboveIndicatorLogic(firstBar, previous, tenkanSen, ref Component[5], ref Component[6]);
break;
case "The bar opens above Kijun Sen":
BarOpensAboveIndicatorLogic(firstBar, previous, kijunSen, ref Component[5], ref Component[6]);
break;
case "Chikou Span is above closing price":
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = chikouSpan[bar - kijun - previous] > Close[bar - kijun - previous] + sigma ? 1 : 0;
Component[6].Value[bar] = chikouSpan[bar - kijun - previous] < Close[bar - kijun - previous] - sigma ? 1 : 0;
}
break;
case "The position opens above Kumo":
for (int bar = firstBar; bar < Bars; bar++)
{
Component[5].Value[bar] = Math.Max(senkouSpanA[bar], senkouSpanB[bar]);
Component[6].Value[bar] = Math.Min(senkouSpanA[bar], senkouSpanB[bar]);
}
Component[5].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
Component[5].DataType = IndComponentType.Other;
Component[5].ShowInDynInfo = false;
Component[6].PosPriceDependence = PositionPriceDependence.PriceSellLower;
Component[6].DataType = IndComponentType.Other;
Component[6].ShowInDynInfo = false;
break;
case "The position opens inside or above Kumo":
for (int bar = firstBar; bar < Bars; bar++)
{
Component[5].Value[bar] = Math.Min(senkouSpanA[bar], senkouSpanB[bar]);
Component[6].Value[bar] = Math.Max(senkouSpanA[bar], senkouSpanB[bar]);
}
Component[5].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
Component[5].DataType = IndComponentType.Other;
Component[5].ShowInDynInfo = false;
Component[6].PosPriceDependence = PositionPriceDependence.PriceSellLower;
Component[6].DataType = IndComponentType.Other;
Component[6].ShowInDynInfo = false;
break;
case "Tenkan Sen is above Kumo":
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = tenkanSen[bar - previous] > Math.Max(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) + sigma ? 1 : 0;
Component[6].Value[bar] = tenkanSen[bar - previous] < Math.Min(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) - sigma ? 1 : 0;
}
break;
case "Tenkan Sen is inside or above Kumo":
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = tenkanSen[bar - previous] > Math.Min(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) + sigma ? 1 : 0;
Component[6].Value[bar] = tenkanSen[bar - previous] < Math.Max(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) - sigma ? 1 : 0;
}
break;
case "Kijun Sen is above Kumo":
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = kijunSen[bar - previous] > Math.Max(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) + sigma ? 1 : 0;
Component[6].Value[bar] = kijunSen[bar - previous] < Math.Min(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) - sigma ? 1 : 0;
}
break;
case "Kijun Sen is inside or above Kumo":
for (int bar = firstBar + previous; bar < Bars; bar++)
{
Component[5].Value[bar] = kijunSen[bar - previous] > Math.Min(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) + sigma ? 1 : 0;
Component[6].Value[bar] = kijunSen[bar - previous] < Math.Max(senkouSpanA[bar - previous], senkouSpanB[bar - previous]) - sigma ? 1 : 0;
}
break;
case "Senkou Span A is higher than Senkou Span B":
IndicatorIsHigherThanAnotherIndicatorLogic(firstBar, previous, senkouSpanA, senkouSpanB, ref Component[5], ref Component[6]);
break;
case "Senkou Span A crosses Senkou Span B upward":
IndicatorCrossesAnotherIndicatorUpwardLogic(firstBar, previous, senkouSpanA, senkouSpanB, ref Component[5], ref Component[6]);
break;
}
}
public override void SetDescription()
{
switch (IndParam.ListParam[0].Text)
{
case "Enter the market at Tenkan Sen":
EntryPointLongDescription = "at Tenkan Sen of " + ToString();
EntryPointShortDescription = "at Tenkan Sen of " + ToString();
break;
case "Enter the market at Kijun Sen":
EntryPointLongDescription = "at Kijun Sen of " + ToString();
EntryPointShortDescription = "at Kijun Sen of " + ToString();
break;
case "Exit the market at Tenkan Sen":
ExitPointLongDescription = "at Tenkan Sen of " + ToString();
ExitPointShortDescription = "at Tenkan Sen of " + ToString();
break;
case "Exit the market at Kijun Sen":
ExitPointLongDescription = "at Kijun Sen of " + ToString();
ExitPointShortDescription = "at Kijun Sen of " + ToString();
break;
case "Tenkan Sen rises":
EntryFilterLongDescription = "Tenkan Sen of " + ToString() + " rises";
EntryFilterShortDescription = "Tenkan Sen of " + ToString() + " fals";
break;
case "Kijun Sen rises":
EntryFilterLongDescription = "Kijun Sen of " + ToString() + " rises";
EntryFilterShortDescription = "Kijun Sen of " + ToString() + " fals";
break;
case "Tenkan Sen is higher than Kijun Sen":
EntryFilterLongDescription = ToString() + " - Tenkan Sen is higher than Kijun Sen";
EntryFilterShortDescription = ToString() + " - Tenkan Sen is lower than Kijun Sen";
break;
case "Tenkan Sen crosses Kijun Sen upward":
EntryFilterLongDescription = ToString() + " - Tenkan Sen crosses Kijun Sen upward";
EntryFilterShortDescription = ToString() + " - Tenkan Sen crosses Kijun Sen downward";
break;
case "The bar opens above Tenkan Sen":
EntryFilterLongDescription = "bar opens above Tenkan Sen of " + ToString();
EntryFilterShortDescription = "bar opens below Tenkan Sen of " + ToString();
break;
case "The bar opens above Kijun Sen":
EntryFilterLongDescription = "bar opens above Kijun Sen of " + ToString();
EntryFilterShortDescription = "bar opens below Kijun Sen of " + ToString();
break;
case "Chikou Span is above closing price":
EntryFilterLongDescription = "Chikou Span of " + ToString() +
" is above closing price of corresponding bar";
EntryFilterShortDescription = "Chikou Span of " + ToString() +
" is below closing price of corresponding bar";
break;
case "The position opens above Kumo":
EntryFilterLongDescription = "position opens above Kumo of " + ToString();
EntryFilterShortDescription = "position opens below Kumo of " + ToString();
break;
case "The position opens inside or above Kumo":
EntryFilterLongDescription = "position opens inside or above Kumo of " + ToString();
EntryFilterShortDescription = "position opens inside or below Kumo of " + ToString();
break;
case "Tenkan Sen is above Kumo":
EntryFilterLongDescription = ToString() + " - Tenkan Sen is above Kumo";
EntryFilterShortDescription = ToString() + " - Tenkan Sen is below Kumo";
break;
case "Tenkan Sen is inside or above Kumo":
EntryFilterLongDescription = ToString() + " - Tenkan Sen is inside or above Kumo";
EntryFilterShortDescription = ToString() + " - Tenkan Sen is inside or below Kumo";
break;
case "Kijun Sen is above Kumo":
EntryFilterLongDescription = ToString() + " - Kijun Sen is above Kumo";
EntryFilterShortDescription = ToString() + " - Kijun Sen is below Kumo";
break;
case "Kijun Sen is inside or above Kumo":
EntryFilterLongDescription = ToString() + " - Kijun Sen is inside or above Kumo";
EntryFilterShortDescription = ToString() + " - Kijun Sen is inside or below Kumo";
break;
case "Senkou Span A is higher than Senkou Span B":
EntryFilterLongDescription = ToString() + " - Senkou Span A is higher than Senkou Span B";
EntryFilterShortDescription = ToString() + " - Senkou Span A is lower than Senkou Span B";
break;
case "Senkou Span A crosses Senkou Span B upward":
EntryFilterLongDescription = ToString() + " - Senkou Span A crosses Senkou Span B upward";
EntryFilterShortDescription = ToString() + " - Senkou Span A crosses Senkou Span B downward";
break;
}
}
public override string ToString()
{
return string.Format("{0}{1} ({2}, {3}, {4})",
IndicatorName,
(IndParam.CheckParam[0].Checked ? "*" : ""),
IndParam.NumParam[0].ValueToString,
IndParam.NumParam[2].ValueToString,
IndParam.NumParam[4].ValueToString);
}
}
}