Keith Watford:
What is the broker time when you are checking?
What is the broker time when you are checking?
The hour was 9 in the log because I have a variable that resets at the open of the London Session. I was able to it to work before, so something must've changed in my code.
//+------------------------------------------------------------------+ //| My_First_EA.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Expert initialization function | //+--------------------------- #include <Trade\Trade.mqh> #include <Trade\AccountInfo.mqh> #include <Object.mqh> #include <Trade\PositionInfo.mqh> #include <Trade\SymbolInfo.mqh> CTrade m_trade; CPositionInfo m_position; CAccountInfo currentSymbol; CSymbolInfo currentInfo; int StopLoss=100; int TakeProfit=100; input int ADX_Period=8; input int Fast_MA_Period=5; input int Slow_MA_Period=100; int Really_Slow_MA_Period = 60; input int EA_Magic=12345; input double ADX_Min=22.0; input double percentCapitalUtilized = 0.5; int adxHandle; int FastMAHandle; int SlowMAHandle; int ReallySlowMAHandle; int pSarHandle; double plsDI[],minDI[],adxVal[]; double FastMAVal[]; double SlowMAVal[]; double ReallySlowMAVal[]; double pSarVal[]; double p_close; double FXdotcom_Balance; double Previous_Spread; double Previous_Balance; int STP,TKP; string PositionType = "None"; bool positionOpenedThisDay = false; int OnInit() { FXdotcom_Balance = currentSymbol.Balance(); Previous_Balance = currentSymbol.Balance(); Print("FXdotcom_Balance = ",FXdotcom_Balance); //--- //adxHandle=iADX(NULL,0,ADX_Period); FastMAHandle=iMA(_Symbol,_Period,Fast_MA_Period,0,MODE_EMA,PRICE_CLOSE); SlowMAHandle=iMA(_Symbol,_Period,Slow_MA_Period,0,MODE_EMA,PRICE_CLOSE); ReallySlowMAHandle=iMA(_Symbol,_Period,Really_Slow_MA_Period,0,MODE_EMA,PRICE_CLOSE); pSarHandle=iSAR(_Symbol,_Period,0.02,0.2); if( FastMAHandle<0 || pSarHandle<0 || SlowMAHandle<0 || ReallySlowMAHandle<0) { Alert("Error creating handles for PSar and MA's - error: ",GetLastError(),"!!"); } STP=StopLoss; TKP=TakeProfit; if(_Digits==5 || _Digits==3) { STP=STP*10; TKP=TKP*10; } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- //IndicatorRelease(adxHandle); IndicatorRelease(FastMAHandle); IndicatorRelease(SlowMAHandle); IndicatorRelease(ReallySlowMAHandle); IndicatorRelease(pSarHandle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(Bars(_Symbol,_Period)<60) { Alert("We have less than 60 bars, EA will now exit!"); return; } //+------------------------------------------------------------------+ static datetime Old_Time; datetime New_Time[1]; /*bool IsNewBar=false; int copied=CopyTime(_Symbol,_Period,0,1,New_Time); if(copied>0) { if(Old_Time!=New_Time[0]) { IsNewBar=true; if(MQL5InfoInteger(MQL5_DEBUGGING)) { Print("We have new bar here ",New_Time[0],"old time was",Old_Time); Old_Time=New_Time[0]; } } } else { Alert("Error in copying historical times data, error=",GetLastError()); ResetLastError(); return; } if(IsNewBar==false) { return; }*/ int MyBars=Bars(_Symbol,_Period); if(MyBars<60) { Alert("We have fewer than 60 bars, EA will now exit!!"); return; } MqlTick latest_price; MqlTradeRequest mrequest; MqlTradeResult mresult; MqlRates mrate[]; MqlDateTime mtime; TimeToStruct(TimeCurrent(),mtime); if (mtime.hour ==9 && mtime.min==0 && mtime.sec ==0) { positionOpenedThisDay = false; Print("positionOpenedThisDay = ",positionOpenedThisDay); } ZeroMemory(mrequest); ZeroMemory(mresult); ArraySetAsSeries(mrate,true); //ArraySetAsSeries(plsDI,true); //ArraySetAsSeries(minDI,true); //ArraySetAsSeries(adxVal,true); ArraySetAsSeries(FastMAVal,true); ArraySetAsSeries(SlowMAVal,true); ArraySetAsSeries(ReallySlowMAVal,true); ArraySetAsSeries(pSarVal,true); if(!SymbolInfoTick(_Symbol,latest_price)) { Alert("Error getting the latest price quote - error:",GetLastError(),"!!"); return; } if(CopyRates(_Symbol,_Period,0,3,mrate)<0) { Alert("Error copying rates/history data - error:",GetLastError(),"!!"); return; } /*if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0 || CopyBuffer(adxHandle,2,0,3,minDI)<0) { Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!"); return; }*/ if(CopyBuffer(FastMAHandle,0,0,3,FastMAVal)<0) { Alert("Error copying MA indicator buffer - error:",GetLastError(),"!!"); return; } if(CopyBuffer(SlowMAHandle,0,0,3,SlowMAVal)<0) { return; } if(CopyBuffer(pSarHandle,0,0,3,pSarVal)<0) { return; } if(CopyBuffer(ReallySlowMAHandle,0,0,3,ReallySlowMAVal)<0) { return; } p_close=mrate[1].close; bool Buy_Condition_MACrossover_Entry = (FastMAVal[0]>SlowMAVal[0] && FastMAVal[1]<SlowMAVal[1]); bool Buy_Condition_PSAR_Entry = latest_price.ask>pSarVal[0]; bool Buy_Condition_slowMA_Entry = latest_price.ask>ReallySlowMAVal[0]; bool Buy_Condition_Opening_Bar_Entry = latest_price.ask > mrate[1].high; if(Buy_Condition_Opening_Bar_Entry && PositionsTotal() ==0 && positionOpenedThisDay==false && ( (mtime.hour > 10 || (mtime.hour==10 && mtime.min >=1) || (mtime.hour < 8 || (mtime.hour =8 && mtime.min <59))) ) ) { Print("mtime.hour = ",mtime.hour); Print("mtime.min = ",mtime.min); Print("mtime.sec = ",mtime.sec); if(PositionsTotal()>0) { Alert("We already have ",PositionsTotal()," ",PositionType," position"); return; } Print("Buy conditions met. PositionType = ",PositionType); mrequest.action=TRADE_ACTION_DEAL; mrequest.price=NormalizeDouble(latest_price.ask,_Digits); //mrequest.sl=NormalizeDouble(latest_price.ask-STP*_Point,_Digits); //mrequest.tp=NormalizeDouble(latest_price.ask+TKP*_Point,_Digits); mrequest.symbol=_Symbol; mrequest.volume=(floor(percentCapitalUtilized*currentSymbol.Balance()/(latest_price.ask * 200))/10)>=0.1 ? (floor(percentCapitalUtilized*currentSymbol.Balance()/(latest_price.ask * 200))/10) :NULL; mrequest.magic=EA_Magic; mrequest.type=ORDER_TYPE_BUY; mrequest.type_filling=ORDER_FILLING_FOK; mrequest.deviation=100; OrderSend(mrequest,mresult); if (mresult.retcode==10006) { Print("Opening long position rejected!"); } else if(mresult.retcode==10009) { positionOpenedThisDay = true; PositionType = "buy"; Print("New long position opened. PositionsTotal() = ",PositionsTotal(),". PositionType = ",PositionType); Print("Spread = ",currentInfo.Spread()); Previous_Spread = currentInfo.Spread(); Print("Equity = ",currentSymbol.Equity()," Margin = ",currentSymbol.Margin()," Balance =",currentSymbol.Balance()); } } bool Sell_Condition_MACrossover_Entry = (FastMAVal[0]<SlowMAVal[0] && FastMAVal[1]>SlowMAVal[1]); bool Sell_Condition_PSAR_Entry = latest_price.bid<pSarVal[0]; bool Sell_Condition_slowMA_Entry = latest_price.bid<ReallySlowMAVal[0]; bool Sell_Condition_Opening_Bar_Entry = latest_price.bid < mrate[1].low; bool Long_Position_Exit_PSAR = latest_price.bid<pSarVal[0]; bool Long_Position_Exit_slowMA = latest_price.bid<ReallySlowMAVal[0]; bool Short_Position_Exit_PSAR = latest_price.ask>pSarVal[0]; bool Short_Position_Exit_slowMA = latest_price.ask>ReallySlowMAVal[0]; if(Sell_Condition_Opening_Bar_Entry && PositionsTotal() ==0 && positionOpenedThisDay==false && ( (mtime.hour > 10 || (mtime.hour==10 && mtime.min >=1) || (mtime.hour < 8 || (mtime.hour =8 && mtime.min <59))) ) ) { Print("Sell conditions are met. PositionType = ",PositionType); if(PositionsTotal()>0) { Alert("We already have ",PositionsTotal()," ",PositionType," positions"); return; } mrequest.action=TRADE_ACTION_DEAL; mrequest.price=NormalizeDouble(latest_price.bid,_Digits); //mrequest.sl=NormalizeDouble(latest_price.bid+STP*_Point,_Digits); // mrequest.tp=NormalizeDouble(latest_price.bid-TKP*_Point,_Digits); // Print("Attempmted stop value: ",NormalizeDouble(latest_price.bid+STP*_Point,_Digits)); mrequest.symbol=_Symbol; mrequest.volume=(floor(percentCapitalUtilized*currentSymbol.Balance()/(latest_price.bid * 200))/10)>=0.1 ? (floor(percentCapitalUtilized*currentSymbol.Balance()/(latest_price.bid * 200))/10) :NULL; mrequest.magic=EA_Magic; mrequest.type=ORDER_TYPE_SELL; mrequest.type_filling=ORDER_FILLING_FOK; mrequest.deviation=100; OrderSend(mrequest,mresult); if (mresult.retcode==10006) { Print("Opening short position rejected!"); } else if(mresult.retcode==10009) { positionOpenedThisDay = true; PositionType = "sell"; Print("New short position opened. PositionsTotal() = ",PositionsTotal(),". PositionType = ",PositionType); Previous_Spread = currentInfo.Spread(); Print("Equity = ",currentSymbol.Equity()," Margin = ",currentSymbol.Margin()," Balance =",currentSymbol.Balance()); } } if(PositionsTotal() == 1 && mtime.hour == 8 && mtime.min == 59) { Print("Conditions met to close position. PositionType = ",PositionType); /*mrequest.action=TRADE_ACTION_CLOSE_BY; mrequest.price=NormalizeDouble(latest_price.bid,_Digits); //mrequest.sl=NormalizeDouble(latest_price.bid+STP*_Point,_Digits); // mrequest.tp=NormalizeDouble(latest_price.bid-TKP*_Point,_Digits); //Print("Attempmted stop value: ",NormalizeDouble(latest_price.bid+STP*_Point,_Digits)); mrequest.symbol=_Symbol; mrequest.volume=Lot; mrequest.magic=EA_Magic; mrequest.type=ORDER_TYPE_CLOSE_BY; mrequest.type_filling=ORDER_FILLING_FOK; //mrequest.deviation=100; OrderSend(mrequest,mresult);*/ if ( PositionsTotal() > 0) { for (int i=0; i < PositionsTotal() ; i++) { if(m_position.SelectByIndex(i)) { if (m_position.Symbol()==Symbol() && m_position.Magic()==EA_Magic) { if (m_position.PositionType() == POSITION_TYPE_SELL || m_position.PositionType() == POSITION_TYPE_BUY ) { m_trade.PositionClose(m_position.Ticket()); // Close the selected position } } } } } if (m_trade.ResultRetcode()==10006) { Print("Closing long position rejected!"); } else if(m_trade.ResultRetcode()==10009) { PositionType = "None"; Print("Long position closed. PositionType = ",PositionType); FXdotcom_Balance = currentSymbol.Balance() + FXdotcom_Balance - Previous_Balance + (10*m_position.Volume()*Previous_Spread/20); Previous_Balance = currentSymbol.Balance(); Print("FXdotcom_Balance = ",FXdotcom_Balance); Print("Equity = ",currentSymbol.Equity()," Margin = ",currentSymbol.Margin()," Balance =",currentSymbol.Balance()); } } if(PositionsTotal() == 1 && mtime.hour == 8 && mtime.min == 59) { Print("conditions met to close position. PositionType = ",PositionType); //ZeroMemory(mrequest); //ZeroMemory(mresult); /* mrequest.action=TRADE_ACTION_CLOSE_BY; // type of trade operation mrequest.price=NormalizeDouble(latest_price.ask,_Digits); //mrequest.sl=NormalizeDouble(latest_price.ask+STP*_Point,_Digits); // mrequest.tp=NormalizeDouble(latest_price.bid-TKP*_Point,_Digits); //Print("Attempmted stop value: ",NormalizeDouble(latest_price.bid+STP*_Point,_Digits)); mrequest.symbol=_Symbol; //mrequest.volume=Lot; mrequest.magic=EA_Magic; mrequest.type=ORDER_TYPE_CLOSE_BY; //mrequest.type_filling=ORDER_FILLING_FOK; //mrequest.deviation=100; OrderSend(mrequest,mresult); */ if ( PositionsTotal() > 0) { for (int i=0; i < PositionsTotal() ; i++) { if(m_position.SelectByIndex(i)) { if (m_position.Symbol()==Symbol() && m_position.Magic()==EA_Magic) { if (m_position.PositionType() == POSITION_TYPE_SELL || m_position.PositionType() == POSITION_TYPE_BUY ) { m_trade.PositionClose(m_position.Ticket()); // Close the selected position } } } } } if (m_trade.ResultRetcode()==10006) { Print("Closing short position rejected!"); } else if(m_trade.ResultRetcode()==10009) { PositionType = "None"; Print("Short position closed. PositionType = ",PositionType); FXdotcom_Balance = currentSymbol.Balance() + FXdotcom_Balance - Previous_Balance + (10*m_position.Volume()*Previous_Spread/20); Previous_Balance = currentSymbol.Balance(); Print("FXdotcom_Balance = ",FXdotcom_Balance); Print("Equity = ",currentSymbol.Equity()," Margin = ",currentSymbol.Margin()," Balance =",currentSymbol.Balance()); } } if(PositionsTotal()>1) { Print("THIS IS NOT GOOD! WE HAVE MULTIPLE OPEN POSITIONS!"); } }
Keith Watford:
What is the broker time when you are checking?
What is the broker time when you are checking?
For some reason mtime. hour suddenly changes to 1. The first line prints when the time is exactly 9:00:00 and the second line prints when the buy conditions are met. For that to happen, a 1 hour bar needs to be available from the London session which would be any hour except 9.
EI 0 08:16:28.479 Core 1 2020.05.26 09:00:00 mtime.hour = 9
KS 0 08:16:28.479 Core 1 2020.05.26
09:00:00 mtime. hour = 1
Hi please see https://www.mql5.com/en/articles/599
MQL5 Programming Basics: Time
- www.mql5.com
MQL5 offers a number of simple functions for working with time and you should not find it difficult getting familiar with them. The range of tasks that require use of date and time is quite small. The main tasks are: To perform certain actions at a given point of time (Fig. 1). These may be actions performed at the same time each day or at a...
I changed my redundant condition of (
(mtime.hour > 10
|| (mtime.hour==10
&& mtime.min >=1)
|| (mtime.hour < 8
|| (mtime.hour =8
&& mtime.min <59)))
) to just mtime.hour != 9 and that fixed the problem.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
When I try to print the hours, minutes, and seconds, the minutes and seconds are fine but the hours just prints 1.