Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+------------------------------------------------------------------+
- //| lazyTrade.mq5 |
- //| Copyright 2019, Strix Capital |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2019, Strix Capital"
- #property link "https://www.mql5.com"
- #property version "1.00"
- //--- input parameters
- input int TakeProfit=1500;
- input int StopLoss=500;
- input int PendingGap=300;
- #include <Trade\Trade.mqh> //include the library for execution of trades
- #include <Trade\PositionInfo.mqh> //include the library for obtaining information on positions
- CTrade m_Trade; //structure for execution of trades
- CPositionInfo m_Position; //structure for obtaining information of positions
- double EURUSDadxArray[];
- double USDJPYadxArray[];
- double EURJPYadxArray[];
- bool EURUSDcanRetry = 0;
- bool EURJPYcanRetry = 0;
- bool USDJPYcanRetry = 0;
- double EURUSDweekStartAsk;
- double EURJPYweekStartAsk;
- double USDJPYweekStartAsk;
- double EURUSDweekStartBid;
- double EURJPYweekStartBid;
- double USDJPYweekStartBid;
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- int bars = Bars(_Symbol,_Period);
- double EURUSDask = SymbolInfoDouble("EURUSD",SYMBOL_ASK);
- double EURUSDbid = SymbolInfoDouble("EURUSD",SYMBOL_BID);
- double EURJPYask = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
- double EURJPYbid = SymbolInfoDouble("EURJPY",SYMBOL_BID);
- double USDJPYask = SymbolInfoDouble("USDJPY",SYMBOL_ASK);
- double USDJPYbid = SymbolInfoDouble("USDJPY",SYMBOL_BID);
- int EURUSDadx = iADX("EURUSD",PERIOD_D1,14);
- CopyBuffer(EURUSDadx,0,0,1,EURUSDadxArray);
- double EURUSDadxValue = NormalizeDouble(EURUSDadxArray[0],2);
- int USDJPYadx = iADX("USDJPY",PERIOD_D1,14);
- CopyBuffer(USDJPYadx,0,0,1,USDJPYadxArray);
- double USDJPYadxValue = NormalizeDouble(USDJPYadxArray[0],2);
- int EURJPYadx = iADX("EURJPY",PERIOD_D1,14);
- CopyBuffer(EURJPYadx,0,0,1,EURJPYadxArray);
- double EURJPYadxValue = NormalizeDouble(EURJPYadxArray[0],2);
- datetime to = TimeCurrent();
- datetime from = to - PeriodSeconds(PERIOD_W1); // check deals for the current week
- ResetLastError();
- if(! HistorySelect(from, to))
- Print(__FUNCTION__, " HistorySelect=false. Error code=", GetLastError());
- unsigned int numOrders = HistoryOrdersTotal();
- unsigned long ticket = HistoryOrderGetTicket(numOrders - 1);
- ENUM_ORDER_REASON reason;
- string symbol;
- ENUM_ORDER_TYPE type;
- if(HistoryOrderSelect(ticket))
- reason = HistoryOrderGetInteger(ticket, ORDER_REASON);
- symbol = HistoryOrderGetString(ticket, ORDER_SYMBOL);
- type = HistoryOrderGetInteger(ticket, ORDER_TYPE);
- if(EnumToString(reason) == "ORDER_REASON_SL" && symbol == "EURUSD" && EURUSDcanRetry){
- if (EnumToString(type) == "ORDER_TYPE_SELL"){
- m_Trade.BuyStop(0.01,EURUSDweekStartAsk+(0.00001*PendingGap),_Symbol,EURUSDweekStartAsk+(0.00001*PendingGap)-(0.00001*StopLoss),EURUSDweekStartAsk+(0.00001*PendingGap)+(0.00001*TakeProfit));
- EURUSDcanRetry = 0;
- }else if (EnumToString(type) == "ORDER_TYPE_BUY"){
- m_Trade.SellStop(0.01,EURUSDweekStartBid-(0.00001*PendingGap),_Symbol,EURUSDweekStartBid-(0.00001*PendingGap)+(0.00001*StopLoss),EURUSDweekStartBid-(0.00001*PendingGap)-(0.00001*TakeProfit));
- EURUSDcanRetry = 0;
- }
- }
- if(EnumToString(reason) == "ORDER_REASON_SL" && symbol == "EURJPY" && EURJPYcanRetry){
- if (EnumToString(type) == "ORDER_TYPE_SELL"){
- m_Trade.BuyStop(0.01,EURJPYweekStartAsk+(0.01*PendingGap),"EURJPY",EURJPYweekStartAsk+(0.01*PendingGap)-(0.01*StopLoss),EURJPYweekStartAsk+(0.01*PendingGap)+(0.01*TakeProfit));
- EURJPYcanRetry = 0;
- }else if (EnumToString(type) == "ORDER_TYPE_BUY"){
- m_Trade.SellStop(0.01,EURJPYweekStartBid-(0.01*PendingGap),"EURJPY",EURJPYweekStartBid-(0.01*PendingGap)+(0.01*StopLoss),EURJPYweekStartBid-(0.01*PendingGap)-(0.01*TakeProfit));
- EURJPYcanRetry = 0;
- }
- }
- if(EnumToString(reason) == "ORDER_REASON_SL" && symbol == "USDJPY" && USDJPYcanRetry){
- if (EnumToString(type) == "ORDER_TYPE_SELL"){
- m_Trade.BuyStop(0.01,USDJPYweekStartAsk+(0.01*PendingGap),"USDJPY",USDJPYweekStartAsk+(0.01*PendingGap)-(0.01*StopLoss),USDJPYweekStartAsk+(0.01*PendingGap)+(0.01*TakeProfit));
- USDJPYcanRetry = 0;
- }else if (EnumToString(type) == "ORDER_TYPE_BUY"){
- m_Trade.SellStop(0.01,USDJPYweekStartBid-(0.01*PendingGap),"USDJPY",USDJPYweekStartBid-(0.01*PendingGap)+(0.01*StopLoss),USDJPYweekStartBid-(0.01*PendingGap)-(0.01*TakeProfit));
- USDJPYcanRetry = 0;
- }
- }
- if (isNewBar())
- {
- EURUSDcanRetry = 0;
- EURJPYcanRetry = 0;
- USDJPYcanRetry = 0;
- EURUSDweekStartAsk = EURUSDask;
- EURJPYweekStartAsk = EURJPYask;
- USDJPYweekStartAsk = USDJPYask;
- EURUSDweekStartBid = EURUSDbid;
- EURJPYweekStartBid = EURJPYbid;
- USDJPYweekStartBid = USDJPYbid;
- for (int i=PositionsTotal()-1;i>=0; i--)
- {
- if(!m_Trade.PositionClose(PositionGetSymbol(i)))
- {
- //--- failure message
- Print(PositionGetSymbol(i), "PositionClose() method failed. Return code=",m_Trade.ResultRetcode(),". Code description: ",m_Trade.ResultRetcodeDescription());
- }
- else
- {
- Print(PositionGetSymbol(i), "PositionClose() method executed successfully. Return code=",m_Trade.ResultRetcode()," (",m_Trade.ResultRetcodeDescription(),")");
- }
- }
- if (EURUSDadxValue > 25){
- m_Trade.SellStop(0.01,EURUSDbid-(0.00001*PendingGap),"EURUSD",EURUSDbid-(0.00001*PendingGap)+(0.00001*StopLoss),EURUSDbid-(0.00001*PendingGap)-(0.00001*TakeProfit));
- m_Trade.BuyStop(0.01,EURUSDask+(0.00001*PendingGap),"EURUSD",EURUSDask+(0.00001*PendingGap)-(0.00001*StopLoss),EURUSDask+(0.00001*PendingGap)+(0.00001*TakeProfit));
- EURUSDcanRetry = 1;
- }
- if (EURJPYadxValue > 25){
- m_Trade.SellStop(0.01,EURJPYbid-(0.01*PendingGap),"EURJPY",EURJPYbid-(0.01*PendingGap)+(0.01*StopLoss),EURJPYbid-(0.01*PendingGap)-(0.01*TakeProfit));
- m_Trade.BuyStop(0.01,EURJPYask+(0.01*PendingGap),"EURJPY",EURJPYask+(0.01*PendingGap)-(0.01*StopLoss),EURJPYask+(0.0001*PendingGap)+(0.01*TakeProfit));
- EURJPYcanRetry = 1;
- }
- if (USDJPYadxValue > 25){
- m_Trade.SellStop(0.01,USDJPYbid-(0.01*PendingGap),"USDJPY",USDJPYbid-(0.01*PendingGap)+(0.01*StopLoss),USDJPYbid-(0.01*PendingGap)-(0.01*TakeProfit));
- m_Trade.BuyStop(0.01,USDJPYask+(0.01*PendingGap),"USDJPY",USDJPYask+(0.01*PendingGap)-(0.01*StopLoss),USDJPYask+(0.01*PendingGap)+(0.01*TakeProfit));
- USDJPYcanRetry = 1;
- }
- }
- }
- //+------------------------------------------------------------------+
- bool isNewBar()
- {
- //--- memorize the time of opening of the last bar in the static variable
- static datetime last_time=0;
- //--- current time
- datetime lastbar_time=SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
- //--- if it is the first call of the function
- if(last_time==0)
- {
- //--- set the time and exit
- last_time=lastbar_time;
- return(false);
- }
- //--- if the time differs
- if(last_time!=lastbar_time)
- {
- //--- memorize the time and return true
- last_time=lastbar_time;
- return(true);
- }
- //--- if we passed to this line, then the bar is not new; return false
- return(false);
- }
Add Comment
Please, Sign In to add comment