-
Notifications
You must be signed in to change notification settings - Fork 2
/
monster_9 EA.mq5
297 lines (242 loc) · 10.6 KB
/
monster_9 EA.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//+------------------------------------------------------------------+
//| My_First_EA.mq5 |
//| Copyright 2010, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
//--- input parameters
input int StopLoss=0; // Stop Loss
input int TakeProfit=30; // Take Profit
input int ADX_Period=8; // ADX Period
input int MA_Period=8; // Moving Average Period
input int EA_Magic=12345; // EA Magic Number
// input double Adx_Min=23.0; // Minimum ADX Value
//--- Other parameters
int adxHandle; // handle for our ADX indicator
int maHandle; // handle for our Moving Average indicator
double plsDI[],minDI[],maVal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each bars
int STP, TKP; // To be used for Stop Loss & Take Profit values
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Get handle for ADX indicator
adxHandle=iADX(NULL,0,ADX_Period);
//--- Get the handle for Moving Average indicator
maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
//--- What if handle returns Invalid Handle
if(adxHandle<0 || maHandle<0)
{
Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
return(-1);
}
//--- Let us handle currency pairs with 5 or 3 digit prices instead of 4
STP = StopLoss;
TKP = TakeProfit;
if(_Digits==5 || _Digits==3)
{
STP = STP*10;
TKP = TKP*10;
}
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- Release our indicator handles
IndicatorRelease(adxHandle);
IndicatorRelease(maHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
CTrade m_trade;
int Oxygen = 0;
int base = 20;
int inhale = base+7; // profit intake period - increases profit potential by waiting for price to increase even more
int exhale = base+49; // -profit intake period - equity neutralize period - REDUCES profit to reduce equity reduce breath
int scrape = base+99; //Scraper does same job as position correction(open space for new potential positions)
// kept high to increase overall profit pontential
// main target is to force market movement and not subsitute target profit
// is a form of inhale
double profit = 31;
double acc_red = -700; // danger zone indicator - to start reversing margin on account.
void OnTick()
{
//--- Do we have enough bars to work with
if(Bars(_Symbol,_Period)<20) // if total bars is less than 60 bars
{
Print("We have less than 20 bars, EA will now exit!!");
return;
}
// We will use the static Old_Time variable to serve the bar time.
// At each OnTick execution we will check the current bar time with the saved one.
// If the bar time isn't equal to the saved time, it indicates that we have a new tick.
static datetime Old_Time;
datetime New_Time[1];
bool IsNewBar=false;
// copying the last bar time to the element New_Time[0]
int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
if(copied>0) // ok, the data has been copied successfully
{
if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
{
IsNewBar=true; // if it isn't a first call, the new bar has appeared
if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
Old_Time=New_Time[0]; // saving bar time
}
}
else
{
Alert("Error in copying historical times data, error =",GetLastError());
ResetLastError();
return;
}
//--- EA should only check for new trade if we have a new bar
if(IsNewBar==false)
{
return;
}
//--- Do we have enough bars to work with
int Mybars=Bars(_Symbol,_Period);
if(Mybars<30) // if total bars is less than 30 bars
{
Alert("We have less than 30 bars, EA will now exit!!");
return;
}
//--- Define some MQL5 Structures we will use for our trade
MqlTick latest_price; // To be used for getting recent/latest price quotes
// the ADX DI+values array
ArraySetAsSeries(plsDI,true);
// the ADX DI-values array
ArraySetAsSeries(minDI,true);
// the MA-8 values arrays
ArraySetAsSeries(maVal,true);
//--- Get the last price quote using the MQL5 MqlTick Structure
if(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
return;
}
//--- Copy the new values of our indicators to buffers (arrays) using the handle
if(CopyBuffer(adxHandle,1,0,3,plsDI)<0 || CopyBuffer(adxHandle,2,0,3,minDI)<0)
{
Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
ResetLastError();
return;
}
if(CopyBuffer(maHandle,0,0,3,maVal)<0)
{
Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
ResetLastError();
return;
}
for(int a=0;a<PositionsTotal();a++){
PositionSelectByTicket(PositionGetTicket(a));
if(AccountInfoDouble(ACCOUNT_PROFIT)>1000 && (Oxygen%inhale==0)){
Print(PositionGetTicket(a)," Profit From Position: ",(PositionGetDouble(POSITION_PROFIT)));
m_trade.PositionClose(PositionGetTicket(a)); // close trade of position if profit greater than 11 dollars
}else if(PositionGetDouble(POSITION_PROFIT)>=profit && (Oxygen%inhale==0)){
Print(PositionGetTicket(a)," Profit From Position: ",(PositionGetDouble(POSITION_PROFIT)));
m_trade.PositionClose(PositionGetTicket(a)); // close trade of position if profit greater than 11 dollars
pump_oxygen();
}else if((PositionGetDouble(POSITION_PROFIT))<-profit && (Oxygen%exhale==0)){
if(AccountInfoDouble(ACCOUNT_PROFIT)<acc_red){
Print(PositionGetTicket(a)," Profit From Position: ",(PositionGetDouble(POSITION_PROFIT)));
m_trade.PositionClose(PositionGetTicket(a));
pump_oxygen();
}
}else if(PositionGetDouble(POSITION_PROFIT)>11 && (Oxygen%scrape==0)){
//(open space for new potential positions)
//Scraper takes profit below normal target to accomodate stagnant market
Print(PositionGetTicket(a)," Profit From Position: ",(PositionGetDouble(POSITION_PROFIT)));
m_trade.PositionClose(PositionGetTicket(a));
pump_oxygen();
}else if((PositionGetDouble(POSITION_PROFIT)<=-7 && PositionGetDouble(POSITION_PROFIT)>=-15) && (Oxygen%exhale==0)){
//Position correction - take loss before it gets out of hand
Print(PositionGetTicket(a)," Profit From Position: ",(PositionGetDouble(POSITION_PROFIT)));
m_trade.PositionClose(PositionGetTicket(a));
pump_oxygen();
}
}
pump_oxygen(); // tick related movement
double LLot = 0.1;
// dont place more than 2 orders at once for accont balance less than 100 dollars - prevent account from reaching 0 equity
if(AccountInfoDouble(ACCOUNT_BALANCE)<=100){
if(PositionsTotal()>=2){
return;
}else{
LLot=0.01;
}
}else if(AccountInfoDouble(ACCOUNT_BALANCE)>10000){
if(PositionsTotal()>=21){
return;
}else{
LLot=0.7;
}
}else if(AccountInfoDouble(ACCOUNT_BALANCE)>5000){
if(PositionsTotal()>=41){
return;
}else{
LLot=0.3;
}
}
if((maVal[0]>maVal[1]) && (maVal[1]>maVal[2])) // MA-8 Increasing upwards
if((plsDI[0]>minDI[0])) // +DI greater than -DI
if (!sym_max(_Symbol))
if(m_trade.Buy(LLot,_Symbol,
latest_price.ask,
NormalizeDouble(latest_price.ask - STP*_Point,_Digits),
NormalizeDouble(latest_price.ask + TKP*_Point,_Digits),
NULL)) //Request is completed or order placed
{
Print("A Buy order has been successfully placed !!");
}
else
{
Print("The Buy order request could not be completed - error:",GetLastError());
ResetLastError();
return;
}
if((maVal[0]<maVal[1]) && (maVal[1]<maVal[2]))//MA-8 decreasing downwards
if((plsDI[0]<minDI[0])) // -DI greater than +DI
if(!sym_max(_Symbol))
if(m_trade.Sell(LLot,_Symbol,
NormalizeDouble(latest_price.bid,_Digits),
NormalizeDouble(latest_price.bid + STP*_Point,_Digits),
NormalizeDouble(latest_price.bid - TKP*_Point,_Digits),
NULL)) //Request is completed or order placed
{
Print("A Sell order has been successfully placed !!");
}
else
{
Print("The Sell order request could not be completed - error:",GetLastError());
ResetLastError();
return;
}
return;
}
void pump_oxygen(){
if(Oxygen>=1000){
Oxygen=0;
}else{
Oxygen++;
}
}
bool sym_max(string sym){
int aa = 0;
for(int a=(PositionsTotal()-1);a>-1;a--){
if(PositionGetSymbol(a)==sym){
aa++;
}
}
return aa>=11;
}
//+-------------------