【MT4のプログラミングをやってみた】EAに各種条件を追加

 

まえがき

こんにちは、こんばんは、脱初心者トレーダーさつま芋です。

 

ちょっと調べたり考えたりすれば分かるような盲点情報などをシェアしたいと思います。

 

 

HTのMQL習得 EA編④ EAに各種条件を追加

今回は次の動画をタイプしてみました。

 

www.youtube.com

 

 

コード

//+------------------------------------------------------------------+
//| EA_demo.mq4 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict

#include <stdlib.mqh>

//--- input parameters
input int MAGIC=123456; // マジックナンバー
input double LOT=0.1; // ロット
input double TP=50.0; // TP [pips]
input double SL=50.0; // SL [pips]
input double SLIP=1.0; // 許容スリッページ [pips]

//--- indicator parameters
input string _time0 = "";
input string _time1 = ""; //[ time ]
input int TIME_S =1; // 開始時間
input int TIME_E =23; // 終了時間

input string _00 = ""; //  
input string _01 = ""; // [ MA1 ]
input int PERIOD0 = 5; // 期間
input int SHIFT0 = 0; // シフト
input ENUM_MA_METHOD METHOD0 = MODE_SMA; // MA種別
input ENUM_APPLIED_PRICE PRICE0 = PRICE_CLOSE; // 適用価格

input string _10 = ""; //  
input string _11 = ""; // [ MA2 ]
input int PERIOD1 = 20; // 期間
input int SHIFT1 = 0; // シフト
input ENUM_MA_METHOD METHOD1 = MODE_SMA; // MA種別
input ENUM_APPLIED_PRICE PRICE1 = PRICE_CLOSE; // 適用価格

input string _rsi0 = ""; //  
input string _rsi1 = ""; // [ RSI ]
input int PERIOD_RSI = 14; // 期間
input double LEVEL_U = 70; // 上限レベル
input double LEVEL_L = 30; // 下限レベル

input string _atr0 = ""; //  
input string _atr1 = ""; // [ ATR ]
input int PERIOD_ATR = 14; // 期間
input double LEVEL_ATR = 1; // 許容値 [pips]

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
int x=0, y=15, w=80, h=50;
ButtonCreate(0,"EA_ButtonSell",0,x,y,w,h,CORNER_LEFT_UPPER,"Sell","Arial Black",20,clrWhite,clrDodgerBlue);
ButtonCreate(0,"EA_ButtonBuy",0,x+w,y,w,h,CORNER_LEFT_UPPER,"Buy","Arial Black",20,clrWhite,clrRed);
ButtonCreate(0,"EA_ButtonExit",0,x,y+h,w*2,h,CORNER_LEFT_UPPER,"Exit","Arial Black",20,clrBlack,clrYellow);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ObjectsDeleteAll(0,"EA_Button");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(ObjectGetInteger(0,"EA_ButtonSell",OBJPROP_STATE)) ObjectSetInteger(0,"EA_ButtonSell",OBJPROP_STATE,false);
if(ObjectGetInteger(0,"EA_ButtonBuy",OBJPROP_STATE)) ObjectSetInteger(0,"EA_ButtonBuy",OBJPROP_STATE,false);
if(ObjectGetInteger(0,"EA_ButtonExit",OBJPROP_STATE)) ObjectSetInteger(0,"EA_ButtonExit",OBJPROP_STATE,false);

if(Hour()<TIME_S || Hour()>TIME_E) return;

int bar =1;
double buy = iCustom(NULL,0,"MA_Cross_Sign_demo.ex4","",PERIOD0,SHIFT0,METHOD0,PRICE0,
clrNONE,0,0,"","",PERIOD1,SHIFT1,METHOD1,PRICE1,clrNONE,0,0,2,bar);
double sell = iCustom(NULL,0,"MA_Cross_Sign_demo.ex4","",PERIOD0,SHIFT0,METHOD0,PRICE0,
clrNONE,0,0,"","",PERIOD1,SHIFT1,METHOD1,PRICE1,clrNONE,0,0,3,bar);

double rsi = iRSI(NULL,PERIOD_CURRENT,PERIOD_RSI,PRICE_CLOSE,bar);

double atr = iATR(NULL,PERIOD_CURRENT,PERIOD_ATR,bar);

