Nenhuma descrição

MK_BOS_CHOCH.mq5 12KB

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