//+------------------------------------------------------------------+ //| Trend_RDS.mq4 | //| Copyright © 2007, Forex-Experts | //| http://www.forex-experts.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2007, Forex-Experts" #property link "http://www.forex-experts.com" #define OrderStr "Trend_RDS" extern double Lots = 0.1; extern int StopLoss = 30; extern int TakeProfit = 0; bool TradeAtCloseBar = false; extern string StartHour = "08:00"; extern string CloseHour = "00:00"; extern bool Reverse = false; 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=false; extern string TradeLog="Trend_RDS"; 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"; int NumberOfTries = 1, //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=""; //+------------------------------------------------------------------+ //| 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; bool Conflict=false; int limit=100; if (TimeCurrent()-StrToTime(StartHour)>=0 && (TimeCurrent()-StrToTime(StartHour))<=600) { for (int shift=1; shift<=limit; shift++) { if ( High[shift]Low[shift+1] && Low[shift+1]>Low[shift+2]) { Conflict=true; } else { Conflict=false; } if (Low[shift]>Low[shift+1] && Low[shift+1]>Low[shift+2] && Conflict==false) { if (Reverse) SellValue=1; else BuyValue=1; break; } if (High[shift]=0 && (TimeCurrent()-StrToTime(CloseHour))<=900) { CloseBuy=1; CloseSell=1; } 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; /*====================== MONEY MANAGEMENT / COMPOUNDING =========================================== Changing the value of mm will give you several money management options mm = -1 : Fractional lots/ balance X the risk factor.(use for Mini accts) mm = 0 : Single Lots. mm = 1 : Full lots/ balance X the risk factor up to 100 lot orders.(use for Regular accounts) *************************************************************************************************** RISK FACTOR: risk can be anything from 1 up. Factor of 5 adds a lot for every $20,000.00 ($2000-MINI) added to the balance. Factor of 10 adds a lot with every $10.000.00 ($1000-MINI) added to the balance. The higher the risk, the easier it is to blow your margin.. ***************************************************************************************************/ Ilo=Lots; if (mm<0) { Ilo=MathCeil(AccountEquity()*Risk/10000)/10-0.1; if (Ilo<0.1) Ilo=0.1; } if (mm>0) { Ilo=MathCeil(AccountEquity()*Risk/10000)/10-1; if (Ilo>1) Ilo=MathCeil(Ilo); if (Ilo<1) Ilo=1; } if (Ilo>100) Ilo=100; OpenPos=0; for(cnt=0; cnt=1) { OpenSell=0; OpenBuy=0; } OpenBuy=0; OpenSell=0; //Conditions to open the position // if (SellValue>0) { OpenSell=1; OpenBuy=0; } if (BuyValue>0) { OpenBuy=1; OpenSell=0; } //Print("OpenSell=",OpenSell," OpenBuy=",OpenBuy); //Conditions to close the positions if (SellValue>0) { CloseBuy=1; } if (BuyValue>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); if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Bid-TakeProfit*Point; if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Bid+StopLoss*Point; PlaySound("alert.wav"); ticket=OrdSend(Symbol(),OP_SELL,Ilo,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); OpenSell=0; return(0); } if (TradeBar==0) { SetText(Time[0],High[0]+1*DotLoc*Point,("Sell"+DoubleToStr(Time[0],0)),CharToStr(234),Red); if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Bid-TakeProfit*Point; if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Bid+StopLoss*Point; PlaySound("alert.wav"); ticket=OrdSend(Symbol(),OP_SELL,Ilo,Bid,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Red); Alerts(OpenBuy, OpenSell, 0, 0,Bid,MyStopLoss,MyTakeProfit,ticket); 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); if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Ask+TakeProfit*Point; if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Ask-StopLoss*Point; PlaySound("alert.wav"); ticket=OrdSend(Symbol(),OP_BUY,Ilo,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Lime); Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); OpenBuy=0; return(0); } if (TradeBar==0) { SetText(Time[0],Low[0]-0.3*DotLoc*Point,("Buy"+DoubleToStr(Time[0],0)),CharToStr(233),Lime); if (TakeProfit==0) MyTakeProfit=0; else MyTakeProfit=Ask+TakeProfit*Point; if (StopLoss==0) MyStopLoss=0; else MyStopLoss=Ask-StopLoss*Point; PlaySound("alert.wav"); ticket=OrdSend(Symbol(),OP_BUY,Ilo,Ask,Slippage,MyStopLoss,MyTakeProfit,OrderStr,MagicNumber,0,Lime); Alerts(OpenBuy, OpenSell, 0, 0,Ask,MyStopLoss,MyTakeProfit,ticket); 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 = "Trend_RDS v.1.02" + NL; sComment = sComment + "StopLoss=" + DoubleToStr(StopLoss,0) + " | "; sComment = sComment + "TakeProfit=" + DoubleToStr(TakeProfit,0) + " | "; sComment = sComment + "TrailingStop=" + DoubleToStr(TrailingStop,0) + NL; sComment = sComment + sp; sComment = sComment + "Lots=" + DoubleToStr(Ilo,2) + " | "; sComment = sComment + "LastTrade=" + DoubleToStr(TradeLast,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;i