//+------------------------------------------------------------------+ //| hiddendiv-exp.mq4 | //| Nick Bilak, beluck[AT]gmail.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Nick Bilak" #property link "http://metatrader.50webs.com/" #include extern int myMagic = 1; extern double TakeProfit = 110; extern double StopLoss = 40; extern double BreakEvenStop = 350; extern double TrailingStop = 350; extern int slippage=2; //slippage for market order processing extern int shift=1; //shift to current bar, extern double Lots = 0.1; extern double MaximumRisk = 5; extern bool FixedLot = true; extern int MFIperiod=3; extern int OrderTriesNumber=2; //to repeate sending orders when got some error extern string EAName="hiddendiv"; bool buysig,sellsig,closebuy,closesell; int lastsig,tries,at,at2; void start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- check for signals CheckForSignals(); if (CalculateCurrentOrders(Symbol())==0) //---- calculate open orders by current symbol CheckForOpen(); else { CheckForClose(); BreakEvenStop(); TrailStop(); } } double LotsRisk(int StopLoss) { double lot=Lots; //---- select lot size if (!FixedLot) lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*0.001/StopLoss,1); else lot=Lots; //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int ord; //---- for(int i=0;i=100) { while (mfi2>=100) { mfi2 = iMFI(NULL,0,MFIperiod,i); i++; } while (mfi2<100) { mfi2 = iMFI(NULL,0,MFIperiod,i); i++; } i--; if (High[shift]0) { mfi2 = iMFI(NULL,0,MFIperiod,i); i++; } i--; if (Low[shift]>Low[i]) { buysig=true; } } //closebuy=sellsig; //closesell=buysig; } void CheckForOpen() { int res,tr; //---- sell conditions if(sellsig && lastsig!=Time[0]) { //if(buysig && lastsig!=Time[0]) { res=OpenAtMarket(OP_SELL,LotsRisk(StopLoss)); if (res<=0) Print("Error SELL order : ",ErrorDescription(GetLastError())); lastsig=Time[0]; return; } //---- buy conditions //if(sellsig && lastsig!=Time[0]) { if(buysig && lastsig!=Time[0]) { res=OpenAtMarket(OP_BUY,LotsRisk(StopLoss)); if (res<=0) Print("Error BUY order : ",ErrorDescription(GetLastError())); lastsig = Time[0]; return; } } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { bool bres; int tr; for(int i=0;i 2 ) { for (int i = 0; i < OrdersTotal(); i++) { if ( OrderSelect (i, SELECT_BY_POS) == false ) continue; if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic ) 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; } void BreakEvenStop() { bool bres; double StopLoss; if ( BreakEvenStop > 2 ) { for (int i = 0; i < OrdersTotal(); i++) { if ( OrderSelect (i, SELECT_BY_POS) == false ) continue; if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic ) continue; if ( OrderType() == OP_BUY ) { if ( Bid < OrderOpenPrice()+BreakEvenStop*Point ) return; StopLoss = OrderOpenPrice(); 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()-BreakEvenStop*Point ) return; StopLoss = OrderOpenPrice(); if ( StopLoss < OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold); if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError())); } } } } return; } int OpenAtMarket(int mode,double lot) { int res,tr,col; double openprice,sl,tp; tries=0; while (res<=0 && tries