//+------------------------------------------------------------------------------------------------------------------------------------------+ //| ObserveCandle_ea_MT5_3.mq5 | //+------------------------------------------------------------------------------------------------------------------------------------------+ /*------------------------------------------------------------------------------------------------------------------------ specification ---// //------------------------------------------------------------------------------------------------------------------------------------------*/ #include CTrade trade; //==========================================================================================================================================// enum ENUM_AP_PRICE { CLOSE=0, OPEN=1, HIGH=2, LOW=3 }; //==========================================================================================================================================// input string _1_="___ trade entry settings ___"; input double FixedLot=0.1; input int CandlesToObserve=3; input double MinMovement=20.0; input double StopLoss=0.0; input double TakeProfit=0.0; input bool Use_TradingBan=true; input int TradingBan_sec=30; input bool Trailing=true; input double TrailStart=20.0; input double TrailStop=20.0; input double TrailStep=1.0; input string _2_="___ other settings ___"; input int MagicNumber=25172; input string TradeComment=""; input ENUM_TIMEFRAMES TF=PERIOD_CURRENT; input int ExpirationTime_sec=3600; //===================================================================================================================== global variables ===// MqlTick tick; datetime TimeNow, TimePrev; double TickSize, TickValue, Spread, StopLevel, MinLot, MaxLot, LotStep, Pnt, Total_result, Result; bool isBO, isSO, isO, _isBO, _isSO, _isO, Ans, Activate, FreeMarginAlert, IsTester, IsModify; //============================================================================================ The initialization function of the expert ===// void OnInit() { Activate=false; if(MQL5InfoInteger(MQL5_TESTING) || MQL5InfoInteger(MQL5_OPTIMIZATION) || MQL5InfoInteger(MQL5_VISUAL_MODE)) IsTester=true; else IsTester=false; Pnt=_Point; trade.SetExpertMagicNumber(MagicNumber); trade.SetDeviationInPoints(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)); trade.SetTypeFilling(ORDER_FILLING_FOK); trade.SetAsyncMode(false); Activate=true; return; } //========================================================================================== The deinitialization function of the expert ===// void OnDeinit(const int reason) { Comment(""); Obj_Del(); if(!IsTester) { if(reason==0 || reason==1) { GVD("IsModify"); GVD("TimePrev"); } } return; } //====================================================================================================== The main function of the expert ===// void OnTick() { if(!Activate) return; if(!GetLatestPrice()) return; GetMarketInfo(); drawFixedLbl("str1","ObserveCandles",0,25,25,14,"Courier New",clrLime); drawFixedLbl("str2","Candles To Count="+IntegerToString(CandlesToObserve),0,25,50,14,"Courier New",clrLime); double movement=0.0; if(!isO && Time(CandlesToObserve-1,TF)>TimePrev) movement=(ND(Get_highest()-Get_lowest()))/Pnt; drawFixedLbl("str3","Movement="+DoubleToString(movement,1)+", Min="+DoubleToString(MinMovement,1),0,25,75,14,"Courier New",clrLime); drawFixedLbl("str4","StopLoss="+DoubleToString(StopLoss,1)+", TakeProfit="+DoubleToString(TakeProfit,1),0,25,100,14,"Courier New",clrLime); drawFixedLbl("str5","Floating PL="+DoubleToString(GetPos("_Profit"),2),0,25,125,14,"Courier New",clrLime); if(Trailing) { drawFixedLbl("str6","TrailStart="+DoubleToString(TrailStart,1)+", TrailStop="+DoubleToString(TrailStop,1)+", TrailStep="+DoubleToString(TrailStep,1),0,25,150,14,"Courier New",clrLime); } CheckPos(); if(!IsTester) { IsModify=(bool)GVG("IsModify"); TimePrev=(datetime)GVG("TimePrev"); } if(!IsModify && isO) { if(isBO) IsModify=Modify_SL_TP(1); else if(isSO) IsModify=Modify_SL_TP(-1); } Trail(); Trade(); if(!IsTester) { GVS("IsModify",(double)IsModify); GVS("TimePrev",(double)TimePrev); } return; } //==========================================================================================================================================// void Trade() { TimeNow=Time(0,TF); if(isBO) { if(TimePrev!=TimeNow) TimePrev=TimeNow; if((StopLoss>0.0 && ND(GetPos("Op")-tick.bid)>=StopLoss*Pnt) || (TakeProfit>0.0 && ND(tick.bid-GetPos("Op"))>=StopLoss*Pnt) || (ExpirationTime_sec>0 && (int)TimeCurrent()-GetPosInt("Ot")>=ExpirationTime_sec && GetPos("_Profit")<0.0)) { trade.PositionClose(_Symbol); } return; } if(isSO) { if(TimePrev!=TimeNow) TimePrev=TimeNow; if((StopLoss>0.0 && ND(tick.ask-GetPos("Op"))>=StopLoss*Pnt) || (TakeProfit>0.0 && ND(GetPos("Op")-tick.ask)>=StopLoss*Pnt) || (ExpirationTime_sec>0 && (int)TimeCurrent()-GetPosInt("Ot")>=ExpirationTime_sec && GetPos("_Profit")<0.0)) { trade.PositionClose(_Symbol); } return; } if(TimePrev=MinMovement*Pnt) { if(!Use_TradingBan || (Use_TradingBan && TimeCurrent()-TimeNow<=TradingBan_sec)) { if(!TradeBuy(FixedLot,TradeComment)) return; IsModify=Modify_SL_TP(1); if(TimePrev!=TimeNow) TimePrev=TimeNow; } } if(TimePrev=MinMovement*Pnt) { if(!Use_TradingBan || (Use_TradingBan && TimeCurrent()-TimeNow<=TradingBan_sec)) { if(!TradeSell(FixedLot,TradeComment)) return; IsModify=Modify_SL_TP(-1); if(TimePrev!=TimeNow) TimePrev=TimeNow; } } } //==========================================================================================================================================// double Get_highest() { double high=0.0, highest=GetBarValue(0,HIGH,TF); int i=0; for(i=1; i<=CandlesToObserve-1; i++) { high=GetBarValue(i,HIGH,TF); if(high>highest) highest=high; } // for return(highest); } //==========================================================================================================================================// double Get_lowest() { double low=0.0, lowest=GetBarValue(0,LOW,TF); int i=0; for(i=1; i<=CandlesToObserve-1; i++) { low=GetBarValue(i,LOW,TF); if(low>lowest) lowest=low; } // for return(lowest); } //==========================================================================================================================================// double GetPos(string value) { if(PositionSelect(_Symbol)) { if(value=="Op") return(PositionGetDouble(POSITION_PRICE_OPEN)); else if(value=="Sl") return(PositionGetDouble(POSITION_SL)); else if(value=="Tp") return(PositionGetDouble(POSITION_TP)); else if(value=="Lot") return(PositionGetDouble(POSITION_VOLUME)); else if(value=="Swap") return(PositionGetDouble(POSITION_SWAP)); else if(value=="Commission") return(PositionGetDouble(POSITION_COMMISSION)); else if(value=="Profit") return(PositionGetDouble(POSITION_PROFIT)); else if(value=="_Profit") return(PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP)+PositionGetDouble(POSITION_COMMISSION)); } return(0.0); } //==========================================================================================================================================// int GetPosInt(string value) { if(PositionSelect(_Symbol)) { if(value=="Ot") return((int)PositionGetInteger(POSITION_TIME)); } return(0.0); } //==========================================================================================================================================// bool Trail() { if(!Trailing || !isO) return(true); double tp=GetPos("Tp"), sl=GetPos("Sl"), op=GetPos("Op"), _sl=0.0; if(isBO) { if(ND(tick.bid-op)0.0) { _sl=NT(op-MathMax(StopLoss*Pnt,StopLevel)); if(ND(tick.bid-_sl)0.0) { _tp=NT(op+MathMax(TakeProfit*Pnt,StopLevel)); if(ND(_tp-tick.bid)0.0) { _sl=NT(op+MathMax(StopLoss*Pnt,StopLevel)); if(ND(_sl-tick.ask)0.0) { _tp=NT(op-MathMax(TakeProfit*Pnt,StopLevel)); if(ND(tick.ask-_tp)=0; i--) { label=ObjectName(0,i); if(StringSubstr(label,0,3)=="str") ObjectDelete(0,label); } /*for*/ } //==========================================================================================================================================//