Navigation:Home > Content >

preBar.mq4

Time: 2017-06-25 | Download file:preBar.mq4

//+------------------------------------------------------------------+
//|         4/8/2007                                     preBar.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
int MAGICMA=20070705;


extern double  Lots           =  0.1;
extern double  MaximumRisk    =  0.03;
extern double  stopFactor = 1;

extern double  TakeProfit1 = 150;
double  Stoploss1;

extern bool takeProfit = true;
extern bool sartrail = true;
extern bool MoneyManagement = true;

extern bool trendFilter = true;
extern int trendThreshold = 15;
extern int adxPeriod = 2;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   
   int buys=0,sells=0;
//----
   for(int i=0;i0) return(buys);
   else       return(-sells);
  }


double LotsOptimized()
  {
//---- select lot size
   double lot;
 lot = NormalizeDouble( (AccountFreeMargin()*MaximumRisk)/( iATR(NULL,0,20,0)*(1/Point)  )*0.1,1);
 
 
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
  
  if(!NewBar())
  return;
  int    res;
  if(MoneyManagement)
   Lots = LotsOptimized();
 
  double close1 = Close[1];
  double open1 = Open[1];
  
  double trendLine =  iADX(NULL,0,adxPeriod,PRICE_WEIGHTED,MODE_MAIN,0);
  bool trendFilterRes = true;
  
  if(trendFilter==true)
  {
  if(trendLine>trendThreshold)
         trendFilterRes = true;
   else
         trendFilterRes = false;
  } 
  
 if ( Close[2]>Open[2] && close1< open1  &&trendFilterRes)
     {
      openSell();
      return;
     }
  
     
   
//---- buy conditions
   if (Close[2] open1  && trendFilterRes)
     {
      openBuy();   
    
      return;
     }
  
//----
  }
  
  void openBuy(){
  Print("Lots "+Lots);
  Stoploss1 = stopFactor * iATR(NULL,0,20,0)+ MarketInfo (Symbol(), MODE_SPREAD) * Point;
  int res;
  if(takeProfit)
         res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss1,Ask+TakeProfit1*Point,"preBar",MAGICMA,0,Blue);
     else
          res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss1,0,"preBar",MAGICMA,0,Blue); 
      if(res>0)
      {
      if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
         Print("SELL order opened : ",OrderOpenPrice());
      Comment("preBar opened buy order " + Ask);
      }
       else Print("Error opening SELL order : ",GetLastError()); 
  
  }
  
  void openSell(){
  Print("Lots "+Lots);
  Stoploss1 = stopFactor * iATR(NULL,0,20,0)+ MarketInfo (Symbol(), MODE_SPREAD) * Point;
  int res;
  if(takeProfit)
         res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss1,Bid-TakeProfit1*Point,"preBar",MAGICMA,0,Red);
     else
         res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss1,0,"preBar",MAGICMA,0,Red); 
     if(res>0)
      {
      if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
         Print("SELL order opened : ",OrderOpenPrice());
      Comment("preBar opened sell order " + Bid);
      }
       else Print("Error opening SELL order : ",GetLastError()); 
  
  }
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {

 
//----
   for(int i=0;i Close[1]) 
            {   OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
                openSell();
            }
            // should it be closed?
          // Function version check Sell condition to exit Buy
       
       // check for trailing stop
         if (  sartrail && (iSAR(NULL,0,0.02,0.2,1) > OrderStopLoss()) && (Bid > iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() < iSAR(NULL,0,0.02,0.2,1)) && (iSAR(NULL,0,0.02,0.2,1) > iSAR(NULL,0,0.02,0.2,2)))
         {
          OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
          Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
          return(0);
         }
            
       }
        if(OrderType()==OP_SELL)
           {
         if(!takeProfit && NewBar() && Open[1]< Close[1])
           { OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
             openBuy();
           }
           
   
            // should it be closed?
       // Function version check Buy condition to exit Sell
   
        if (sartrail && (iSAR(NULL,0,0.02,0.2,1) < OrderStopLoss()) && (Ask < iSAR(NULL,0,0.02,0.2,1)) && (OrderOpenPrice() > iSAR(NULL,0,0.02,0.2,1)) && (iSAR(NULL,0,0.02,0.2,1) < iSAR(NULL,0,0.02,0.2,2)))
         {
          OrderModify(OrderTicket(),OrderOpenPrice(),iSAR(NULL,0,0.02,0.2,1),OrderTakeProfit(),0,Blue);
          Print("Order # ",OrderTicket()," updated at ",Hour(),":",Minute(),":",Seconds());
          return(0);
         }  
              
        }
        }
     }
   return(0);
  }
// the end.
//----

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
 
   setVariables(Symbol());
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+--------------------------------------------------------------

bool NewBar()
{
static datetime lastbar; 
datetime curbar = Time[0]; 
if(lastbar!=curbar){
   lastbar=curbar; 
   return (true); 
}   else 
{  return(false);   }

}

void setVariables(string symbol){

//if ( symbol=="AUDNZD") {MAGICMA =550001; tragetProfit = 4; stopLossFactor = 3.5; bandSD = 3.6; pipCross = 30; ratioTarget = 0.4; TrailingStop   = 50; TrailingStep   = 20; }
if ( symbol=="CHFJPY") {MAGICMA =240002; MaximumRisk   =  0.01;stopFactor = 4;TakeProfit1 = 120;trendThreshold = 20;adxPeriod = 3; }
if ( symbol=="GBPUSD") {MAGICMA =240003;  MaximumRisk   =  0.01;stopFactor = 3.2;TakeProfit1 = 350;trendThreshold =37;adxPeriod = 13; }
//if ( symbol=="EURGBP") {MAGICMA =550003; tragetProfit = 9; stopLossFactor = 4;bandSD = 5; pipCross = 55; ratioTarget = 0.3; TrailingStop   = 120; TrailingStep   = 5; }


 
}



Recommend