系统交易 게시글

AK-47 Scalper EA – MetaTrader 4的自动交易专家

첨부파일
42236.zip (5.76 KB, 다운로드 0회)

1. 输入参数

#define ExtBotName "AK-47 Scalper EA" //机器人名称
#define  Version "1.00"

//--- 输入参数 ---
extern string  EASettings        = "---------------------------------------------"; //-------- <EA 设置> --------
input int      InpMagicNumber    = 124656;   //魔术数字

extern string  TradingSettings   = "---------------------------------------------"; //-------- <交易设置> --------
input double   Inpuser_lot       = 0.01;     //手数
input double   InpSL_Pips        = 3.5      //止损(点数)
input double   InpMax_spread     = 0.5      //允许的最大点差(点数)(0 = 浮动)

extern string  MoneySettings     = "---------------------------------------------"; //-------- <资金设置> --------
input bool     isVolume_Percent  = true;     //允许按比例计算手数
input double   InpRisk           = 3        //风险百分比(%)

input string   TimeSettings      = "---------------------------------------------"; //-------- <交易时间设置> --------
input bool     InpTimeFilter     = true      //交易时间过滤
input int      InpStartHour      = 2         //开始小时
input int      InpStartMinute    = 30        //开始分钟
input int      InpEndHour        = 21        //结束小时
input int      InpEndMinute      = 0         //结束分钟

2. 本地变量初始化
//--- 变量 ---
int      Pips2Points;               // 滑点  3点    3=点数    30=点数
double   Pips2Double;               // 止损 15点    0.015      0.0150
int      InpMax_slippage   = 3;     // 允许的最大滑点
bool     isOrder           = false; // 仅打开1个订单
int      slippage;
string   strComment        = "";

3. 主代码

a/ 专家初始化函数

int OnInit()
  {
//---   
   //3或5位数字检测
   //点与点数
   if (Digits % 2 == 1)
   {
      Pips2Double  = _Point*10; 
      Pips2Points  = 10;
      slippage = 10* InpMax_slippage;
   } 
   else
   {    
      Pips2Double  = _Point;
      Pips2Points  =  1;
      slippage = InpMax_slippage;
   }
   
//---
   return(INIT_SUCCEEDED);
  }

b/ 专家滴答函数

void OnTick()
  {
//---
     if(IsTradeAllowed() == false)
     {
      Comment("AK-47 EA\n交易不允许.");
      return;
     }
     
       MqlDateTime structTime;
       TimeCurrent(structTime);
       structTime.sec = 0;
       
       //设置开始时间
       structTime.hour = InpStartHour;
       structTime.min = InpStartMinute;       
       datetime timeStart = StructToTime(structTime);
       
       //设置结束时间
       structTime.hour = InpEndHour;
       structTime.min = InpEndMinute;
       datetime timeEnd = StructToTime(structTime);
       
       double acSpread = MarketInfo(Symbol(), MODE_SPREAD);
       StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
      
      strComment = "\n" + ExtBotName + " - v." + (string)Version;
      strComment += "\nGMT时间 = " + TimeToString(TimeGMT(),TIME_DATE|TIME_SECONDS);
      strComment += "\n交易时间 = [" + (string)InpStartHour + "h" + (string)InpStartMinute + " --> " +  (string)InpEndHour + "h" + (string)InpEndMinute + "]";
      
      strComment += "\n当前点差 = " + (string)acSpread + " 点数";
      strComment += "\n当前止损水平 = " + (string)StopLevel + " 点数";
      
      Comment(strComment);
   
      //更新值
      UpdateOrders();
      
      TrailingStop();
      
      //检查交易时间
      if(InpTimeFilter)
      {
         if(TimeCurrent() >= timeStart && TimeCurrent() < timeEnd)
         {
            if(!isOrder) OpenOrder();
         }
      }
      else
      {
         if(!isOrder) OpenOrder();
      }
  }

3.1 计算信号以发送订单

void OpenOrder(){
   
   //int OrdType = OP_SELL;//-1;

   double TP = 0;
   double SL = 0;
   string comment = ExtBotName;

   //计算手数
   double lot1 = CalculateVolume();
   
   //if(OrdType == OP_SELL){
      double OpenPrice = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits);
      SL = NormalizeDouble(Ask + StopLevel * _Point + InpSL_Pips/2 * Pips2Double, Digits);
       
      if(CheckSpreadAllow())                                    //检查点差
      {
         if(!OrderSend(_Symbol, OP_SELLSTOP, lot1, OpenPrice, slippage, SL, TP, comment, InpMagicNumber, 0, clrRed))
         Print(__FUNCTION__,"--> 发送订单错误 ",GetLastError());
      }
   //}
}

3.2 计算手数

double CalculateVolume()
  {
   double LotSize = 0;

   if(isVolume_Percent == false)
     {
      LotSize = Inpuser_lot;
     }
   else
     {

      LotSize = (InpRisk) * AccountFreeMargin();
      LotSize = LotSize /100000;
      double n = MathFloor(LotSize/Inpuser_lot);
      //Comment((string)n);
      LotSize = n * Inpuser_lot;

      if(LotSize < Inpuser_lot)
         LotSize = Inpuser_lot;

      if(LotSize > MarketInfo(Symbol(),MODE_MAXLOT))
         LotSize = MarketInfo(Symbol(),MODE_MAXLOT);

      if(LotSize < MarketInfo(Symbol(),MODE_MINLOT))
         LotSize = MarketInfo(Symbol(),MODE_MINLOT);
     }

   return(LotSize);
  }

3.3 EA具有“跟踪止损”功能, 止损会在每次价格变动时改变(下跌)

void TrailingStop()
  {
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if((OrderMagicNumber() == InpMagicNumber) && (OrderSymbol() == Symbol()))   //_Symbol))
           {

            //对于卖出订单
            if(OrderType() == OP_SELL)
              {
                  //--当价格变化时计算止损
                  double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double;
                  if(SL_in_Pip > InpSL_Pips){
                        double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + InpSL_Pips * Pips2Double, Digits);
                        if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrRed))
                        {
                           Print(__FUNCTION__,"--> 修改订单错误 ",GetLastError());
                         continue  
                        }
                }
              }
             
            //对于卖出止损订单
            else if(OrderType() == OP_SELLSTOP)
              {
                  double SL_in_Pip = NormalizeDouble(OrderStopLoss() - (StopLevel * _Point) - Ask, Digits) / Pips2Double;
                  
                  if(SL_in_Pip < InpSL_Pips/2){
                     double newOP = NormalizeDouble(Bid - (StopLevel * _Point) - (InpSL_Pips/2) * Pips2Double, Digits);
                     double newSL = NormalizeDouble(Ask + (StopLevel * _Point) + (InpSL_Pips/2) * Pips2Double, Digits);
                     
                     if(!OrderModify(OrderTicket(), newOP, newSL, OrderTakeProfit(), 0, clrRed))
                         {
                             Print(__FUNCTION__,"--> 修改挂单错误!", GetLastError());
                        continue  
                     }
                        }
              }
              
          }
     }
  }


연관 포스트

댓글 (0)