-
Notifications
You must be signed in to change notification settings - Fork 1
/
3EMA+stoch.mq4
181 lines (159 loc) · 11.3 KB
/
3EMA+stoch.mq4
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Advisor parameters
extern int stoploss = 75;
extern int takeprofit = 225;
extern double lot = 0.1; // lot size
extern int magic_num = 68405; // identifying (magic) number of order
// Parameters of fast exponential moving average
extern int MA1_period = 3;
extern int MA1_shift = 0;
// Parameters of middle exponential moving average
extern int MA2_period = 18;
extern int MA2_shift = 0;
// Parameters of slow exponential moving average
extern int MA3_period = 75;
extern int MA3_shift = 0;
// If = true, then the deal is closed when the dotted line of Stochastic (% D) crossed the price level.
// To close a buy trade, the dotted line must cross the 60 level top down. Sell - level 40 upwards.
extern bool close_at_stoch = true;
// Parameters of stochastic oscillator
extern int stoch_param1 = 12; // Period of the %K line
extern int stoch_param2 = 9; // Period of the %D line
extern int stoch_param3 = 5; // Slowing value
// Risk management
extern double max_risk = 0.03;
//--------------------------------------------------------------------
// Check if new bar(candle) is opened
bool isNewBar()
{
static datetime BarTime;
bool res=false;
if (BarTime != Time[0])
{
BarTime = Time[0];
res = true;
}
return(res);
}
//--------------------------------------------------------------------
// Return number of orders by certain type
int OrdersTotalByType(int type, int mn, string sym)
{
int num_orders = 0;
for(int i=OrdersTotal()-1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == mn && type == OrderType() && sym==OrderSymbol())
num_orders++;
}
return(num_orders);
}
//--------------------------------------------------------------------
// Optimize lot size
double LotsOptimized()
{
double lot_size = lot;
lot_size = NormalizeDouble(AccountFreeMargin()*max_risk/stoploss/lot*0.01, 1);
Alert(lot_size);
if(lot_size < 0.1)
lot_size=0.1;
return(lot_size);
}
//--------------------------------------------------------------------
bool open_order;
int buy_cond = 0;
int sell_cond = 0;
int order_id;
int start()
{
if(!isNewBar())return(0);
int total = OrdersTotal(); // Total amount of market and pending orders
open_order = false;
for(int pos=0; pos < total; pos++)
{
if(OrderSelect(pos, SELECT_BY_POS)==true)
{
if (( OrderSymbol() == Symbol())&& (OrderMagicNumber() == magic_num ))
{
open_order = true;
order_id = OrderTicket(); // Ticket number of the currently selected order.
}
}
}
// Create moving averages
double MA1_next2 = iMA(NULL, 0, MA1_period, MA1_shift, MODE_EMA, PRICE_CLOSE, 8);
double MA3_next2 = iMA(NULL, 0, MA3_period, MA3_shift, MODE_EMA, PRICE_CLOSE, 8);
double MA2_next = iMA(NULL, 0, MA2_period, MA2_shift, MODE_EMA, PRICE_CLOSE, 4);
double MA3_next = iMA(NULL, 0, MA3_period, MA3_shift, MODE_EMA, PRICE_CLOSE, 4);
double MA1_current = iMA(NULL, 0, MA1_period, MA1_shift, MODE_EMA, PRICE_CLOSE, 0);
double MA2_current = iMA(NULL, 0, MA2_period, MA2_shift, MODE_EMA, PRICE_CLOSE, 0);
double MA3_current = iMA(NULL, 0, MA3_period, MA3_shift, MODE_EMA, PRICE_CLOSE, 0);
if (buy_cond == 0)
{
if ((MA1_next2 < MA3_next2)&&(MA1_current > MA3_current))
{
Print("МА1 crossed МА3 top down.");
buy_cond = 1;
sell_cond = 0;
}
}
if (sell_cond == 0)
{
if ((MA1_next2 > MA3_next2)&&(MA1_current < MA3_current))
{
Print("МА1 crossed МА3 upwards.");
sell_cond = 1;
buy_cond = 0;
}
}
if ((buy_cond == 1) && (open_order == false))
{
if ((MA2_next < MA3_next)&&(MA2_current > MA3_current))
{
Print("МА2 crossed МА3 top down, open buy order.");
buy_cond = 2;
sell_cond = 0;
if(OrderSend(Symbol(), OP_BUY,LotsOptimized(), Ask, 30, Ask-stoploss*k*Point, Ask+takeprofit*k*Point, "", magic_num, 0, Blue) < 0)
Alert("Opening position error № ", GetLastError());
}
}
if ((sell_cond == 1) && (open_order == false))
{
if ((MA2_next > MA3_next)&&(MA2_current < MA3_current))
{
Print("МА2 crossed МА3 upwards, open sell order.");
sell_cond = 2;
buy_cond = 0;
if(OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, 30, Bid+stoploss*k*Point, Bid-takeprofit*k*Point, "", magic_num, 0, Red) < 0)
Alert("Opening position error № ", GetLastError());
}
}
// Create stochastic oscillator
double stoch = iStochastic(NULL, 0, stoch_param1, stoch_param2, stoch_param3, MODE_SMA, 0, MODE_SIGNAL, 1);
if ((open_order == true)&&(buy_cond == 2)&&(close_at_stoch == true))
{
if((stoch >= 60)&&(stoch < 60))
{
OrderClose(order_id, OrderLots(), Bid, 30, Green);
buy_cond = 0;
sell_cond = 0;
}
}
if ((open_order == true)&&(sell_cond == 2)&&(close_at_stoch == true))
{
if((stoch <= 40)&&(stoch > 40))
{
OrderClose(order_id, OrderLots(), Ask, 30, Green);
buy_cond = 0;
sell_cond = 0;
}
}
if ((open_order == false)&&(close_at_stoch == false)&&((sell_cond == 2)||(buy_cond == 2)))
{
buy_cond = 0;
sell_cond = 0;
}
if(OrdersTotalByType(OP_BUY, magic_num, Symbol()) > 0)
if(OrdersTotalByType(OP_SELL, magic_num, Symbol()) > 0)
return(0);
}
//+------------------------------------------------------------------+