Bez popisu

MK_BOS_CHOCH.mq5 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //+------------------------------------------------------------------+
  2. //| MK_BOS_CHOCH.mq5 |
  3. //| Copyright 2023, MetaQuotes Ltd. |
  4. //| https://www.mql5.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2023, MetaQuotes Ltd."
  7. #property link "https://www.mql5.com"
  8. #property version "1.00"
  9. sinput string str = "<><><><><><> Object Name <><><><><><>";
  10. input string object_name = "line";
  11. string trend;
  12. bool bearish_found = false;
  13. bool bullish_found = false;
  14. int index_of_low = 0;
  15. int high_index=0;
  16. double high,low,low1,high1;
  17. //+------------------------------------------------------------------+
  18. //| Expert initialization function |
  19. //+------------------------------------------------------------------+
  20. void foo(int index)
  21. {
  22. for(int i=index; i > 0; i--)
  23. {
  24. if(checkCandle(index) == "Bullish")
  25. {
  26. trend = "uptrend";
  27. //double high,low = 0.0;
  28. // to store high and low of the value after the object
  29. high = iHigh(Symbol(),PERIOD_CURRENT,index);
  30. //low = iLow(Symbol(),PERIOD_CURRENT,index-1);
  31. if(trend == "uptrend")
  32. {
  33. high1 = iHigh(Symbol(),PERIOD_CURRENT,i);
  34. //double low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  35. if(high1 > high)
  36. {
  37. high = high1;
  38. // Print("high break at index value",i);
  39. // Print("index is ",index);
  40. int count = (index-1) - i;
  41. Print("count is ", count);
  42. // Print("index updated ",index);
  43. // Print("count",count);
  44. for(int j=1; j <= count; j++)
  45. {
  46. if(checkCandle(index-j) == "Bearish")
  47. {
  48. low = iLow(Symbol(),PERIOD_CURRENT,index-j);
  49. bearish_found = true;
  50. }
  51. }
  52. if(bearish_found == true)
  53. {
  54. //double low1 ;
  55. //Print("where");
  56. for(int k=1; k <= count; k++)
  57. {
  58. low1 = iLow(Symbol(),PERIOD_CURRENT,index - k);
  59. if(low1 <= low)
  60. {
  61. //Print("here");
  62. low = low1;
  63. index_of_low = index - k;
  64. Print("low is updated at index : ",index - k);
  65. }
  66. }
  67. bearish_found = false;
  68. }
  69. Print(" high price is ", high);
  70. Print("low price is ", low);
  71. Print("index of low is ", index_of_low);
  72. for(int l=index_of_low; l > 0; l--)
  73. {
  74. // print("hi");
  75. double close = iClose(Symbol(),PERIOD_CURRENT,l);
  76. if(close < low)
  77. {
  78. double price_low = iLow(Symbol(),PERIOD_CURRENT,l+1);
  79. double time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  80. double time1 = iTime(Symbol(),PERIOD_CURRENT,l+1);
  81. ObjectCreate(0,"tline1",OBJ_TREND,0,time,price_low,time1,price_low);
  82. trend = "downtrend";
  83. Print("choch");
  84. Print("close", close);
  85. break;
  86. }
  87. }
  88. index = i;
  89. }
  90. }
  91. //
  92. }
  93. if(checkCandle(index) == "Bearish")
  94. {
  95. trend = "downtrend";
  96. // double high,low = 0.0;
  97. // to store high and low of the value after the object
  98. low = iLow(Symbol(),PERIOD_CURRENT,index);
  99. //low = iLow(Symbol(),PERIOD_CURRENT,index-1);
  100. if(trend == "downtrend")
  101. {
  102. low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  103. //double low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  104. if(low1 < low)
  105. {
  106. low = low1;
  107. // Print("high break at index value",i);
  108. // Print("index is ",index);
  109. int count = (index-1) - i;
  110. Print("count is ", count);
  111. // Print("index updated ",index);
  112. // Print("count",count);
  113. for(int j=1; j <= count; j++)
  114. {
  115. if(checkCandle(index-j) == "Bullish")
  116. {
  117. high = iHigh(Symbol(),PERIOD_CURRENT,index-j);
  118. bullish_found = true;
  119. }
  120. }
  121. if(bullish_found == true)
  122. {
  123. high1;
  124. //Print("where");
  125. for(int k=1; k <= count; k++)
  126. {
  127. high1 = iHigh(Symbol(),PERIOD_CURRENT,index - k);
  128. if(high1 > high)
  129. {
  130. //Print("here");
  131. high = high1;
  132. index_of_low = index - k;
  133. Print("high is updated at index : ",index - k);
  134. }
  135. }
  136. bullish_found = false;
  137. }
  138. Print(" high price is ", high);
  139. Print("low price is ", low);
  140. Print("index of low is ", index_of_low);
  141. for(int l=index_of_low; l > 0; l--)
  142. {
  143. // print("hi");
  144. double close = iClose(Symbol(),PERIOD_CURRENT,l);
  145. if(close > high)
  146. {
  147. double price_high = iLow(Symbol(),PERIOD_CURRENT,l+1);
  148. double time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  149. double time1 = iTime(Symbol(),PERIOD_CURRENT,l+1);
  150. ObjectCreate(0,"tline2",OBJ_TREND,0,time,price_high,time1,price_high);
  151. trend = "uptrend";
  152. Print("choch");
  153. Print(l);
  154. Print("close", close);
  155. break;
  156. }
  157. }
  158. index = i;
  159. }
  160. }
  161. //
  162. }
  163. }
  164. }
  165. //+------------------------------------------------------------------+
  166. //| |
  167. //+------------------------------------------------------------------+
  168. int OnInit()
  169. {
  170. ObjectCreate(0,"line",OBJ_VLINE,0,D'2023.07.01 10:00:27',0);
  171. object_find(object_name);
  172. //Print(ObjectFind(0,"line")); // to find the object drawn on the chart
  173. if(object_find(object_name) == true)
  174. {
  175. datetime time_of_candle = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  176. Print("line time: ",time_of_candle);
  177. int index = iBarShift(Symbol(), PERIOD_CURRENT, time_of_candle, false);
  178. Print(index);
  179. // datetime time_of_candle = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  180. // Print("time is " , time_of_candle);
  181. //
  182. // int index = iBarShift(0, PERIOD_CURRENT,time_of_candle, true);
  183. // Print("index is " , index);
  184. foo(index);
  185. }
  186. else
  187. {
  188. Print("Object Not Found");
  189. }
  190. return(INIT_SUCCEEDED);
  191. }
  192. //+------------------------------------------------------------------+
  193. //| Expert deinitialization function |
  194. //+------------------------------------------------------------------+
  195. void OnDeinit(const int reason)
  196. {
  197. //---
  198. }
  199. //+------------------------------------------------------------------+
  200. //| Expert tick function |
  201. //+------------------------------------------------------------------+
  202. void OnTick()
  203. {
  204. if(NewBar())
  205. {
  206. ObjectSetInteger(0,"tline1",OBJPROP_TIME,iTime(Symbol(), PERIOD_CURRENT,0));
  207. ObjectSetInteger(0,"tline2",OBJPROP_TIME,iTime(Symbol(), PERIOD_CURRENT,0));
  208. }
  209. }
  210. //+------------------------------------------------------------------+
  211. //| |
  212. //+------------------------------------------------------------------+
  213. bool NewBar()
  214. {
  215. static datetime Last_bar ;
  216. datetime Current_bar = iTime(Symbol(), PERIOD_CURRENT,0);
  217. if(Current_bar != Last_bar)
  218. {
  219. Last_bar = Current_bar;
  220. return (true);
  221. }
  222. else
  223. {
  224. return (false);
  225. }
  226. }
  227. //+------------------------------------------------------------------+
  228. //| |
  229. //+------------------------------------------------------------------+
  230. bool object_find(string obj) // to find object placed by user in the chart
  231. {
  232. if(ObjectFind(0,obj) >= 0)
  233. {
  234. return true;
  235. }
  236. return false;
  237. }
  238. //+------------------------------------------------------------------+
  239. //| |
  240. //+------------------------------------------------------------------+
  241. string checkCandle(int i) // to check the candle is bullish or bearish
  242. {
  243. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  244. double open = iOpen(Symbol(),PERIOD_CURRENT,i);
  245. if(close > open)
  246. {
  247. return "Bullish";
  248. }
  249. else
  250. if(close < open)
  251. {
  252. return "Bearish";
  253. }
  254. return "empty";
  255. }
  256. //+------------------------------------------------------------------+
  257. //+------------------------------------------------------------------+