-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple_copier.mq4
239 lines (213 loc) · 16.9 KB
/
simple_copier.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
//+------------------------------------------------------------------+
//| Simple Copier |
//| Copyright 2015, Vladimir V. Tkach |
//+------------------------------------------------------------------+
#property version "1.2"
#property copyright "Copyright © 2015, Vladimir V. Tkach"
#property description "Trades copying utility with multiplyer."
#property strict
//+------------------------------------------------------------------+
//| Enumerator of working mode |
//+------------------------------------------------------------------+
enum copier_mode
{
master,
slave,
};
input copier_mode mode=0; // Working mode
input int slip=10; // Slippage (in pips)
input double mult=1.0; // Multiplyer (for slave)
int
opened_list[500],
ticket,
type,
filehandle;
string
symbol;
double
lot,
price,
sl,
tp;
//+------------------------------------------------------------------+
//|Initialisation function |
//+------------------------------------------------------------------+
void init()
{
ObjectsDeleteAll();
EventSetTimer(1);
return;
}
//+------------------------------------------------------------------+
//|Deinitialisation function |
//+------------------------------------------------------------------+
void deinit()
{
ObjectsDeleteAll();
EventKillTimer();
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTimer()
{
if(!ObjectFind("mode") || ObjectDescription("mode")!=EnumToString(mode))
{
ObjectDelete("mode");
ObjectCreate("mode",OBJ_LABEL,0,0,0);
ObjectSet("mode",OBJPROP_XDISTANCE,10);
ObjectSet("mode",OBJPROP_YDISTANCE,10);
ObjectSet("mode",OBJPROP_COLOR,Red);
ObjectSetText("mode",EnumToString(mode));
ObjectSet("mode",OBJPROP_FONTSIZE,10);
}
//--- Master working mode
if(EnumToString(mode)=="master")
{
//--- Saving information about opened deals
if(OrdersTotal()==0)
{
filehandle=FileOpen("C4F.csv",FILE_WRITE|FILE_CSV|FILE_COMMON);
FileWrite(filehandle,"");
FileClose(filehandle);
}
else
{
filehandle=FileOpen("C4F.csv",FILE_WRITE|FILE_CSV|FILE_COMMON);
if(filehandle!=INVALID_HANDLE)
{
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) break;
symbol=OrderSymbol();
if(StringSubstr(OrderComment(),0,3)!="C4F") FileWrite(filehandle,OrderTicket(),symbol,OrderType(),OrderOpenPrice(),OrderLots(),OrderStopLoss(),OrderTakeProfit());
FileFlush(filehandle);
}
FileClose(filehandle);
}
}
}
//--- Slave working mode
if(EnumToString(mode)=="slave")
{
//--- Checking for the new deals and stop loss/take profit changes
filehandle=FileOpen("C4F.csv",FILE_READ|FILE_CSV|FILE_COMMON);
if(filehandle!=INVALID_HANDLE)
{
int o=0;
opened_list[o]=0;
while(!FileIsEnding(filehandle))
{
ticket=StrToInteger(FileReadString(filehandle));
symbol=FileReadString(filehandle);
type=StrToInteger(FileReadString(filehandle));
price=StrToDouble(FileReadString(filehandle));
lot=StrToDouble(FileReadString(filehandle))*mult;
sl=StrToDouble(FileReadString(filehandle));
tp=StrToDouble(FileReadString(filehandle));
string
OrdComm="C4F"+IntegerToString(ticket);
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) continue;
if(OrderComment()!=OrdComm) continue;
opened_list[o]=ticket;
opened_list[o+1]=0;
o++;
if(OrderType()>1 && OrderOpenPrice()!=price)
{
if(!OrderModify(OrderTicket(),price,0,0,0))
Print("Error: ",GetLastError()," during modification of the order.");
}
if(tp!=OrderTakeProfit() || sl!=OrderStopLoss())
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0))
Print("Error: ",GetLastError()," during modification of the order.");
}
break;
}
//--- If deal was not opened yet on slave-account, open it.
if(InList(ticket)==-1 && ticket!=0)
{
FileClose(filehandle);
if(type<2) OpenMarketOrder(ticket,symbol,type,price,lot);
if(type>1) OpenPendingOrder(ticket,symbol,type,price,lot);
return;
}
}
FileClose(filehandle);
}
else return;
//--- If deal was closed on master-account, close it on slave-accont
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) continue;
if(StringSubstr(OrderComment(),0,3)!="C4F") continue;
if(InList(StrToInteger(StringSubstr(OrderComment(),StringLen("C4F"),0)))==-1)
{
if(OrderType()==0)
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slip))
Print("Error: ",GetLastError()," during closing the order.");
}
else if(OrderType()==1)
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slip))
Print("Error: ",GetLastError()," during closing the order.");
}
else if(OrderType()>1)
{
if(!OrderDelete(OrderTicket()))
Print("Error: ",GetLastError()," during deleting the pending order.");
}
}
}
}
}
//+------------------------------------------------------------------+
//|Checking list |
//+------------------------------------------------------------------+
int InList(int ticket_)
{
int h=0;
while(opened_list[h]!=0)
{
if(opened_list[h]==ticket_) return(1);
h++;
}
return(-1);
}
//+------------------------------------------------------------------+
//|Open market execution orders |
//+------------------------------------------------------------------+
void OpenMarketOrder(int ticket_,string symbol_,int type_,double price_,double lot_)
{
double market_price=MarketInfo(symbol_,MODE_BID);
if(type_==0) market_price=MarketInfo(symbol_,MODE_ASK);
double delta;
delta=MathAbs(market_price-price_)/MarketInfo(symbol_,MODE_POINT);
if(delta>slip) return;
if(!OrderSend(symbol_,type_,LotNormalize(lot_),market_price,slip,0,0,"C4F"+IntegerToString(ticket_))) Print("Error: ",GetLastError()," during opening the market order.");
return;
}
//+------------------------------------------------------------------+
//|Open pending orders |
//+------------------------------------------------------------------+
void OpenPendingOrder(int ticket_,string symbol_,int type_,double price_,double lot_)
{
if(!OrderSend(symbol_,type_,LotNormalize(lot_),price_,slip,0,0,"C4F"+IntegerToString(ticket_))) Print("Error: ",GetLastError()," during setting the pending order.");
return;
}
//+------------------------------------------------------------------+
//|Normalize lot size |
//+------------------------------------------------------------------+
double LotNormalize(double lot_)
{
double minlot=MarketInfo(symbol,MODE_MINLOT);
if(minlot==0.001) return(NormalizeDouble(lot_,3));
else if(minlot==0.01) return(NormalizeDouble(lot_,2));
else if(minlot==0.1) return(NormalizeDouble(lot_,1));
return(NormalizeDouble(lot_,0));
}
//+------------------------------------------------------------------+