/
RoundNumber.cs
239 lines (212 loc) · 8.97 KB
/
RoundNumber.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
//==============================================================
// 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 RoundNumber : Indicator
{
public RoundNumber()
{
IndicatorName = "Round Number";
PossibleSlots = SlotTypes.Open | SlotTypes.Close;
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;
IndParam.IsAllowLTF = false;
// The ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
if (SlotType == SlotTypes.Open)
IndParam.ListParam[0].ItemList = new[]
{
"Enter long at the higher round number",
"Enter long at the lower round number"
};
else if (SlotType == SlotTypes.Close)
IndParam.ListParam[0].ItemList = new[]
{
"Exit long at the higher round number",
"Exit long at the lower round number"
};
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.";
// The NumericUpDown parameters
IndParam.NumParam[0].Caption = "Vertical shift";
IndParam.NumParam[0].Value = 0;
IndParam.NumParam[0].Min = -2000;
IndParam.NumParam[0].Max = +2000;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "A vertical shift above the higher and below the lower round number.";
// The NumericUpDown parameters
IndParam.NumParam[1].Caption = "Digits";
IndParam.NumParam[1].Value = 2;
IndParam.NumParam[1].Min = 2;
IndParam.NumParam[1].Max = 4;
IndParam.NumParam[1].Enabled = true;
IndParam.NumParam[1].ToolTip = "Number of digits to be rounded.";
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
// Reading the parameters
double shift = IndParam.NumParam[0].Value * Point;
var digids = (int)IndParam.NumParam[1].Value;
// Calculation
var upperRn = new double[Bars];
var lowerRn = new double[Bars];
const int firstBar = 2;
for (int bar = firstBar; bar < Bars; bar++)
{
double nearestRound;
int digidsCut = Digits - digids;
if (digidsCut >= 0)
nearestRound = Math.Round(Open[bar], digidsCut);
else
nearestRound = Math.Round(Open[bar] * Math.Pow(10, digidsCut)) / Math.Pow(10, digidsCut);
if (nearestRound < Open[bar])
{
upperRn[bar] = nearestRound + (Point * Math.Pow(10, digids));
lowerRn[bar] = nearestRound;
}
else
{
upperRn[bar] = nearestRound;
lowerRn[bar] = nearestRound - (Point * Math.Pow(10, digids));
}
}
// Saving the components
Component = new IndicatorComp[4];
Component[0] = new IndicatorComp
{
CompName = "Higher round number",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Level,
ChartColor = Color.SpringGreen,
FirstBar = firstBar,
Value = upperRn
};
Component[1] = new IndicatorComp
{
CompName = "Lower round number",
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Level,
ChartColor = Color.DarkRed,
FirstBar = firstBar,
Value = lowerRn
};
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]
};
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.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;
}
switch (IndParam.ListParam[0].Text)
{
case "Enter long at the higher round number":
case "Exit long at the higher round number":
for (int bar = firstBar; bar < Bars; bar++)
{
Component[2].Value[bar] = upperRn[bar] + shift;
Component[3].Value[bar] = lowerRn[bar] - shift;
}
break;
case "Enter long at the lower round number":
case "Exit long at the lower round number":
for (int bar = firstBar; bar < Bars; bar++)
{
Component[2].Value[bar] = lowerRn[bar] - shift;
Component[3].Value[bar] = upperRn[bar] + shift;
}
break;
}
}
public override void SetDescription()
{
var iShift = (int)IndParam.NumParam[0].Value;
string sUpperTrade;
string sLowerTrade;
if (iShift > 0)
{
sUpperTrade = iShift + " points above the ";
sLowerTrade = iShift + " points below the ";
}
else if (iShift == 0)
{
sUpperTrade = "at the ";
sLowerTrade = "at the ";
}
else
{
sUpperTrade = -iShift + " points below the ";
sLowerTrade = -iShift + " points above the ";
}
switch (IndParam.ListParam[0].Text)
{
case "Enter long at the higher round number":
EntryPointLongDescription = sUpperTrade + "higher round number";
EntryPointShortDescription = sLowerTrade + "lower round number";
break;
case "Exit long at the higher round number":
ExitPointLongDescription = sUpperTrade + "higher round number";
ExitPointShortDescription = sLowerTrade + "lower round number";
break;
case "Enter long at the lower round number":
EntryPointLongDescription = sLowerTrade + "lower round number";
EntryPointShortDescription = sUpperTrade + "higher round number";
break;
case "Exit long at the lower round number":
ExitPointLongDescription = sLowerTrade + "lower round number";
ExitPointShortDescription = sUpperTrade + "higher round number";
break;
}
}
public override string ToString()
{
return IndicatorName + " (" +
IndParam.NumParam[0].ValueToString + ")"; // Vertical shift
}
}
}