Forex Indicators
Forex Software Forex Strategy Builder Forex Strategy Trader Strategies Blog Forum
Overview Download Rates Source Help Power Up Awards

Technical Indicators » Momentum MA Oscillator

01 // Momentum MA 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 // This code or any part of it cannot be used in other applications without a permission.
06 // Copyright (c) 2006 - 2009 Miroslav Popov - All rights reserved.
07 
08 using System;
09 
10 namespace Forex_Strategy_Builder
11 {
12     /// <summary>
13     /// Momentum MA Oscillator Indicator
14     /// </summary>
15     public class Momentum_MA_Oscillator : Indicator
16     {
17         /// <summary>
18         /// Sets the default indicator parameters for the designated slot type
19         /// </summary>
20         public Momentum_MA_Oscillator(SlotTypes slotType)
21         {
22             // General properties
23             IndicatorName  = "Momentum MA Oscillator";
24             PossibleSlots  = SlotTypes.OpenFilter | SlotTypes.CloseFilter;
25             SeparatedChart = true;
26 
27             // Setting up the indicator parameters
28             IndParam = new IndicatorParam();
29             IndParam.IndicatorName = IndicatorName;
30             IndParam.SlotType      = slotType;
31             IndParam.IndicatorType = TypeOfIndicator.IndicatorsMA;
32 
33             // The ComboBox parameters
34             IndParam.ListParam[0].Caption = "Logic";
35             IndParam.ListParam[0].ItemList = new string[]
36             {
37                 "The Oscillator rises",
38                 "The Oscillator falls",
39                 "The Oscillator is higher than the zero line",
40                 "The Oscillator is lower than the zero line",
41                 "The Oscillator crosses the zero line upward",
42                 "The Oscillator crosses the zero line downward",
43                 "The Oscillator changes its direction upward",
44                 "The 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 Oscillator.";
50 
51             IndParam.ListParam[1].Caption  = "Smoothing method";
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 Moving Average method used for smoothing the Momentum value.";
57 
58             IndParam.ListParam[2].Caption  = "Signal line method";
59             IndParam.ListParam[2].ItemList = Enum.GetNames(typeof(MAMethod));
60             IndParam.ListParam[2].Index    = (int)MAMethod.Exponential;
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 Moving Average method used for smoothing the signal line.";
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 Momentum is based on.";
71 
72             // The NumericUpDown parameters
73             IndParam.NumParam[0].Caption = "Momentum period";
74             IndParam.NumParam[0].Value   = 10;
75             IndParam.NumParam[0].Min     = 1;
76             IndParam.NumParam[0].Max     = 200;
77             IndParam.NumParam[0].Enabled = true;
78             IndParam.NumParam[0].ToolTip = "The period of Momentum.";
79 
80             IndParam.NumParam[1].Caption = "Signal line period";
81             IndParam.NumParam[1].Value   = 14;
82             IndParam.NumParam[1].Min     = 1;
83             IndParam.NumParam[1].Max     = 200;
84             IndParam.NumParam[1].Enabled = true;
85             IndParam.NumParam[1].ToolTip = "The period of signal line.";
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  maMethod         = (MAMethod )IndParam.ListParam[1].Index;
103             MAMethod  maSignalMAMethod = (MAMethod )IndParam.ListParam[2].Index;
104             BasePrice basePrice        = (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 + 2;
111             double[] adIndicator1 = new double[Bars];
112             double[] adIndicator2 = new double[Bars];
113             double[] adOscllator  = new double[Bars];
114 
115 // ---------------------------------------------------------
116             Momentum Momentum1 = new Momentum(slotType);
117             Momentum1.IndParam.ListParam[1].Index    = IndParam.ListParam[1].Index;
118             Momentum1.IndParam.ListParam[2].Index    = IndParam.ListParam[3].Index;
119             Momentum1.IndParam.NumParam[0].Value     = IndParam.NumParam[0].Value;
120             Momentum1.IndParam.CheckParam[0].Checked = IndParam.CheckParam[0].Checked;
121             Momentum1.Calculate(slotType);
122 
123             adIndicator1 = Momentum1.Component[0].Value;
124             adIndicator2 = MovingAverage(iPeriod2, 0, maSignalMAMethod, adIndicator1);
125 // ----------------------------------------------------------
126 
127             for (int iBar = iFirstBar; iBar < Bars; iBar++)
128             {
129                 adOscllator[iBar] = adIndicator1[iBar] - adIndicator2[iBar];
130             }
131 
132             // Saving the components
133             Component = new IndicatorComp[3];
134 
135             Component[0] = new IndicatorComp();
136             Component[0].CompName  = "Histogram";
137             Component[0].DataType  = IndComponentType.IndicatorValue;
138             Component[0].ChartType = IndChartType.Histogram;
139             Component[0].FirstBar  = iFirstBar;
140             Component[0].Value     = adOscllator;
141 
142             Component[1] = new IndicatorComp();
143             Component[1].ChartType = IndChartType.NoChart;
144             Component[1].FirstBar  = iFirstBar;
145             Component[1].Value     = new double[Bars];
146 
147             Component[2] = new IndicatorComp();
148             Component[2].ChartType = IndChartType.NoChart;
149             Component[2].FirstBar  = iFirstBar;
150             Component[2].Value     = new double[Bars];
151 
152             // Sets the Component's type
153             if (slotType == SlotTypes.OpenFilter)
154             {
155                 Component[1].DataType = IndComponentType.AllowOpenLong;
156                 Component[1].CompName = "Is long entry allowed";
157                 Component[2].DataType = IndComponentType.AllowOpenShort;
158                 Component[2].CompName = "Is short entry allowed";
159             }
160             else if (slotType == SlotTypes.CloseFilter)
161             {
162                 Component[1].DataType = IndComponentType.ForceCloseLong;
163                 Component[1].CompName = "Close out long position";
164                 Component[2].DataType = IndComponentType.ForceCloseShort;
165                 Component[2].CompName = "Close out short position";
166             }
167 
168             // Calculation of the logic
169             IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter;
170 
171             switch (IndParam.ListParam[0].Text)
172             {
173                 case "The Oscillator rises":
174                     indLogic = IndicatorLogic.The_indicator_rises;
175                     break;
176 
177                 case "The Oscillator falls":
178                     indLogic = IndicatorLogic.The_indicator_falls;
179                     break;
180 
181                 case "The Oscillator is higher than the zero line":
182                     indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
183                     break;
184 
185                 case "The Oscillator is lower than the zero line":
186                     indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
187                     break;
188 
189                 case "The Oscillator crosses the zero line upward":
190                     indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
191                     break;
192 
193                 case "The Oscillator crosses the zero line downward":
194                     indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
195                     break;
196 
197                 case "The Oscillator changes its direction upward":
198                     indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
199                     break;
200 
201                 case "The Oscillator changes its direction downward":
202                     indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
203                     break;
204 
205                 default:
206                     break;
207             }
208 
209             OscillatorLogic(iFirstBar, iPrvs, adOscllator, 0, 0, ref Component[1], ref Component[2], indLogic);
210 
211             return;
212         }
213 
214         /// <summary>
215         /// Sets the indicator logic description
216         /// </summary>
217         public override void SetDescription(SlotTypes slotType)
218         {
219             EntryFilterLongDescription  = "the " + ToString() + " ";
220             EntryFilterShortDescription = "the " + ToString() + " ";
221             ExitFilterLongDescription   = "the " + ToString() + " ";
222             ExitFilterShortDescription  = "the " + ToString() + " ";
223 
224             switch (IndParam.ListParam[0].Text)
225             {
226                 case "The Oscillator rises":
227                     EntryFilterLongDescription  += "rises";
228                     EntryFilterShortDescription += "falls";
229                     ExitFilterLongDescription   += "rises";
230                     ExitFilterShortDescription  += "falls";
231                     break;
232 
233                 case "The Oscillator falls":
234                     EntryFilterLongDescription  += "falls";
235                     EntryFilterShortDescription += "rises";
236                     ExitFilterLongDescription   += "falls";
237                     ExitFilterShortDescription  += "rises";
238                     break;
239 
240                 case "The Oscillator is higher than the zero line":
241                     EntryFilterLongDescription  += "is higher than the zero line";
242                     EntryFilterShortDescription += "is lower than the zero line";
243                     ExitFilterLongDescription   += "is higher than the zero line";
244                     ExitFilterShortDescription  += "is lower than the zero line";
245                     break;
246 
247                 case "The Oscillator is lower than the zero line":
248                     EntryFilterLongDescription  += "is lower than the zero line";
249                     EntryFilterShortDescription += "is higher than the zero line";
250                     ExitFilterLongDescription   += "is lower than the zero line";
251                     ExitFilterShortDescription  += "is higher than the zero line";
252                     break;
253 
254                 case "The Oscillator crosses the zero line upward":
255                     EntryFilterLongDescription  += "crosses the zero line upward";
256                     EntryFilterShortDescription += "crosses the zero line downward";
257                     ExitFilterLongDescription   += "crosses the zero line upward";
258                     ExitFilterShortDescription  += "crosses the zero line downward";
259                     break;
260 
261                 case "The Oscillator crosses the zero line downward":
262                     EntryFilterLongDescription  += "crosses the zero line downward";
263                     EntryFilterShortDescription += "crosses the zero line upward";
264                     ExitFilterLongDescription   += "crosses the zero line downward";
265                     ExitFilterShortDescription  += "crosses the zero line upward";
266                     break;
267 
268                 case "The Oscillator changes its direction upward":
269                     EntryFilterLongDescription  += "changes its direction upward";
270                     EntryFilterShortDescription += "changes its direction downward";
271                     ExitFilterLongDescription   += "changes its direction upward";
272                     ExitFilterShortDescription  += "changes its direction downward";
273                     break;
274 
275                 case "The Oscillator changes its direction downward":
276                     EntryFilterLongDescription  += "changes its direction downward";
277                     EntryFilterShortDescription += "changes its direction upward";
278                     ExitFilterLongDescription   += "changes its direction downward";
279                     ExitFilterShortDescription  += "changes its direction upward";
280                     break;
281 
282                 default:
283                     break;
284             }
285 
286             return;
287         }
288 
289         /// <summary>
290         /// Indicator to string
291         /// </summary>
292         public override string ToString()
293         {
294             string sString = IndicatorName +
295                 (IndParam.CheckParam[0].Checked ? "* (" : " (") +
296                 IndParam.ListParam[1].Text         + ", " + // Method
297                 IndParam.ListParam[2].Text         + ", " + // Method
298                 IndParam.ListParam[3].Text         + ", " + // Price
299                 IndParam.NumParam[0].ValueToString + ", " + // Period
300                 IndParam.NumParam[1].ValueToString + ")";   // Period
301 
302             return sString;
303         }
304     }
305 }

Top

There are no comments yet

Leave a Comment


?
?