Navigation:Home > Content >

vForce_Like_EA_v1.1.mq4

Time: 2016-03-06 | Download file:vForce_Like_EA_v1.1.mq4

//+------------------------------------------------------------------+
//|                                     _expert advisor template.mq4 |
//|                              Copyright © 2009, TradingSytemForex |
//|                                http://www.tradingsystemforex.com |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, TradingSytemForex"
#property link "http://www.tradingsystemforex.com"

//|----------------------------------------------you can modify this expert
//|----------------------------------------------you can change the name
//|----------------------------------------------you can add "modified by you"
//|----------------------------------------------but you are not allowed to erase the copyrights

#define EAName "_expert advisor template"

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

extern int  StochKPeriod  = 5;
extern int  StochDPeriod  = 3;
extern int  StochSlowing  = 3;
extern bool OnlyAtStochSignal=false;
extern int  RSIPeriod     = 14;
extern int  RSILevel      = 50;
extern bool OnlyAtRSISignal=false;
extern int  ADXPeriod     = 14;
extern bool OnlyAtADXSignal=false;
extern int  BearsPeriod   = 13;
extern bool OnlyAtBearsSignal=false;
extern int  BullsPeriod   = 13;
extern bool OnlyAtBullsSignal=false;
extern int  MACDFast      = 12;
extern int  MACDSlow      = 26;
extern int  MACDSMA       = 9;
extern bool OnlyAtMacdSignal=false;
extern int  RPeriod       = 10;
extern bool OnlyAtROCSignal=false;
extern int  BBPeriod      = 20;
extern bool OnlyAtBBSignal=false;

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

extern double Lots=0.1;//|-----------------------lots size
extern double LotsPercent1=80;
extern double LotsPercent2=20;
extern bool RiskMM=false;//|---------------------risk management
extern double RiskPercent=1;//|------------------risk percentage
extern bool Martingale=false;//|-----------------martingale
extern double Multiplier=2.0;//|-----------------multiplier martingale
extern double MinLots=0.01;//|-------------------minlots
extern double MaxLots=100;//|--------------------maxlots

/*
extern bool BasketProfitLoss=false;//|-----------use basket loss/profit
extern int BasketProfit=100000;//|---------------if equity reaches this level, close trades
extern int BasketLoss=9999;//|-------------------if equity reaches this negative level, close trades
*/

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

extern int MarginPips=10;
extern int StopLoss=0;//|------------------------stop loss
extern int TakeProfit=20;//|---------------------take profit
extern bool HideSL=false;//|---------------------hide stop loss
extern bool HideTP=false;//|---------------------hide take profit
extern int TrailingStop=50;//|-------------------trailing stop
extern int TrailingStep=0;//|--------------------trailing step
extern int BreakEven=0;//|-----------------------break even
extern int MaxOrders=100;//|---------------------maximum orders allowed
extern int Slippage=3;//|------------------------slippage
extern int Magic1=20091;//|----------------------magic number
extern int Magic2=20092;//|----------------------magic number

/*
extern string S4="---------------- MA Filter";

extern bool MAFilter=false;//|-------------------moving average filter
extern int MAPeriod=20;//|-----------------------ma filter period
extern int MAMethod=0;//|------------------------ma filter method
extern int MAPrice=0;//|-------------------------ma filter price
*/

/*
extern string S5="---------------- 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 string S6="---------------- Extras";

extern bool ReverseSystem=false;//|--------------buy instead of sell, sell instead of buy
extern int Expiration=60;//|--------------------expiration in minute for the reverse pending order

/*
extern bool Hedge=false;//|----------------------enter an opposite trade
extern int HedgeSL=0;//|-------------------------stop loss
extern int HedgeTP=0;//|-------------------------take profit
extern bool ReverseAtStop=false;//|--------------buy instead of sell, sell instead of buy
extern bool Comments=true;//|--------------------allow comments on chart
*/

datetime PreviousBarTime1;
datetime PreviousBarTime2;

