forked from PopovMP/FSB_Pro_Indicators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHourlyHighLow.cs
More file actions
459 lines (416 loc) · 21.6 KB
/
HourlyHighLow.cs
File metadata and controls
459 lines (416 loc) · 21.6 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
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
//==============================================================
// 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 HourlyHighLow : Indicator
{
public HourlyHighLow()
{
IndicatorName = "Hourly High Low";
PossibleSlots = SlotTypes.Open | SlotTypes.OpenFilter | SlotTypes.Close | SlotTypes.CloseFilter;
IndicatorAuthor = "Miroslav Popov";
IndicatorVersion = "2.0";
IndicatorDescription = "Bundled in FSB distribution.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
// Setting up the indicator parameters
IndParam.IndicatorType = TypeOfIndicator.Additional;
// The ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
if (SlotType == SlotTypes.Open)
IndParam.ListParam[0].ItemList = new[]
{
"Enter long at the hourly high",
"Enter long at the hourly low"
};
else if (SlotType == SlotTypes.OpenFilter)
IndParam.ListParam[0].ItemList = new[]
{
"The position opens above the hourly high",
"The position opens below the hourly high",
"The position opens above the hourly low",
"The position opens below the hourly low"
};
else if (SlotType == SlotTypes.Close)
IndParam.ListParam[0].ItemList = new[]
{
"Exit long at the hourly high",
"Exit long at the hourly low"
};
else if (SlotType == SlotTypes.CloseFilter)
IndParam.ListParam[0].ItemList = new[]
{
"The bar closes above the hourly high",
"The bar closes below the hourly high",
"The bar closes above the hourly low",
"The bar closes below the hourly low"
};
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 = "Logic of application of the indicator.";
IndParam.ListParam[1].Caption = "Base price";
IndParam.ListParam[1].ItemList = new[] {"High and Low"};
IndParam.ListParam[1].Index = 0;
IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index];
IndParam.ListParam[1].Enabled = true;
IndParam.ListParam[1].ToolTip = "Used price from the indicator.";
// The NumericUpDown parameters
IndParam.NumParam[0].Caption = "Start hour (incl.)";
IndParam.NumParam[0].Value = 0;
IndParam.NumParam[0].Min = 0;
IndParam.NumParam[0].Max = 24;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "The starting hour of the period.";
IndParam.NumParam[1].Caption = "Start minutes (incl.)";
IndParam.NumParam[1].Value = 0;
IndParam.NumParam[1].Min = 0;
IndParam.NumParam[1].Max = 59;
IndParam.NumParam[1].Enabled = true;
IndParam.NumParam[1].ToolTip = "The starting minutes of the period.";
IndParam.NumParam[2].Caption = "End hour (excl.)";
IndParam.NumParam[2].Value = 24;
IndParam.NumParam[2].Min = 0;
IndParam.NumParam[2].Max = 24;
IndParam.NumParam[2].Enabled = true;
IndParam.NumParam[2].ToolTip = "The ending hour of the period.";
IndParam.NumParam[3].Caption = "End minutes (excl.)";
IndParam.NumParam[3].Value = 0;
IndParam.NumParam[3].Min = 0;
IndParam.NumParam[3].Max = 59;
IndParam.NumParam[3].Enabled = true;
IndParam.NumParam[3].ToolTip = "The ending minutes of the period.";
IndParam.NumParam[4].Caption = "Vertical shift";
IndParam.NumParam[4].Value = 0;
IndParam.NumParam[4].Min = -2000;
IndParam.NumParam[4].Max = +2000;
IndParam.NumParam[4].Enabled = true;
IndParam.NumParam[4].ToolTip = "A vertical shift above the high and below the low price.";
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
var fromHour = (int) IndParam.NumParam[0].Value;
var fromMin = (int) IndParam.NumParam[1].Value;
var untilHour = (int) IndParam.NumParam[2].Value;
var untilMin = (int) IndParam.NumParam[3].Value;
var tsFromTime = new TimeSpan(fromHour, fromMin, 0);
var tsUntilTime = new TimeSpan(untilHour, untilMin, 0);
double shift = IndParam.NumParam[4].Value*Point;
const int firstBar = 2;
// Calculation
var adHighPrice = new double[Bars];
var adLowPrice = new double[Bars];
double dMinPrice = double.MaxValue;
double dMaxPrice = double.MinValue;
adHighPrice[0] = 0;
adLowPrice[0] = 0;
bool bPrevPeriod = false;
for (int iBar = 1; iBar < Bars; iBar++)
{
bool bPeriod;
if (tsFromTime < tsUntilTime)
bPeriod = Time[iBar].TimeOfDay >= tsFromTime && Time[iBar].TimeOfDay < tsUntilTime;
else if (tsFromTime > tsUntilTime)
bPeriod = Time[iBar].TimeOfDay >= tsFromTime || Time[iBar].TimeOfDay < tsUntilTime;
else
bPeriod = true;
if (bPeriod)
{
if (dMaxPrice < High[iBar]) dMaxPrice = High[iBar];
if (dMinPrice > Low[iBar]) dMinPrice = Low[iBar];
}
if (!bPeriod && bPrevPeriod)
{
adHighPrice[iBar] = dMaxPrice;
adLowPrice[iBar] = dMinPrice;
dMaxPrice = double.MinValue;
dMinPrice = double.MaxValue;
}
else
{
adHighPrice[iBar] = adHighPrice[iBar - 1];
adLowPrice[iBar] = adLowPrice[iBar - 1];
}
bPrevPeriod = bPeriod;
}
// Shifting the price
var adUpperBand = new double[Bars];
var adLowerBand = new double[Bars];
for (int iBar = firstBar; iBar < Bars; iBar++)
{
adUpperBand[iBar] = adHighPrice[iBar] + shift;
adLowerBand[iBar] = adLowPrice[iBar] - shift;
}
// Saving the components
Component = new IndicatorComp[4];
Component[0] = new IndicatorComp
{
CompName = "Hourly High",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Level,
ChartColor = Color.DarkGreen,
FirstBar = firstBar,
Value = adHighPrice
};
Component[1] = new IndicatorComp
{
CompName = "Hourly Low",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Level,
ChartColor = Color.DarkRed,
FirstBar = firstBar,
Value = adLowPrice
};
Component[2] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
Component[3] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
// Sets the Component's type
if (SlotType == SlotTypes.Open)
{
Component[2].CompName = "Long position entry price";
Component[2].DataType = IndComponentType.OpenLongPrice;
Component[3].CompName = "Short position entry price";
Component[3].DataType = IndComponentType.OpenShortPrice;
}
else if (SlotType == SlotTypes.OpenFilter)
{
Component[2].CompName = "Is long entry allowed";
Component[2].DataType = IndComponentType.AllowOpenLong;
Component[3].CompName = "Is short entry allowed";
Component[3].DataType = IndComponentType.AllowOpenShort;
}
else if (SlotType == SlotTypes.Close)
{
Component[2].CompName = "Long position closing price";
Component[2].DataType = IndComponentType.CloseLongPrice;
Component[3].CompName = "Short position closing price";
Component[3].DataType = IndComponentType.CloseShortPrice;
}
else if (SlotType == SlotTypes.CloseFilter)
{
Component[2].CompName = "Close out long position";
Component[2].DataType = IndComponentType.ForceCloseLong;
Component[3].CompName = "Close out short position";
Component[3].DataType = IndComponentType.ForceCloseShort;
}
switch (IndParam.ListParam[0].Text)
{
case "Enter long at the hourly high":
case "Exit long at the hourly high":
Component[2].Value = adUpperBand;
Component[3].Value = adLowerBand;
break;
case "Enter long at the hourly low":
case "Exit long at the hourly low":
Component[2].Value = adLowerBand;
Component[3].Value = adUpperBand;
break;
case "The bar closes below the hourly high":
BandIndicatorLogic(firstBar, 0, adUpperBand, adLowerBand, ref Component[2], ref Component[3],
BandIndLogic.The_bar_closes_below_the_Upper_Band);
break;
case "The bar closes above the hourly high":
BandIndicatorLogic(firstBar, 0, adUpperBand, adLowerBand, ref Component[2], ref Component[3],
BandIndLogic.The_bar_closes_above_the_Upper_Band);
break;
case "The bar closes below the hourly low":
BandIndicatorLogic(firstBar, 0, adUpperBand, adLowerBand, ref Component[2], ref Component[3],
BandIndLogic.The_bar_closes_below_the_Lower_Band);
break;
case "The bar closes above the hourly low":
BandIndicatorLogic(firstBar, 0, adUpperBand, adLowerBand, ref Component[2], ref Component[3],
BandIndLogic.The_bar_closes_above_the_Lower_Band);
break;
case "The position opens above the hourly high":
Component[0].DataType = IndComponentType.Other;
Component[1].DataType = IndComponentType.Other;
Component[2].CompName = "Shifted hourly high";
Component[2].DataType = IndComponentType.Other;
Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
Component[3].CompName = "Shifted hourly low";
Component[3].DataType = IndComponentType.Other;
Component[3].PosPriceDependence = PositionPriceDependence.PriceSellLower;
Component[2].Value = adUpperBand;
Component[3].Value = adLowerBand;
break;
case "The position opens below the hourly high":
Component[0].DataType = IndComponentType.Other;
Component[1].DataType = IndComponentType.Other;
Component[2].CompName = "Shifted hourly high";
Component[2].DataType = IndComponentType.Other;
Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyLower;
Component[3].CompName = "Shifted hourly low";
Component[3].DataType = IndComponentType.Other;
Component[3].PosPriceDependence = PositionPriceDependence.PriceSellHigher;
Component[2].Value = adUpperBand;
Component[3].Value = adLowerBand;
break;
case "The position opens above the hourly low":
Component[0].DataType = IndComponentType.Other;
Component[1].DataType = IndComponentType.Other;
Component[2].CompName = "Shifted hourly low";
Component[2].DataType = IndComponentType.Other;
Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
Component[3].CompName = "Shifted hourly high";
Component[3].DataType = IndComponentType.Other;
Component[3].PosPriceDependence = PositionPriceDependence.PriceSellLower;
Component[2].Value = adLowerBand;
Component[3].Value = adUpperBand;
break;
case "The position opens below the hourly low":
Component[0].DataType = IndComponentType.Other;
Component[1].DataType = IndComponentType.Other;
Component[2].CompName = "Shifted hourly low";
Component[2].DataType = IndComponentType.Other;
Component[2].PosPriceDependence = PositionPriceDependence.PriceBuyLower;
Component[3].CompName = "Shifted hourly high";
Component[3].DataType = IndComponentType.Other;
Component[3].PosPriceDependence = PositionPriceDependence.PriceSellHigher;
Component[2].Value = adLowerBand;
Component[3].Value = adUpperBand;
break;
}
}
public override void SetDescription()
{
var iShift = (int) IndParam.NumParam[4].Value;
var iFromHour = (int) IndParam.NumParam[0].Value;
var iFromMin = (int) IndParam.NumParam[1].Value;
var iUntilHour = (int) IndParam.NumParam[2].Value;
var iUntilMin = (int) IndParam.NumParam[3].Value;
string sFromTime = iFromHour.ToString("00") + ":" + iFromMin.ToString("00");
string sUntilTime = iUntilHour.ToString("00") + ":" + iUntilMin.ToString("00");
string sInterval = "(" + sFromTime + " - " + sUntilTime + ")";
string sUpperTrade;
string sLowerTrade;
if (iShift > 0)
{
sUpperTrade = iShift + " points above the ";
sLowerTrade = iShift + " points below the ";
}
else if (iShift == 0)
{
if (IndParam.ListParam[0].Text == "Enter long at the hourly high" ||
IndParam.ListParam[0].Text == "Enter long at the hourly low" ||
IndParam.ListParam[0].Text == "Exit long at the hourly high" ||
IndParam.ListParam[0].Text == "Exit long at the hourly low")
{
sUpperTrade = "at the ";
sLowerTrade = "at the ";
}
else
{
sUpperTrade = "the ";
sLowerTrade = "the ";
}
}
else
{
sUpperTrade = -iShift + " points below the ";
sLowerTrade = -iShift + " points above the ";
}
switch (IndParam.ListParam[0].Text)
{
case "Enter long at the hourly high":
EntryPointLongDescription = sUpperTrade + "hourly high " + sInterval;
EntryPointShortDescription = sLowerTrade + "hourly low " + sInterval;
break;
case "Enter long at the hourly low":
EntryPointLongDescription = sLowerTrade + "hourly low " + sInterval;
EntryPointShortDescription = sUpperTrade + "hourly high " + sInterval;
break;
case "Exit long at the hourly high":
ExitPointLongDescription = sUpperTrade + "hourly high " + sInterval;
ExitPointShortDescription = sLowerTrade + "hourly low " + sInterval;
break;
case "Exit long at the hourly low":
ExitPointLongDescription = sLowerTrade + "hourly low " + sInterval;
ExitPointShortDescription = sUpperTrade + "hourly high " + sInterval;
break;
case "The position opens below the hourly high":
EntryFilterLongDescription = "the position opens lower than " + sUpperTrade + "hourly high " +
sInterval;
EntryFilterShortDescription = "the position opens higher than " + sLowerTrade + "hourly low " +
sInterval;
break;
case "The position opens above the hourly high":
EntryFilterLongDescription = "the position opens higher than " + sUpperTrade + "hourly high " +
sInterval;
EntryFilterShortDescription = "the position opens lower than " + sLowerTrade + "hourly low " +
sInterval;
break;
case "The position opens below the hourly low":
EntryFilterLongDescription = "the position opens lower than " + sLowerTrade + "hourly low " +
sInterval;
EntryFilterShortDescription = "the position opens higher than " + sUpperTrade + "hourly high " +
sInterval;
break;
case "The position opens above the hourly low":
EntryFilterLongDescription = "the position opens higher than " + sLowerTrade + "hourly low " +
sInterval;
EntryFilterShortDescription = "the position opens lower than " + sUpperTrade + "hourly high " +
sInterval;
break;
case "The bar closes below the hourly high":
ExitFilterLongDescription = "the bar closes lower than " + sUpperTrade + "hourly high " + sInterval;
ExitFilterShortDescription = "the bar closes higher than " + sLowerTrade + "hourly low " + sInterval;
break;
case "The bar closes above the hourly high":
ExitFilterLongDescription = "the bar closes higher than " + sUpperTrade + "hourly high " + sInterval;
ExitFilterShortDescription = "the bar closes lower than " + sLowerTrade + "hourly low " + sInterval;
break;
case "The bar closes below the hourly low":
ExitFilterLongDescription = "the bar closes lower than " + sLowerTrade + "hourly low " + sInterval;
ExitFilterShortDescription = "the bar closes higher than " + sUpperTrade + "hourly high " +
sInterval;
break;
case "The bar closes above the hourly low":
ExitFilterLongDescription = "the bar closes higher than " + sLowerTrade + "hourly low " + sInterval;
ExitFilterShortDescription = "the bar closes lower than " + sUpperTrade + "hourly high " + sInterval;
break;
}
}
public override string ToString()
{
var fromHour = (int) IndParam.NumParam[0].Value;
var fromMin = (int) IndParam.NumParam[1].Value;
var untilHour = (int) IndParam.NumParam[2].Value;
var untilMin = (int) IndParam.NumParam[3].Value;
string fromTime = fromHour.ToString("00") + ":" + fromMin.ToString("00");
string untilTime = untilHour.ToString("00") + ":" + untilMin.ToString("00");
return IndicatorName + " (" +
fromTime + " - " + // Start time
untilTime + ", " + // End time
IndParam.NumParam[4].ValueToString + ")"; // Vertical shift
}
}
}