//+------------------------------------------------------------------+ //| DemarkLinesEA.mq4 | //| Copyright © 2008, Forex-Experts | //| http://www.forex-experts.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Forex-Experts" #property link "http://www.forex-experts.com" extern bool Reverse = false; extern bool TradeOrder1 = true; extern string TradeComment1 = "Trade1"; extern double TradeLots1 = 0.1; extern int TradeSL1 = 30; extern int TradeTS1 = 50; extern int TradeTP1 = 0; extern bool TradeOrder2 = true; extern string TradeComment2 = "Trade2"; extern double TradeLots2 = 0.1; extern int TradeSL2 = 50; extern int TradeTS2 = 80; extern int TradeTP2 = 0; extern bool TradeOrder3 = true; extern string TradeComment3 = "Trade3"; extern double TradeLots3 = 0.1; extern int TradeSL3 = 80; extern int TradeTS3 = 120; extern int TradeTP3 = 0; extern bool TradeOrder4 = true; extern string TradeComment4 = "Trade4"; extern double TradeLots4 = 0.1; extern int TradeSL4 = 120; extern int TradeTS4 = 160; extern int TradeTP4 = 0; extern bool TradeAtCloseBar = true; extern int TrailingStop = 0; extern int TrailingStep = 1; //Trailing step extern int BreakEven = 0; extern int MagicNumber = 0; //For alerts: extern int Repeat=3; extern int Periods=5; extern bool UseAlert=false; extern bool SendEmail=true; extern string TradeLog="DemarkLinesEA"; extern int Slippage = 3; int mm = 0; double Risk = 10; int Crepeat=0; int AlertTime=0; double AheadTradeSec = 0; double AheadExitSec = 0; int TradeBar = 0; double MaxTradeTime = 300; extern string Indicator_Setting = "---------- Indicator Setting"; extern int showBars=3000; // if showBars= 0 so the indicator will be shown for all the chart extern int LevDP=2; // Demar Pint Levels; 2 = central bar will be higher (lower) then 2 bars on the left side and 2 bars on the right one. extern int qSteps=1; // number of steps/ no more than 3 extern int BackStep=0; // number of staps back extern int startBar=0; // ifstartBar=0 so we have recommendation for the current bar, if 1 - for possible next bar extern bool TrendLine=false; // false = no trend lines extern bool HorizontLine=false; // true = break levels extern bool ChannelLine=false; // true = channels which is parallel with trend lines extern bool TakeLines=false; // true = take profit lines extern bool Comments=false; // true = comments extern int Trend=0; // 1 = for UpTrendLines only, -1 = for DownTrendLines only, 0 = for all TrendLines extern bool ShowArrows=true; extern bool CustomFeatures=true; extern color UpTrendColor=Green; extern color DownTrendColor=Red; extern int TrendlineWidth=3; extern bool ShowAlerts=false; extern bool EmailAlert=false; int NumberOfTries = 5, //Number of tries to set, close orders; RetryTime = 1; double Ilo = 0; int DotLoc=7; static int TradeLast=0; string sound="alert.wav"; double sig_cur=0, sig_prev=0; int Spread=0; string filename=""; string OrderStr="DemarkLinesEA"; double sig_buy=0, sig_sell=0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- Crepeat=Repeat; //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if (TradeAtCloseBar) TradeBar=1; else TradeBar=0; filename=Symbol() + TradeLog + "-" + Month() + "-" + Day() + ".log"; Spread=MarketInfo(Symbol(),MODE_SPREAD); //---- int i=0; double BuyValue=0, SellValue=0; BuyValue=0; SellValue=0; if (CntOrd(OP_BUY,MagicNumber)>0) TradeLast=1; if (CntOrd(OP_SELL,MagicNumber)>0) TradeLast=-1; if (sig_cur>0) { BuyValue=1; } if (sig_cur<0) { SellValue=1; } int cnt=0,OpenPos=0,OpenSell=0,OpenBuy=0,CloseSell=0,CloseBuy=0; double mode=0,Stop=0,NewBarTime=0; //Here we found if new bar has just opened static int prevtime=0; int NewBar=0,FirstRun=1; if (FirstRun==1) { FirstRun=0; prevtime=Time[0]; } if ((prevtime == Time[0]) && (CurTime()-prevtime)>MaxTradeTime) { NewBar=0; } else { prevtime = Time[0]; NewBar=1; } int AllowTrade=0,AllowExit=0; //Trade before bar current bar closed if (CurTime()>= Time[0]+Period()*60-AheadTradeSec) AllowTrade=1; else AllowTrade=0; if (CurTime()>= Time[0]+Period()*60-AheadExitSec) AllowExit=1; else AllowExit=0; if (AheadTradeSec==0) AllowTrade=1; if (AheadExitSec==0) AllowExit=1; OpenPos=0; for(cnt=0; cnt=1) { OpenSell=0; OpenBuy=0; } OpenBuy=0; OpenSell=0; CloseBuy=0; CloseSell=0; sig_buy=iCustom(NULL,0,"DeMark lines",showBars,LevDP,qSteps,BackStep,startBar,TrendLine,HorizontLine,ChannelLine,TakeLines,Comments,Trend,ShowArrows,CustomFeatures,UpTrendColor,DownTrendColor,TrendlineWidth,ShowAlerts,EmailAlert,2,TradeBar); sig_sell=iCustom(NULL,0,"DeMark lines",showBars,LevDP,qSteps,BackStep,startBar,TrendLine,HorizontLine,ChannelLine,TakeLines,Comments,Trend,ShowArrows,CustomFeatures,UpTrendColor,DownTrendColor,TrendlineWidth,ShowAlerts,EmailAlert,3,TradeBar); //Conditions to open the position // if (sig_buy>0 && sig_buy!=EMPTY_VALUE) { OpenBuy=1; OpenSell=0; } if (sig_sell>0 && sig_sell!=EMPTY_VALUE) { OpenSell=1; OpenBuy=0; } if (OpenBuy>0 && OpenSell>0) { OpenSell=0; OpenBuy=0; } //Print("OpenSell=",OpenSell," OpenBuy=",OpenBuy); int OpTmp=0; if (Reverse) { OpTmp=OpenSell; OpenSell=OpenBuy; OpenBuy=OpTmp; } //Conditions to close the positions if (OpenSell>0) { CloseBuy=1; } if (OpenBuy>0) { CloseSell=1; } subPrintDetails(); for(cnt=0; cnt0) { SetText(Time[0],High[0]+1*DotLoc*Point,("CloseBuy"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta); PlaySound("alert.wav"); OrdClose(OrderTicket(),OrderLots(),Bid,Slippage,Red); Alerts(0, 0, CloseBuy, CloseSell,Bid,0,0,OrderTicket()); return(0); } if (TradeBar==0) { SetText(Time[0],High[0]+1*DotLoc*Point,("CloseBuy"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta); PlaySound("alert.wav"); OrdClose(OrderTicket(),OrderLots(),Bid,Slippage,Red); Alerts(0, 0, CloseBuy, CloseSell,Bid,0,0,OrderTicket()); return(0); } } } if (OrderType()==OP_SELL && OrderSymbol()==Symbol() && ((OrderMagicNumber () == MagicNumber) || MagicNumber==0)) { if (CloseSell==1 && AllowExit==1) { if (NewBar==1 && TradeBar>0) { SetText(Time[0],High[0]-0.3*DotLoc*Point,("CloseSell"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta); PlaySound("alert.wav"); OrdClose(OrderTicket(),OrderLots(),Ask,Slippage,Red); Alerts(0, 0, CloseBuy, CloseSell,Ask,0,0,OrderTicket()); return(0); } if (TradeBar==0) { SetText(Time[0],High[0]-0.3*DotLoc*Point,("CloseSell"+DoubleToStr(Time[0],0)),CharToStr(251),Magenta); PlaySound("alert.wav"); OrdClose(OrderTicket(),OrderLots(),Ask,Slippage,Red); Alerts(0, 0, CloseBuy, CloseSell,Ask,0,0,OrderTicket()); return(0); } } } } double MyStopLoss=0, MyTakeProfit=0; int ticket=0; //Should we open a position? if (OpenPos==0) { if (OpenSell==1 && AllowTrade==1) { if (NewBar==1 && TradeBar>0) { SetText(Time[0],High[0]+1*DotLoc*Point,("Sell"+DoubleToStr(Time[0],0)),CharToStr(234),Red); PlaySound("alert.wav"); if (TradeOrder1) { OrderStr=TradeComment1; if (TradeTP1==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP1*Point; if (TradeSL1==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL1*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots1,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder2) { OrderStr=TradeComment2; if (TradeTP2==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP2*Point; if (TradeSL2==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL2*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots2,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder3) { OrderStr=TradeComment3; if (TradeTP3==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP3*Point; if (TradeSL3==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL3*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots3,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder4) { OrderStr=TradeComment4; if (TradeTP4==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP4*Point; if (TradeSL4==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL4*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots4,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } OpenSell=0; return(0); } if (TradeBar==0) { SetText(Time[0],High[0]+1*DotLoc*Point,("Sell"+DoubleToStr(Time[0],0)),CharToStr(234),Red); PlaySound("alert.wav"); if (TradeOrder1) { OrderStr=TradeComment1; if (TradeTP1==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP1*Point; if (TradeSL1==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL1*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots1,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder2) { OrderStr=TradeComment2; if (TradeTP2==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP2*Point; if (TradeSL2==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL2*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots2,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder3) { OrderStr=TradeComment3; if (TradeTP3==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP3*Point; if (TradeSL3==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL3*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots3,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder4) { OrderStr=TradeComment4; if (TradeTP4==0) MyTakeProfit=0; else MyTakeProfit=Bid-TradeTP4*Point; if (TradeSL4==0) MyStopLoss=0; else MyStopLoss=Bid+TradeSL4*Point; Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_SELL,TradeLots4,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } OpenSell=0; return(0); } } if (OpenBuy==1 && AllowTrade==1) { if (NewBar==1 && TradeBar>0) { SetText(Time[0],Low[0]-0.3*DotLoc*Point,("Buy"+DoubleToStr(Time[0],0)),CharToStr(233),Lime); PlaySound("alert.wav"); if (TradeOrder1) { OrderStr=TradeComment1; if (TradeTP1==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP1*Point; if (TradeSL1==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL1*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots1,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Green); } if (TradeOrder2) { OrderStr=TradeComment2; if (TradeTP2==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP2*Point; if (TradeSL2==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL2*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots2,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Green); } if (TradeOrder3) { OrderStr=TradeComment3; if (TradeTP3==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP3*Point; if (TradeSL3==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL3*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots3,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Green); } if (TradeOrder4) { OrderStr=TradeComment4; if (TradeTP3==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP4*Point; if (TradeSL3==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL4*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots4,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Green); } OpenBuy=0; return(0); } if (TradeBar==0) { SetText(Time[0],Low[0]-0.3*DotLoc*Point,("Buy"+DoubleToStr(Time[0],0)),CharToStr(233),Lime); PlaySound("alert.wav"); if (TradeOrder1) { OrderStr=TradeComment1; if (TradeTP1==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP1*Point; if (TradeSL1==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL1*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots1,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder2) { OrderStr=TradeComment2; if (TradeTP2==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP2*Point; if (TradeSL2==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL2*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots2,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder3) { OrderStr=TradeComment3; if (TradeTP3==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP3*Point; if (TradeSL3==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL3*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots3,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } if (TradeOrder4) { OrderStr=TradeComment4; if (TradeTP3==0) MyTakeProfit=0; else MyTakeProfit=Ask+TradeTP4*Point; if (TradeSL3==0) MyStopLoss=0; else MyStopLoss=Ask-TradeSL4*Point; Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); ticket=OrdSend(Symbol(),OP_BUY,TradeLots4,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); } OpenBuy=0; return(0); } } } for (i=0; i0 && (CurTime()-AlertTime)>Periods) { if (_buy==1) { AlertStr=AlertStr+"Buy @ "+DoubleToStr(_op,Digits)+"; SL: "+DoubleToStr(_sl,Digits)+"; TP: "+DoubleToStr(_tp,Digits)+" at "+CurDate+" Order:"+DoubleToStr(_ticket,0)+"."; Alert(Symbol()," ",Period(), ": ",AlertStr); if (SendEmail) { SendMail(Symbol()+" "+Period()+ ": ",Symbol()+" "+Period()+": "+AlertStr); } Crepeat=Crepeat-1; AlertTime=CurTime(); } } } if (_sell==1) { if (Crepeat==Repeat) { AlertTime=0; } if (Crepeat>0 && (CurTime()-AlertTime)>Periods) { if (_sell==1) { AlertStr=AlertStr+"Sell @ "+DoubleToStr(_op,Digits)+"; SL: "+DoubleToStr(_sl,Digits)+"; TP: "+DoubleToStr(_tp,Digits)+" at "+CurDate+" Order:"+DoubleToStr(_ticket,0)+"."; Alert(Symbol()," ",Period(), ": ",AlertStr); if (SendEmail) { SendMail(Symbol()+" "+Period()+ ": ",Symbol()+" "+Period()+": "+AlertStr); } Crepeat=Crepeat-1; AlertTime=CurTime(); } } } if (_exitsell==1) { if (Crepeat==Repeat) { AlertTime=0; } if (Crepeat>0 && (CurTime()-AlertTime)>Periods) { if (_exitsell==1) { AlertStr=AlertStr+" Close Sell @ "+DoubleToStr(_op,Digits)+" at "+CurDate+" Order:"+DoubleToStr(_ticket,0)+"."; Alert(Symbol()," ",Period(), ": ", AlertStr); if (SendEmail) { SendMail(Symbol()+" "+Period()+ ": ",Symbol()+" "+Period()+": "+AlertStr); } Crepeat=Crepeat-1; AlertTime=CurTime(); } } } if (_exitbuy==1) { if (Crepeat==Repeat) { AlertTime=0; } if (Crepeat>0 && (CurTime()-AlertTime)>Periods) { if (_exitbuy==1) { AlertStr=AlertStr+" Close Buy @ "+DoubleToStr(_op,Digits)+" at "+CurDate+" Order:"+DoubleToStr(_ticket,0)+"."; Alert(Symbol()," ",Period(), ": ",AlertStr); if (SendEmail) { SendMail(Symbol()+" "+Period()+ ": ",Symbol()+" "+Period()+": "+AlertStr); } Crepeat=Crepeat-1; AlertTime=CurTime(); } } } if (_exitbuy==0 && _exitsell==0 && _buy==0 && _sell==0) { Crepeat=Repeat; AlertTime=0; } } // //---- return; } //----------------------- PRINT COMMENT FUNCTION void subPrintDetails() { string sComment = ""; string sp = "----------------------------------------\n"; string NL = "\n"; string sDirection = ""; sComment = "DemarkLinesEA v.1.0" + NL; sComment = sComment + sp; sComment = sComment + "TradeLots1=" + DoubleToStr(TradeLots1,2) + " | " + "TradeSL1=" + DoubleToStr(TradeSL1,0) + " | " + "TradeTP1=" + DoubleToStr(TradeTP1,0) + " | " + "TradeTS1=" + DoubleToStr(TradeTS1,0) + NL; sComment = sComment + "TradeLots2=" + DoubleToStr(TradeLots2,2) + " | " + "TradeSL2=" + DoubleToStr(TradeSL2,0) + " | " + "TradeTP2=" + DoubleToStr(TradeTP2,0) + " | " + "TradeTS2=" + DoubleToStr(TradeTS2,0) + NL; sComment = sComment + "TradeLots3=" + DoubleToStr(TradeLots3,2) + " | " + "TradeSL3=" + DoubleToStr(TradeSL3,0) + " | " + "TradeTP3=" + DoubleToStr(TradeTP3,0) + " | " + "TradeTS3=" + DoubleToStr(TradeTS3,0) + NL; sComment = sComment + "TradeLots4=" + DoubleToStr(TradeLots4,2) + " | " + "TradeSL4=" + DoubleToStr(TradeSL4,0) + " | " + "TradeTP4=" + DoubleToStr(TradeTP4,0) + " | " + "TradeTS4=" + DoubleToStr(TradeTS4,0) + NL; sComment = sComment + "TrailingStop=" + DoubleToStr(TrailingStop,0) + NL; sComment = sComment + sp; Comment(sComment); } int CntOrd(int Type, int Magic) { //return number of orders with specific parameters int _CntOrd; _CntOrd=0; for(int i=0;i0 && OrderComment()==com) { if ((pBid-OrderOpenPrice())>trailing*pp) { if (OrderStopLoss()0 && OrderComment()==com) { if (OrderOpenPrice()-pAsk>trailing*pp) { if (OrderStopLoss()>pAsk+(trailing+TrailingStep-1)*pp || OrderStopLoss()==0) { ModifyStopLoss(pAsk+trailing*pp); return; } } } } }