-
Notifications
You must be signed in to change notification settings - Fork 0
/
moving_average.mq5
142 lines (118 loc) · 10.4 KB
/
moving_average.mq5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//+------------------------------------------------------------------+
//| moving_average.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//| Input varaiables |
//+------------------------------------------------------------------+
input int InpFastPeriod = 20; // fast period
input int InpSlowPeriod = 100; // slow period
input int InpStopLoss = 100; // stop loss in points
input int InpTakeProfit = 200; // Take profit in points
//+------------------------------------------------------------------+
//| Global varaiables |
//+------------------------------------------------------------------+
int fastHandle;
int slowHandle;
double fastBuffer[];
double slowBuffer[];
datetime openTimeBuy=0;
datetime openTimeSell=0;
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit(){
// check user input
if(InpFastPeriod<=0){
Alert("Fast period <= 0");
return INIT_PARAMETERS_INCORRECT;
}
if(InpSlowPeriod<=0){
Alert("slow period <= 0");
return INIT_PARAMETERS_INCORRECT;
}
if(InpSlowPeriod<=InpFastPeriod){
Alert("slow period <= fast period");
return INIT_PARAMETERS_INCORRECT;
}
if(InpStopLoss<=0){
Alert("Stop Loss <= 0");
return INIT_PARAMETERS_INCORRECT;
}
if(InpTakeProfit<=0){
Alert("Take Profit <= 0");
return INIT_PARAMETERS_INCORRECT;
}
// create handles
fastHandle = iMA(_Symbol,PERIOD_CURRENT,InpFastPeriod,0,MODE_SMA,PRICE_CLOSE);
if(fastHandle == INVALID_HANDLE){
Alert("Failed to create fastHandle");
return INIT_FAILED;
}
slowHandle = iMA(_Symbol,PERIOD_CURRENT,InpSlowPeriod,0,MODE_SMA,PRICE_CLOSE);
if(slowHandle == INVALID_HANDLE){
Alert("Failed to create slowHandle");
return INIT_FAILED;
}
ArraySetAsSeries(fastBuffer, true);
ArraySetAsSeries(slowBuffer, true);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
if(fastHandle != INVALID_HANDLE){IndicatorRelease(fastHandle);}
if(slowHandle != INVALID_HANDLE){IndicatorRelease(slowHandle);}
}
void TryToClose(long type){
if(!PositionSelect(_Symbol))return;
if(PositionGetInteger(POSITION_TYPE)!=type)return;
trade.PositionClose(_Symbol);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick(){
// get indicator values
int values = CopyBuffer(fastHandle,0,0,2, fastBuffer);
if(values != 2) {
Print("Not enough data for fast MA");
return;
}
values = CopyBuffer(slowHandle,0,0,2, slowBuffer);
if(values != 2) {
Print("Not enough data for slow MA");
return;
}
// check for cross buy
if(fastBuffer[1] < slowBuffer[1] && fastBuffer[0] > slowBuffer[0] && openTimeBuy != iTime(_Symbol, PERIOD_CURRENT, 0)){
TryToClose(POSITION_TYPE_BUY);
TryToClose(ORDER_TYPE_SELL);
openTimeBuy = iTime(_Symbol, PERIOD_CURRENT, 0);
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double sl = ask - InpStopLoss * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
double tp = ask + InpTakeProfit * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
trade.PositionOpen(_Symbol, ORDER_TYPE_BUY, 1.0, ask, sl,tp, "cross EA buy");
}
// check for cross sell
if(fastBuffer[1] > slowBuffer[1] && fastBuffer[0] < slowBuffer[0] && openTimeSell != iTime(_Symbol, PERIOD_CURRENT, 0)){
TryToClose(POSITION_TYPE_BUY);
TryToClose(ORDER_TYPE_SELL);
openTimeSell = iTime(_Symbol, PERIOD_CURRENT, 0);
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double sl = bid + InpStopLoss * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
double tp = bid - InpTakeProfit * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
trade.PositionOpen(_Symbol, ORDER_TYPE_SELL, 1.0, bid, sl,tp, "cross EA sell");
}
}
//+------------------------------------------------------------------+