//+------------------------------------------------------------------+ //| SimpleDailyRangeBreakExpert_v1.2.mq4 | //| Copyright © 2006, Forex-TSD.com | //| Written by IgorAD,igorad2003@yahoo.co.uk | //| http://finance.groups.yahoo.com/group/TrendLaboratory | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Forex-TSD.com " #property link "http://www.forex-tsd.com/" #include <stdlib.mqh> #include <Tracert.mqh> //---- input parameters extern string Expert_Name = "---- SimpleDailyRangeBreakExpert_v1.2 ----"; extern int Magic=1007; extern int Slippage=6; extern bool Trace = false; // Trace Switch extern string Main_Parameters = " Trade Volume & Trade Method"; extern double Lots = 0.1; extern bool TrailingStop = false; // Trailing Stop Switch extern bool InitialStop = true; // Initial Stop Switch extern int TimeZone = 1; extern string Data = " Input Data "; extern double BuyPercent = 90; // Percent from Daily Range for BUY extern double SellPercent = 70; // Percent from Daily Range for SELL extern double StopPercent = 50; // Percent from Daily Range for StopLoss extern int TradePeriod = 5; // Max days in trade extern string Trade = " Trade Days of Week"; extern bool Monday = true; // Day of the Week for Trade extern bool Tuesday = true; extern bool Wednesday = true; extern bool Thursday = true; extern bool Friday = true; extern string MM_Parameters = " MoneyManagement by L.Williams "; extern bool MM=false; // ÌÌ Switch extern double MMRisk=0.15; // Risk Factor extern double LossMax=1000; // Maximum Loss by 1 Lot int i,cnt=0, ticket, mode=0, digit=0, wenum=0,OrderOpenDay; double high=0,low=0, close=0, open=0, range=0, spread=0, Profit=0; double smin=0, smax=0, BuyStop=0, SellStop=0, Lotsi=0, rates_h1[][6]; bool BuyInTrade=false, SellInTrade=false; datetime StartTime, prevwe=0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } // ---- Money Management double MoneyManagement ( bool flag, double Lots, double risk, double maxloss) { double Lotsi=Lots; if ( flag ) Lotsi=NormalizeDouble(Lots*AccountFreeMargin()*risk/maxloss,1); if (Lotsi<0.1) Lotsi=0.1; return(Lotsi); } // Closing of Pending Orders void PendOrdDel() { int total=OrdersTotal(); for (int cnt=total-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES); if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic ) { int mode=OrderType(); bool result = false; switch(mode) { case OP_BUYSTOP : {result = OrderDelete( OrderTicket() ); Print(" PendBUY"); } case OP_SELLSTOP : {result = OrderDelete( OrderTicket() ); Print(" PendSELL"); } if(!result) { Print("OrderSend failed with error #",GetLastError()); return(0); } //Print(" cnt=",cnt, " total=",total," MODE = ",mode," Ticket=",OrderTicket() ); } } } return; } // void CloseOrdbyTime() { int total=OrdersTotal(); for (cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS); mode=OrderType(); if ( mode <= OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { datetime OpenTime=StrToTime(TimeToStr(OrderOpenTime(),TIME_DATE)); datetime CurnTime=StrToTime(TimeToStr(CurTime(),TIME_DATE)); int weekday=TimeDayOfWeek(OrderOpenTime()); if (weekday <=5 && DayOfWeek()==1 && CurnTime > prevwe && CurnTime > OpenTime) {wenum =wenum + 1; prevwe = CurnTime;} //if (OrderOpenDay <= 5 && DayOfWeek()>0) int CalcBars=TradePeriod+2*wenum; //else CalcBars=TradePeriod; if ((CurTime()-OpenTime)>=PERIOD_D1*CalcBars*60 || OrderProfit()>0) { if (mode==OP_BUY ) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Yellow); if (mode==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White); } } } } // ---- void TrailStops() { int total=OrdersTotal(); for (cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS); mode=OrderType(); if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic ) { if ( mode==OP_BUY && smin > OrderStopLoss() ) { Profit=0; OrderModify(OrderTicket(),OrderOpenPrice(),smin,Profit,0,LightGreen); return(0); } if ( mode==OP_SELL && smax < OrderStopLoss()) { Profit=0; OrderModify(OrderTicket(),OrderOpenPrice(),smax,Profit,0,Yellow); return(0); } } } } void SellOrdOpen() { double SellPrice=open - range*SellPercent/100.0 - spread; if (InitialStop) SellStop=SellPrice + StopPercent*range/100.0; else SellStop=0; Profit=0; ticket = OrderSend(Symbol(),OP_SELLSTOP,Lotsi, NormalizeDouble(SellPrice,digit), Slippage, NormalizeDouble(SellStop,digit), Profit,"SELL",Magic,0,Red); OrderOpenDay = DayOfWeek(); SellInTrade=false; if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } } void BuyOrdOpen() { double BuyPrice =open + range*BuyPercent/100.0 + spread; if (InitialStop) BuyStop = BuyPrice - StopPercent*range/100.0; else BuyStop=0; Profit=0; ticket = OrderSend(Symbol(),OP_BUYSTOP ,Lotsi, NormalizeDouble(BuyPrice ,digit), Slippage, NormalizeDouble(BuyStop ,digit), Profit,"BUY",Magic,0,Blue); OrderOpenDay = DayOfWeek(); BuyInTrade=false; if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } } void ExtraOrdDel() { int total = OrdersTotal(); for (cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS); mode=OrderType(); if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic ) { if (mode==OP_BUY && !BuyInTrade ) BuyInTrade =true; if (mode==OP_SELL && !SellInTrade) SellInTrade=true; if (mode == OP_SELLSTOP && BuyInTrade ) { OrderDelete(OrderTicket()); SellInTrade=false; Print(" ExtraSell"); } if (mode == OP_BUYSTOP && SellInTrade ) { OrderDelete(OrderTicket()); BuyInTrade=false; Print(" ExtraBuy"); } } } } // ---- Scan Trades int ScanTrades() { int total = OrdersTotal(); int numords = 0; for(cnt=0; cnt<total; cnt++) { OrderSelect(cnt, SELECT_BY_POS); if(OrderSymbol() == Symbol() && OrderType()>=OP_BUY && OrderMagicNumber() == Magic) numords++; } return(numords); } datetime OrderOpenDate() { int total = OrdersTotal(); datetime date; for(cnt=0; cnt<total; cnt++) { OrderSelect(cnt, SELECT_BY_POS); if(OrderSymbol() == Symbol() && OrderType()>=OP_BUY && OrderMagicNumber() == Magic) date = StrToTime(TimeToStr(OrderOpenTime(),TIME_DATE)); } return(date); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if(Bars < 1) {Print("Not enough bars for this strategy"); return(0);} if ( Trace ) SetTrace(); string TimeTrade = "00:00"; StartTime = StrToTime(TimeTrade) + TimeZone*3600; if(Hour() >= TimeZone && Hour() <= TimeZone+1) { if ( OrderOpenDate() < StrToTime(TimeToStr( StartTime,TIME_DATE))) { PendOrdDel(); if( TradePeriod > 0 )CloseOrdbyTime(); } if(ScanTrades()<1) { spread= MarketInfo(Symbol(),MODE_SPREAD)*Point; digit = MarketInfo(Symbol(),MODE_DIGITS); Lotsi = MoneyManagement ( MM, Lots, MMRisk, LossMax); if (TrailingStop) InitialStop=true; ArrayCopyRates(rates_h1, Symbol(), PERIOD_H1); open = rates_h1[0][1]; high=0; low=10000000; for (i=24;i>=1;i--) { high = MathMax( high, rates_h1[i][3]); low = MathMin( low , rates_h1[i][2]); } Print(" high=",high,"low=",low); range =(high-low); smin = Bid - StopPercent*range/100.0; smax = Ask + StopPercent*range/100.0; if ( Monday ) if(DayOfWeek()==1){BuyOrdOpen(); SellOrdOpen();} if ( Tuesday ) if(DayOfWeek()==2){BuyOrdOpen(); SellOrdOpen();} if ( Wednesday ) if(DayOfWeek()==3){BuyOrdOpen(); SellOrdOpen();} if ( Thursday ) if(DayOfWeek()==4){BuyOrdOpen(); SellOrdOpen();} if ( Friday ) if(DayOfWeek()==5){BuyOrdOpen(); SellOrdOpen();} } } ExtraOrdDel(); if (TrailingStop) TrailStops(); return(0); }//int start //+------------------------------------------------------------------+
Sample
Analysis
Market Information Used:
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
It can change open orders parameters, due to possible stepping strategy
Other Features: