Navigation:Home > Content >

TrendCapture_(Modified)_v102.mq4

Time: 2012-06-05 | Download file:TrendCapture_(Modified)_v102.mq4

//+------------------------------------------------------------------+
//|                                                         Prev.mq4 |
//|                              Copyright © 2006, Yury V. Reshetov. |
//|                                       http://betaexpert.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Yury V. Reshetov. ICQ: 282715499"
#property link      "http://betaexpert.narod.ru"

#include 

extern string S1="---------------- Entry Settings";

extern double Step=0.02;
extern double Maximum=0.2;
extern int ADXPeriod=14;
extern int ADXLevel=20;

extern string S2="---------------- Money Management";

extern double MaximumRisk=0.03;

extern string S3="---------------- Order Management";

extern double TakeProfit=180.0;
extern double StopLoss=50.0; 
static int prevtime=0;
static int MagicNumber=16384;

extern string S4="---------------- Time Filter";

extern bool TradeOnSunday=true;//|---------------time filter on sunday
extern bool MondayToThursdayTimeFilter=false;//|-time filter the week
extern int MondayToThursdayStartHour=0;//|-------start hour time filter the week
extern int MondayToThursdayEndHour=24;//|--------end hour time filter the week
extern bool FridayTimeFilter=false;//|-----------time filter on friday
extern int FridayStartHour=0;//|-----------------start hour time filter on friday
extern int FridayEndHour=21;//|------------------end hour time filter on friday
extern bool CloseOutSide=false;//|---------------close the trades outside the time filter

int digits;
double Guard=5.0;
double point;

int init()
{
   if(Digits<4)
   {
      point=0.01;
      digits=2;
   }
   else
   {
      point=0.0001;
      digits=4;
   }
   return(0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
   if(Time[0]==prevtime)return(0);
   prevtime=Time[0];
   int total=OrdersTotal();
   int ticket=-1;
   for(int cnt=0;cntOP_SELL) || OrderMagicNumber()!=MagicNumber) 
            continue;
         if(OrderProfit()>0)
         {
            cmd=OrderType();
         }
         else
         {
            if(OrderType()==OP_SELL)
            {
               cmd=OP_BUY;
            }
            else
            {
               cmd=OP_SELL;
            }
         }
      }  
      OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
      {
         if(Bid-OrderOpenPrice()>point*Guard)
         {
            if(OrderStopLoss() < OrderOpenPrice())
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
               return(0);
            }
         }       
      }
      else
      {          
         if(OrderOpenPrice()-Ask>point*Guard && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         {
            if(OrderStopLoss()>OrderOpenPrice())
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
               return(0);
            }
         }
      }
      if((TradeOnSunday==false&&DayOfWeek()==0)||(MondayToThursdayTimeFilter&&DayOfWeek()>=1&&DayOfWeek()<=4&&!(Hour()>=MondayToThursdayStartHour
      &&Hour()=FridayStartHour&&Hour()0)
            {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                  Print("Buy Order Opened at : ",OrderOpenPrice());
            }
            else Print(ErrorDescription(GetLastError()));
            return(0); 
         }
      }
      else 
      {
         if((sar > Close[0]) && (adx < ADXLevel))
         { 
            ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Ask + StopLoss * point,Bid-TakeProfit*point,"TrendCapture",MagicNumber,0,Red);
            if(ticket>0)
            {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                  Print("Sell Order Opened at : ",OrderOpenPrice());
            }
            else Print(ErrorDescription(GetLastError()));
            return(0); 
         }
      } 
   } 
   return(0);
}

//+------------------------------------------------------------------+
//| Money Management                                                 |
//+------------------------------------------------------------------+

double LotsOptimized()
{
   double lot=0.1;
   int losses=0;
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
   if(lot>100)lot=100;
   if(lot<0.1)lot=0.1;
   return(lot);
}

//|---------close buy orders

int CloseBuyOrders(int Magic)
{
  int total=OrdersTotal();

  for (int cnt=total-1;cnt>=0;cnt--)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
    {
      if(OrderType()==OP_BUY)
      {
        OrderClose(OrderTicket(),OrderLots(),Bid,3);
      }
    }
  }
  return(0);
}

//|---------close sell orders

int CloseSellOrders(int Magic)
{
  int total=OrdersTotal();

  for(int cnt=total-1;cnt>=0;cnt--)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
    {
      if(OrderType()==OP_SELL)
      {
        OrderClose(OrderTicket(),OrderLots(),Ask,3);
      }
    }
  }
  return(0);
}

Recommend