Merge branch 'master' of git://repo.or.cz/mqlkit
[mqlkit.git] / indicators / Market_Outlook.mq4
blob0f7d2a65ced985d6248a809ba63d17992941b89d
1 //+------------------------------------------------------------------+\r
2 //|                                               Market_Outlook.mq4 |\r
3 //|                                   Copyright © 2011, Zarko Asenov |\r
4 //|                                              http://jaltoh.6x.to |\r
5 //+------------------------------------------------------------------+\r
6 #property copyright "Copyright © 2011, Zarko Asenov"\r
7 #property link      "http://jaltoh.6x.to"\r
8 \r
9 #property indicator_chart_window\r
10 #property indicator_buffers 6\r
11 #property indicator_color1 Goldenrod\r
12 #property indicator_color2 Goldenrod\r
13 #property indicator_color3 Lime\r
14 #property indicator_color4 Lime\r
15 #property indicator_color5 MediumSlateBlue\r
16 #property indicator_color6 DarkOrange\r
17 #property indicator_width1 3\r
18 #property indicator_width2 3\r
19 #property indicator_width3 4\r
20 #property indicator_width4 4\r
21 #property indicator_width5 5\r
22 #property indicator_width6 5\r
23 #property indicator_style1 STYLE_SOLID\r
24 #property indicator_style2 STYLE_SOLID\r
25 #property indicator_style3 STYLE_SOLID\r
26 #property indicator_style4 STYLE_SOLID\r
27 #property indicator_style5 STYLE_SOLID\r
28 #property indicator_style6 STYLE_SOLID\r
31 //--- input parameters\r
32 extern int       Maximum_Resolution = PERIOD_M1;\r
33 <<<<<<< HEAD
34 extern int       Time_Frame = PERIOD_H1;\r
35 =======
36 extern int       Time_Frame = 0;\r
37 >>>>>>> d1f8eca3054a866379156d71012f6feb4bddbde9
38 extern int       Bars_Computed = 10;\r
39 extern double    Time_Weight = 2.0;\r
40 extern bool      Rough_Estimates = false;\r
41 extern string    _ = ""; // separator\r
42 extern int       Price_Mode = PRICE_CLOSE;\r
43 extern int       Hint_Price_Close = PRICE_CLOSE;\r
44 extern int       Hint_Price_High = PRICE_HIGH;\r
45 extern int       Hint_Price_Low = PRICE_LOW;\r
46 extern int       Hint_Price_Median = PRICE_MEDIAN;\r
47 extern int       Hint_Price_Typical = PRICE_TYPICAL;\r
48 extern int       Hint_Price_Weighted = PRICE_WEIGHTED;\r
51 int max_resolution_secs;\r
52 int sub_periods_count;\r
53 double stop_level;\r
54 double time_factor;\r
55 double display_time_ratio;\r
56 int time_frame;\r
58 double avg_hi[];\r
59 double avg_lo[];\r
60 double avg_hi_weighted[];\r
61 double avg_lo_weighted[];\r
62 double avg_price[];\r
63 double avg_weighted[];\r
65 //+------------------------------------------------------------------+\r
66 //| Custom indicator initialization function                         |\r
67 //+------------------------------------------------------------------+\r
68 int init()\r
69   {\r
70 //---- indicators\r
71 //----\r
72      if (Time_Frame == 0) time_frame = Period();\r
73      else time_frame = Time_Frame;\r
74      \r
75      if (Maximum_Resolution == 0) Maximum_Resolution = PERIOD_M1;\r
76      \r
77      if (Time_Weight == 0.0) time_factor = 1.0 + 1.0 * Maximum_Resolution / time_frame;\r
78      else time_factor = Time_Weight;\r
79      \r
80      max_resolution_secs = Maximum_Resolution * 60;\r
81      sub_periods_count = time_frame / Maximum_Resolution;\r
83      stop_level = MarketInfo(Symbol(), MODE_STOPLEVEL);\r
84      display_time_ratio = 1.0 * time_frame / Period();\r
85      \r
86      SetIndexBuffer(0, avg_hi);\r
87      SetIndexBuffer(1, avg_lo);\r
88      SetIndexBuffer(2, avg_hi_weighted);\r
89      SetIndexBuffer(3, avg_lo_weighted);\r
90      SetIndexBuffer(4, avg_price);\r
91      SetIndexBuffer(5, avg_weighted);\r
92      \r
93      SetIndexStyle(0, DRAW_HISTOGRAM);\r
94      SetIndexStyle(1, DRAW_HISTOGRAM);\r
95      SetIndexStyle(2, DRAW_HISTOGRAM);\r
96      SetIndexStyle(3, DRAW_HISTOGRAM);\r
97      SetIndexStyle(4, DRAW_HISTOGRAM);\r
98      SetIndexStyle(5, DRAW_HISTOGRAM);\r
99      \r
100      SetIndexLabel(0, "Average High Price");\r
101      SetIndexLabel(1, "Average Low Price");\r
102      SetIndexLabel(2, "Average Weighted High Price");\r
103      SetIndexLabel(3, "Average Weighted Low Price");\r
104      SetIndexLabel(4, "Average Price");\r
105      SetIndexLabel(5, "Average Weighted Price");\r
106      \r
107      return(0);\r
108   }\r
109 //+------------------------------------------------------------------+\r
110 //| Custom indicator deinitialization function                       |\r
111 //+------------------------------------------------------------------+\r
112 int deinit()\r
113   {\r
114 //----\r
115      Comment("");\r
116 //----\r
117      return(0);\r
118   }\r
120 double\r
121 get_price(\r
122      int price_mode,\r
123      double close,\r
124      double hi,\r
125      double lo)\r
127      double price;\r
128      \r
129      switch (price_mode) {\r
130           case PRICE_CLOSE:\r
131                price = close;\r
132                break;\r
133           case PRICE_HIGH:\r
134                price = hi;\r
135                break;\r
136           case PRICE_LOW:\r
137                price = lo;\r
138                break;\r
139           case PRICE_MEDIAN:\r
140                price = (hi + lo) / 2.0;\r
141                break;\r
142           case PRICE_TYPICAL:\r
143                price = (hi + lo + close) / 3.0;\r
144                break;\r
145           case PRICE_WEIGHTED:\r
146                price = (hi + lo + close + close) / 4.0;\r
147                break;\r
148           default:\r
149                price = 0.0;\r
150                Print("Invalid price mode: " + Price_Mode);\r
151      }\r
152      \r
153      return (price);\r
155   \r
156 bool\r
157 get_weighted_hilo(\r
158      datetime start_time, \r
159      datetime end_time,\r
160      double &high,\r
161      double &low,\r
162      double &high_weight,\r
163      double &low_weight,\r
164      double &flat,\r
165      double &weighted)\r
167      int sub_period_start;\r
168      int sub_period_end;\r
169     \r
170      sub_period_start = iBarShift(Symbol(), Maximum_Resolution, start_time);\r
171      sub_period_end = iBarShift(Symbol(), Maximum_Resolution, end_time);\r
173      int pos_ctr = 0;\r
174      for (int ix = sub_period_start; ix >= sub_period_end; ix--) {\r
175      \r
176           double sub_high = iHigh(Symbol(), Maximum_Resolution, ix);\r
177           double sub_low = iLow(Symbol(), Maximum_Resolution, ix);\r
178           double sub_close = iClose(Symbol(), Maximum_Resolution, ix);\r
179      \r
180           high = (pos_ctr * high + sub_high) / (pos_ctr + 1.0);\r
181           low = (pos_ctr * low + sub_low) / (pos_ctr + 1.0);\r
182           high_weight = (pos_ctr * high_weight + time_factor * sub_high) / (pos_ctr + time_factor);\r
183           low_weight = (pos_ctr * low_weight + time_factor * sub_low) / (pos_ctr + time_factor);\r
184           \r
185           double price = get_price(\r
186                               Price_Mode, \r
187                               sub_close,\r
188                               sub_high,\r
189                               sub_low);\r
190                \r
191           weighted = (pos_ctr * weighted + time_factor * price) / (pos_ctr + time_factor);\r
192           flat = (pos_ctr * flat + price) / (pos_ctr + 1.0);\r
193           \r
194           pos_ctr++;\r
195      }\r
198 string\r
199 get_diff_string(\r
200      double price1,\r
201      double price2)\r
203      return (\r
204           " ( " + DoubleToStr( (price1 - price2) / Point, 0) + " ) "\r
205           );\r
208 //+------------------------------------------------------------------+\r
209 //| Custom indicator iteration function                              |\r
210 //+------------------------------------------------------------------+\r
211 int \r
212 start()\r
213   {\r
214      int draw_bars  = Bars - IndicatorCounted() - 1;\r
215      if (draw_bars < 0) return (0);\r
216      \r
217 //----\r
219      int compute_start_bar;\r
220      if (draw_bars == 0) compute_start_bar = 0;\r
221      else compute_start_bar = draw_bars / display_time_ratio;\r
222           \r
223      for (int ix = compute_start_bar; ix >= 0; ix--) {\r
224           if (ix < Bars_Computed) {\r
225                double current_avg_hi;\r
226                double current_avg_lo;\r
227                double current_avg_hi_weigh;\r
228                double current_avg_lo_weigh;\r
229                double current_avg_flat;\r
230                double current_avg_weighted;\r
231                \r
232                datetime open_time = iTime(Symbol(), time_frame, ix);\r
233                datetime close_time;\r
234                \r
235                if (ix > 0) \r
236                     close_time = iTime(Symbol(), time_frame, ix - 1) - max_resolution_secs;\r
237                else \r
238                     close_time = TimeCurrent();\r
239           \r
240                get_weighted_hilo(\r
241                     open_time, \r
242                     close_time, \r
243                     current_avg_hi, \r
244                     current_avg_lo, \r
245                     current_avg_hi_weigh, \r
246                     current_avg_lo_weigh, \r
247                     current_avg_flat,\r
248                     current_avg_weighted);\r
250           } else if (Rough_Estimates == true) {\r
252                // Fake averages\r
253                current_avg_hi = High[ix];\r
254                current_avg_lo = Low[ix];\r
255                current_avg_hi_weigh = (High[ix] + High[ix] + High[ix + 1]) / 3.0;\r
256                current_avg_lo_weigh = (Low[ix] + Low[ix] + Low[ix + 1]) / 3.0;\r
257                current_avg_weighted = get_price(PRICE_WEIGHTED, Close[ix], avg_hi_weighted[ix], avg_lo_weighted[ix]);\r
258                current_avg_flat = get_price(PRICE_MEDIAN, Close[ix], avg_hi[ix], avg_lo[ix]);\r
259           } else {\r
260                continue;\r
261           }\r
262           \r
263           for (int display_ix = ix * display_time_ratio; \r
264                display_ix < (ix + 1) * display_time_ratio; \r
265                display_ix++) {\r
266                avg_hi[display_ix] = current_avg_hi;\r
267                avg_lo[display_ix] = current_avg_lo;\r
268                avg_hi_weighted[display_ix] = current_avg_hi_weigh;\r
269                avg_lo_weighted[display_ix] = current_avg_lo_weigh;\r
270                avg_weighted[display_ix] = current_avg_flat;\r
271                avg_price[display_ix] = current_avg_weighted;\r
272           }\r
274      }\r
276      double spread = MarketInfo(Symbol(), MODE_SPREAD);\r
277      double mid_price = (High[0] + Low[0]) / 2.0;\r
278      double typical_price = (High[0] + Low[0] + Close[0]) / 3.0;\r
279      double weighted_price = (High[0] + Low[0] + Close[0] + Close[0]) / 4.0;\r
281      double minutes_left = ( time_frame * 60.0 - (TimeCurrent() - iTime(Symbol(), time_frame, 0)) ) / 60.0;\r
283      ix = 0;\r
285      Comment( \r
286           "Symbol: " + Symbol() + "   " +\r
287           "Calculated period: " + time_frame + " min\n" +\r
288           "Spread: " + DoubleToStr(spread, 0) + "  " +\r
289           "Min Stop: " + DoubleToStr(stop_level, 0) + "\n" +\r
290           "Bar in T-" + DoubleToStr(minutes_left, 2) + " min\n" +\r
291           "Time weight: " + DoubleToStr(time_factor, 2) + "  " +\r
292           "Subperiod: " + Maximum_Resolution + " min\n\n" +\r
294           "Midprice HL: " + DoubleToStr(mid_price, Digits) + get_diff_string(Close[0], mid_price) + "\n" +\r
295           "Typical HLC: " + DoubleToStr(typical_price, Digits) + get_diff_string(Close[0], typical_price) + "\n" +\r
296           "Weighted HLCC: " + DoubleToStr(weighted_price, Digits) + get_diff_string(Close[0], weighted_price) + "\n\n" +\r
297            \r
298           "Average Price: " + DoubleToStr(avg_price[ix], Digits) + get_diff_string(Close[0], avg_price[ix]) + "\n" +\r
299           "Average Weighted: " + DoubleToStr(avg_weighted[ix], Digits) + get_diff_string(Close[0], avg_weighted[ix]) + "\n" +\r
300           "Average High: " + DoubleToStr(avg_hi[ix], Digits) + get_diff_string(Close[0], avg_hi[ix]) + "\n" +\r
301           "Average Low: " + DoubleToStr(avg_lo[ix], Digits) + get_diff_string(Close[0], avg_lo[ix]) + "\n\n"\r
302           );\r
303 //----\r
304      return(0);\r
305   }\r
306 //+------------------------------------------------------------------+