forked from ryogrid/fx_systrade
/
MA_touch_EA_MT4.cpp
155 lines (141 loc) · 4.21 KB
/
MA_touch_EA_MT4.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
#define MAGICMA 20150810
#define TRADE_TYPE_BUY 1
#define TRADE_TYPE_SELL 2
#define TRADE_TYPE_NOT_HAVE 3
#define MA_PERIOD 20
#define FALSE 0
#define TRUE 1
int touch_top(double ma_cur, double ma_prev1, double ma_prev2)
{
if (Close[2] < ma_prev1 && Close[1] > ma_prev2 && Close[0] < ma_cur){
return TRUE;
} else {
return FALSE;
}
}
int touch_bottom(double ma_cur, double ma_prev1, double ma_prev2)
{
if (Close[2] > ma_prev1 && Close[1] < ma_prev2 && Close[0] > ma_cur){
return TRUE;
} else {
return FALSE;
}
}
int cross_above(double ma_cur, double ma_prev1)
{
if (Close[1] < ma_prev1 && Close[0] > ma_cur){
return TRUE;
} else {
return FALSE;
}
}
int cross_below(double ma_cur, double ma_prev1)
{
if (Close[1] > ma_prev1 && Close[0] < ma_cur){
return TRUE;
} else {
return FALSE;
}
}
int check_active_order()
{
for(int i = 0;i < OrdersTotal();i++) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){
break;
}
if(OrderMagicNumber() != MAGICMA || OrderSymbol() != Symbol()){
continue;
}
return TRUE;
}
return FALSE;
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma_cur;
double ma_prev1;
double ma_prev2;
//---- go trading only for first tiks of new bar
if(Volume[0] > 1){
return;
}
//---- get Moving Average
ma_cur = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 0);
ma_prev1 = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 1);
ma_prev2 = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 2);
//---- buy conditions
if(touch_bottom(ma_cur, ma_prev1, ma_prev2)) {
OrderSend(Symbol(), OP_SELL, NormalizeDouble(AccountFreeMargin()*0.1/1000.0,1), Bid, 3, 0, 0, "", MAGICMA, 0, Blue);
return;
}
//---- sell conditions
if(touch_top(ma_cur, ma_prev1, ma_prev2)) {
OrderSend(Symbol(), OP_BUY, NormalizeDouble(AccountFreeMargin()*0.1/1000.0,1), Ask, 3, 0, 0, "", MAGICMA, 0, Red);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double ma_cur;
double ma_prev1;
double ma_prev2;
//---- go trading only for first tiks of new bar
if(Volume[0] > 1) {
return;
}
//---- get Moving Average
ma_cur = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 0);
ma_prev1 = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 1);
ma_prev2 = iMA(NULL, 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, 2);
//----
for(int i = 0;i < OrdersTotal();i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){
break;
}
if(OrderMagicNumber() != MAGICMA || OrderSymbol() != Symbol()){
continue;
}
//---- check order type
if(OrderType() == OP_BUY)
{
if(cross_below(ma_cur, ma_prev1) || touch_bottom(ma_cur, ma_prev1, ma_prev2) || OrderProfit() > AccountFreeMargin()*(-0.07)){
OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);
}
break;
}
if(OrderType() == OP_SELL)
{
if(cross_above(ma_cur, ma_prev1) || touch_top(ma_cur, ma_prev1, ma_prev2)|| OrderProfit() > AccountFreeMargin()*(-0.07)){
OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);
}
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if (Bars < 100 || IsTradeAllowed() == false){
return;
}
//---- calculate open orders by current symbol
if (check_active_order() == FALSE){
CheckForOpen();
} else {
CheckForClose();
}
//----
}
//+------------------------------------------------------------------+