static datetime timeEntry;
if(Time[0]>timeEntry) {
if(buy != EMPTY_VALUE && rsi <= LEVEL_L && atr >= LEVEL_ATR*Point*10) {
MakePosition(OP_BUY, Ask, Bid-SL*Point*10, Bid+TP*Point*10, clrRed);
timeEntry =Time[0];
}
}
if(Time[0]>timeEntry) {
if(sell != EMPTY_VALUE && rsi >= LEVEL_U && atr >= LEVEL_ATR*Point*10) {
MakePosition(OP_SELL, Bid, Ask+SL*Point*10, Ask-TP*Point*10, clrDodgerBlue);
timeEntry =Time[0];
}
}

 

}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if(id==CHARTEVENT_OBJECT_CLICK) {
if(sparam=="EA_ButtonBuy" && ObjectGetInteger(0,"EA_ButtonBuy",OBJPROP_STATE)) {
MakePosition(OP_BUY, Ask, Bid-SL*Point*10, Bid+TP*Point*10, clrRed);
}

if(sparam=="EA_ButtonSell" && ObjectGetInteger(0,"EA_ButtonSell",OBJPROP_STATE)) {
MakePosition(OP_SELL, Bid, Ask+SL*Point*10, Ask-TP*Point*10, clrDodgerBlue);
}

if(sparam=="EA_ButtonExit" && ObjectGetInteger(0,"EA_ButtonExit",OBJPROP_STATE)) {
for(int i = OrdersTotal()-1; i>=0; i--) {
if(!OrderSelect(i,SELECT_BY_POS)) continue;
if(OrderSymbol() != _Symbol || OrderMagicNumber() != MAGIC) continue;
int type = OrderType();
if(type != OP_BUY && type != OP_SELL) continue;
if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),int(SLIP*10),clrYellow)) {
Print("OrderClose placed successfully");
PlaySound("ok.wav");
} else {
int error_code = GetLastError();
Print("OrderClose failed with error #",error_code);
Alert("OrderClose error \n",error_code," ",ErrorDescription(error_code));
};
}
}
}
}

//+------------------------------------------------------------------+
//| Entry Function |
//+------------------------------------------------------------------+
void MakePosition(int type, double price, double sl, double tp, color clr)
{
//--- calculated SL and TP prices must be normalized
double stoploss=NormalizeDouble(sl,Digits);
double takeprofit=NormalizeDouble(tp,Digits);
//--- place market order to buy 1 lot
int ticket=OrderSend(NULL,type,LOT,price,int(SLIP*10),stoploss,takeprofit,"OrderTest",MAGIC,0,clr);
if(ticket<0) {
int error_code = GetLastError();
Print("OrderSend failed with error #",error_code);
Alert("OrderSend error \n",error_code," ",ErrorDescription(error_code));
} else {
Print("OrderSend placed successfully");
PlaySound("ok.wav");
}
//---
}

//+------------------------------------------------------------------+
//| Create the button |
//+------------------------------------------------------------------+
bool ButtonCreate(const long chart_ID=0, // chart's ID
const string name="Button", // button name
const int sub_window=0, // subwindow index
const int x=0, // X coordinate
const int y=0, // Y coordinate
const int width=50, // button width
const int height=18, // button height
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
const string text="Button", // text
const string font="Arial", // font
const int font_size=10, // font size
const color clr=clrBlack, // text color
const color back_clr=C'236,233,216', // background color
const color border_clr=clrNONE, // border color
const bool state=false, // pressed/released
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- reset the error value
ResetLastError();
//--- create the button
if(!ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0)) {
Print(__FUNCTION__,
": failed to create the button! Error code = ",GetLastError());
return(false);
}
//--- set button coordinates
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- set button size
ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
//--- set the chart's corner, relative to which point coordinates are defined
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- set the text
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set text color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set background color
ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
//--- set border color
ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
//--- display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- set button state
ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);
//--- enable (true) or disable (false) the mode of moving the button by mouse
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}

//+------------------------------------------------------------------+

 

 

あとがき

コンパイル&実行した画像は次の状態でした。

  

f:id:satsumaim0:20201120173250p:plain


見た目は前回と大差ありませんが…
 

 

以上、さつま芋でした。