Sen descrición

BOS_CHOCH.mq5 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. int low_index =0;
  22. double high,low,low1,high1,low2,high2;
  23. int Store_bos_last_index = 0;
  24. //int ind = 0;
  25. bool choch = false;
  26. int index = 0;
  27. //+------------------------------------------------------------------+
  28. //| Expert initialization function |
  29. //+------------------------------------------------------------------+
  30. void Bos(int i)
  31. {
  32. if(trend == "uptrend")
  33. {
  34. high = iHigh(Symbol(),PERIOD_CURRENT,high_index);
  35. high1 = iHigh(Symbol(),PERIOD_CURRENT,i);
  36. if(high1 > high)
  37. {
  38. high = high1;
  39. ObjectCreate(0,"line1"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),high);
  40. ObjectSetString(0,"line1"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"bos");
  41. Store_bos_last_index = i;
  42. int count = (high_index-1) - i;
  43. for(int j=1; j <= count; j++)
  44. {
  45. if(checkCandle(high_index-j) == "Bearish")
  46. {
  47. low = iLow(Symbol(),PERIOD_CURRENT,high_index-j);
  48. bearish_found = true;
  49. }
  50. }
  51. if(bearish_found == true)
  52. {
  53. for(int k=1; k < count; k++)
  54. {
  55. low1 = iLow(Symbol(),PERIOD_CURRENT,high_index - k);
  56. if(low1 <= low)
  57. {
  58. low = low1;
  59. index_of_low =high_index- k;
  60. }
  61. }
  62. bearish_found = false;
  63. }
  64. Print(" high price is ", high);
  65. Print("low price is ", low);
  66. high_index = i;
  67. }
  68. }
  69. if(trend == "downtrend")
  70. {
  71. low = iLow(Symbol(),PERIOD_CURRENT,low_index);
  72. low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  73. if(low1 < low)
  74. {
  75. Store_bos_last_index = i;
  76. low = low1;
  77. ObjectCreate(0,"line2"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),low);
  78. ObjectSetString(0,"line2"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"bos");
  79. int count = (low_index-1) - i;
  80. for(int j=1; j <= count; j++)
  81. {
  82. if(checkCandle(low_index-j) == "Bullish")
  83. {
  84. high = iHigh(Symbol(),PERIOD_CURRENT,low_index-j);
  85. bullish_found = true;
  86. }
  87. }
  88. if(bullish_found == true)
  89. {
  90. for(int k=1; k < count; k++)
  91. {
  92. high1 = iHigh(Symbol(),PERIOD_CURRENT,low_index - k);
  93. if(high1 >= high)
  94. {
  95. high = high1;
  96. index_of_high = low_index - k;
  97. }
  98. }
  99. bullish_found = false;
  100. }
  101. Print("low price is ", low);
  102. Print(" high price is ", high);
  103. low_index = i;
  104. }
  105. }
  106. }
  107. //+------------------------------------------------------------------+
  108. //| |
  109. //+------------------------------------------------------------------+
  110. void choch(int i)
  111. {
  112. if(trend == "uptrend")
  113. {
  114. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  115. if(close < low)
  116. {
  117. ObjectCreate(0,"line3"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),close);
  118. ObjectSetString(0,"line3"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"choch");
  119. ObjectCreate(0,"line8"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_ARROW_DOWN,0,iTime(Symbol(),PERIOD_CURRENT,i),iHigh(Symbol(),PERIOD_CURRENT,i)+100*Point());
  120. ObjectSetInteger(0,"line8"+iTime(Symbol(),PERIOD_CURRENT,i),OBJPROP_COLOR,clrRed);
  121. Print("last bos index is ",Store_bos_last_index);
  122. hi = iHigh(Symbol(),PERIOD_CURRENT,Store_bos_last_index+1);
  123. lo = iLow(Symbol(),PERIOD_CURRENT,Store_bos_last_index+1);
  124. low = iLow(Symbol(),PERIOD_CURRENT,i);
  125. Print("low update due to choch : ", low);
  126. trend = "downtrend";
  127. Print("choch at uptrend index --- ", iTime(Symbol(),PERIOD_CURRENT,i));
  128. }
  129. }
  130. if(trend == "downtrend")
  131. {
  132. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  133. if(close > high)
  134. {
  135. ObjectCreate(0,"line4"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),close);
  136. ObjectSetString(0,"line4"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"choch");
  137. ObjectCreate(0,"line7"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_ARROW_UP,0,iTime(Symbol(),PERIOD_CURRENT,i),iLow(Symbol(),PERIOD_CURRENT,i)-100*Point());
  138. ObjectSetInteger(0,"line7"+iTime(Symbol(),PERIOD_CURRENT,i),OBJPROP_COLOR,clrGreen);
  139. Print("last bos index is ",Store_bos_last_index);
  140. hi = iHigh(Symbol(),PERIOD_CURRENT,Store_bos_last_index+1);
  141. lo = iLow(Symbol(),PERIOD_CURRENT,Store_bos_last_index+1);
  142. high = iHigh(Symbol(),PERIOD_CURRENT,i);
  143. trend = "uptrend";
  144. Print("high is update due to choch : ", high);
  145. Print("choch at downtrend index ---- ", iTime(Symbol(),PERIOD_CURRENT,i));
  146. }
  147. }
  148. }
  149. //+------------------------------------------------------------------+
  150. //| |
  151. //+------------------------------------------------------------------+
  152. void third_Condition(int i)
  153. {
  154. if(trend == "uptrend")
  155. {
  156. low2 = iLow(Symbol(),PERIOD_CURRENT,i);
  157. if(low2 < low)
  158. {
  159. ObjectCreate(0,"line5"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),low2);
  160. ObjectSetString(0,"line5"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"3_C");
  161. low = low2;
  162. Print("low update due to third condition",low);
  163. }
  164. }
  165. if(trend == "downtrend")
  166. {
  167. high2 = iHigh(Symbol(),PERIOD_CURRENT,i);
  168. if(high2 > high)
  169. {
  170. ObjectCreate(0,"line6"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),high2);
  171. ObjectSetString(0,"line6"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"3_C");
  172. high = high2;
  173. Print("high update due to third condition",high);
  174. }
  175. }
  176. }
  177. //+------------------------------------------------------------------+
  178. //| |
  179. //+------------------------------------------------------------------+
  180. void foo(int value)
  181. {
  182. int l = 0;
  183. for(int i=value-1; i > 0; i--)
  184. {
  185. //Print("hi");
  186. //if(l < 2)
  187. {
  188. Bos(i);
  189. choch(i);
  190. third_Condition(i);
  191. l++;
  192. }
  193. //Bos(i);
  194. }
  195. }
  196. //+------------------------------------------------------------------+
  197. //| |
  198. //+------------------------------------------------------------------+
  199. int OnInit()
  200. {
  201. object_find(object_name);
  202. if(object_find(object_name) == true)
  203. {
  204. datetime time_of_candle = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  205. index = iBarShift(Symbol(), PERIOD_CURRENT, time_of_candle, false);
  206. checkCandle(index);
  207. if(checkCandle(index) == "Bullish")
  208. {
  209. trend = "uptrend";
  210. }
  211. if(checkCandle(index) == "Bearish")
  212. {
  213. trend = "downtrend";
  214. }
  215. high = iHigh(Symbol(),PERIOD_CURRENT,index);
  216. low = iLow(Symbol(),PERIOD_CURRENT,index);
  217. high_index = index;
  218. low_index = index;
  219. foo(index);
  220. }
  221. else
  222. {
  223. Print("Object Not Found");
  224. }
  225. return(INIT_SUCCEEDED);
  226. }
  227. //+------------------------------------------------------------------+
  228. //| Expert deinitialization function |
  229. //+------------------------------------------------------------------+
  230. void OnDeinit(const int reason)
  231. {
  232. //---
  233. }
  234. //+------------------------------------------------------------------+
  235. //| Expert tick function |
  236. //+------------------------------------------------------------------+
  237. void OnTick()
  238. {
  239. }
  240. //+------------------------------------------------------------------+
  241. //| |
  242. //+------------------------------------------------------------------+
  243. bool NewBar()
  244. {
  245. static datetime Last_bar ;
  246. datetime Current_bar = iTime(Symbol(), PERIOD_CURRENT,0);
  247. if(Current_bar != Last_bar)
  248. {
  249. Last_bar = Current_bar;
  250. return (true);
  251. }
  252. else
  253. {
  254. return (false);
  255. }
  256. }
  257. //+------------------------------------------------------------------+
  258. //| |
  259. //+------------------------------------------------------------------+
  260. bool object_find(string obj) // to find object placed by user in the chart
  261. {
  262. if(ObjectFind(0,obj) >= 0)
  263. {
  264. return true;
  265. }
  266. return false;
  267. }
  268. //+------------------------------------------------------------------+
  269. //| |
  270. //+------------------------------------------------------------------+
  271. string checkCandle(int i) // to check the candle is bullish or bearish
  272. {
  273. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  274. double open = iOpen(Symbol(),PERIOD_CURRENT,i);
  275. if(close > open)
  276. {
  277. return "Bullish";
  278. }
  279. else
  280. if(close < open)
  281. {
  282. return "Bearish";
  283. }
  284. return "empty";
  285. }
  286. //+------------------------------------------------------------------+
  287. //+------------------------------------------------------------------+