システムトレード 게시글

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;      //ストップロス(ピップ)
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/ EA初期化関数

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/ EAティック関数

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__,"--> OrderSendエラー ",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__,"--> OrderModifyエラー ",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)