EAサンプルコード
EA、サンプルコードが見にくいと思ったので整理しました
MAの関数とZigzagの関数をサンプルとしていれてあります。
もっときれいに出来たらまた綺麗にまとめておきます。
コピペでそのまま使えます。
ここから↓
//+------------------------------------------------------------------+
//| sample.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
static int TicketNumber;
extern string Main ="Main";
input int Magic1 = 1234;
input double Slippage = 3.0;
input int MaxSpread = 20;
input int MaxPosition = 1;
input double Lots = 0.01;
input int TakePfofit = 15;
input int StopLoss = 30;
extern string EntryTime ="Entry Time";
input bool NoEntryTime = false;
input string NoEntryStartTime ="07:00";
input string NoEntryEndTime ="22:00";
extern string Exit ="ExitMode";
input bool TrailingStop = false;
extern int Trailing_Stop = 15;
extern int Minimum_Profit = 10;
input bool BreakEven = false;
extern double Break_Even = 20;
input bool AllClose = false;
input int TotalProfit = 20;
extern string 福利厚生 ="福利厚生";
input bool MM = false;
input double Risk = 1.0;
extern string Indicater ="インジケータ";
input string ZigZag ="ZigZag";
input int Depth = 25;
input int Deviation = 5;
input int Backstep = 3;
input int Bar = 200;
input int TimeFreame1 = 15;
input string MovingAverage ="Moving Average";
input int S_MA = 25;
input int C_MA = 70;
input int TimeFreame2 = 15;
//+------------------------------------------------------------------+
//|Void Function |
//+------------------------------------------------------------------+
//トレーリングストップ_利益確保型
void TrailingStop()
{
if(TrailingStop==true)
{
double new_sl;
int res;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic1)
{
if(OrderType()==OP_BUY && OrderOpenPrice()+Trailing_Stop*AdjustPoint(Symbol())<Bid && OrderStopLoss()<Bid)
{
new_sl = Bid-((Trailing_Stop-Minimum_Profit)*AdjustPoint(Symbol()));
if( new_sl > OrderStopLoss()+0.005*AdjustPoint(Symbol()))
{
res=OrderModify(OrderTicket(),OrderOpenPrice(),new_sl,OrderTakeProfit(),0,clrNONE);
}
}
else if(OrderType()==OP_SELL && OrderOpenPrice()-Trailing_Stop*AdjustPoint(Symbol())>Ask && (OrderStopLoss()>Ask || OrderStopLoss()==0) )
{
new_sl = Ask+((Trailing_Stop-Minimum_Profit)*AdjustPoint(Symbol()));
if( new_sl < OrderStopLoss()-0.005*AdjustPoint(Symbol()) || OrderStopLoss()==0 )
{
res=OrderModify(OrderTicket(),OrderOpenPrice(),new_sl,OrderTakeProfit(),0,clrNONE);
}
}
}
}
}
}
//ブレークイーブンストップ関数
void BreakEvenStop()
{
if(BreakEven==true)
{
int res;
double Pips_Profit;
double Min_Profit;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic1)
{
if(OrderType()==OP_BUY)
{
Pips_Profit=Bid-OrderOpenPrice();
Min_Profit=Break_Even*AdjustPoint(Symbol());
if (Pips_Profit>=Min_Profit && OrderOpenPrice()!=OrderStopLoss())
{
res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Orange);
}
}
else if(OrderType()==OP_SELL)
{
Pips_Profit=OrderOpenPrice()-Ask;
Min_Profit=Break_Even*AdjustPoint(Symbol());
if (Pips_Profit>=Min_Profit && OrderOpenPrice()!=OrderStopLoss())
{
res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Orange);
}
}
}
}
}
}
//すべて決済する関数
void allClose(color clr)
{
int TotalNum;
int i;
double profit=0;
double totalProfit=0;
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
continue;
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=Magic1)
continue;
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,NormalizeDouble(Slippage,0),clr);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,NormalizeDouble(Slippage,0),clr);
}
}
//+------------------------------------------------------------------+
//| Position function |
//+------------------------------------------------------------------+
//ロングポジション数を取得
int LongPosition()
{
int buys=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic1)
{
if(OrderType()==OP_BUY) buys++;
}
}
return(buys);
}
//ショートポジション数を取得
int ShortPosition()
{
int sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic1)
{
if(OrderType()==OP_SELL) sells++;
}
}
return(sells);
}
//ポジションクローズ関数
void CloseOrder(int ClosePosition)
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
int res;
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderMagicNumber()==Magic1 && OrderSymbol()==Symbol())
{
if(OrderType()==OP_SELL && (ClosePosition==-1 || ClosePosition==0 )) //売りポジションのクローズ
{
res=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,Silver);
}
else if(OrderType()==OP_BUY && (ClosePosition==1 || ClosePosition==0 ) ) //買いポジションのクローズ
{
res=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,Silver);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//void呼び出し
static bool checkDone;
int sec = TimeSeconds(TimeGMT());
if(sec == 0 || sec == 10 || sec == 20|| sec == 30|| sec == 40|| sec == 50 )
{
if(checkDone==false)
{
TrailingStop();
BreakEvenStop();
checkDone = true;
}
}
else
{
checkDone = false;
}
// ニューバーの発生直後以外は取引しない
static datetime bartime=Time[0];
if (Time[0]==bartime) return;
bartime=Time[0];
checkDone = false;
//Position
int LongNum=LongPosition();
int ShortNum=ShortPosition();
int TotalNum=ShortNum+LongNum;
int time_filter=0;
//+---------------------------EntryTime-------------------------------+
if(NoEntryTime==true)
{
if(!EntryTimeCheck(NoEntryStartTime,NoEntryEndTime))time_filter = true;
}
//+---------------------------Zigzag----------------------------------+
int m,n;
m=0;
n=0;
double Top[10];
double Bottom[10];
int tPosition[10];
int bPosition[10];
for(int i=0;i<=Bar;i++)
{
double Zg=NormalizeDouble(iCustom(NULL,TimeFreame1,"ZigZag",Depth,Deviation,Backstep,0,i),5);
if(Zg!=0 && Zg==NormalizeDouble(High[i],5))
{
Top[m++]=Zg;
tPosition[m-1]=i;
if(m>=10)break;
}
if(Zg!=0 && Zg==NormalizeDouble(Low[i],5))
{
Bottom[n++]=Zg;
bPosition[n-1]=i;
if(n>=10)break;
}
}
//+-----------------------------Indicater-----------------------------+
double s_ma=iMA(NULL,TimeFreame2,S_MA,0,0,PRICE_CLOSE,1);
double c_ma=iMA(NULL,TimeFreame2,C_MA,0,0,PRICE_CLOSE,1);
//+-------------------------------------------------------------------+
if(MM==true)//複利がONの場合
{
if((NoEntryTime == false || time_filter == true)&&TotalNum<MaxPosition && MarketInfo(Symbol(),MODE_SPREAD)< MaxSpread&&s_ma>c_ma&&Top[0]>Top[1]&&Bottom[0]>Bottom[1])
{
TicketNumber=OrderSend(Symbol(),OP_BUY,LotSize(),Ask,Slippage,Ask-StopLoss*Point*10,Ask+TakePfofit*Point*10,"Logic1",Magic1,0,Red);
}
if((NoEntryTime == false || time_filter == true)&&TotalNum<MaxPosition && MarketInfo(Symbol(),MODE_SPREAD)< MaxSpread&&s_ma<c_ma&&Top[0]<Top[1]&&Bottom[0]<Bottom[1])
{
TicketNumber=OrderSend(Symbol(),OP_SELL,LotSize(),Bid,Slippage,Bid+StopLoss*Point*10,Bid-TakePfofit*Point*10,"Logic1",Magic1,0,Blue);
}
}
else
{
if((NoEntryTime == false || time_filter == true)&&TotalNum<MaxPosition && MarketInfo(Symbol(),MODE_SPREAD)< MaxSpread&&s_ma>c_ma&&Top[0]>Top[1]&&Bottom[0]>Bottom[1])
{
TicketNumber=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point*10,Ask+TakePfofit*Point*10,"Logic1",Magic1,0,Red);
}
if((NoEntryTime == false || time_filter == true)&&TotalNum<MaxPosition && MarketInfo(Symbol(),MODE_SPREAD)< MaxSpread&&s_ma<c_ma&&Top[0]<Top[1]&&Bottom[0]<Bottom[1])
{
TicketNumber=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point*10,Bid-TakePfofit*Point*10,"Logic1",Magic1,0,Blue);
}
}
double Total_Profit=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderMagicNumber()==Magic1 && OrderSymbol()==Symbol())
{
double Or;
if (OrderType()==OP_BUY)
{
Or=OrderLots();
Total_Profit=Total_Profit+((Bid-OrderOpenPrice())*(Or/Lots));
}
if (OrderType()==OP_SELL)
{
Or=OrderLots();
Total_Profit=Total_Profit+((OrderOpenPrice()-Ask)*(Or/Lots));
}
}
}
}
if(AllClose==true)
{
if(Total_Profit>TotalProfit*AdjustPoint(Symbol()) )
{
CloseOrder(TotalNum<MaxPosition);
}
}
//---
}
//+------------------------------------------------------------------+
//ポイント調整
double AdjustPoint(string Currency)
{
int Symbol_Digits=(int)MarketInfo(Currency,MODE_DIGITS);
double Calculated_Point=0;
if (Symbol_Digits==2 || Symbol_Digits==3)
{
Calculated_Point=0.01;
}
else if (Symbol_Digits==4 || Symbol_Digits==5)
{
Calculated_Point=0.0001;
}
else if (Symbol_Digits==1)
{
Calculated_Point=0.1;
}
else if (Symbol_Digits==0)
{
Calculated_Point=1;
}
return(Calculated_Point);
}
//MM
double LotSize()
{
int magnification;//倍率
string currency = AccountCurrency(); //口座の通貨タイプ
if(currency == "JPY")//円口座の場合
{
magnification = 10000;//1万円を対象
}
else//ドル口座の場合
{
magnification = 100;//$100を対象
}
//複利計算
double lotMM = MathCeil(AccountFreeMargin()*Risk /magnification)/100;
lotMM = lotMM - 0.01;
if(lotMM < 0.01) lotMM = 0.01;//最小ロット
if(lotMM > 1.0) lotMM = MathCeil(lotMM);
if(lotMM > 100) lotMM = 100;//最大ロット
return (lotMM);
}
bool EntryTimeCheck(string stime,string etime){
string StartDate=TimeToStr(TimeCurrent(),TIME_DATE); //現在の年月日を取得
datetime StartTime=StrToTime(StartDate+" "+stime); //年月日と時分を連結
datetime EndTime=StrToTime(StartDate+" "+etime); //年月日と時分を連結
if(StartTime<EndTime){
if(StartTime<=TimeCurrent() && TimeCurrent()<EndTime)return(true); //現在時刻が禁止時間内の場合
else return(false);
}else{
if(EndTime<=TimeCurrent() && TimeCurrent()<StartTime)return(false); //現在時刻が禁止時間外の場合
else return(true);
}
return(false);
}
0コメント