Ei kuvausta

BOS_CHOCH.mq5 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. #define orderNum 200
  10. struct tradeOrder
  11. {
  12. int choch_index;
  13. int reversal_index;
  14. string Signal;
  15. datetime Time_OF_Candle;
  16. datetime Time_of_candle_For_trade;
  17. tradeOrder()
  18. {
  19. choch_index = -1;
  20. Signal = "";
  21. reversal_index = 0;
  22. Time_OF_Candle = 0.0;
  23. Time_of_candle_For_trade = 0.0;
  24. }
  25. };
  26. tradeOrder od_1[orderNum];
  27. sinput string str = "<><><><><><> Set Object Name <><><><><><>";
  28. input string object_name = "line";
  29. sinput string str1 = "<><><><><><> Set time of vertical line <><><><><><>";
  30. input datetime inpTime = 0;//First Line Draw time
  31. int reversal_candle_index = 0;
  32. int ind = 0;
  33. string trend = " ";
  34. bool bearish_found = false;
  35. bool bullish_found = false;
  36. int index_of_last_high = 0;
  37. int index_of_last_low = 0;
  38. double hi;
  39. double lo;
  40. int index_of_low = 0;
  41. datetime index_of_low_time;
  42. int index_of_high = 0;
  43. datetime index_of_high_time;
  44. int high_index=0;
  45. datetime high_index_time;
  46. int low_index =0;
  47. datetime low_index_time;
  48. double high,low,low1,high1,low2,high2;
  49. int Store_bos_last_index = 0;
  50. datetime store_last_bos_index_time;
  51. //int ind = 0;
  52. bool choch = false;
  53. int index = 0;
  54. string signal = "";
  55. datetime time_of_candle = 0.0;
  56. datetime time_to_place_trade = 0.0;
  57. //double close1 = 0.0;
  58. //+------------------------------------------------------------------+
  59. //| Expert initialization function |
  60. //+------------------------------------------------------------------+
  61. void Bos(int i)
  62. {
  63. //Print("in boss");
  64. //Print("trend is ",trend);
  65. if(trend == "uptrend")
  66. {
  67. //high = iHigh(Symbol(),PERIOD_CURRENT,high_index);
  68. high1 = iHigh(Symbol(),PERIOD_CURRENT,i);
  69. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  70. //Print("high is ",high);
  71. //Print("close is ", close,"time is ",iTime(Symbol(),PERIOD_CURRENT,i));
  72. if(close > high)
  73. {
  74. if(object_find("high") > 0)
  75. {
  76. ObjectDelete(0,"high");
  77. }
  78. high = high1;
  79. Print(" high is updated : ",high);
  80. ObjectCreate(0,"high",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),high,iTime(Symbol(),PERIOD_CURRENT,0),high);
  81. ObjectSetInteger(0,"high",OBJPROP_RAY_RIGHT,true);
  82. ObjectSetInteger(0,"high",OBJPROP_COLOR,clrBlue);
  83. ObjectCreate(0,"line1"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),high);
  84. ObjectSetString(0,"line1"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"bos");
  85. store_last_bos_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  86. Print(store_last_bos_index_time);
  87. Store_bos_last_index = iBarShift(Symbol(), PERIOD_CURRENT, store_last_bos_index_time, false);
  88. Print("last bos index is ",Store_bos_last_index);
  89. high_index = iBarShift(Symbol(), PERIOD_CURRENT, high_index_time, false);
  90. Print("high index ",high_index);
  91. int count = (high_index-1) - i;
  92. Print("count is ",count);
  93. for(int j=count; j >= 1; j--)
  94. {
  95. if(checkCandle(high_index-j) == "Bearish")
  96. {
  97. Print("bearish candle found and its low is ",low);
  98. low = iLow(Symbol(),PERIOD_CURRENT,high_index-j);
  99. Print("bearish candle found and its low is ",low);
  100. bearish_found = true;
  101. count = high_index-j;
  102. index_of_low_time = iTime(Symbol(),PERIOD_CURRENT,high_index-j);
  103. break;
  104. }
  105. }
  106. if(bearish_found == true)
  107. {
  108. for(int k=count-1; k > Store_bos_last_index ; k--)
  109. {
  110. low1 = iLow(Symbol(),PERIOD_CURRENT,k);
  111. if(low1 < low)
  112. {
  113. low = low1;
  114. index_of_low_time = iTime(Symbol(),PERIOD_CURRENT,k);
  115. //index_of_low = high_index- k;
  116. Print(" ------------------index of low ",index_of_low);
  117. bearish_found = false;
  118. }
  119. }
  120. if(object_find("low") > 0)
  121. {
  122. if(ObjectDelete(0,"low"))
  123. {
  124. Print("object deleted :");
  125. }
  126. }
  127. index_of_low = iBarShift(Symbol(), PERIOD_CURRENT, index_of_low_time, false);
  128. ObjectCreate(0,"low",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,index_of_low),low,iTime(Symbol(),PERIOD_CURRENT,0),low);
  129. ObjectSetInteger(0,"low",OBJPROP_RAY_RIGHT,true);
  130. ObjectSetInteger(0,"low",OBJPROP_RAY_LEFT,false);
  131. ObjectSetInteger(0,"low",OBJPROP_COLOR,clrRed);
  132. }
  133. Print("high of uptrend is ", high);
  134. Print("low of uptrend is ", low);
  135. high_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  136. }
  137. }
  138. if(trend == "downtrend")
  139. {
  140. //low = iLow(Symbol(),PERIOD_CURRENT,low_index);
  141. low1 = iLow(Symbol(),PERIOD_CURRENT,i);
  142. double close1 = iClose(Symbol(),PERIOD_CURRENT,i);
  143. if(close1 < low)
  144. {
  145. if(object_find("low") > 0)
  146. {
  147. ObjectDelete(0,"low");
  148. }
  149. low = low1;
  150. ObjectCreate(0,"low",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),low,iTime(Symbol(),PERIOD_CURRENT,0),low);
  151. ObjectSetInteger(0,"low",OBJPROP_RAY_RIGHT,true);
  152. ObjectSetInteger(0,"low",OBJPROP_COLOR,clrRed);
  153. ObjectCreate(0,"line2"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),low);
  154. ObjectSetString(0,"line2"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"bos");
  155. store_last_bos_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  156. Print(store_last_bos_index_time);
  157. Store_bos_last_index = iBarShift(Symbol(), PERIOD_CURRENT, store_last_bos_index_time, false);
  158. Print("last bos index is ",Store_bos_last_index);
  159. //Store_bos_last_index = i;
  160. low_index = iBarShift(Symbol(), PERIOD_CURRENT, low_index_time, false);
  161. Print("low index ",low_index);
  162. int count = (low_index-1) - i;
  163. Print("count is ",count);
  164. for(int j=count; j >= 1; j--)
  165. {
  166. if(checkCandle(low_index-j) == "Bullish")
  167. {
  168. Print("bullish candle found and its high is ",high);
  169. high = iHigh(Symbol(),PERIOD_CURRENT,low_index-j);
  170. Print("bullish candle found and its high is ",high);
  171. bullish_found = true;
  172. count = low_index-j;
  173. index_of_high_time = iTime(Symbol(),PERIOD_CURRENT,low_index-j);
  174. break;
  175. }
  176. }
  177. if(bullish_found == true)
  178. {
  179. for(int k=count-1; k > Store_bos_last_index ; k--)
  180. {
  181. high1 = iHigh(Symbol(),PERIOD_CURRENT,k);
  182. if(high1 > high)
  183. {
  184. high = high1;
  185. index_of_high_time = iTime(Symbol(),PERIOD_CURRENT,k);
  186. //index_of_low = high_index- k;
  187. bullish_found = false;
  188. }
  189. }
  190. if(object_find("high") > 0)
  191. {
  192. ObjectDelete(0,"high");
  193. }
  194. index_of_high = iBarShift(Symbol(), PERIOD_CURRENT, index_of_high_time, false);
  195. ObjectCreate(0,"high",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,index_of_high),high,iTime(Symbol(),PERIOD_CURRENT,0),high);
  196. ObjectSetInteger(0,"high",OBJPROP_RAY_RIGHT,true);
  197. ObjectSetInteger(0,"high",OBJPROP_COLOR,clrBlue);
  198. bullish_found = false;
  199. }
  200. Print("high is ", high);
  201. Print("low is ", low);
  202. low_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  203. }
  204. }
  205. }
  206. //+------------------------------------------------------------------+
  207. //| |
  208. //+------------------------------------------------------------------+
  209. void Entry_Action_1_down(int Store_bos_last_index, int i,int diff)
  210. {
  211. // i is the index of the choch
  212. for(int j=Store_bos_last_index+1; j < iBars(Symbol(),PERIOD_CURRENT); j++)
  213. {
  214. diff++;
  215. if(checkCandle(j) == "Bullish")
  216. {
  217. //Print("bullish found at index ",j);
  218. ind = j; // reversal candle index
  219. bullish_found = true;
  220. break;
  221. }
  222. }
  223. Print("condition satisfied : ",check_values_less_than_choch_high(diff,i));
  224. Print("choch candle open is ",iOpen(Symbol(),PERIOD_CURRENT,i));
  225. Print("reversal candle open is ",iOpen(Symbol(),PERIOD_CURRENT,ind));
  226. if(check_values_less_than_choch_high(diff,i) == true)
  227. {
  228. if(iOpen(Symbol(),PERIOD_CURRENT,ind) > iOpen(Symbol(),PERIOD_CURRENT,i) && iClose(Symbol(),PERIOD_CURRENT,ind) < iClose(Symbol(),PERIOD_CURRENT,i))
  229. {
  230. signal = "buy";
  231. time_of_candle = iTime(Symbol(),PERIOD_CURRENT,i);
  232. Print("Place buy order -----------------------------------------------------------------------: ");
  233. }
  234. }
  235. //Print(diff);
  236. if(bullish_found == true)
  237. {
  238. ObjectCreate(0,"line14"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iHigh(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iHigh(Symbol(),PERIOD_CURRENT,ind));
  239. ObjectCreate(0,"line15"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iLow(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iLow(Symbol(),PERIOD_CURRENT,ind));
  240. ObjectSetInteger(0,"line14"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_COLOR,clrBlue);
  241. ObjectSetInteger(0,"line15"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_COLOR,clrBlue);
  242. ObjectCreate(0,"line16"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iClose(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iClose(Symbol(),PERIOD_CURRENT,ind));
  243. ObjectCreate(0,"line17"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iOpen(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iOpen(Symbol(),PERIOD_CURRENT,ind));
  244. ObjectSetInteger(0,"line16"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_STYLE,STYLE_DOT);
  245. ObjectSetInteger(0,"line17"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_STYLE,STYLE_DOT);
  246. diff = 0;
  247. bullish_found = false;
  248. }
  249. }
  250. //+------------------------------------------------------------------+
  251. //| |
  252. //+------------------------------------------------------------------+
  253. void Entry_Action_1_up(int Store_bos_last_index, int i,int diff)
  254. {
  255. for(int j=Store_bos_last_index+1; j < iBars(Symbol(),PERIOD_CURRENT); j++)
  256. {
  257. diff++;
  258. if(checkCandle(j) == "Bearish")
  259. {
  260. ind = j; // reversal candle index
  261. bearish_found = true;
  262. break;
  263. }
  264. }
  265. //Print("diff is ", diff);
  266. Print("condition satisfied : ",check_values_less_than_choch_low(diff,i));
  267. Print("choch candle open is ",iOpen(Symbol(),PERIOD_CURRENT,i));
  268. Print("reversal candle open is ",iOpen(Symbol(),PERIOD_CURRENT,ind));
  269. if(check_values_less_than_choch_low(diff,i) == true)
  270. {
  271. if(iOpen(Symbol(),PERIOD_CURRENT,ind) < iOpen(Symbol(),PERIOD_CURRENT,i) && iClose(Symbol(),PERIOD_CURRENT,ind) > iClose(Symbol(),PERIOD_CURRENT,i))
  272. {
  273. signal = "sell";
  274. time_of_candle = iTime(Symbol(),PERIOD_CURRENT,i);
  275. Print("Place sell order -----------------------------------------------------------------------: ");
  276. }
  277. }
  278. if(bearish_found == true)
  279. {
  280. ObjectCreate(0,"line10"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iHigh(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iHigh(Symbol(),PERIOD_CURRENT,ind));
  281. ObjectCreate(0,"line11"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iLow(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iLow(Symbol(),PERIOD_CURRENT,ind));
  282. ObjectSetInteger(0,"line10"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_COLOR,clrBlue);
  283. ObjectSetInteger(0,"line11"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_COLOR,clrBlue);
  284. ObjectCreate(0,"line12"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iClose(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iClose(Symbol(),PERIOD_CURRENT,ind));
  285. ObjectCreate(0,"line13"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,ind),iOpen(Symbol(),PERIOD_CURRENT,ind),iTime(Symbol(),PERIOD_CURRENT,i),iOpen(Symbol(),PERIOD_CURRENT,ind));
  286. ObjectSetInteger(0,"line12"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_STYLE,STYLE_DOT);
  287. ObjectSetInteger(0,"line13"+iTime(Symbol(),PERIOD_CURRENT,ind),OBJPROP_STYLE,STYLE_DOT);
  288. bearish_found = false;
  289. }
  290. }
  291. //+------------------------------------------------------------------+
  292. //| |
  293. //+------------------------------------------------------------------+
  294. bool check_values_less_than_choch_high(int diff,int i) // downtrend
  295. {
  296. bool found = false;
  297. int value = (diff + i) - 1;
  298. for(int k=value; k>i; k--)
  299. {
  300. //Print("high values ",iHigh(Symbol(),PERIOD_CURRENT,k));
  301. //Print("close values ",iClose(Symbol(),PERIOD_CURRENT,i));
  302. if(iHigh(Symbol(),PERIOD_CURRENT,k) > iClose(Symbol(),PERIOD_CURRENT,i))
  303. {
  304. found = true;
  305. }
  306. }
  307. if(found == true)
  308. {
  309. return false;
  310. }
  311. return true;
  312. }
  313. //+------------------------------------------------------------------+
  314. //| |
  315. //+------------------------------------------------------------------+
  316. bool check_values_less_than_choch_low(int diff,int i) // uptrend
  317. {
  318. bool found = false;
  319. int value = (diff + i) - 1; // from which it start to check value is less in index
  320. //Print("value to be start is ",value);
  321. for(int k=value; k>i; k--)
  322. {
  323. if(iLow(Symbol(),PERIOD_CURRENT,k) < iClose(Symbol(),PERIOD_CURRENT,i))
  324. {
  325. found = true;
  326. }
  327. }
  328. if(found == true)
  329. {
  330. return false;
  331. }
  332. return true;
  333. }
  334. //+------------------------------------------------------------------+
  335. //| |
  336. //+------------------------------------------------------------------+
  337. void choch(int i)
  338. {
  339. if(trend == "uptrend")
  340. {
  341. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  342. if(close < low)
  343. {
  344. ObjectCreate(0,"line3"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),iLow(Symbol(),PERIOD_CURRENT,i));
  345. ObjectSetString(0,"line3"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"choch");
  346. ObjectCreate(0,"line8"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_ARROW_DOWN,0,iTime(Symbol(),PERIOD_CURRENT,i),iHigh(Symbol(),PERIOD_CURRENT,i)+100*Point());
  347. ObjectSetInteger(0,"line8"+iTime(Symbol(),PERIOD_CURRENT,i),OBJPROP_COLOR,clrRed);
  348. //Print("last bos index is ",Store_bos_last_index);
  349. // int diff1 = Store_bos_last_index - (i);
  350. // store_last_bos_index_time
  351. // Print("last boss index is ",Store_bos_last_index);
  352. // // Print("diff between bos and last choch is ",diff1);
  353. //
  354. // Entry_Action_1_up(Store_bos_last_index,i,diff1);
  355. low = iLow(Symbol(),PERIOD_CURRENT,i);
  356. low_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  357. low_index = iBarShift(Symbol(), PERIOD_CURRENT, low_index_time, false);
  358. low_index = i;
  359. if(object_find("low") > 0)
  360. {
  361. ObjectDelete(0,"low");
  362. }
  363. ObjectCreate(0,"low",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),low,iTime(Symbol(),PERIOD_CURRENT,0),low);
  364. ObjectSetInteger(0,"low",OBJPROP_RAY_RIGHT,true);
  365. ObjectSetInteger(0,"low",OBJPROP_COLOR,clrRed);
  366. // Entry_Action_1_up(Store_bos_last_index,i);
  367. Print("low update due to choch : ", low);
  368. trend = "downtrend";
  369. Print("choch at uptrend index --- ", iTime(Symbol(),PERIOD_CURRENT,i));
  370. }
  371. }
  372. if(trend == "downtrend")
  373. {
  374. double close = iClose(Symbol(),PERIOD_CURRENT,i);
  375. if(close > high)
  376. {
  377. ObjectCreate(0,"line4"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),iHigh(Symbol(),PERIOD_CURRENT,i));
  378. ObjectSetString(0,"line4"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"choch");
  379. ObjectCreate(0,"line7"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_ARROW_UP,0,iTime(Symbol(),PERIOD_CURRENT,i),iLow(Symbol(),PERIOD_CURRENT,i)-100*Point());
  380. ObjectSetInteger(0,"line7"+iTime(Symbol(),PERIOD_CURRENT,i),OBJPROP_COLOR,clrGreen);
  381. // int diff = Store_bos_last_index - (i);
  382. // Print("last boss index ia ",Store_bos_last_index);
  383. //
  384. // Entry_Action_1_down(Store_bos_last_index,i,diff);
  385. high = iHigh(Symbol(),PERIOD_CURRENT,i);
  386. high_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  387. high_index = iBarShift(Symbol(), PERIOD_CURRENT, high_index_time, false);
  388. high_index = i;
  389. if(object_find("high") > 0)
  390. {
  391. ObjectDelete(0,"high");
  392. }
  393. ObjectCreate(0,"high",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),high,iTime(Symbol(),PERIOD_CURRENT,0),high);
  394. ObjectSetInteger(0,"high",OBJPROP_RAY_RIGHT,true);
  395. ObjectSetInteger(0,"high",OBJPROP_COLOR,clrBlue);
  396. trend = "uptrend";
  397. Print("high is update due to choch : ", high);
  398. Print("choch at downtrend index ---- ", iTime(Symbol(),PERIOD_CURRENT,i));
  399. }
  400. }
  401. }
  402. //+------------------------------------------------------------------+
  403. //| |
  404. //+------------------------------------------------------------------+
  405. void third_Condition(int i)
  406. {
  407. if(trend == "uptrend")
  408. {
  409. low2 = iLow(Symbol(),PERIOD_CURRENT,i);
  410. high2 = iHigh(Symbol(),PERIOD_CURRENT,i);
  411. if(low2 < low)
  412. {
  413. //ObjectCreate(0,"line5"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),iLow(Symbol(),PERIOD_CURRENT,i));
  414. //ObjectSetString(0,"line5"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"3_C");
  415. low = low2;
  416. low_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  417. low_index = iBarShift(Symbol(), PERIOD_CURRENT, low_index_time, false);
  418. low_index = i; // addtion to be minus
  419. if(object_find("low") > 0)
  420. {
  421. ObjectDelete(0,"low");
  422. }
  423. ObjectCreate(0,"low",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),low,iTime(Symbol(),PERIOD_CURRENT,0),low);
  424. ObjectSetInteger(0,"low",OBJPROP_RAY_RIGHT,true);
  425. ObjectSetInteger(0,"low",OBJPROP_COLOR,clrRed);
  426. Print("low update due to third condition",low," time of the candle is ",iTime(Symbol(),PERIOD_CURRENT,i));
  427. }
  428. if(high2 > high)
  429. {
  430. //ObjectCreate(0,"line51"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),iHigh(Symbol(),PERIOD_CURRENT,i));
  431. //ObjectSetString(0,"line51"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"3_C");
  432. high = high2;
  433. high_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  434. high_index = iBarShift(Symbol(), PERIOD_CURRENT, low_index_time, false);
  435. high_index = i; // addtion to be minus
  436. if(object_find("high") > 0)
  437. {
  438. ObjectDelete(0,"high");
  439. }
  440. ObjectCreate(0,"high",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),high,iTime(Symbol(),PERIOD_CURRENT,0),high);
  441. ObjectSetInteger(0,"high",OBJPROP_RAY_RIGHT,true);
  442. ObjectSetInteger(0,"high",OBJPROP_COLOR,clrBlue);
  443. Print("high update due to third condition",high," time of the candle is ",iTime(Symbol(),PERIOD_CURRENT,i));
  444. }
  445. }
  446. if(trend == "downtrend")
  447. {
  448. high2 = iHigh(Symbol(),PERIOD_CURRENT,i);
  449. low2 = iLow(Symbol(),PERIOD_CURRENT,i);
  450. if(high2 > high)
  451. {
  452. //ObjectCreate(0,"line6"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),iHigh(Symbol(),PERIOD_CURRENT,i));
  453. //ObjectSetString(0,"line6"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"3_C");
  454. high = high2;
  455. high_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  456. high_index = iBarShift(Symbol(), PERIOD_CURRENT, low_index_time, false);
  457. high_index = i; // addtion to be minus
  458. if(object_find("high") > 0)
  459. {
  460. ObjectDelete(0,"high");
  461. }
  462. ObjectCreate(0,"high",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),high,iTime(Symbol(),PERIOD_CURRENT,0),high);
  463. ObjectSetInteger(0,"high",OBJPROP_RAY_RIGHT,true);
  464. ObjectSetInteger(0,"high",OBJPROP_COLOR,clrBlue);
  465. Print("high update due to third condition",high);
  466. }
  467. if(low2 < low)
  468. {
  469. //ObjectCreate(0,"line61"+iTime(Symbol(),PERIOD_CURRENT,i),OBJ_TEXT,0,iTime(Symbol(),PERIOD_CURRENT,i),iLow(Symbol(),PERIOD_CURRENT,i));
  470. //ObjectSetString(0,"line61"+iTime(Symbol(),PERIOD_CURRENT,i), OBJPROP_TEXT,"3_C");
  471. low = low2;
  472. low_index_time = iTime(Symbol(),PERIOD_CURRENT,i);
  473. low_index = iBarShift(Symbol(), PERIOD_CURRENT, low_index_time, false);
  474. low_index = i; // addtion to be minus
  475. if(object_find("low") > 0)
  476. {
  477. ObjectDelete(0,"low");
  478. }
  479. Print("low value is ", low);
  480. ObjectCreate(0,"low",OBJ_TREND,0,iTime(Symbol(),PERIOD_CURRENT,i),low,iTime(Symbol(),PERIOD_CURRENT,0),low);
  481. ObjectSetInteger(0,"low",OBJPROP_RAY_RIGHT,true);
  482. ObjectSetInteger(0,"low",OBJPROP_COLOR,clrRed);
  483. Print("low update due to third condition",low);
  484. }
  485. }
  486. }
  487. //+------------------------------------------------------------------+
  488. //| |
  489. //+------------------------------------------------------------------+
  490. void store_value_in_structure(int i,int ind,string signal,datetime time_of_candle,datetime time_to_place_trade)
  491. {
  492. for(int l=0; l<200; l++)
  493. {
  494. if(od_1[l].choch_index == -1)
  495. {
  496. od_1[l].choch_index = i;
  497. od_1[l].reversal_index = ind;
  498. od_1[l].Signal = signal;
  499. od_1[l].Time_OF_Candle = time_of_candle;
  500. Print("saved time in the structure ",time_of_candle);
  501. od_1[l].Time_of_candle_For_trade = time_to_place_trade;
  502. Print("saved time in the structure ",time_to_place_trade);
  503. break;
  504. }
  505. }
  506. }
  507. //+------------------------------------------------------------------+
  508. //| |
  509. //+------------------------------------------------------------------+
  510. void foo(int value)
  511. {
  512. for(int i=value; i > 0; i--)
  513. {
  514. //Print("hi");
  515. //if(l < 2)
  516. Bos(i);
  517. choch(i);
  518. third_Condition(i);
  519. if(signal == "buy" || signal == "sell")
  520. {
  521. MqlDateTime edate;
  522. time_to_place_trade = 0.0;
  523. //print("time of the candle ", time_of_candle);
  524. int current_time_frame = Period();
  525. string current_time_frame_string;
  526. switch(current_time_frame)
  527. {
  528. case PERIOD_M1:
  529. current_time_frame_string = "1 minute";
  530. break;
  531. case PERIOD_M5:
  532. current_time_frame_string = "5 minutes";
  533. break;
  534. case PERIOD_M15:
  535. current_time_frame_string = "15 minutes";
  536. break;
  537. case PERIOD_M30:
  538. current_time_frame_string = "30 minutes";
  539. break;
  540. case PERIOD_H1:
  541. current_time_frame_string = "1 hour";
  542. if(TimeToStruct(time_of_candle,edate))
  543. {
  544. edate.hour = edate.hour + 2;
  545. edate.min = 0;
  546. edate.sec = 0;
  547. }
  548. // print("hour of the candle is ",structtotime(edate));
  549. time_to_place_trade = StructToTime(edate);
  550. // for(int m=0; m < orderNum; m++)
  551. // {
  552. // if(od_1[m].choch_index != -1)
  553. // {
  554. // Print(" time to match the condition : ",TimeCurrent());
  555. //
  556. // if(TimeCurrent() > od_1[m].Time_of_candle_For_trade && od_1[m].Signal == "buy")
  557. // {
  558. // Print("time to match the condition : ",TimeCurrent());
  559. //
  560. // //if(ihigh(symbol(),period_current,od_1[m].reversal_index) > ilow(symbol(),period_current,0) && iclose(symbol(),period_current,od_1[m].choch_index) < iclose(symbol(),period_current,0))
  561. // {
  562. // ObjectCreate(0,"buy trade"+time_to_place_trade,OBJ_ARROW_BUY,0,time_to_place_trade,iHigh(Symbol(),PERIOD_CURRENT,0));
  563. // Print("trade placed buy at index ",0);
  564. // od_1[m].choch_index = -1;
  565. //
  566. // }
  567. // Print(" time to take trade is ",od_1[m].Time_of_candle_For_trade);
  568. //
  569. //
  570. // }
  571. //
  572. // if(TimeCurrent() >= od_1[m].Time_of_candle_For_trade && od_1[m].Signal == "sell")
  573. // {
  574. // Print(TimeCurrent());
  575. // //
  576. // Print(" time to take trade is ",od_1[m].Time_of_candle_For_trade);
  577. // od_1[m].choch_index = -1;
  578. //
  579. // }
  580. // }
  581. // }
  582. // if(timecurrent() >= time_to_place_trade)
  583. // {
  584. // print("hi");
  585. // if(signal == "buy")
  586. // {
  587. // int index = ibarshift(symbol(), period_current, time_of_candle, false);
  588. // print("index of the 2 hour plus index is ",index);
  589. //
  590. // print("choch index ", i);
  591. // print("reversal index ",ind);
  592. // print("value to be check index ",i-3);
  593. //
  594. // //if(ihigh(symbol(),period_current,ind) > ilow(symbol(),period_current,i-2) && iclose(symbol(),period_current,i) < iclose(symbol(),period_current,i-2))
  595. // // {
  596. // // objectcreate(0,"buy trade"+time_to_place_trade,obj_arrow_buy,0,time_to_place_trade,ihigh(symbol(),period_current,i-3));
  597. // // print("trade placed buy at index ",i-3);
  598. // // }
  599. // print(time_to_place_trade);
  600. //
  601. // }
  602. // if(signal == "sell")
  603. // {
  604. // print("trade placed ");
  605. // objectcreate(0,"sell trade"+time_to_place_trade,obj_arrow_sell,0,time_to_place_trade,ilow(symbol(),period_current,i-3));
  606. //
  607. // print(ind);
  608. // print(time_to_place_trade);
  609. // }
  610. // }
  611. //else
  612. // {
  613. // time_of_candle = iTime(Symbol(),PERIOD_CURRENT,i);
  614. // }
  615. break;
  616. default:
  617. current_time_frame_string = "unknown";
  618. break;
  619. }
  620. //print("current timeframe: ", current_time_frame_string);
  621. signal = "";
  622. }
  623. }
  624. }
  625. //+------------------------------------------------------------------+
  626. //| |
  627. //+------------------------------------------------------------------+
  628. void foo1(int value)
  629. {
  630. for(int i=value; i > 0; i--)
  631. {
  632. //Print("hi");
  633. //if(l < 2)
  634. Bos(i);
  635. choch(i);
  636. third_Condition(i);
  637. if(signal == "buy" || signal == "sell")
  638. {
  639. MqlDateTime edate;
  640. time_to_place_trade = 0.0;
  641. //print("time of the candle ", time_of_candle);
  642. int current_time_frame = Period();
  643. string current_time_frame_string;
  644. switch(current_time_frame)
  645. {
  646. case PERIOD_M1:
  647. current_time_frame_string = "1 minute";
  648. break;
  649. case PERIOD_M5:
  650. current_time_frame_string = "5 minutes";
  651. break;
  652. case PERIOD_M15:
  653. current_time_frame_string = "15 minutes";
  654. break;
  655. case PERIOD_M30:
  656. current_time_frame_string = "30 minutes";
  657. break;
  658. case PERIOD_H1:
  659. current_time_frame_string = "1 hour";
  660. if(TimeToStruct(time_of_candle,edate))
  661. {
  662. edate.hour = edate.hour + 2;
  663. edate.min = 0;
  664. edate.sec = 0;
  665. }
  666. // print("hour of the candle is ",structtotime(edate));
  667. time_to_place_trade = StructToTime(edate);
  668. store_value_in_structure(1,ind,signal,time_of_candle,time_to_place_trade);
  669. // for(int m=0; m < orderNum; m++)
  670. // {
  671. // if(od_1[m].choch_index != -1)
  672. // {
  673. // Print(" time to match the condition : ",TimeCurrent());
  674. //
  675. // if(TimeCurrent() > od_1[m].Time_of_candle_For_trade && od_1[m].Signal == "buy")
  676. // {
  677. // Print("time to match the condition : ",TimeCurrent());
  678. //
  679. // //if(ihigh(symbol(),period_current,od_1[m].reversal_index) > ilow(symbol(),period_current,0) && iclose(symbol(),period_current,od_1[m].choch_index) < iclose(symbol(),period_current,0))
  680. // {
  681. // ObjectCreate(0,"buy trade"+time_to_place_trade,OBJ_ARROW_BUY,0,time_to_place_trade,iHigh(Symbol(),PERIOD_CURRENT,0));
  682. // Print("trade placed buy at index ",0);
  683. // od_1[m].choch_index = -1;
  684. //
  685. // }
  686. // Print(" time to take trade is ",od_1[m].Time_of_candle_For_trade);
  687. //
  688. //
  689. // }
  690. //
  691. // if(TimeCurrent() >= od_1[m].Time_of_candle_For_trade && od_1[m].Signal == "sell")
  692. // {
  693. // Print(TimeCurrent());
  694. // //
  695. // Print(" time to take trade is ",od_1[m].Time_of_candle_For_trade);
  696. // od_1[m].choch_index = -1;
  697. //
  698. // }
  699. // }
  700. // }
  701. // if(timecurrent() >= time_to_place_trade)
  702. // {
  703. // print("hi");
  704. // if(signal == "buy")
  705. // {
  706. // int index = ibarshift(symbol(), period_current, time_of_candle, false);
  707. // print("index of the 2 hour plus index is ",index);
  708. //
  709. // print("choch index ", i);
  710. // print("reversal index ",ind);
  711. // print("value to be check index ",i-3);
  712. //
  713. // //if(ihigh(symbol(),period_current,ind) > ilow(symbol(),period_current,i-2) && iclose(symbol(),period_current,i) < iclose(symbol(),period_current,i-2))
  714. // // {
  715. // // objectcreate(0,"buy trade"+time_to_place_trade,obj_arrow_buy,0,time_to_place_trade,ihigh(symbol(),period_current,i-3));
  716. // // print("trade placed buy at index ",i-3);
  717. // // }
  718. // print(time_to_place_trade);
  719. //
  720. // }
  721. // if(signal == "sell")
  722. // {
  723. // print("trade placed ");
  724. // objectcreate(0,"sell trade"+time_to_place_trade,obj_arrow_sell,0,time_to_place_trade,ilow(symbol(),period_current,i-3));
  725. //
  726. // print(ind);
  727. // print(time_to_place_trade);
  728. // }
  729. // }
  730. //else
  731. // {
  732. // time_of_candle = iTime(Symbol(),PERIOD_CURRENT,i);
  733. // }
  734. break;
  735. default:
  736. current_time_frame_string = "unknown";
  737. break;
  738. }
  739. //print("current timeframe: ", current_time_frame_string);
  740. signal = "";
  741. }
  742. }
  743. }
  744. //+------------------------------------------------------------------+
  745. //| |
  746. //+------------------------------------------------------------------+
  747. int OnInit()
  748. {
  749. int user_input_index = iBarShift(Symbol(), PERIOD_CURRENT,inpTime, false);
  750. if(ObjectCreate(0,"line",OBJ_VLINE,0,inpTime,iLow(Symbol(),PERIOD_CURRENT,user_input_index)))
  751. {
  752. Print("vertical line created on chart : ");
  753. }
  754. else
  755. {
  756. Print("error in creating line : ");
  757. }
  758. //int user_input_index = iBarShift(Symbol(), PERIOD_CURRENT, D'2022.09.01 12:30:27', false);
  759. //if(ObjectCreate(0,"line",OBJ_VLINE,0,D'2022.09.01 12:30:27',iLow(Symbol(),PERIOD_CURRENT,user_input_index)))
  760. // {
  761. // Print("object created on chart : ");
  762. // }
  763. //else
  764. // {
  765. // Print("object not created : ");
  766. // }
  767. object_find(object_name);
  768. if(object_find(object_name) == true)
  769. {
  770. Print("object found");
  771. // datetime time_of_candle1 = (datetime)ObjectGetInteger(0,"line",OBJPROP_TIME,0);
  772. //
  773. // index = iBarShift(Symbol(), PERIOD_CURRENT, time_of_candle1, false);
  774. Print("candle is ",checkCandle(user_input_index));
  775. if(checkCandle(user_input_index) == "Bullish")
  776. {
  777. trend = "uptrend";
  778. }
  779. if(checkCandle(user_input_index) == "Bearish")
  780. {
  781. trend = "downtrend";
  782. }
  783. high = iHigh(Symbol(),PERIOD_CURRENT,user_input_index);
  784. low = iLow(Symbol(),PERIOD_CURRENT,user_input_index);
  785. Print(high);
  786. Print(low);
  787. high_index = user_input_index;
  788. low_index = user_input_index;
  789. Print("index is : ",user_input_index);
  790. foo(user_input_index);
  791. }
  792. else
  793. {
  794. Print("Object Not Found");
  795. }
  796. return(INIT_SUCCEEDED);
  797. }
  798. //+------------------------------------------------------------------+
  799. //| Expert deinitialization function |
  800. //+------------------------------------------------------------------+
  801. void OnDeinit(const int reason)
  802. {
  803. //---
  804. }
  805. //+------------------------------------------------------------------+
  806. //| Expert tick function |
  807. //+------------------------------------------------------------------+
  808. void OnTick()
  809. {
  810. foo1(1);
  811. for(int m=0; m < orderNum; m++)
  812. {
  813. if(od_1[m].choch_index != -1)
  814. {
  815. //Print(" time to match the condition : ",TimeCurrent());
  816. if(TimeCurrent() > od_1[m].Time_of_candle_For_trade && od_1[m].Signal == "buy")
  817. {
  818. //Print("time to match the condition : ",TimeCurrent());
  819. if(iHigh(Symbol(),PERIOD_CURRENT,od_1[m].reversal_index) > iLow(Symbol(),PERIOD_CURRENT,0) && iClose(Symbol(),PERIOD_CURRENT,od_1[m].choch_index) < iClose(Symbol(),PERIOD_CURRENT,0))
  820. {
  821. ObjectCreate(0,"buy trade"+TimeCurrent(),OBJ_ARROW_BUY,0,TimeCurrent(),iHigh(Symbol(),PERIOD_CURRENT,0));
  822. Print("trade placed buy at index ",0);
  823. od_1[m].choch_index = -1;
  824. }
  825. Print(" time to take trade is ",od_1[m].Time_of_candle_For_trade);
  826. }
  827. if(TimeCurrent() >= od_1[m].Time_of_candle_For_trade && od_1[m].Signal == "sell")
  828. {
  829. Print(TimeCurrent());
  830. //
  831. // Print(" time to take trade is ",od_1[m].Time_of_candle_For_trade);
  832. od_1[m].choch_index = -1;
  833. }
  834. }
  835. }
  836. }
  837. //+------------------------------------------------------------------+
  838. //| |
  839. //+------------------------------------------------------------------+
  840. bool NewBar()
  841. {
  842. static datetime Last_bar ;
  843. datetime Current_bar = iTime(Symbol(), PERIOD_CURRENT,0);
  844. if(Current_bar != Last_bar)
  845. {
  846. Last_bar = Current_bar;
  847. return (true);
  848. }
  849. else
  850. {
  851. return (false);
  852. }
  853. }
  854. //+------------------------------------------------------------------+
  855. //| |
  856. //+------------------------------------------------------------------+
  857. bool object_find(string obj) // to find object placed by user in the chart
  858. {
  859. if(ObjectFind(0,obj) >= 0)
  860. {
  861. return true;
  862. }
  863. return false;
  864. }
  865. //+------------------------------------------------------------------+
  866. //| |
  867. //+------------------------------------------------------------------+
  868. string checkCandle(int v) // to check the candle is bullish or bearish
  869. {
  870. double close = iClose(Symbol(),PERIOD_CURRENT,v);
  871. double open = iOpen(Symbol(),PERIOD_CURRENT,v);
  872. if(close > open)
  873. {
  874. return "Bullish";
  875. }
  876. else
  877. if(close < open)
  878. {
  879. return "Bearish";
  880. }
  881. return "empty";
  882. }
  883. //+------------------------------------------------------------------+
  884. //+------------------------------------------------------------------+