double maxEquity,minEquity,Balance=0.0;
double LotsFactor=1;
double InitialLotsFactor=1;

//|---------initialization

int init()
{
   //|---------martingale initialization
  
   int tempfactor,total=OrdersTotal();
   if(tempfactor==0 && total>0)
   {
      for(int cnt=0;cnt0)
   {
      for(cnt=0;cnt0)
   LotsFactor=tempfactor;

   /*if(Comments)Comment("\nLoading...");*/
   return(0);
}

//|---------deinitialization

/*int deinit()
{
  return(0);
}*/

int start()
{

//|---------trailing stop

   if(TrailingStop>0)MoveTrailingStop();

//|---------break even

   if(BreakEven>0)MoveBreakEven();
   
/*
//|---------basket profit loss

   if(BasketProfitLoss)
   {
      double CurrentProfit=0,CurrentBasket=0;
      CurrentBasket=AccountEquity()-AccountBalance();
      if(CurrentBasket>maxEquity)maxEquity=CurrentBasket;
      if(CurrentBasket=BasketProfit||CurrentBasket<=(BasketLoss*(-1)))
      {
         CloseBuyOrders(Magic);
         CloseSellOrders(Magic);
         return(0);
      }
   }
*/

/*
//|---------time filter

   if((TradeOnSunday==false&&DayOfWeek()==0)||(MondayToThursdayTimeFilter&&DayOfWeek()>=1&&DayOfWeek()<=4&&!(Hour()>=MondayToThursdayStartHour&&Hour()=FridayStartHour&&Hour()MAF))MABUY="true";
      if((MAFilter==false)||(MAFilter&&AskStoS) || (OnlyAtStochSignal && StoMaStoS ))
      && ((OnlyAtRSISignal==false && RSI>RSILevel) || (OnlyAtRSISignal && RSIaRSILevel ))
      && ((OnlyAtADXSignal==false && ADXP>ADXM) || (OnlyAtADXSignal && ADXPaADXM ))
      && ((OnlyAtBearsSignal==false && Bears>0) || (OnlyAtBearsSignal && Bearsa<0 && Bears>0 ))
      && ((OnlyAtBullsSignal==false && Bulls>0) || (OnlyAtBullsSignal && Bullsa<0 && Bulls>0 ))
      && ((OnlyAtMacdSignal==false && MacdM>MacdS) || (OnlyAtMacdSignal && MacdMaMacdS ))
      && ((OnlyAtROCSignal==false && ROC>0) || (OnlyAtROCSignal && ROCa<0 && ROC>0 ))
      && ((OnlyAtBBSignal==false && Close[i]>BBands) || (OnlyAtBBSignal && Close[i+1]BBands ))
      )BUY="true";
      if(
      ((OnlyAtStochSignal==false && StoM>StoS) || (OnlyAtStochSignal && StoMa>StoSa && StoMRSILevel && RSIADXMa && ADXP0 && Bears<0 ))
      && ((OnlyAtBullsSignal==false && Bulls<0) || (OnlyAtBullsSignal && Bullsa>0 && Bulls<0 ))
      && ((OnlyAtMacdSignal==false && MacdMMacdSa && MacdM0 && ROC<0 ))
      && ((OnlyAtBBSignal==false && Close[i]BBandsa && Close[i]0)Expire=TimeCurrent()+(Expiration*60)-5;
   
   if((CountOrders(OP_BUY,Magic1)+CountOrders(OP_SELL,Magic1)+CountOrders(OP_BUY,Magic2)+CountOrders(OP_SELL,Magic2))0){SL=Low[i]-(MarginPips+StopLoss)*Point;/*OPP=Bid-StopLoss*Point;SLP=Bid;*/}else {SL=0;/*SLP=0;*/}
         if(HideTP==false&&TakeProfit>0){TP=High[i]+(MarginPips+TakeProfit)*Point;/*TPP=Bid-(TakeProfit*2)*Point;*/}else {TP=0;/*TPP=0;*/}
         /*if(HideSL==false&&HedgeSL>0)SLH=Bid+HedgeSL*Point;else SLH=0;
         if(HideTP==false&&HedgeTP>0)TPH=Bid-HedgeTP*Point;else TPH=0;*/
         if(Martingale)ILots=NormalizeDouble(Lots*MartingaleFactor(),2);else ILots=Lots;
         if(ILotsMaxLots)ILots=MaxLots;
         ILots1=NormalizeDouble(ILots*(LotsPercent1/100),2);
         ILots2=NormalizeDouble(ILots*(LotsPercent2/100),2);
         
         Ticket1=OrderSend(Symbol(),OP_BUYSTOP,ILots1,High[i]+MarginPips*Point,Slippage,SL,TP,EAName,Magic1,Expire,Blue);
         Ticket2=OrderSend(Symbol(),OP_BUYSTOP,ILots2,High[i]+MarginPips*Point,Slippage,SL,0,EAName,Magic2,Expire,Blue);
         /*if(Hedge)TicketH=OrderSend(Symbol(),OP_SELL,ILots,Bid,Slippage,SLH,TPH,EAName,Magic,0,Red);
         if(ReverseAtStop&&StopLoss>0)TicketP=OrderSend(Symbol(),OP_SELLSTOP,Lots,OPP,Slippage,SLP,TPP,EAName,Magic,Expire,Red);*/
      }
      if(SignalSELL=="true"&&NewBarSell())
      {
         if(HideSL==false&&StopLoss>0){SL=High[i]+(MarginPips+StopLoss)*Point;/*OPP=Ask+StopLoss*Point;SLP=Ask;*/}else {SL=0;/*SLP=0;*/}
         if(HideTP==false&&TakeProfit>0){TP=Low[i]-(MarginPips+TakeProfit)*Point;/*TPP=Ask+(TakeProfit*2)*Point;*/}else {TP=0;/*TPP=0;*/}
         /*if(HideSL==false&&HedgeSL>0)SLH=Ask-HedgeSL*Point;else SLH=0;
         if(HideTP==false&&HedgeTP>0)TPH=Ask+HedgeTP*Point;else TPH=0;*/
         if(Martingale)ILots=NormalizeDouble(Lots*MartingaleFactor(),2);else ILots=Lots;
         if(ILotsMaxLots)ILots=MaxLots;
         ILots1=NormalizeDouble(ILots*(LotsPercent1/100),2);
         ILots2=NormalizeDouble(ILots*(LotsPercent2/100),2);
         
         Ticket1=OrderSend(Symbol(),OP_SELLSTOP,ILots1,Low[i]-MarginPips*Point,Slippage,SL,TP,EAName,Magic1,Expire,Red);
         Ticket2=OrderSend(Symbol(),OP_SELLSTOP,ILots2,Low[i]-MarginPips*Point,Slippage,SL,0,EAName,Magic2,Expire,Red);
         /*if(Hedge)TicketH=OrderSend(Symbol(),OP_BUY,ILots,Ask,Slippage,SLH,TPH,EAName,Magic,0,Blue);
         if(ReverseAtStop&&StopLoss>0)TicketP=OrderSend(Symbol(),OP_BUYSTOP,Lots,OPP,Slippage,SLP,TPP,EAName,Magic,Expire,Red);*/
      }
   }

//|---------close orders
/* 
   if(Hedge==false&&SELL=="true")
   {
      if(ReverseSystem)CloseSellOrders(Magic);else CloseBuyOrders(Magic);
   }
   if(Hedge==false&&BUY=="true")
   {
      if(ReverseSystem)CloseBuyOrders(Magic);else CloseSellOrders(Magic);
   }
   
   //|---------hidden sl-tp
   
   if(Hedge==false&&HideSL&&StopLoss>0)
   {
      CloseBuyOrdersHiddenSL(Magic);CloseSellOrdersHiddenSL(Magic);
   }
   if(Hedge==false&&HideTP&&TakeProfit>0)
   {
      CloseBuyOrdersHiddenTP(Magic);CloseSellOrdersHiddenTP(Magic);
   }
*/
//|---------not enough money warning

   int err=0;
   if(Ticket1<0&&Ticket2<0)
   {
      if(GetLastError()==134)
      {
         err=1;
         Print("Not enough money!");
      }
      return (-1);
   }
   
/*
   if(Comments)
   {
      Comment("\nCopyright © 2009, TradingSytemForex",
              "\n\nL o t s                   =  " + DoubleToStr(Lots,2),
              "\nB a l a n c e         =  " + DoubleToStr(AccountBalance(),2),
              "\nE q u i t y            =  " + DoubleToStr(AccountEquity(),2));
   }
*/

   return(0);
}

//|---------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);
}

int CloseBuyOrdersHiddenTP(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&&Bid>(OrderOpenPrice()+TakeProfit*Point))
      {
        OrderClose(OrderTicket(),OrderLots(),Bid,3);
      }
    }
  }
  return(0);
}

int CloseBuyOrdersHiddenSL(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&&Bid<(OrderOpenPrice()-StopLoss*Point))
      {
        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);
}

int CloseSellOrdersHiddenTP(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&&Ask<(OrderOpenPrice()-TakeProfit*Point))
      {
        OrderClose(OrderTicket(),OrderLots(),Ask,3);
      }
    }
  }
  return(0);
}

int CloseSellOrdersHiddenSL(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&&Ask>(OrderOpenPrice()+StopLoss*Point))
      {
        OrderClose(OrderTicket(),OrderLots(),Ask,3);
      }
    }
  }
  return(0);
}

//|---------count orders

int CountOrders(int Type,int Magic)
{
   int _CountOrd;
   _CountOrd=0;
   for(int i=0;iTrailingStop*Point)
         {
            if(TrailingStop>0&&Ask>NormalizeDouble(OrderOpenPrice(),Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)TrailingStop*Point)
         {
            if(TrailingStop>0&&Bid(NormalizeDouble(Ask+Point*(TrailingStop+TrailingStep),Digits)))||(OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Red);
                  return(0);
               }
            }
         }
      }
   }
}

//|---------break even

void MoveBreakEven()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt0)
            {
               if(NormalizeDouble((Bid-OrderOpenPrice()),Digits)>BreakEven*Point)
               {
                  if(NormalizeDouble((OrderStopLoss()-OrderOpenPrice()),Digits)<0)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+0*Point,Digits),OrderTakeProfit(),0,Blue);
                     return(0);
                  }
               }
            }
         }
         else
         {
            if(BreakEven>0)
            {
               if(NormalizeDouble((OrderOpenPrice()-Ask),Digits)>BreakEven*Point)
               {
                  if(NormalizeDouble((OrderOpenPrice()-OrderStopLoss()),Digits)<0)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-0*Point,Digits),OrderTakeProfit(),0,Red);
                     return(0);
                  }
               }
            }
         }
      }
   }
}

//|---------allow one action per bar

bool NewBarBuy()
{
   if(PreviousBarTime1MaxLots)Lots=MaxLots;
   return(0);
}

//|---------martingale

int MartingaleFactor()
{
   int histotal=OrdersHistoryTotal();
   if (histotal>0)
   {
      for(int cnt=histotal-1;cnt>=0;cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY))
         {
            if(OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic1)||(OrderMagicNumber()==Magic2)))
            {
               if(OrderProfit()<0)
               {
                  LotsFactor=LotsFactor*Multiplier;
                  return(LotsFactor);
               }
               else
               {
                  LotsFactor=InitialLotsFactor;
                  if(LotsFactor<=0)
                  {
                     LotsFactor=1;
                  }
                  return(LotsFactor);
               }
            }
         }
      }
   }
   return (LotsFactor);
}

Recommend