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 取引が許可されていません。"); 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 = " " + ExtBotName + " - v." + (string)Version; strComment += " GMT時間 = " + TimeToString(TimeGMT(),TIME_DATE|TIME_SECONDS); strComment += " 取引時間 = [" + (string)InpStartHour + "h" + (string)InpStartMinute + " --> " + (string)InpEndHour + "h" + (string)InpEndMinute + "]"; strComment += " 現在のスプレッド = " + (string)acSpread + " ポイント"; strComment += " 現在のストップレベル = " + (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()); { 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 } } } } } } }