01 // Detrended Oscillator Indicator
02 // Last changed on 2009-05-05
03 // Part of Forex Strategy Builder v2.8.3.7+
04 // Website http://forexsb.com/
05 // Copyright (c) 2006 - 2009 Miroslav Popov - All rights reserved.
06 // This code or any part of it cannot be used in other applications without a permission.
07
08 using System;
09 using System.Drawing;
10
11 namespace Forex_Strategy_Builder
12 {
13 /// <summary>
14 /// Detrended Oscillator Indicator
15 /// </summary>
16 public class Detrended_Oscillator : Indicator
17 {
18 /// <summary>
19 /// Sets the default indicator parameters for the designated slot type
20 /// </summary>
21 public Detrended_Oscillator(SlotTypes slotType)
22 {
23 // General properties
24 IndicatorName = "Detrended Oscillator";
25 PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter;
26 SeparatedChart = true;
27
28 // Setting up the indicator parameters
29 IndParam = new IndicatorParam();
30 IndParam.IndicatorName = IndicatorName;
31 IndParam.SlotType = slotType;
32
33 // The ComboBox parameters
34 IndParam.ListParam[0].Caption = "Logic";
35 IndParam.ListParam[0].ItemList = new string[]
36 {
37 "The Detrended Oscillator rises",
38 "The Detrended Oscillator falls",
39 "The Detrended Oscillator is higher than the zero line",
40 "The Detrended Oscillator is lower than the zero line",
41 "The Detrended Oscillator crosses the zero line upward",
42 "The Detrended Oscillator crosses the zero line downward",
43 "The Detrended Oscillator changes its direction upward",
44 "The Detrended Oscillator changes its direction downward"
45 };
46 IndParam.ListParam[0].Index = 0;
47 IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index];
48 IndParam.ListParam[0].Enabled = true;
49 IndParam.ListParam[0].ToolTip = "Logic of application of the indicator.";
50
51 IndParam.ListParam[1].Caption = "Smoothing method MA1";
52 IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod));
53 IndParam.ListParam[1].Index = (int)MAMethod.Simple;
54 IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index];
55 IndParam.ListParam[1].Enabled = true;
56 IndParam.ListParam[1].ToolTip = "The method of smoothing of MA1.";
57
58 IndParam.ListParam[2].Caption = "Smoothing method MA2";
59 IndParam.ListParam[2].ItemList = Enum.GetNames(typeof(MAMethod));
60 IndParam.ListParam[2].Index = (int)MAMethod.Simple;
61 IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index];
62 IndParam.ListParam[2].Enabled = true;
63 IndParam.ListParam[2].ToolTip = "The method of smoothing of MA2.";
64
65 IndParam.ListParam[3].Caption = "Base price";
66 IndParam.ListParam[3].ItemList = Enum.GetNames(typeof(BasePrice));
67 IndParam.ListParam[3].Index = (int)BasePrice.Close;
68 IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index];
69 IndParam.ListParam[3].Enabled = true;
70 IndParam.ListParam[3].ToolTip = "The price the MA is based on.";
71
72 // The NumericUpDown parameters
73 IndParam.NumParam[0].Caption = "Period MA1";
74 IndParam.NumParam[0].Value = 13;
75 IndParam.NumParam[0].Min = 1;
76 IndParam.NumParam[0].Max = 100;
77 IndParam.NumParam[0].Enabled = true;
78 IndParam.NumParam[0].ToolTip = "The smoothing period of MA1.";
79
80 IndParam.NumParam[1].Caption = "Period MA2";
81 IndParam.NumParam[1].Value = 3;
82 IndParam.NumParam[1].Min = 1;
83 IndParam.NumParam[1].Max = 100;
84 IndParam.NumParam[1].Enabled = true;
85 IndParam.NumParam[1].ToolTip = "The smoothing period of MA2.";
86
87 // The CheckBox parameters
88 IndParam.CheckParam[0].Caption = "Use previous bar value";
89 IndParam.CheckParam[0].Checked = PrepareUsePrevBarValueCheckBox(slotType);
90 IndParam.CheckParam[0].Enabled = true;
91 IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar.";
92
93 return;
94 }
95
96 /// <summary>
97 /// Calculates the indicator's components
98 /// </summary>
99 public override void Calculate(SlotTypes slotType)
100 {
101 // Reading the parameters
102 MAMethod maMethod1 = (MAMethod)IndParam.ListParam[1].Index;
103 MAMethod maMethod2 = (MAMethod)IndParam.ListParam[2].Index;
104 BasePrice price = (BasePrice)IndParam.ListParam[3].Index;
105 int iPeriod1 = (int)IndParam.NumParam[0].Value;
106 int iPeriod2 = (int)IndParam.NumParam[1].Value;
107 int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0;
108
109 // Calculation
110 int iFirstBar = iPeriod1 + iPeriod2 + 1;
111
112 double[] adPrice = Price(price);
113 double[] adMA = MovingAverage(iPeriod1, 0, maMethod1, adPrice);
114 double[] adMAPr = new double[Bars];
115
116 for (int iBar = 0; iBar < Bars; iBar++)
117 {
118 adMAPr[iBar] = adPrice[iBar] - adMA[iBar];
119 }
120
121 double[] adDO = MovingAverage(iPeriod2, 0, maMethod2, adMAPr);
122
123 // Saving the components
124 Component = new IndicatorComp[3];
125
126 Component[0] = new IndicatorComp();
127 Component[0].CompName = "Detrended Oscillator";
128 Component[0].DataType = IndComponentType.IndicatorValue;
129 Component[0].ChartType = IndChartType.Line;
130 Component[0].ChartColor = Color.LightSeaGreen;
131 Component[0].FirstBar = iFirstBar;
132 Component[0].Value = adDO;
133
134 Component[1] = new IndicatorComp();
135 Component[1].ChartType = IndChartType.NoChart;
136 Component[1].FirstBar = iFirstBar;
137 Component[1].Value = new double[Bars];
138
139 Component[2] = new IndicatorComp();
140 Component[2].ChartType = IndChartType.NoChart;
141 Component[2].FirstBar = iFirstBar;
142 Component[2].Value = new double[Bars];
143
144 // Sets the Component's type
145 if (slotType == SlotTypes.OpenFilter)
146 {
147 Component[1].DataType = IndComponentType.AllowOpenLong;
148 Component[1].CompName = "Is long entry allowed";
149 Component[2].DataType = IndComponentType.AllowOpenShort;
150 Component[2].CompName = "Is short entry allowed";
151 }
152 else if (slotType == SlotTypes.CloseFilter)
153 {
154 Component[1].DataType = IndComponentType.ForceCloseLong;
155 Component[1].CompName = "Close out long position";
156 Component[2].DataType = IndComponentType.ForceCloseShort;
157 Component[2].CompName = "Close out short position";
158 }
159
160 // Calculation of the logic
161 IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter;
162
163 switch (IndParam.ListParam[0].Text)
164 {
165 case "The Detrended Oscillator rises":
166 indLogic = IndicatorLogic.The_indicator_rises;
167 break;
168
169 case "The Detrended Oscillator falls":
170 indLogic = IndicatorLogic.The_indicator_falls;
171 break;
172
173 case "The Detrended Oscillator is higher than the zero line":
174 indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
175 break;
176
177 case "The Detrended Oscillator is lower than the zero line":
178 indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
179 break;
180
181 case "The Detrended Oscillator crosses the zero line upward":
182 indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
183 break;
184
185 case "The Detrended Oscillator crosses the zero line downward":
186 indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
187 break;
188
189 case "The Detrended Oscillator changes its direction upward":
190 indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
191 break;
192
193 case "The Detrended Oscillator changes its direction downward":
194 indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
195 break;
196
197 default:
198 break;
199 }
200
201 OscillatorLogic(iFirstBar, iPrvs, adDO, 0, 0, ref Component[1], ref Component[2], indLogic);
202
203 return;
204 }
205
206 /// <summary>
207 /// Sets the indicator logic description
208 /// </summary>
209 public override void SetDescription(SlotTypes slotType)
210 {
211 EntryFilterLongDescription = "the " + ToString() + " ";
212 EntryFilterShortDescription = "the " + ToString() + " ";
213 ExitFilterLongDescription = "the " + ToString() + " ";
214 ExitFilterShortDescription = "the " + ToString() + " ";
215
216 switch (IndParam.ListParam[0].Text)
217 {
218 case "The Detrended Oscillator rises":
219 EntryFilterLongDescription += "rises";
220 EntryFilterShortDescription += "falls";
221 ExitFilterLongDescription += "rises";
222 ExitFilterShortDescription += "falls";
223 break;
224
225 case "The Detrended Oscillator falls":
226 EntryFilterLongDescription += "falls";
227 EntryFilterShortDescription += "rises";
228 ExitFilterLongDescription += "falls";
229 ExitFilterShortDescription += "rises";
230 break;
231
232 case "The Detrended Oscillator is higher than the zero line":
233 EntryFilterLongDescription += "is higher than the zero line";
234 EntryFilterShortDescription += "is lower than the zero line";
235 ExitFilterLongDescription += "is higher than the zero line";
236 ExitFilterShortDescription += "is lower than the zero line";
237 break;
238
239 case "The Detrended Oscillator is lower than the zero line":
240 EntryFilterLongDescription += "is lower than the zero line";
241 EntryFilterShortDescription += "is higher than the zero line";
242 ExitFilterLongDescription += "is lower than the zero line";
243 ExitFilterShortDescription += "is higher than the zero line";
244 break;
245
246 case "The Detrended Oscillator crosses the zero line upward":
247 EntryFilterLongDescription += "crosses the zero line upward";
248 EntryFilterShortDescription += "crosses the zero line downward";
249 ExitFilterLongDescription += "crosses the zero line upward";
250 ExitFilterShortDescription += "crosses the zero line downward";
251 break;
252
253 case "The Detrended Oscillator crosses the zero line downward":
254 EntryFilterLongDescription += "crosses the zero line downward";
255 EntryFilterShortDescription += "crosses the zero line upward";
256 ExitFilterLongDescription += "crosses the zero line downward";
257 ExitFilterShortDescription += "crosses the zero line upward";
258 break;
259
260 case "The Detrended Oscillator changes its direction upward":
261 EntryFilterLongDescription += "changes its direction upward";
262 EntryFilterShortDescription += "changes its direction downward";
263 ExitFilterLongDescription += "changes its direction upward";
264 ExitFilterShortDescription += "changes its direction downward";
265 break;
266
267 case "The Detrended Oscillator changes its direction downward":
268 EntryFilterLongDescription += "changes its direction downward";
269 EntryFilterShortDescription += "changes its direction upward";
270 ExitFilterLongDescription += "changes its direction downward";
271 ExitFilterShortDescription += "changes its direction upward";
272 break;
273
274 default:
275 break;
276 }
277
278 return;
279 }
280
281 /// <summary>
282 /// Indicator to string
283 /// </summary>
284 public override string ToString()
285 {
286 string sString = IndicatorName +
287 (IndParam.CheckParam[0].Checked ? "* (" : " (") +
288 IndParam.ListParam[1].Text + ", " + // Method1
289 IndParam.NumParam[0].ValueToString + ", " + // Period1
290 IndParam.ListParam[2].Text + ", " + // Method2
291 IndParam.NumParam[1].ValueToString + ", " + // Period2
292 IndParam.ListParam[3].Text + ")"; // Price
293
294 return sString;
295 }
296 }
297 }
Top