【MT4のプログラミングをやってみた】EAに決済機能を追加

 

まえがき

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

 

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

 

 

HTのMQL習得 EA編② EAに決済機能を追加

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

 

www.youtube.com

 

 

コード

//+------------------------------------------------------------------+
//| PanelTest.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;
input double SL=50.0;
input double SLIP=1.0;
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
//| 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)) {
//--- calculated SL and TP prices must be normalized
double stoploss=NormalizeDouble(Bid-SL*Point*10,Digits);
double takeprofit=NormalizeDouble(Bid+TP*Point*10,Digits);
//--- place market order to buy 1 lot
int ticket=OrderSend(NULL,OP_BUY,LOT,Ask,int(SLIP*10),stoploss,takeprofit,"OrderTest",MAGIC,0,clrDodgerBlue);
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");
}
//---
}

if(sparam=="EA_ButtonSell" && ObjectGetInteger(0,"EA_ButtonSell",OBJPROP_STATE)) {
//--- calculated SL and TP prices must be normalized
double stoploss=NormalizeDouble(Ask+SL*Point*10,Digits);
double takeprofit=NormalizeDouble(Ask-TP*Point*10,Digits);
//--- place market order to buy 1 lot
int ticket=OrderSend(NULL,OP_SELL,LOT,Bid,int(SLIP*10),stoploss,takeprofit,"OrderTest",MAGIC,0,clrDodgerBlue);
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");
}
//---
}

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));
};
}
}
}
}

//+------------------------------------------------------------------+
//| 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:20201120150915p:plain




 

 

 

以上、さつま芋でした。