Няма описание

MK_BOS_CHOCH.mq5 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. int Store_last_index_of_high = 0;
  23. int Store_last_index_of_low = 0;
  24. //int ind = 0;
  25. bool choch = false;
  26. //+------------------------------------------------------------------+
  27. //| Expert initialization function |
  28. //+------------------------------------------------------------------+
  29. void foo(int index)
  30. {
  31. for(int i=index; i > 0; i--)
  32. {
  33. //ind = index;
  34. if(trend == "uptrend")
  35. {
  36. high = iHigh(Symbol(),PERIOD_CURRENT,index);
  37. high1 = iHigh(Symbol(),PERIOD_CURRENT,i);
  38. if(high1 > high)
  39. {
  40. Store_last_index_of_high = i;
  41. high = high1;
  42. int count = (index-1) - i;
  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. //Print("bearish is found at index ",index-j);
  51. }
  52. }
  53. if(bearish_found == true)
  54. {
  55. for(int k=1; k <= count; k++)
  56. {
  57. low1 = iLow(Symbol(),PERIOD_CURRENT,index - k);
  58. if(low1 <= low)
  59. {
  60. //Print("here");
  61. low = low1;
  62. index_of_low =index- k;
  63. Print("low update due to 2 condition : ",index- k);
  64. }
  65. }
  66. bearish_found = false;
  67. }
  68. Print(" high price is ", high);
  69. Print("low price is ", low);
  70. Print("index of last updated low is ", index_of_low);
  71. Print("index value is re", index);
  72. Print("ith value is ",i);
  73. index = i;
  74. // Print("index is ", i);
  75. for(int l=index_of_low; l > 0; l--)
  76. {
  77. // Print(l);
  78. // Print("index of low ", index_of_low," to check ", index);
  79. double close = iClose(Symbol(),PERIOD_CURRENT,l);
  80. double low3 = iLow(Symbol(),PERIOD_CURRENT,l);
  81. if(close < low)
  82. {
  83. //double price_low = iLow(Symbol(),PERIOD_CURRENT,l+1);
  84. //datetime time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  85. //datetime time1 = iTime(Symbol(),PERIOD_CURRENT,l-1);
  86. ////ObjectCreate(0,"tline1",OBJ_TREND,0,time,price_low,time1,price_low);
  87. low = iLow(Symbol(),PERIOD_CURRENT,l);
  88. trend = "downtrend";
  89. index_of_last_high = Store_last_index_of_high;
  90. Print("choch at index --------------------------------------------------------------------------------------- ", l);
  91. choch = true;
  92. break;
  93. }
  94. if(low3 < low)
  95. {
  96. low = low3;
  97. Print("low update due to third condition",low);
  98. }
  99. }
  100. if(choch == true)
  101. {
  102. Print("here we are",index_of_last_high+1);
  103. hi = iHigh(Symbol(),PERIOD_CURRENT,index_of_last_high+1);
  104. datetime ti = iTime(Symbol(),PERIOD_CURRENT,index_of_last_high+1);
  105. lo = iLow(Symbol(),PERIOD_CURRENT,index_of_last_high+1);
  106. Print(" buy and sell high index is ",index_of_last_high+1);
  107. Print(" high price is ", hi);
  108. Print(" low price is ", lo);
  109. ObjectCreate(0,"line111"+iTime(Symbol(),PERIOD_CURRENT,index_of_last_high+1),OBJ_HLINE,0,ti,hi);
  110. ObjectCreate(0,"line12"+iTime(Symbol(),PERIOD_CURRENT,index_of_last_high+1),OBJ_HLINE,0,ti,lo);
  111. hi = 0.0;
  112. lo =0.0;
  113. choch = false;
  114. }
  115. }
  116. }
  117. if(trend == "downtrend")
  118. {
  119. low = iLow(Symbol(),PERIOD_CURRENT,index);
  120. low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  121. if(low1 < low)
  122. {
  123. Store_last_index_of_low = i;
  124. low = low1;
  125. int count = (index-1) - i;
  126. //Print("count",count);
  127. for(int j=1; j < count; j++)
  128. {
  129. if(checkCandle(index-j) == "Bullish")
  130. {
  131. high = iHigh(Symbol(),PERIOD_CURRENT,index-j);
  132. bullish_found = true;
  133. //Print("bearish is found at index ",index-j);
  134. }
  135. }
  136. if(bullish_found == true)
  137. {
  138. for(int k=1; k <= count; k++)
  139. {
  140. high1 = iHigh(Symbol(),PERIOD_CURRENT,index - k);
  141. if(high1 >= high)
  142. {
  143. //Print("here");
  144. high = high1;
  145. index_of_high =index- k;
  146. Print("high update due to 2 condition : ",index- k);
  147. }
  148. }
  149. bearish_found = false;
  150. }
  151. Print(" high price is ", high);
  152. Print("low price is ", low);
  153. Print("index of last updated high is ", index_of_high);
  154. Print("index value is re", index);
  155. Print("ith value is ",i);
  156. index = i;
  157. // Print("index is ", i);
  158. }
  159. for(int l=index_of_high; l > 0; l--)
  160. {
  161. //Print(l);
  162. //Print("index of low ", index_of_high," to check ", index);
  163. double close = iClose(Symbol(),PERIOD_CURRENT,l);
  164. double high3 = iHigh(Symbol(),PERIOD_CURRENT,l);
  165. if(close > high)
  166. {
  167. //double price_low = iLow(Symbol(),PERIOD_CURRENT,l+1);
  168. //datetime time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  169. //datetime time1 = iTime(Symbol(),PERIOD_CURRENT,l-1);
  170. ////ObjectCreate(0,"tline1",OBJ_TREND,0,time,price_low,time1,price_low);
  171. high = iHigh(Symbol(),PERIOD_CURRENT,l);
  172. trend = "uptrend";
  173. index_of_last_low = Store_last_index_of_low;
  174. Print("choch at index ", l);
  175. choch = true;
  176. break;
  177. }
  178. if(high3 > high)
  179. {
  180. high = high3;
  181. Print("high update due to third condition",high);
  182. }
  183. }
  184. if(choch == true)
  185. {
  186. hi = iHigh(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  187. lo = iLow(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  188. Print(" buy and sell high index is ",index_of_last_low+1);
  189. Print(" high price is ", hi);
  190. Print(" low price is ", lo);
  191. hi = 0.0;
  192. lo =0.0;
  193. choch = false;
  194. }
  195. }
  196. // if(trend == "downtrend")
  197. // {
  198. // low = iLow(Symbol(),PERIOD_CURRENT,index);
  199. //
  200. // low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  201. //
  202. // if(low1 < low)
  203. // {
  204. // Store_last_index_of_low = i;
  205. //
  206. // low = low1;
  207. //
  208. //
  209. // int count = (index-1) - i;
  210. //
  211. // //Print("count",count);
  212. //
  213. // for(int j=1; j < count; j++)
  214. // {
  215. //
  216. // if(checkCandle(index-j) == "Bullish")
  217. // {
  218. //
  219. // high = iHigh(Symbol(),PERIOD_CURRENT,index-j);
  220. // bullish_found = true;
  221. //
  222. // //Print("bearish is found at index ",index-j);
  223. //
  224. //
  225. // }
  226. //
  227. // }
  228. //
  229. // if(bullish_found == true)
  230. // {
  231. //
  232. // for(int k=1; k < count; k++)
  233. // {
  234. //
  235. // high1 = iHigh(Symbol(),PERIOD_CURRENT,index - k);
  236. // if(high1 >= high)
  237. // {
  238. //
  239. // high = high1;
  240. // index_of_high =index- k;
  241. // Print(" high update due to 2 condition : ",index- k);
  242. //
  243. // }
  244. // }
  245. // bullish_found = false;
  246. //
  247. // }
  248. //
  249. // Print(" high price is ", high);
  250. // Print("low price is ", low);
  251. //
  252. // Print("index of last updated high is ", index_of_high);
  253. //
  254. //
  255. //
  256. // index = i;
  257. //
  258. //
  259. // // Print("index is ", i);
  260. //
  261. //
  262. //
  263. //
  264. // }
  265. //
  266. //
  267. // for(int l=index_of_high; l >= index; l--)
  268. // {
  269. // // print("hi");
  270. // double close = iClose(Symbol(),PERIOD_CURRENT,l);
  271. // double high3 = iHigh(Symbol(),PERIOD_CURRENT,l);
  272. //
  273. // if(close > high)
  274. // {
  275. //
  276. // //double price_low = iLow(Symbol(),PERIOD_CURRENT,l+1);
  277. // //datetime time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  278. // //datetime time1 = iTime(Symbol(),PERIOD_CURRENT,l-1);
  279. // ////ObjectCreate(0,"tline1",OBJ_TREND,0,time,price_low,time1,price_low);
  280. //
  281. //
  282. // high = iHigh(Symbol(),PERIOD_CURRENT,l);
  283. // trend = "uptrend";
  284. //
  285. // index_of_last_low = Store_last_index_of_low;
  286. //
  287. //
  288. // choch = true;
  289. // break;
  290. //
  291. // }
  292. //
  293. // if(high3 > high)
  294. // {
  295. // high = high3;
  296. // Print("high update due to third condition",high);
  297. // }
  298. //
  299. // }
  300. //
  301. // if(choch == true)
  302. // {
  303. // hi = iHigh(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  304. // lo = iLow(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  305. //
  306. // Print(" buy and sell high index is ",index_of_last_low+1);
  307. // Print(" high price is ", hi);
  308. // Print(" low price is ", lo);
  309. //
  310. // hi = 0.0;
  311. // lo =0.0;
  312. // choch = false;
  313. // }
  314. //
  315. //
  316. //
  317. //
  318. //
  319. // }
  320. // if(checkCandle(index) == "Bearish")
  321. // {
  322. //
  323. // trend = "downtrend";
  324. //
  325. // // double high,low = 0.0;
  326. //
  327. // // to store high and low of the value after the object
  328. // low = iLow(Symbol(),PERIOD_CURRENT,index);
  329. //
  330. // //low = iLow(Symbol(),PERIOD_CURRENT,index-1);
  331. //
  332. // if(trend == "downtrend")
  333. // {
  334. //
  335. //
  336. //
  337. //
  338. // low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  339. //
  340. // //double low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  341. //
  342. // if(low1 < low)
  343. // {
  344. // low = low1;
  345. //
  346. // // Print("high break at index value",i);
  347. // // Print("index is ",index);
  348. //
  349. // int count = (index-1) - i;
  350. // Print("count is ", count);
  351. //
  352. // // Print("index updated ",index);
  353. //
  354. // // Print("count",count);
  355. //
  356. // for(int j=1; j <= count; j++)
  357. // {
  358. //
  359. // if(checkCandle(index-j) == "Bullish")
  360. // {
  361. // high = iHigh(Symbol(),PERIOD_CURRENT,index-j);
  362. // bullish_found = true;
  363. // }
  364. //
  365. // }
  366. //
  367. // if(bullish_found == true)
  368. // {
  369. // high1;
  370. // //Print("where");
  371. // for(int k=1; k <= count; k++)
  372. // {
  373. //
  374. // high1 = iHigh(Symbol(),PERIOD_CURRENT,index - k);
  375. // if(high1 >= high)
  376. // {
  377. // //Print("here");
  378. // high = high1;
  379. // index_of_high = index - k;
  380. // Print("high is updated at index : ",index - k);
  381. //
  382. // }
  383. // }
  384. // bullish_found = false;
  385. //
  386. // }
  387. //
  388. // Print(" high price is ", high);
  389. // Print("low price is ", low);
  390. // Print("index of last updated high is ", index_of_high);
  391. //
  392. // for(int l=index_of_high; l > 0; l--)
  393. // {
  394. // // print("hi");
  395. // double close = iClose(Symbol(),PERIOD_CURRENT,l);
  396. // double high3 = iHigh(Symbol(),PERIOD_CURRENT,l);
  397. //
  398. // if(close > high)
  399. // {
  400. //
  401. // double price_high = iLow(Symbol(),PERIOD_CURRENT,l+1);
  402. // datetime time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  403. // datetime time1 = iTime(Symbol(),PERIOD_CURRENT,l-1);
  404. // //ObjectCreate(0,"tline2",OBJ_TREND,0,time,price_high,time1,price_high);
  405. //
  406. //
  407. // trend = "uptrend";
  408. // Print("choch");
  409. // Print(l);
  410. // index_of_last_low = i;
  411. // Print("close", close);
  412. // choch = true;
  413. // break;
  414. //
  415. // }
  416. //
  417. // if(high3 > high)
  418. // {
  419. //
  420. // double price_high = iLow(Symbol(),PERIOD_CURRENT,l+1);
  421. // datetime time = iTime(Symbol(),PERIOD_CURRENT,l+1);
  422. // datetime time1 = iTime(Symbol(),PERIOD_CURRENT,l-1);
  423. // // ObjectCreate(0,"tline2",OBJ_TREND,0,time,price_high,time1,price_high);
  424. //
  425. //
  426. // trend = "uptrend";
  427. // Print("choch in condition 3");
  428. // Print(l);
  429. // index_of_last_low = i;
  430. // Print("close", close);
  431. // choch = true;
  432. // break;
  433. //
  434. // }
  435. //
  436. //
  437. //
  438. //
  439. // }
  440. // if(choch == true)
  441. // {
  442. //
  443. // hi = iHigh(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  444. // lo = iLow(Symbol(),PERIOD_CURRENT,index_of_last_low+1);
  445. //
  446. // Print(" buy and sell high index is ",index_of_last_low+1);
  447. // Print(" high price is ", hi);
  448. // Print(" low price is ", lo);
  449. //
  450. // hi = 0.0;
  451. // lo = 0.0;
  452. // choch = false;
  453. // }
  454. // index = i;
  455. //
  456. // }
  457. //
  458. // }
  459. // //
  460. //
  461. // }
  462. }
  463. }
  464. //+------------------------------------------------------------------+
  465. //| |
  466. //+------------------------------------------------------------------+
  467. int OnInit()
  468. {
  469. //ObjectCreate(0,"line",OBJ_VLINE,0,D'2023.07.01 10:00:27',0);
  470. object_find(object_name);
  471. //Print(ObjectFind(0,"line")); // to find the object drawn on the chart
  472. if(object_find(object_name) == true)
  473. {
  474. datetime time_of_candle = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  475. // Print("line time: ",time_of_candle);
  476. int index = iBarShift(Symbol(), PERIOD_CURRENT, time_of_candle, false);
  477. checkCandle(index);
  478. if(checkCandle(index) == "Bullish")
  479. {
  480. trend = "uptrend";
  481. }
  482. if(checkCandle(index) == "Bearish")
  483. {
  484. trend = "downtrend";
  485. }
  486. //Print(index);
  487. // Print(index);
  488. // datetime time_of_candle = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  489. // Print("time is " , time_of_candle);
  490. //
  491. // int index = iBarShift(0, PERIOD_CURRENT,time_of_candle, true);
  492. // Print("index is " , index);
  493. foo(index);
  494. }
  495. else
  496. {
  497. Print("Object Not Found");
  498. }
  499. return(INIT_SUCCEEDED);
  500. }
  501. //+------------------------------------------------------------------+
  502. //| Expert deinitialization function |
  503. //+------------------------------------------------------------------+
  504. void OnDeinit(const int reason)
  505. {
  506. //---
  507. }
  508. //+------------------------------------------------------------------+
  509. //| Expert tick function |
  510. //+------------------------------------------------------------------+
  511. void OnTick()
  512. {
  513. if(NewBar())
  514. {
  515. ObjectSetInteger(0,"tline1",OBJPROP_TIME,iTime(Symbol(), PERIOD_CURRENT,0));
  516. ObjectSetInteger(0,"tline2",OBJPROP_TIME,iTime(Symbol(), PERIOD_CURRENT,0));
  517. }
  518. }
  519. //+------------------------------------------------------------------+
  520. //| |
  521. //+------------------------------------------------------------------+
  522. bool NewBar()
  523. {
  524. static datetime Last_bar ;
  525. datetime Current_bar = iTime(Symbol(), PERIOD_CURRENT,0);
  526. if(Current_bar != Last_bar)
  527. {
  528. Last_bar = Current_bar;
  529. return (true);
  530. }
  531. else
  532. {
  533. return (false);
  534. }
  535. }
  536. //+------------------------------------------------------------------+
  537. //| |
  538. //+------------------------------------------------------------------+
  539. bool object_find(string obj) // to find object placed by user in the chart
  540. {
  541. if(ObjectFind(0,obj) >= 0)
  542. {
  543. return true;
  544. }
  545. return false;
  546. }
  547. //+------------------------------------------------------------------+
  548. //| |
  549. //+------------------------------------------------------------------+
  550. string checkCandle(int i) // to check the candle is bullish or bearish
  551. {
  552. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  553. double open = iOpen(Symbol(),PERIOD_CURRENT,i);
  554. if(close > open)
  555. {
  556. return "Bullish";
  557. }
  558. else
  559. if(close < open)
  560. {
  561. return "Bearish";
  562. }
  563. return "empty";
  564. }
  565. //+------------------------------------------------------------------+
  566. //+------------------------------------------------------------------+