Bez popisu

BOS_CHOCH.mq5 10KB

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