Guest User

Untitled

a guest
Mar 17th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //| lazyTrade.mq5 |
  3. //| Copyright 2019, Strix Capital |
  4. //| https://www.mql5.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2019, Strix Capital"
  7. #property link "https://www.mql5.com"
  8. #property version "1.00"
  9. //--- input parameters
  10. input int TakeProfit=1500;
  11. input int StopLoss=500;
  12. input int PendingGap=300;
  13.  
  14. #include <Trade\Trade.mqh> //include the library for execution of trades
  15. #include <Trade\PositionInfo.mqh> //include the library for obtaining information on positions
  16.  
  17. CTrade m_Trade; //structure for execution of trades
  18. CPositionInfo m_Position; //structure for obtaining information of positions
  19.  
  20. double EURUSDadxArray[];
  21. double USDJPYadxArray[];
  22. double EURJPYadxArray[];
  23.  
  24. bool EURUSDcanRetry = 0;
  25. bool EURJPYcanRetry = 0;
  26. bool USDJPYcanRetry = 0;
  27.  
  28. double EURUSDweekStartAsk;
  29. double EURJPYweekStartAsk;
  30. double USDJPYweekStartAsk;
  31. double EURUSDweekStartBid;
  32. double EURJPYweekStartBid;
  33. double USDJPYweekStartBid;
  34.  
  35. //+------------------------------------------------------------------+
  36. //| Expert initialization function |
  37. //+------------------------------------------------------------------+
  38. int OnInit()
  39. {
  40. return(INIT_SUCCEEDED);
  41. }
  42. //+------------------------------------------------------------------+
  43. //| Expert tick function |
  44. //+------------------------------------------------------------------+
  45. void OnTick()
  46. {
  47. int bars = Bars(_Symbol,_Period);
  48.  
  49. double EURUSDask = SymbolInfoDouble("EURUSD",SYMBOL_ASK);
  50. double EURUSDbid = SymbolInfoDouble("EURUSD",SYMBOL_BID);
  51. double EURJPYask = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
  52. double EURJPYbid = SymbolInfoDouble("EURJPY",SYMBOL_BID);
  53. double USDJPYask = SymbolInfoDouble("USDJPY",SYMBOL_ASK);
  54. double USDJPYbid = SymbolInfoDouble("USDJPY",SYMBOL_BID);
  55.  
  56. int EURUSDadx = iADX("EURUSD",PERIOD_D1,14);
  57. CopyBuffer(EURUSDadx,0,0,1,EURUSDadxArray);
  58. double EURUSDadxValue = NormalizeDouble(EURUSDadxArray[0],2);
  59.  
  60. int USDJPYadx = iADX("USDJPY",PERIOD_D1,14);
  61. CopyBuffer(USDJPYadx,0,0,1,USDJPYadxArray);
  62. double USDJPYadxValue = NormalizeDouble(USDJPYadxArray[0],2);
  63.  
  64. int EURJPYadx = iADX("EURJPY",PERIOD_D1,14);
  65. CopyBuffer(EURJPYadx,0,0,1,EURJPYadxArray);
  66. double EURJPYadxValue = NormalizeDouble(EURJPYadxArray[0],2);
  67.  
  68. datetime to = TimeCurrent();
  69. datetime from = to - PeriodSeconds(PERIOD_W1); // check deals for the current week
  70. ResetLastError();
  71. if(! HistorySelect(from, to))
  72. Print(__FUNCTION__, " HistorySelect=false. Error code=", GetLastError());
  73.  
  74. unsigned int numOrders = HistoryOrdersTotal();
  75. unsigned long ticket = HistoryOrderGetTicket(numOrders - 1);
  76.  
  77. ENUM_ORDER_REASON reason;
  78. string symbol;
  79. ENUM_ORDER_TYPE type;
  80. if(HistoryOrderSelect(ticket))
  81. reason = HistoryOrderGetInteger(ticket, ORDER_REASON);
  82. symbol = HistoryOrderGetString(ticket, ORDER_SYMBOL);
  83. type = HistoryOrderGetInteger(ticket, ORDER_TYPE);
  84.  
  85. if(EnumToString(reason) == "ORDER_REASON_SL" && symbol == "EURUSD" && EURUSDcanRetry){
  86. if (EnumToString(type) == "ORDER_TYPE_SELL"){
  87. m_Trade.BuyStop(0.01,EURUSDweekStartAsk+(0.00001*PendingGap),_Symbol,EURUSDweekStartAsk+(0.00001*PendingGap)-(0.00001*StopLoss),EURUSDweekStartAsk+(0.00001*PendingGap)+(0.00001*TakeProfit));
  88. EURUSDcanRetry = 0;
  89. }else if (EnumToString(type) == "ORDER_TYPE_BUY"){
  90. m_Trade.SellStop(0.01,EURUSDweekStartBid-(0.00001*PendingGap),_Symbol,EURUSDweekStartBid-(0.00001*PendingGap)+(0.00001*StopLoss),EURUSDweekStartBid-(0.00001*PendingGap)-(0.00001*TakeProfit));
  91. EURUSDcanRetry = 0;
  92. }
  93. }
  94.  
  95. if(EnumToString(reason) == "ORDER_REASON_SL" && symbol == "EURJPY" && EURJPYcanRetry){
  96. if (EnumToString(type) == "ORDER_TYPE_SELL"){
  97. m_Trade.BuyStop(0.01,EURJPYweekStartAsk+(0.01*PendingGap),"EURJPY",EURJPYweekStartAsk+(0.01*PendingGap)-(0.01*StopLoss),EURJPYweekStartAsk+(0.01*PendingGap)+(0.01*TakeProfit));
  98. EURJPYcanRetry = 0;
  99. }else if (EnumToString(type) == "ORDER_TYPE_BUY"){
  100. m_Trade.SellStop(0.01,EURJPYweekStartBid-(0.01*PendingGap),"EURJPY",EURJPYweekStartBid-(0.01*PendingGap)+(0.01*StopLoss),EURJPYweekStartBid-(0.01*PendingGap)-(0.01*TakeProfit));
  101. EURJPYcanRetry = 0;
  102. }
  103. }
  104.  
  105. if(EnumToString(reason) == "ORDER_REASON_SL" && symbol == "USDJPY" && USDJPYcanRetry){
  106. if (EnumToString(type) == "ORDER_TYPE_SELL"){
  107. m_Trade.BuyStop(0.01,USDJPYweekStartAsk+(0.01*PendingGap),"USDJPY",USDJPYweekStartAsk+(0.01*PendingGap)-(0.01*StopLoss),USDJPYweekStartAsk+(0.01*PendingGap)+(0.01*TakeProfit));
  108. USDJPYcanRetry = 0;
  109. }else if (EnumToString(type) == "ORDER_TYPE_BUY"){
  110. m_Trade.SellStop(0.01,USDJPYweekStartBid-(0.01*PendingGap),"USDJPY",USDJPYweekStartBid-(0.01*PendingGap)+(0.01*StopLoss),USDJPYweekStartBid-(0.01*PendingGap)-(0.01*TakeProfit));
  111. USDJPYcanRetry = 0;
  112. }
  113. }
  114.  
  115. if (isNewBar())
  116. {
  117. EURUSDcanRetry = 0;
  118. EURJPYcanRetry = 0;
  119. USDJPYcanRetry = 0;
  120.  
  121. EURUSDweekStartAsk = EURUSDask;
  122. EURJPYweekStartAsk = EURJPYask;
  123. USDJPYweekStartAsk = USDJPYask;
  124. EURUSDweekStartBid = EURUSDbid;
  125. EURJPYweekStartBid = EURJPYbid;
  126. USDJPYweekStartBid = USDJPYbid;
  127.  
  128. for (int i=PositionsTotal()-1;i>=0; i--)
  129. {
  130. if(!m_Trade.PositionClose(PositionGetSymbol(i)))
  131. {
  132. //--- failure message
  133. Print(PositionGetSymbol(i), "PositionClose() method failed. Return code=",m_Trade.ResultRetcode(),". Code description: ",m_Trade.ResultRetcodeDescription());
  134. }
  135. else
  136. {
  137. Print(PositionGetSymbol(i), "PositionClose() method executed successfully. Return code=",m_Trade.ResultRetcode()," (",m_Trade.ResultRetcodeDescription(),")");
  138. }
  139. }
  140.  
  141. if (EURUSDadxValue > 25){
  142. m_Trade.SellStop(0.01,EURUSDbid-(0.00001*PendingGap),"EURUSD",EURUSDbid-(0.00001*PendingGap)+(0.00001*StopLoss),EURUSDbid-(0.00001*PendingGap)-(0.00001*TakeProfit));
  143. m_Trade.BuyStop(0.01,EURUSDask+(0.00001*PendingGap),"EURUSD",EURUSDask+(0.00001*PendingGap)-(0.00001*StopLoss),EURUSDask+(0.00001*PendingGap)+(0.00001*TakeProfit));
  144. EURUSDcanRetry = 1;
  145. }
  146.  
  147. if (EURJPYadxValue > 25){
  148. m_Trade.SellStop(0.01,EURJPYbid-(0.01*PendingGap),"EURJPY",EURJPYbid-(0.01*PendingGap)+(0.01*StopLoss),EURJPYbid-(0.01*PendingGap)-(0.01*TakeProfit));
  149. m_Trade.BuyStop(0.01,EURJPYask+(0.01*PendingGap),"EURJPY",EURJPYask+(0.01*PendingGap)-(0.01*StopLoss),EURJPYask+(0.0001*PendingGap)+(0.01*TakeProfit));
  150. EURJPYcanRetry = 1;
  151. }
  152.  
  153. if (USDJPYadxValue > 25){
  154. m_Trade.SellStop(0.01,USDJPYbid-(0.01*PendingGap),"USDJPY",USDJPYbid-(0.01*PendingGap)+(0.01*StopLoss),USDJPYbid-(0.01*PendingGap)-(0.01*TakeProfit));
  155. m_Trade.BuyStop(0.01,USDJPYask+(0.01*PendingGap),"USDJPY",USDJPYask+(0.01*PendingGap)-(0.01*StopLoss),USDJPYask+(0.01*PendingGap)+(0.01*TakeProfit));
  156. USDJPYcanRetry = 1;
  157. }
  158. }
  159.  
  160. }
  161. //+------------------------------------------------------------------+
  162.  
  163. bool isNewBar()
  164. {
  165. //--- memorize the time of opening of the last bar in the static variable
  166. static datetime last_time=0;
  167. //--- current time
  168. datetime lastbar_time=SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
  169.  
  170. //--- if it is the first call of the function
  171. if(last_time==0)
  172. {
  173. //--- set the time and exit
  174. last_time=lastbar_time;
  175. return(false);
  176. }
  177.  
  178. //--- if the time differs
  179. if(last_time!=lastbar_time)
  180. {
  181. //--- memorize the time and return true
  182. last_time=lastbar_time;
  183. return(true);
  184. }
  185. //--- if we passed to this line, then the bar is not new; return false
  186. return(false);
  187. }
Add Comment
Please, Sign In to add comment