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

Technical Indicators » Bar Range

01 // Bar Range Indicator
02 // Last changed on 2009-05-11
03 // Part of Forex Strategy Builder v2.8.3.8+
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 
10 namespace Forex_Strategy_Builder
11 {
12     /// <summary>
13     /// Bar Range Indicator
14     /// </summary>
15     public class Bar_Range : Indicator
16     {
17         /// <summary>
18         /// Sets the default indicator parameters for the designated slot type
19         /// </summary>
20         public Bar_Range(SlotTypes slotType)
21         {
22             // General properties
23             IndicatorName  = "Bar Range";
24             PossibleSlots  = SlotTypes.OpenFilter | SlotTypes.CloseFilter;
25             SeparatedChart = true;
26             SeparatedChartMinValue = 0;
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 Bar Range rises",
38                 "The Bar Range falls",
39                 "The Bar Range is higher than the Level line",
40                 "The Bar Range is lower than the Level line"
41             };
42             IndParam.ListParam[0].Index   = 0;
43             IndParam.ListParam[0].Text    = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index];
44             IndParam.ListParam[0].Enabled = true;
45             IndParam.ListParam[0].ToolTip = "Logic of application of the indicator.";
46 
47             // The NumericUpDown parameters
48             IndParam.NumParam[0].Caption = "Number of bars";
49             IndParam.NumParam[0].Value   = 1;
50             IndParam.NumParam[0].Min     = 1;
51             IndParam.NumParam[0].Max     = 200;
52             IndParam.NumParam[0].Enabled = true;
53             IndParam.NumParam[0].ToolTip = "The number of bars to calculate the range.";
54 
55             IndParam.NumParam[1].Caption = "Level";
56             IndParam.NumParam[1].Value   = 0;
57             IndParam.NumParam[1].Min     = 0;
58             IndParam.NumParam[1].Max     = 5000;
59             IndParam.NumParam[1].Point   = 0;
60             IndParam.NumParam[1].Enabled = true;
61             IndParam.NumParam[1].ToolTip = "A critical level (for the appropriate logic).";
62 
63             // The CheckBox parameters
64             IndParam.CheckParam[0].Caption = "Use previous bar value";
65             IndParam.CheckParam[0].Checked = PrepareUsePrevBarValueCheckBox(slotType);
66             IndParam.CheckParam[0].Enabled = true;
67             IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar.";
68 
69             return;
70         }
71 
72         /// <summary>
73         /// Calculates the indicator's components
74         /// </summary>
75         public override void Calculate(SlotTypes slotType)
76         {
77             // Reading the parameters
78             int    nBars  = (int)IndParam.NumParam[0].Value;
79             double dLevel = IndParam.NumParam[1].Value;
80             int    iPrvs  = IndParam.CheckParam[0].Checked ? 1 : 0;
81 
82             // Calculation
83             int iFirstBar = nBars + 1;
84 
85             double[] adRange = new double[Bars];
86 
87             for (int iBar = iFirstBar; iBar < Bars; iBar++)
88             {
89                 double maxHigh = double.MinValue;
90                 double minLow  = double.MaxValue;
91                 for (int i = 0; i < nBars; i++)
92                 {
93                     if (High[iBar - i] > maxHigh)
94                         maxHigh = High[iBar - i];
95                     if (Low[iBar - i] < minLow)
96                         minLow = Low[iBar - i];
97                 }
98                 adRange[iBar] = maxHigh - minLow;
99             }
100 
101             // Saving the components
102             Component = new IndicatorComp[3];
103 
104             Component[0] = new IndicatorComp();
105             Component[0].CompName  = "Bar Range";
106             Component[0].DataType  = IndComponentType.IndicatorValue;
107             Component[0].ChartType = IndChartType.Histogram;
108             Component[0].FirstBar  = iFirstBar;
109             Component[0].Value     = new double[Bars];
110             for (int i = 0; i < Bars; i++)
111                 Component[0].Value[i] = (double)Math.Round(adRange[i] / Point);
112 
113             Component[1] = new IndicatorComp();
114             Component[1].ChartType = IndChartType.NoChart;
115             Component[1].FirstBar  = iFirstBar;
116             Component[1].Value     = new double[Bars];
117 
118             Component[2] = new IndicatorComp();
119             Component[2].ChartType = IndChartType.NoChart;
120             Component[2].FirstBar  = iFirstBar;
121             Component[2].Value     = new double[Bars];
122 
123             // Sets the Component's type
124             if (slotType == SlotTypes.OpenFilter)
125             {
126                 Component[1].DataType = IndComponentType.AllowOpenLong;
127                 Component[1].CompName = "Is long entry allowed";
128                 Component[2].DataType = IndComponentType.AllowOpenShort;
129                 Component[2].CompName = "Is short entry allowed";
130             }
131             else if (slotType == SlotTypes.CloseFilter)
132             {
133                 Component[1].DataType = IndComponentType.ForceCloseLong;
134                 Component[1].CompName = "Close out long position";
135                 Component[2].DataType = IndComponentType.ForceCloseShort;
136                 Component[2].CompName = "Close out short position";
137             }
138 
139             // Calculation of the logic
140             IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter;
141 
142             switch (IndParam.ListParam[0].Text)
143             {
144                 case "The Bar Range rises":
145                     indLogic = IndicatorLogic.The_indicator_rises;
146                     break;
147 
148                 case "The Bar Range falls":
149                     indLogic = IndicatorLogic.The_indicator_falls;
150                     break;
151 
152                 case "The Bar Range is higher than the Level line":
153                     indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
154                     SpecialValues = new double[1] { dLevel };
155                     break;
156 
157                 case "The Bar Range is lower than the Level line":
158                     indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
159                     SpecialValues = new double[1] { dLevel };
160                     break;
161 
162                 default:
163                     break;
164             }
165 
166             NoDirectionOscillatorLogic(iFirstBar, iPrvs, adRange, dLevel * Point, ref Component[1], indLogic);
167             Component[2].Value = Component[1].Value;
168 
169             return;
170         }
171 
172         /// <summary>
173         /// Sets the indicator logic description
174         /// </summary>
175         public override void SetDescription(SlotTypes slotType)
176         {
177             int    nBars       = (int) IndParam.NumParam[0].Value;
178             string sLevelLong  = IndParam.NumParam[1].ValueToString;
179             string sLevelShort = sLevelLong;
180 
181             if (nBars == 1)
182             {
183                 EntryFilterLongDescription  = "the range of the bar ";
184                 EntryFilterShortDescription = "the range of the bar ";
185                 ExitFilterLongDescription   = "the range of the bar ";
186                 ExitFilterShortDescription  = "the range of the bar ";
187             }
188             else
189             {
190                 EntryFilterLongDescription  = "the range of the last " + nBars.ToString() + " bars ";
191                 EntryFilterShortDescription = "the range of the last " + nBars.ToString() + " bars ";
192                 ExitFilterLongDescription   = "the range of the last " + nBars.ToString() + " bars ";
193                 ExitFilterShortDescription  = "the range of the last " + nBars.ToString() + " bars ";
194             }
195             switch (IndParam.ListParam[0].Text)
196             {
197                 case "The Bar Range rises":
198                     EntryFilterLongDescription  += "rises";
199                     EntryFilterShortDescription += "rises";
200                     ExitFilterLongDescription   += "falls";
201                     ExitFilterShortDescription  += "falls";
202                     break;
203 
204                 case "The Bar Range falls":
205                     EntryFilterLongDescription  += "falls";
206                     EntryFilterShortDescription += "falls";
207                     ExitFilterLongDescription   += "rises";
208                     ExitFilterShortDescription  += "rises";
209                     break;
210 
211                 case "The Bar Range is higher than the Level line":
212                     EntryFilterLongDescription  += "is higher than " + sLevelLong  + " pips";
213                     EntryFilterShortDescription += "is higher than " + sLevelShort + " pips";
214                     ExitFilterLongDescription   += "is higher than " + sLevelLong  + " pips";
215                     ExitFilterShortDescription  += "is higher than " + sLevelShort + " pips";
216                     break;
217 
218                 case "The Bar Range is lower than the Level line":
219                     EntryFilterLongDescription  += "is lower than " + sLevelLong  + " pips";
220                     EntryFilterShortDescription += "is lower than " + sLevelShort + " pips";
221                     ExitFilterLongDescription   += "is lower than " + sLevelLong  + " pips";
222                     ExitFilterShortDescription  += "is lower than " + sLevelShort + " pips";
223                     break;
224 
225                 default:
226                     break;
227             }
228 
229             return;
230         }
231 
232         /// <summary>
233         /// Indicator to string
234         /// </summary>
235         public override string ToString()
236         {
237             string sString = IndicatorName +
238                 (IndParam.CheckParam[0].Checked ? "* (" : " (") +
239                 IndParam.NumParam[0].ValueToString + ", " + // Number of bars
240                 IndParam.NumParam[1].ValueToString + ")";   // Level
241 
242             return sString;
243         }
244     }
245 }

Top

There are no comments yet

Leave a Comment


?
?