Нема описа

MK_BOS_CHOCH.mq5 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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_last_high = 0;
  15. int index_of_last_low = 0;
  16. double hi;
  17. double lo;
  18. int index_of_low = 0;
  19. int index_of_high = 0;
  20. int high_index=0;
  21. double high,low,low1,high1;
  22. //+------------------------------------------------------------------+
  23. //| Expert initialization function |
  24. //+------------------------------------------------------------------+
  25. void foo(int index)
  26. {
  27. for(int i=index; i > 0; i--)
  28. {
  29. if(checkCandle(index) == "Bullish")
  30. {
  31. trend = "uptrend";
  32. //double high,low = 0.0;
  33. // to store high and low of the value after the object
  34. high = iHigh(Symbol(),PERIOD_CURRENT,index);
  35. //low = iLow(Symbol(),PERIOD_CURRENT,index-1);
  36. if(trend == "uptrend")
  37. {
  38. high1 = iHigh(Symbol(),PERIOD_CURRENT,i);
  39. //double low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  40. if(high1 > high)
  41. {
  42. high = high1;
  43. // Print("high break at index value",i);
  44. // Print("index is ",index);
  45. int count = (index-1) - i;
  46. Print("index is : : ", i);
  47. // Print("index updated ",index);
  48. // Print("count",count);
  49. for(int j=1; j <= count; j++)
  50. {
  51. if(checkCandle(index-j) == "Bearish")
  52. {
  53. low = iLow(Symbol(),PERIOD_CURRENT,index-j);
  54. bearish_found = true;
  55. }
  56. }
  57. if(bearish_found == true)
  58. {
  59. //double low1 ;
  60. //Print("where");
  61. for(int k=1; k <= count; k++)
  62. {
  63. low1 = iLow(Symbol(),PERIOD_CURRENT,index - k);
  64. if(low1 <= low)
  65. {
  66. //Print("here");
  67. low = low1;
  68. index_of_low = index - k;
  69. Print("low is updated at index : ",index - k);
  70. }
  71. }
  72. bearish_found = false;
  73. }
  74. Print(" high price is ", high);
  75. Print("low price is ", low);
  76. Print("index of last updated low is ", index_of_low);
  77. for(int l=index_of_low; l > 0; l--)
  78. {
  79. // print("hi");
  80. double close = iClose(Symbol(),PERIOD_CURRENT,l);
  81. if(close < low)
  82. {
  83. double price_low = iLow(Symbol(),PERIOD_CURRENT,l+1);
  84. double time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  85. double time1 = iTime(Symbol(),PERIOD_CURRENT,l-1);
  86. ObjectCreate(0,"tline1",OBJ_TREND,0,time,price_low,time1,price_low);
  87. trend = "downtrend";
  88. Print("choch");
  89. index_of_last_high = i;
  90. Print("close", close);
  91. break;
  92. }
  93. }
  94. Print("index of last updated high is : ",index_of_last_high);
  95. hi = iHigh(Symbol(),PERIOD_CURRENT,index_of_last_high+1);
  96. lo = iLow(Symbol(),PERIOD_CURRENT,index_of_last_high+1);
  97. Print("hy",hi, "index is : ",index_of_last_high+1);
  98. Print("ho",lo);
  99. index = i;
  100. }
  101. }
  102. //
  103. }
  104. if(checkCandle(index) == "Bearish")
  105. {
  106. trend = "downtrend";
  107. // double high,low = 0.0;
  108. // to store high and low of the value after the object
  109. low = iLow(Symbol(),PERIOD_CURRENT,index);
  110. //low = iLow(Symbol(),PERIOD_CURRENT,index-1);
  111. if(trend == "downtrend")
  112. {
  113. low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  114. //double low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  115. if(low1 < low)
  116. {
  117. low = low1;
  118. // Print("high break at index value",i);
  119. // Print("index is ",index);
  120. int count = (index-1) - i;
  121. Print("count is ", count);
  122. // Print("index updated ",index);
  123. // Print("count",count);
  124. for(int j=1; j <= count; j++)
  125. {
  126. if(checkCandle(index-j) == "Bullish")
  127. {
  128. high = iHigh(Symbol(),PERIOD_CURRENT,index-j);
  129. bullish_found = true;
  130. }
  131. }
  132. if(bullish_found == true)
  133. {
  134. high1;
  135. //Print("where");
  136. for(int k=1; k <= count; k++)
  137. {
  138. high1 = iHigh(Symbol(),PERIOD_CURRENT,index - k);
  139. if(high1 > high)
  140. {
  141. //Print("here");
  142. high = high1;
  143. index_of_high = index - k;
  144. Print("high is updated at index : ",index - k);
  145. }
  146. }
  147. bullish_found = false;
  148. }
  149. Print(" high price is ", high);
  150. Print("low price is ", low);
  151. Print("index of last updated high is ", index_of_high);
  152. for(int l=index_of_high; l > 0; l--)
  153. {
  154. // print("hi");
  155. double close = iClose(Symbol(),PERIOD_CURRENT,l);
  156. if(close > high)
  157. {
  158. double price_high = iLow(Symbol(),PERIOD_CURRENT,l+1);
  159. double time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  160. double time1 = iTime(Symbol(),PERIOD_CURRENT,l-1);
  161. bool a = ObjectCreate(0,"tline2",OBJ_TREND,0,time,price_high,time1,price_high);
  162. Print("object created : ", a);
  163. trend = "uptrend";
  164. Print("choch");
  165. Print(l);
  166. index_of_last_low = i;
  167. Print("close", close);
  168. break;
  169. }
  170. }
  171. hi = iHigh(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  172. lo = iLow(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  173. index = i;
  174. }
  175. }
  176. //
  177. }
  178. }
  179. }
  180. //+------------------------------------------------------------------+
  181. //| |
  182. //+------------------------------------------------------------------+
  183. int OnInit()
  184. {
  185. //ObjectCreate(0,"line",OBJ_VLINE,0,D'2023.07.01 10:00:27',0);
  186. object_find(object_name);
  187. //Print(ObjectFind(0,"line")); // to find the object drawn on the chart
  188. if(object_find(object_name) == true)
  189. {
  190. datetime time_of_candle = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  191. Print("line time: ",time_of_candle);
  192. int index = iBarShift(Symbol(), PERIOD_CURRENT, time_of_candle, false);
  193. Print(index);
  194. Print(index);
  195. // datetime time_of_candle = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  196. // Print("time is " , time_of_candle);
  197. //
  198. // int index = iBarShift(0, PERIOD_CURRENT,time_of_candle, true);
  199. // Print("index is " , index);
  200. foo(index);
  201. }
  202. else
  203. {
  204. Print("Object Not Found");
  205. }
  206. return(INIT_SUCCEEDED);
  207. }
  208. //+------------------------------------------------------------------+
  209. //| Expert deinitialization function |
  210. //+------------------------------------------------------------------+
  211. void OnDeinit(const int reason)
  212. {
  213. //---
  214. }
  215. //+------------------------------------------------------------------+
  216. //| Expert tick function |
  217. //+------------------------------------------------------------------+
  218. void OnTick()
  219. {
  220. if(NewBar())
  221. {
  222. ObjectSetInteger(0,"tline1",OBJPROP_TIME,iTime(Symbol(), PERIOD_CURRENT,0));
  223. ObjectSetInteger(0,"tline2",OBJPROP_TIME,iTime(Symbol(), PERIOD_CURRENT,0));
  224. }
  225. }
  226. //+------------------------------------------------------------------+
  227. //| |
  228. //+------------------------------------------------------------------+
  229. bool NewBar()
  230. {
  231. static datetime Last_bar ;
  232. datetime Current_bar = iTime(Symbol(), PERIOD_CURRENT,0);
  233. if(Current_bar != Last_bar)
  234. {
  235. Last_bar = Current_bar;
  236. return (true);
  237. }
  238. else
  239. {
  240. return (false);
  241. }
  242. }
  243. //+------------------------------------------------------------------+
  244. //| |
  245. //+------------------------------------------------------------------+
  246. bool object_find(string obj) // to find object placed by user in the chart
  247. {
  248. if(ObjectFind(0,obj) >= 0)
  249. {
  250. return true;
  251. }
  252. return false;
  253. }
  254. //+------------------------------------------------------------------+
  255. //| |
  256. //+------------------------------------------------------------------+
  257. string checkCandle(int i) // to check the candle is bullish or bearish
  258. {
  259. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  260. double open = iOpen(Symbol(),PERIOD_CURRENT,i);
  261. if(close > open)
  262. {
  263. return "Bullish";
  264. }
  265. else
  266. if(close < open)
  267. {
  268. return "Bearish";
  269. }
  270. return "empty";
  271. }
  272. //+------------------------------------------------------------------+
  273. //+------------------------------------------------------------------+
  274. /////////////////////////////////////
  275. //void third condition()
  276. // {
  277. // if(checkCandle(index) == "Bearish")
  278. // {
  279. //
  280. // trend = "downtrend";
  281. //
  282. // // double high,low = 0.0;
  283. //
  284. // // to store high and low of the value after the object
  285. // low = iLow(Symbol(),PERIOD_CURRENT,index);
  286. //
  287. // //low = iLow(Symbol(),PERIOD_CURRENT,index-1);
  288. //
  289. // if(trend == "downtrend")
  290. // {
  291. //
  292. // low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  293. //
  294. //
  295. // if(low1 < low)
  296. // {
  297. // low = low1;
  298. // for(int l=i; l > 0; l--)
  299. // {
  300. // // print("hi");
  301. // double high2 = iHigh(Symbol(),PERIOD_CURRENT,l);
  302. // if(high2 > high)
  303. // {
  304. //
  305. // high = high2;
  306. //
  307. // }
  308. //
  309. // }
  310. // }
  311. //
  312. // }
  313. // //
  314. // }
  315. //
  316. // }
  317. //+------------------------------------------------------------------+