//+------------------------------------------------------------------+ //| Grail_2.mq4 | //| Copyright © 2007, Bolla Corp. | //+------------------------------------------------------------------+ #property copyright "Copyright © 2007, Bolla Corp." #include <stdlib.mqh> extern int magic_2 = 2; // Magic EA_2 extern int Time_1_EA2 = 18; // Starting time EA2 extern int Time_2_EA2 = 8; // Finishing time EA2 extern int period_MA = 13; // MA period extern int StopLoss = 30; // StopLoss //----------------------------------------------------------------------------- extern int Shift_Time = 0; // For different Alpari Time Brokers extern bool UseMM = false; // Toggle Anti-Martingala/Martingala Money Management extern double Lots = 0.01; // Min Lot extern int PercentMMmin = 10; // Martingala MM minimum % extern int PercentMMmax = 40; // Martingala MM maximum % extern int StepMM = 5; // Martingala MM step //============================================================================================ int tickets=0, // initial ticket err=0, // initial error b=0, // 1 = Buy order availability s=0, // 1 = Sell order availability rip; // # of repeat for OrderSend when Lot > Max Lot Limit allowed //----------------------------------------------------------------------------- double PercentMM=10, // initial Percent MM spread=2, // initial spread Clsb=2, // initial gain Clss=2, // initial gain OPb, // OpenPrice (absolute points) OPs, // OpenPrice (absolute points) MA; // MA value (rate) //----------------------------------------------------------------------------- bool OtherOrder=false; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- spread=NormalizeDouble((Ask-Bid)/Point,0); grail2(); comment(); //---- return(0); } //============================================================================================ void grail2() { b=0; s=0; int total=OrdersTotal(); // Count of lots for (int i=total; i>=0; i--) // For all orders { bool ticket=false; if (OrderSelect(i,SELECT_BY_POS)==true && // Select an order OrderSymbol()==Symbol() && OrderMagicNumber()==magic_2) { if (OrderType()==OP_BUY) { b=1; if (NormalizeDouble(Bid-OrderOpenPrice(),Digits)>=Clsb*Point || NormalizeDouble(OrderOpenPrice()-Bid,Digits)>=StopLoss*Point) // Buy order { // The order found while (!ticket) { while (!IsTradeAllowed()||!IsConnected()||IsTradeContextBusy()){} RefreshRates(); ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),1,CLR_NONE); // Close if (!ticket) {Error();Sleep(1000);} } b=0; Clsb=spread; } } if (OrderType()==OP_SELL) { s=1; if (NormalizeDouble(OrderOpenPrice()-Ask,Digits)>=Clss*Point || NormalizeDouble(Ask-OrderOpenPrice(),Digits)>=StopLoss*Point) // Sell order { while (!ticket) { while (!IsTradeAllowed()||!IsConnected()||IsTradeContextBusy()){} RefreshRates(); ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),1,CLR_NONE); // Close if (!ticket) {Error();Sleep(1000);} } s=0; Clss=spread; } } } } MA = iMA(NULL,0,period_MA,0,MODE_LWMA,PRICE_TYPICAL,0);// The MA current value Open_order() ; // Activate opening return; } //-------------------------------------------------------------------------------------------- void Open_order() // An opening function { double Lotsi=Lots(StopLoss); if (Tradetime(Time_1_EA2,Time_2_EA2)==1) { OPb=NormalizeDouble(MA-spread*Point,Digits); // Normalizing (MA gives the 5th digit) if (OPb>=NormalizeDouble(Ask,Digits)) { if (b==0) { for (int i=1;i<=rip;i++) { int ticket=-1; while (!IsTradeAllowed()||!IsConnected()||IsTradeContextBusy()){} RefreshRates(); ticket=OrderSend(Symbol(),OP_BUY,Lotsi,NormalizeDouble(Ask,Digits),1,0,0,"grail_2",magic_2,0,CLR_NONE); if (ticket==-1) Error(); } } if ((OPb-NormalizeDouble(Ask,Digits))/Point+spread>Clsb) Clsb=MathMin((OPb-NormalizeDouble(Ask,Digits))/Point+spread,spread*2); } OPs=NormalizeDouble(MA+(spread+spread)*Point,Digits); // Normalizing (MA gives the 5th digit) if (OPs<=NormalizeDouble(Bid,Digits)) { if (s==0) { for (int j=1;j<=rip;j++) { ticket=-1; while (!IsTradeAllowed()||!IsConnected()||IsTradeContextBusy()){} RefreshRates(); ticket=OrderSend(Symbol(),OP_SELL,Lotsi,NormalizeDouble(Bid,Digits),1,0,0,"grail_2",magic_2,0,CLR_NONE); if (ticket==-1) Error(); } } if ((NormalizeDouble(Bid,Digits)-OPs)/Point+spread>Clss) Clss=MathMin((NormalizeDouble(Bid,Digits)-OPs)/Point+spread,spread*2); } } return; } //============================================================================================ double Lots(int SL) { if (NormalizeDouble(MarketInfo(Symbol(),MODE_LOTSTEP),2)==0.01) int lotsdigit=2; else lotsdigit=1; double ProfitPoint; if (UseMM) { int total=OrdersHistoryTotal(); for (int cnt=total-1;cnt>=0;cnt--) { if (!OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY) || OrderType()>OP_SELL) continue; if (OrderTicket()!=tickets) { tickets=OrderTicket(); if (OrderType()==OP_BUY) ProfitPoint=(OrderClosePrice()-OrderOpenPrice())/Point; else ProfitPoint=(OrderOpenPrice()-OrderClosePrice())/Point; if (ProfitPoint<-3) PercentMM=PercentMMmax; OtherOrder=true; } else { if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) && OtherOrder) { PercentMM=PercentMM-StepMM; OtherOrder=false; } } if (PercentMM<PercentMMmin) PercentMM=PercentMMmin; break; } double Lot=(NormalizeDouble((AccountBalance()*(PercentMM/100)/(MarketInfo(Symbol(),MODE_TICKVALUE)*SL)),lotsdigit)); } else Lot=Lots; rip=1; if (Lot>NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),2)) { rip=MathCeil(Lot/NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),2)); Lot=NormalizeDouble(Lot/rip,lotsdigit); } if (Lot<NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),2)) Lot=NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),2); return(Lot); } //============================================================================================ int Tradetime(int start,int stop) { int TradingTime=0; if (TimeHour(Time[0])>=(start+Shift_Time)||TimeHour(Time[0])<(stop+Shift_Time)) TradingTime=1; return(TradingTime); } //============================================================================================ void Error() { err=GetLastError(); Print("Errore n°: ",err,", ",ErrorDescription(err)); } //============================================================================================ void comment() { Comment("\nMA(",period_MA,")=",MA, "\nIf OPb=",OPb," >= Ask=",NormalizeDouble(Ask,Digits)," ---> BUY", "\nIf OPs=",OPs," <= Bid=",NormalizeDouble(Bid,Digits)," ---> SELL", "\nClsB=",Clsb," ClsS=",Clss, "\nPercentMM=",PercentMM); }
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Indicator Curves created:
Indicators Used:
Moving average indicator
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
It automatically opens orders when conditions are reached
Checks for the total of closed orders
Other Features: