시스템트레이딩 게시글

AK-47 스캘퍼 EA - MetaTrader 4를 위한 자동매매 시스템

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

1. 입력 매개변수

#define ExtBotName "AK-47 스캘퍼 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;      // 손절 (Pips 단위)
input double   InpMax_spread     = 0.5;      // 최대 허용 스프레드 (Pips 단위) (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 pips    3=points    30=points
double   Pips2Double;               // 손절 15 pips    0.015      0.0150
int      InpMax_slippage   = 3;     // 허용 최대 슬리피지.
bool     isOrder           = false; // 단일 주문만 오픈
int      slippage;
string   strComment        = "";

3. 주요 코드

a/ 전문가 초기화 함수

int OnInit()
  {
//---   
   //3 또는 5 자리 감지
   //Pip과 point
   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의 기능은 "트레일링 스톱"으로, 가격이 변할 때마다 SL이 변경됩니다.

 

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)
              {
                  //-- 가격 변동 시 SL 계산
                  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());
                        }
                    }
              }
             
            //판매 스탑 주문의 경우
            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)