//+------------------------------------------------------------------+ //| mrmon-exp.mq4 | //| Nick Bilak, beluck[AT]gmail.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Nick Bilak" #property link "http://www.mql4.info/" #include <stdlib.mqh> extern int expertId = 1; extern int TakeProfit = 17; extern int StopLoss = 60; extern int TrailingStop = 10; extern double Lots = 0.1; //fixed lot size extern double MinLot = 0.1; //minimum lot size extern double MaximumRisk = 5; //% risk. lot size will be calculated so that stoploss was equal to risk% of balance extern bool FixedLot = true; //trigger to use MM extern int TimeStart=0700; extern int TimeStop=2400; extern int EmaPeriod=10; extern int SmaPeriod=40; extern int slippage=2; //slippage for market order processing extern int shift=1; //shift to current bar, extern int OrderTriesNumber=2; //to repeate sending orders when got some error extern string EAName="mrmon"; bool buysig,sellsig,closebuy,closesell; int lastsig,tries,x,y,co,lastdir=999,lastc; double xp,xpc; void start() { //if (TimeCurrent()>1168560000) { Alert("1040-exp version expired"); return; } //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; CheckForSignals(); CheckForOpen(); TrailStop(); } double LotsRisk(int StopLoss) { //MM lot size calculation double lot=Lots; //---- select lot size if (!FixedLot) lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*0.001/StopLoss,1); else lot=Lots; //---- return lot size if(lot<MinLot) lot=MinLot; return(lot); } //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int ord; //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==symbol && OrderMagicNumber()==expertId) ord++; } //---- return orders volume return(ord); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForSignals() { //indicators variables double ema1 = iCustom(NULL,0,"humaverages",EmaPeriod,0,MODE_EMA,0,shift); //double ema2 = iCustom(NULL,0,"humaverages",EmaPeriod,0,MODE_EMA,0,shift+1); double sma1 = iCustom(NULL,0,"humaverages",SmaPeriod,0,MODE_SMA,0,shift); //double sma2 = iCustom(NULL,0,"humaverages",SmaPeriod,0,MODE_SMA,0,shift+1); buysig=false; sellsig=false; closebuy=false; closesell=false; if (ema1>sma1) { if (lastdir==999) { lastdir=1; return; } closesell=true; buysig=true; } if (ema1<sma1) { if (lastdir==999) { lastdir=-1; return; } closebuy=true; sellsig=true; } } void CheckForOpen() { int res,tr; int ct=Hour()*100+Minute(); if ( !(ct>=TimeStart && ct<TimeStop) ) return; //time restrictions for entry //---- sell conditions co=CalculateCurrentOrders(Symbol()); if (co>0) CheckForClose(); co=CalculateCurrentOrders(Symbol()); if(sellsig && lastdir>0) { if (co==0) { res = OpenAtMarket(OP_SELL,LotsRisk(StopLoss)); } if (res>0) { lastdir=-1; lastsig=Time[0]; } return; } //---- buy conditions if(buysig && lastdir<0) { if (co==0) { res = OpenAtMarket(OP_BUY,LotsRisk(StopLoss)); } if (res>0) { lastdir=1; lastsig=Time[0]; } return; } } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { bool bres; int tr; for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=expertId || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) if (closebuy) { bres=CloseAtMarket(OrderTicket(),OrderLots()); break; } if(OrderType()==OP_SELL) if (closesell) { bres=CloseAtMarket(OrderTicket(),OrderLots()); break; } } } int OpenAtMarket(int mode,double lot) { int res,tr,col; double openprice,sl,tp; tries=0; while (res<=0 && tries<OrderTriesNumber) { tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); } RefreshRates(); if (mode==OP_SELL) { openprice=Bid; sl=openprice+StopLoss*Point; tp=openprice-TakeProfit*Point; col=Red; } else { //openprice=nd(Ask); openprice=Ask; sl=openprice-StopLoss*Point; tp=openprice+TakeProfit*Point; col=Blue; } res=OrderSend(Symbol(),mode,lot,openprice,slippage,sl,tp,EAName+expertId,expertId,0,col); tries++; } return(res); } bool CloseAtMarket(int ticket,double lot) { bool bres=false; int tr; tries=0; while (!bres && tries<OrderTriesNumber) { tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); } RefreshRates(); bres=OrderClose(ticket,lot,OrderClosePrice(),slippage,White); if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError())); tries++; } } void TrailStop() { bool bres; double StopLoss; if ( TrailingStop > 2 ) { for (int i = 0; i < OrdersTotal(); i++) { if ( OrderSelect (i, SELECT_BY_POS) == false ) continue; if ( OrderSymbol() != Symbol() || OrderMagicNumber() != expertId ) continue; if ( OrderType() == OP_BUY ) { if ( Bid < OrderOpenPrice()+TrailingStop*Point ) return; StopLoss = Bid-TrailingStop*Point; if ( StopLoss > OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White); if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError())); } } if ( OrderType() == OP_SELL ) { if ( Ask > OrderOpenPrice()-TrailingStop*Point ) return; StopLoss = Ask+TrailingStop*Point; if ( StopLoss < OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold); if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError())); } } } } return; }
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
humaverages
Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached
It Closes Orders by itself
Other Features: