诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 4 > MT4 EA > 正文

vForce Like EA

vForce Like EA是一款挂STOP突破单交易的EA,在信号上面可以选择KD、RSI、MACD等。

vForce Like EA源码:

//+------------------------------------------------------------------+
//|                                               vForce Like EA.mq4 |
//|                               Copyright @2009, TradingSytemForex |
//+------------------------------------------------------------------+
#property copyright "Copyright @2009, TradingSytemForex"
#property link "http://www.125808047.com"

#define EAName "vForce Like EA"
input string S1="---------------- Entry Settings";
input int  StochKPeriod  = 5;
input int  StochDPeriod  = 3;
input int  StochSlowing  = 3;
input bool OnlyAtStochSignal=false;
input int  RSIPeriod     = 14;
input int  RSILevel      = 50;
input bool OnlyAtRSISignal=false;
input int  ADXPeriod     = 14;
input bool OnlyAtADXSignal=false;
input int  BearsPeriod   = 13;
input bool OnlyAtBearsSignal=false;
input int  BullsPeriod   = 13;
input bool OnlyAtBullsSignal=false;
input int  MACDFast      = 12;
input int  MACDSlow      = 26;
input int  MACDSMA       = 9;
input bool OnlyAtMacdSignal=false;
input int  RPeriod       = 10;
input bool OnlyAtROCSignal=false;
input int  BBPeriod      = 20;
input bool OnlyAtBBSignal=false;

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

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

input string S6="---------------- Extras";

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

datetime PreviousBarTime1;
datetime PreviousBarTime2;

double maxEquity,minEquity,Balance=0.0;
double LotsFactor=1;
double InitialLotsFactor=1;
bool cg = false;

int OnInit()
  { 
   int tempfactor,total=OrdersTotal();
   if(tempfactor==0 && total>0)
   {
      for(int cnt=0;cnt<total;cnt++)
      {
         if(OrderSelect(cnt,SELECT_BY_POS))
         {
            if(OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic1)||(OrderMagicNumber()==Magic2)))
            {
               tempfactor=NormalizeDouble(OrderLots()/Lots,1+(MarketInfo(Symbol(),MODE_MINLOT)==0.01));
               break;
            }
         }
      }
   }
   int histotal=OrdersHistoryTotal();

   if(tempfactor==0&&histotal>0)
   {
      for(cnt=0;cnt<histotal;cnt++)
      {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY))
         {
            if(OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic1)||(OrderMagicNumber()==Magic2)))
            {
               tempfactor=NormalizeDouble(OrderLots()/Lots,1+(MarketInfo(Symbol(),MODE_MINLOT)==0.01));
               break;
            }
         }
      }
   }
   
   if (tempfactor>0)
   LotsFactor=tempfactor;
   return(0);
}

