-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiss_Pyramid_Advisor.mq4
256 lines (214 loc) · 7.14 KB
/
Swiss_Pyramid_Advisor.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
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
//+------------------------------------------------------------------+
//| Swiss_Pyramid.mq4 |
//| |
//| SCRIPT MONITORS ALL POSITIONS, IF TOTAL LOSS IS BIGGER THAN |
//| LIMIT ALL OPEN AND PENDING ORDERS WILL BE REMOVED |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//--- input parameters
input int LIMIT=50;
#define SYMBOLS_MAX 1024
#define DEALS 0
#define BUY_LOTS 1
#define BUY_PRICE 2
#define SELL_LOTS 3
#define SELL_PRICE 4
#define NET_LOTS 5
#define PROFIT 6
string ExtName="SwissPyramid Advisor";
string ExtSymbols[SYMBOLS_MAX];
int ExtSymbolsTotal=0;
double ExtSymbolsSummaries[SYMBOLS_MAX][7];
int ExtLines=-1;
string ExtCols[8]={"Symbol",
"Deals",
"Buy lots",
"Buy price",
"Sell lots",
"Sell price",
"Net lots",
"Profit"};
int ExtShifts[8]={ 10, 80, 130, 180, 260, 310, 390, 460 };
int ExtVertShift=14;
double ExtMapBuffer[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
void removePyramid(string vSymbol)
{
int total= OrdersTotal();
int ticket;
string curr = vSymbol;
bool result;
StringToUpper( curr ) ;
for(int i=total-1;i>=0;i--){
ticket = OrderSelect(i,SELECT_BY_POS);
int type=OrderType();
string symbol=OrderSymbol();
StringToUpper(symbol);
if( StringLen(curr) > 0 && curr != symbol)
continue;
RefreshRates();
switch(OrderType()){
//Close opened long positions
case OP_BUY :
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
Print("Closing: ",OrderTicket());
break;
//Close opened short positions
case OP_SELL :
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
Print("Closing: ", OrderTicket());
break;
default :
result = OrderDelete(OrderTicket());
Print("Deleting: ", OrderTicket());
break;
}
if( result == false)
Print("Last error code: ");
else
Sleep(120);
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
int SymbolsIndex(string SymbolName)
{
bool found=false;
int i;
//----
for(i=0; i<ExtSymbolsTotal; i++)
{
if(SymbolName==ExtSymbols[i])
{
found=true;
break;
}
}
//----
if(found)
return(i);
if(ExtSymbolsTotal>=SYMBOLS_MAX)
return(-1);
//----
i=ExtSymbolsTotal;
ExtSymbolsTotal++;
ExtSymbols[i]=SymbolName;
ExtSymbolsSummaries[i][DEALS]=0;
ExtSymbolsSummaries[i][BUY_LOTS]=0;
ExtSymbolsSummaries[i][BUY_PRICE]=0;
ExtSymbolsSummaries[i][SELL_LOTS]=0;
ExtSymbolsSummaries[i][SELL_PRICE]=0;
ExtSymbolsSummaries[i][NET_LOTS]=0;
ExtSymbolsSummaries[i][PROFIT]=0;
//----
return(i);
}
void OnTick()
{
//---
double profit;
int i,index,type,total=OrdersTotal();
//----
for(i=0; i<total; i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) continue;
type=OrderType();
if(type!=OP_BUY && type!=OP_SELL) continue;
index=SymbolsIndex(OrderSymbol());
if(index<0 || index>=SYMBOLS_MAX) continue;
//----
ExtSymbolsSummaries[index][DEALS]=0;
profit=0;
ExtSymbolsSummaries[index][PROFIT]=profit;
if(type==OP_BUY)
{
ExtSymbolsSummaries[index][BUY_LOTS]=0;
ExtSymbolsSummaries[index][BUY_PRICE]=0;
}
else
{
ExtSymbolsSummaries[index][SELL_LOTS]=0;
ExtSymbolsSummaries[index][SELL_PRICE]=0;
}
}
for(i=0; i<total; i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) continue;
type=OrderType();
if(type!=OP_BUY && type!=OP_SELL) continue;
index=SymbolsIndex(OrderSymbol());
if(index<0 || index>=SYMBOLS_MAX) continue;
//----
ExtSymbolsSummaries[index][DEALS]++;
profit=OrderProfit()+OrderCommission()+OrderSwap();
ExtSymbolsSummaries[index][PROFIT]+=profit;
if(type==OP_BUY)
{
ExtSymbolsSummaries[index][BUY_LOTS]+=OrderLots();
ExtSymbolsSummaries[index][BUY_PRICE]+=OrderOpenPrice()*OrderLots();
}
else
{
ExtSymbolsSummaries[index][SELL_LOTS]+=OrderLots();
ExtSymbolsSummaries[index][SELL_PRICE]+=OrderOpenPrice()*OrderLots();
}
}
total=OrdersTotal();
for(i=0; i<total; i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) continue;
type=OrderType();
if(type!=OP_BUY && type!=OP_SELL) continue;
index=SymbolsIndex(OrderSymbol());
if(index<0 || index>=SYMBOLS_MAX) continue;
//----
if( ExtSymbolsSummaries[index][PROFIT] < (LIMIT * -1) ){
removePyramid(ExtSymbols[index]);
// MessageBox("zamykamy" , "Ssss"+string(ExtSymbolsSummaries[index][PROFIT])+ExtSymbols[index], 0 );
}else{
// MessageBox("moze byc" , "Ssss"+string(ExtSymbolsSummaries[index][PROFIT]), 0 );
}
}
//----
Sleep(2000);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| Tester function |
//+------------------------------------------------------------------+
double OnTester()
{
//---
double ret=0.0;
//---
//---
return(ret);
}
//+------------------------------------------------------------------+