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

Technical Indicators » Heiken Ashi

01 // Heiken Ashi 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.Drawing;
09 
10 namespace Forex_Strategy_Builder
11 {
12     /// <summary>
13     /// Heiken Ashi Indicator
14     /// </summary>
15     public class Heiken_Ashi : Indicator
16     {
17         /// <summary>
18         /// Sets the default indicator parameters for the designated slot type
19         /// </summary>
20         public Heiken_Ashi(SlotTypes slotType)
21         {
22             // General properties
23             IndicatorName = "Heiken Ashi";
24             PossibleSlots = SlotTypes.Open | SlotTypes.OpenFilter | SlotTypes.Close | SlotTypes.CloseFilter;
25 
26             // Setting up the indicator parameters
27             IndParam = new IndicatorParam();
28             IndParam.IndicatorName = IndicatorName;
29             IndParam.SlotType      = slotType;
30             IndParam.IndicatorType = TypeOfIndicator.Indicator;
31 
32             // The ComboBox parameters
33             IndParam.ListParam[0].Caption = "Logic";
34             if (slotType == SlotTypes.Open)
35                 IndParam.ListParam[0].ItemList = new string[]
36                 {
37                     "Enter long at the H.A. High",
38                     "Enter long at the H.A. Low",
39                 };
40             else if (slotType == SlotTypes.OpenFilter)
41                 IndParam.ListParam[0].ItemList = new string[]
42                 {
43                     "White H.A. bar without lower shadow",
44                     "White H.A. bar",
45                     "Black H.A. bar",
46                     "Black H.A. bar without upper shadow"
47                 };
48             else if (slotType == SlotTypes.Close)
49                 IndParam.ListParam[0].ItemList = new string[]
50                 {
51                     "Exit long at the H.A. High",
52                     "Exit long at the H.A. Low",
53                 };
54             else if (slotType == SlotTypes.CloseFilter)
55                 IndParam.ListParam[0].ItemList = new string[]
56                 {
57                     "Black H.A. bar without upper shadow",
58                     "Black H.A. bar",
59                     "White H.A. bar",
60                     "White H.A. bar without lower shadow"
61                 };
62             else
63                 IndParam.ListParam[0].ItemList = new string[]
64                 {
65                     "Not Defined"
66                 };
67             IndParam.ListParam[0].Index    = 0;
68             IndParam.ListParam[0].Text     = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index];
69             IndParam.ListParam[0].Enabled  = true;
70             IndParam.ListParam[0].ToolTip  = "Logic of application of the Donchian Channel.";
71 
72             IndParam.ListParam[1].Caption  = "Base price";
73             IndParam.ListParam[1].ItemList = new string[] { "High, Low, Open, Close" };
74             IndParam.ListParam[1].Index    = 0;
75             IndParam.ListParam[1].Text     = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index];
76             IndParam.ListParam[1].Enabled  = true;
77 
78             // The CheckBox parameters
79             IndParam.CheckParam[0].Caption = "Use previous bar value";
80             IndParam.CheckParam[0].Checked = PrepareUsePrevBarValueCheckBox(slotType);
81             IndParam.CheckParam[0].Enabled = true;
82             IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar.";
83 
84             return;
85         }
86 
87         /// <summary>
88         /// Calculates the indicator's components
89         /// </summary>
90         public override void Calculate(SlotTypes slotType)
91         {
92             // Reading the parameters
93             int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0;
94 
95             // Calculation
96             double[] adHAOpen  = new double[Bars];
97             double[] adHAHigh  = new double[Bars];
98             double[] adHALow   = new double[Bars];
99             double[] adHAClose = new double[Bars];
100 
101             adHAOpen[0]  = Open[0];
102             adHAHigh[0]  = High[0];
103             adHALow[0]   = Low[0];
104             adHAClose[0] = Close[0];
105 
106             int iFirstBar = 1 + iPrvs;
107 
108             for (int iBar = 1; iBar < Bars; iBar++)
109             {
110                 adHAClose[iBar] = (Open[iBar] + High[iBar] + Low[iBar] + Close[iBar]) / 4;
111                 adHAOpen[iBar]  = (adHAOpen[iBar - 1] + adHAClose[iBar - 1]) / 2;
112                 adHAHigh[iBar]  = High[iBar] > adHAOpen[iBar] ? High[iBar] : adHAOpen[iBar];
113                 adHAHigh[iBar]  = adHAClose[iBar] > adHAHigh[iBar] ? adHAClose[iBar] : adHAHigh[iBar];
114                 adHALow[iBar]   = Low[iBar] < adHAOpen[iBar] ? Low[iBar] : adHAOpen[iBar];
115                 adHALow[iBar]   = adHAClose[iBar] < adHALow[iBar] ? adHAClose[iBar] : adHALow[iBar];
116             }
117 
118             // Saving the components
119             Component = new IndicatorComp[6];
120 
121             Component[0] = new IndicatorComp();
122             Component[0].CompName   = "H.A. Open";
123             Component[0].DataType   = IndComponentType.IndicatorValue;
124             Component[0].ChartType  = IndChartType.Dot;
125             Component[0].ChartColor = Color.Green;
126             Component[0].FirstBar   = iFirstBar;
127             Component[0].Value      = adHAOpen;
128 
129             Component[1] = new IndicatorComp();
130             Component[1].CompName   = "H.A. High";
131             Component[1].DataType   = IndComponentType.IndicatorValue;
132             Component[1].ChartType  = IndChartType.Dot;
133             Component[1].ChartColor = Color.Blue;
134             Component[1].FirstBar   = iFirstBar;
135             Component[1].Value      = adHAHigh;
136 
137             Component[2] = new IndicatorComp();
138             Component[2].CompName   = "H.A. Low";
139             Component[2].DataType   = IndComponentType.IndicatorValue;
140             Component[2].ChartType  = IndChartType.Dot;
141             Component[2].ChartColor = Color.Blue;
142             Component[2].FirstBar   = iFirstBar;
143             Component[2].Value      = adHALow;
144 
145             Component[3] = new IndicatorComp();
146             Component[3].CompName   = "H.A. Close";
147             Component[3].DataType   = IndComponentType.IndicatorValue;
148             Component[3].ChartType  = IndChartType.Dot;
149             Component[3].ChartColor = Color.Red;
150             Component[3].FirstBar   = iFirstBar;
151             Component[3].Value      = adHAClose;
152 
153             Component[4] = new IndicatorComp();
154             Component[4].ChartType = IndChartType.NoChart;
155             Component[4].FirstBar  = iFirstBar;
156             Component[4].Value     = new double[Bars];
157 
158             Component[5] = new IndicatorComp();
159             Component[5].ChartType = IndChartType.NoChart;
160             Component[5].FirstBar  = iFirstBar;
161             Component[5].Value     = new double[Bars];
162 
163             // Sets the Component's type
164             if (slotType == SlotTypes.Open)
165             {
166                 Component[4].DataType = IndComponentType.OpenLongPrice;
167                 Component[4].CompName = "Long position entry price";
168                 Component[5].DataType = IndComponentType.OpenShortPrice;
169                 Component[5].CompName = "Short position entry price";
170             }
171             else if (slotType == SlotTypes.OpenFilter)
172             {
173                 Component[4].DataType = IndComponentType.AllowOpenLong;
174                 Component[4].CompName = "Is long entry allowed";
175                 Component[5].DataType = IndComponentType.AllowOpenShort;
176                 Component[5].CompName = "Is short entry allowed";
177             }
178             else if (slotType == SlotTypes.Close)
179             {
180                 Component[4].DataType = IndComponentType.CloseLongPrice;
181                 Component[4].CompName = "Long position closing price";
182                 Component[5].DataType = IndComponentType.CloseShortPrice;
183                 Component[5].CompName = "Short position closing price";
184             }
185             else if (slotType == SlotTypes.CloseFilter)
186             {
187                 Component[4].DataType = IndComponentType.ForceCloseLong;
188                 Component[4].CompName = "Close out long position";
189                 Component[5].DataType = IndComponentType.ForceCloseShort;
190                 Component[5].CompName = "Close out short position";
191             }
192 
193             if (slotType == SlotTypes.Open || slotType == SlotTypes.Close)
194             {
195                 for (int iBar = 2; iBar < Bars; iBar++)
196                 {
197                     if (IndParam.ListParam[0].Text == "Enter long at the H.A. High" ||
198                         IndParam.ListParam[0].Text == "Exit long at the H.A. High")
199                     {
200                         Component[4].Value[iBar] = adHAHigh[iBar - iPrvs];
201                         Component[5].Value[iBar] = adHALow[iBar - iPrvs];
202                     }
203                     else
204                     {
205                         Component[4].Value[iBar] = adHALow[iBar - iPrvs];
206                         Component[5].Value[iBar] = adHAHigh[iBar - iPrvs];
207                     }
208                 }
209             }
210             else
211             {
212                 switch (IndParam.ListParam[0].Text)
213                 {
214                     case "White H.A. bar without lower shadow":
215                         for (int iBar = iFirstBar; iBar < Bars; iBar++)
216                         {
217                             Component[4].Value[iBar] = adHAClose[iBar - iPrvs] > adHAOpen[iBar - iPrvs] &&
218                                 adHALow[iBar - iPrvs] == adHAOpen[iBar - iPrvs] ? 1 : 0;
219                             Component[5].Value[iBar] = adHAClose[iBar - iPrvs] < adHAOpen[iBar - iPrvs] &&
220                                 adHAHigh[iBar - iPrvs] == adHAOpen[iBar - iPrvs] ? 1 : 0;
221                         }
222                         break;
223 
224                     case "White H.A. bar":
225                         for (int iBar = iFirstBar; iBar < Bars; iBar++)
226                         {
227                             Component[4].Value[iBar] = adHAClose[iBar - iPrvs] > adHAOpen[iBar - iPrvs] ? 1 : 0;
228                             Component[5].Value[iBar] = adHAClose[iBar - iPrvs] < adHAOpen[iBar - iPrvs] ? 1 : 0;
229                         }
230                         break;
231 
232                     case "Black H.A. bar":
233                         for (int iBar = iFirstBar; iBar < Bars; iBar++)
234                         {
235                             Component[4].Value[iBar] = adHAClose[iBar - iPrvs] < adHAOpen[iBar - iPrvs] ? 1 : 0;
236                             Component[5].Value[iBar] = adHAClose[iBar - iPrvs] > adHAOpen[iBar - iPrvs] ? 1 : 0;
237                         }
238                         break;
239 
240                     case "Black H.A. bar without upper shadow":
241                         for (int iBar = iFirstBar; iBar < Bars; iBar++)
242                         {
243                             Component[4].Value[iBar] = adHAClose[iBar - iPrvs] < adHAOpen[iBar - iPrvs] &&
244                                 adHAHigh[iBar - iPrvs] == adHAOpen[iBar - iPrvs] ? 1 : 0;
245                             Component[5].Value[iBar] = adHAClose[iBar - iPrvs] > adHAOpen[iBar - iPrvs] &&
246                                 adHALow[iBar - iPrvs] == adHAOpen[iBar - iPrvs] ? 1 : 0;
247                         }
248                         break;
249 
250                     default:
251                         break;
252                 }
253             }
254 
255             return;
256         }
257 
258         /// <summary>
259         /// Sets the indicator logic description
260         /// </summary>
261         public override void SetDescription(SlotTypes slotType)
262         {
263             switch (IndParam.ListParam[0].Text)
264             {
265                 case "Enter long at the H.A. High":
266                     EntryPointLongDescription  = "at the " + ToString() + " High";
267                     EntryPointShortDescription = "at the " + ToString() + " Low";
268                     break;
269 
270                 case "Enter long at the H.A. Low":
271                     EntryPointLongDescription  = "at the " + ToString() + " Low";
272                     EntryPointShortDescription = "at the " + ToString() + " High";
273                     break;
274 
275                 case "Exit long at the H.A. High":
276                     ExitPointLongDescription  = "at the " + ToString() + " High";
277                     ExitPointShortDescription = "at the " + ToString() + " Low";
278                     break;
279 
280                 case "Exit long at the H.A. Low":
281                     ExitPointLongDescription  = "at the " + ToString() + " Low";
282                     ExitPointShortDescription = "at the " + ToString() + " High";
283                     break;
284 
285                 case "White H.A. bar without lower shadow":
286                     EntryFilterLongDescription  = "the " + ToString() + " bar is white and without lower shadow";
287                     EntryFilterShortDescription = "the " + ToString() + " bar is black and without upper shadow";
288                     ExitFilterLongDescription   = "the " + ToString() + " bar is white and without lower shadow";
289                     ExitFilterShortDescription  = "the " + ToString() + " bar is black and without upper shadow";
290                     break;
291 
292                 case "Black H.A. bar without upper shadow":
293                     EntryFilterLongDescription  = "the " + ToString() + " bar is black and without upper shadow";
294                     EntryFilterShortDescription = "the " + ToString() + " bar is white and without lower shadow";
295                     ExitFilterLongDescription   = "the " + ToString() + " bar is black and without upper shadow";
296                     ExitFilterShortDescription  = "the " + ToString() + " bar is white and without lower shadow";
297                     break;
298 
299                 case "White H.A. bar":
300                     EntryFilterLongDescription  = "the " + ToString() + " bar is white";
301                     EntryFilterShortDescription = "the " + ToString() + " bar is black";
302                     ExitFilterLongDescription   = "the " + ToString() + " bar is white";
303                     ExitFilterShortDescription  = "the " + ToString() + " bar is black";
304                     break;
305 
306                 case "Black H.A. bar":
307                     EntryFilterLongDescription  = "the " + ToString() + " bar is black";
308                     EntryFilterShortDescription = "the " + ToString() + " bar is white";
309                     ExitFilterLongDescription   = "the " + ToString() + " bar is black";
310                     ExitFilterShortDescription  = "the " + ToString() + " bar is white";
311                     break;
312 
313                 default:
314                     break;
315             }
316 
317             return;
318         }
319 
320         /// <summary>
321         /// Indicator to string
322         /// </summary>
323         public override string ToString()
324         {
325             string sString = IndicatorName + (IndParam.CheckParam[0].Checked ? "*" : "");
326 
327             return sString;
328         }
329     }
330 }

Top

There are no comments yet

Leave a Comment


?
?