void OnTick(void)
  {
   if(TrailingStop>0)MoveTrailingStop();

   if(BreakEven>0)MoveBreakEven();
   
   int limit=1;
   for(int i=1;i<=limit;i++)
   {
      double StoMa=iStochastic(NULL,0,StochKPeriod,StochDPeriod,StochSlowing,MODE_SMA,0,MODE_MAIN,i+1);
      double StoSa=iStochastic(NULL,0,StochKPeriod,StochDPeriod,StochSlowing,MODE_SMA,0,MODE_SIGNAL,i+1);
      double StoM=iStochastic(NULL,0,StochKPeriod,StochDPeriod,StochSlowing,MODE_SMA,0,MODE_MAIN,i);
      double StoS=iStochastic(NULL,0,StochKPeriod,StochDPeriod,StochSlowing,MODE_SMA,0,MODE_SIGNAL,i);
      
      double RSIa=iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,i+1);
      double RSI=iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,i);
      
      double ADXPa=iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_PLUSDI,i+1);
      double ADXMa=iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MINUSDI,i+1);
      double ADXP=iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_PLUSDI,i);
      double ADXM=iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MINUSDI,i);
      
      double Bearsa=iBearsPower(NULL,0,BearsPeriod,PRICE_CLOSE,i+1);
      double Bullsa=iBullsPower(NULL,0,BullsPeriod,PRICE_CLOSE,i+1);
      double Bears=iBearsPower(NULL,0,BearsPeriod,PRICE_CLOSE,i);
      double Bulls=iBullsPower(NULL,0,BullsPeriod,PRICE_CLOSE,i);
      
      double MacdMa=iMACD(NULL,0,MACDFast,MACDSlow,MACDSMA,PRICE_CLOSE,MODE_MAIN,i+1);
      double MacdSa=iMACD(NULL,0,MACDFast,MACDSlow,MACDSMA,PRICE_CLOSE,MODE_SIGNAL,i+1);
      double MacdM=iMACD(NULL,0,MACDFast,MACDSlow,MACDSMA,PRICE_CLOSE,MODE_MAIN,i);
      double MacdS=iMACD(NULL,0,MACDFast,MACDSlow,MACDSMA,PRICE_CLOSE,MODE_SIGNAL,i);
      
      double CurrentClosea=iClose(NULL,0,i+RPeriod);
      double PrevClosea=iClose(NULL,0,i+RPeriod+RPeriod);
      double ROCa=CurrentClosea-PrevClosea;
      
      double CurrentClose=iClose(NULL,0,i);
      double PrevClose=iClose(NULL,0,i+RPeriod);
      double ROC=CurrentClose-PrevClose;
      
      double BBandsa=iMA(Symbol(),0,BBPeriod,0,MODE_SMA,PRICE_CLOSE,i+1);
      double BBands=iMA(Symbol(),0,BBPeriod,0,MODE_SMA,PRICE_CLOSE,i);
 
      string BUY="false";
      string SELL="false";

      if(
      ((OnlyAtStochSignal==false && StoM>StoS) || (OnlyAtStochSignal && StoMa<StoSa && StoM>StoS ))
      && ((OnlyAtRSISignal==false && RSI>RSILevel) || (OnlyAtRSISignal && RSIa<RSILevel && RSI>RSILevel ))
      && ((OnlyAtADXSignal==false && ADXP>ADXM) || (OnlyAtADXSignal && ADXPa<ADXMa && ADXP>ADXM ))
      && ((OnlyAtBearsSignal==false && Bears>0) || (OnlyAtBearsSignal && Bearsa<0 && Bears>0 ))
      && ((OnlyAtBullsSignal==false && Bulls>0) || (OnlyAtBullsSignal && Bullsa<0 && Bulls>0 ))
      && ((OnlyAtMacdSignal==false && MacdM>MacdS) || (OnlyAtMacdSignal && MacdMa<MacdSa && MacdM>MacdS ))
      && ((OnlyAtROCSignal==false && ROC>0) || (OnlyAtROCSignal && ROCa<0 && ROC>0 ))
      && ((OnlyAtBBSignal==false && Close[i]>BBands) || (OnlyAtBBSignal && Close[i+1]<BBandsa && Close[i]>BBands ))
      )BUY="true";
      if(
      ((OnlyAtStochSignal==false && StoM>StoS) || (OnlyAtStochSignal && StoMa>StoSa && StoM<StoS ))
      && ((OnlyAtRSISignal==false && RSI>RSILevel) || (OnlyAtRSISignal && RSIa>RSILevel && RSI<RSILevel ))
      && ((OnlyAtADXSignal==false && ADXP>ADXM) || (OnlyAtADXSignal && ADXPa>ADXMa && ADXP<ADXM ))
      && ((OnlyAtBearsSignal==false && Bears<0) || (OnlyAtBearsSignal && Bearsa>0 && Bears<0 ))
      && ((OnlyAtBullsSignal==false && Bulls<0) || (OnlyAtBullsSignal && Bullsa>0 && Bulls<0 ))
      && ((OnlyAtMacdSignal==false && MacdM<MacdS) || (OnlyAtMacdSignal && MacdMa>MacdSa && MacdM<MacdS ))
      && ((OnlyAtROCSignal==false && ROC<0) || (OnlyAtROCSignal && ROCa>0 && ROC<0 ))
      && ((OnlyAtBBSignal==false && Close[i]<BBands) || (OnlyAtBBSignal && Close[i+1]>BBandsa && Close[i]<BBands ))
      )SELL="true";
      
      string SignalBUY="false";
      string SignalSELL="false";
      
      if(BUY=="true")if(ReverseSystem)SignalSELL="true";else SignalBUY="true";
      if(SELL=="true")if(ReverseSystem)SignalBUY="true";else SignalSELL="true";
      
   }
   if(RiskMM)CalculateMM();

   double SL,TP,ILots,ILots1,ILots2;
   int Ticket1,Ticket2,Expire=0;
   if(Expiration>0)Expire=TimeCurrent()+(Expiration*60)-5;
   
   if((CountOrders(OP_BUY,Magic1)+CountOrders(OP_SELL,Magic1)+CountOrders(OP_BUY,Magic2)+CountOrders(OP_SELL,Magic2))<MaxOrders)
   {  
      if(SignalBUY=="true"&&NewBarBuy())
      {
         if(HideSL==false&&StopLoss>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(Martingale)ILots=NormalizeDouble(Lots*MartingaleFactor(),2);else ILots=Lots;
         if(ILots<MinLots)ILots=MinLots;if(ILots>MaxLots)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(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(Martingale)ILots=NormalizeDouble(Lots*MartingaleFactor(),2);else ILots=Lots;
         if(ILots<MinLots)ILots=MinLots;if(ILots>MaxLots)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);
      }
   }

   int err=0;
   if(Ticket1<0&&Ticket2<0)
   {
      if(GetLastError()==134)
      {
         err=1;
         Print("Not enough money!");
      }
      return;
   }
}

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

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

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

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

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

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

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

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

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

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

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

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

int CountOrders(int Type,int Magic)
{
   int _CountOrd;
   _CountOrd=0;
   for(int i=0;i<OrdersTotal();i++)
   {
      cg = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol())
      {
         if((OrderType()==Type && (OrderMagicNumber()==Magic || Magic==0)))_CountOrd++;
      }
   }
   return(_CountOrd);
}

void MoveTrailingStop()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      cg = OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol()&&((OrderMagicNumber()==Magic2)))
      {
         if(OrderType()==OP_BUY&&NormalizeDouble((Ask-OrderOpenPrice()),Digits)>TrailingStop*Point)
         {
            if(TrailingStop>0&&Ask>NormalizeDouble(OrderOpenPrice(),Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-Point*(TrailingStop+TrailingStep),Digits))||(OrderStopLoss()==0))
               {
                  cg = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
               }
            }
         }
         if(OrderType()==OP_SELL&&NormalizeDouble((OrderOpenPrice()-Bid),Digits)>TrailingStop*Point)
         {
            if(TrailingStop>0&&Bid<NormalizeDouble(OrderOpenPrice(),Digits))  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)>(NormalizeDouble(Ask+Point*(TrailingStop+TrailingStep),Digits)))||(OrderStopLoss()==0))
               {
                  cg = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Red);
               }
            }
         }
      }
   }
}

void MoveBreakEven()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      cg = OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol()&&((OrderMagicNumber()==Magic2)))
      {
         if(OrderType()==OP_BUY)
         {
            if(BreakEven>0)
            {
               if(NormalizeDouble((Bid-OrderOpenPrice()),Digits)>BreakEven*Point)
               {
                  if(NormalizeDouble((OrderStopLoss()-OrderOpenPrice()),Digits)<0)
                  {
                     cg = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+0*Point,Digits),OrderTakeProfit(),0,Blue);
                  }
               }
            }
         }
         else
         {
            if(BreakEven>0)
            {
               if(NormalizeDouble((OrderOpenPrice()-Ask),Digits)>BreakEven*Point)
               {
                  if(NormalizeDouble((OrderOpenPrice()-OrderStopLoss()),Digits)<0)
                  {
                     cg = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-0*Point,Digits),OrderTakeProfit(),0,Red);
                  }
               }
            }
         }
      }
   }
}

bool NewBarBuy()
{
   if(PreviousBarTime1<Time[0])
   {
      PreviousBarTime1=Time[0];
      return(true);
   }
   return(false);
}

bool NewBarSell()
{
   if(PreviousBarTime2<Time[0])
   {
      PreviousBarTime2=Time[0];
      return(true);
   }
   return(false);
}

void CalculateMM()
{
   MinLots=MarketInfo(Symbol(),MODE_MINLOT);
   MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);
   Lots=AccountFreeMargin()/100000*RiskPercent;
   Lots=MathMin(MaxLots,MathMax(MinLots,Lots));
   if(MinLots<0.1)Lots=NormalizeDouble(Lots,2);
   else
   {
     if(MinLots<1)Lots=NormalizeDouble(Lots,1);
     else Lots=NormalizeDouble(Lots,0);
   }
   if(Lots<MinLots)Lots=MinLots;
   if(Lots>MaxLots)Lots=MaxLots;
}

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);
}
打赏
版权所有转载请注明标题及链接:峰汇在线 » vForce Like EA
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