-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bridge.mq4
179 lines (139 loc) · 4.78 KB
/
Bridge.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
//+------------------------------------------------------------------+
//| Bridge.mq4 |
//| Copyright © 2012, w3net |
//| http://www.w3net.pl |
//+------------------------------------------------------------------+
#include <helpers.mqh>
#include <server.mqh>
#include <trade.mqh>
#property copyright "Copyright © 2012, Marek Mikuliszyn"
extern double risk = 0.01;
extern int tp_sl_margin = 500;
string signal = "";
double pnt, dig, spr, cash;
int init()
{
pnt = MarketInfo(Symbol(), MODE_POINT);
dig = MarketInfo(Symbol(), MODE_DIGITS);
spr = MarketInfo(Symbol(), MODE_SPREAD);
if (cash == 0) cash = AccountBalance();
return(0);
}
int deinit()
{
return(0);
}
int start()
{
spr = MarketInfo(Symbol(), MODE_SPREAD);
everyTick();
if (Volume[0] == 1) everyBar();
return(0);
}
void everyTick()
{
int handle = FileOpen(Symbol() + ".csv", FILE_CSV|FILE_READ, ';');
if (handle > -1)
{
while (FileIsEnding(handle) == false)
{
signal = FileReadString(handle);
processSignal();
if (FileIsEnding(handle) == true)
break;
}
FileClose(handle);
FileDelete(Symbol() + ".csv");
}
manageTrades();
}
void everyBar()
{
signal = "";
// refresh cash
if (Hour() == 0 && Minute() == 0 && AccountBalance() > cash) cash = AccountBalance();
}
// --- Trading
void processSignal()
{
string csv[], comment;
int dir, ticket;
double entry, tp, sl, size;
if (signal != "") Split(signal, ",", csv);
if (csv[0] == "BUY") dir = OP_BUY;
else dir = OP_SELL;
size = StrToDouble(csv[1]);
entry = StrToDouble(csv[2]);
tp = StrToDouble(csv[3]);
sl = StrToDouble(csv[4]);
comment = csv[5];
if (csv[0] == "CLOSE") {
signal = "";
return;
}
// tylko raz jeden sygnal
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderComment() == comment)
{
Print("Cancelled signal");
signal = "";
return;
}
}
}
if (dir == OP_BUY) ticket = openTrade(Symbol(), dir, getLot(Symbol(), risk, size), sl - tp_sl_margin * pnt, tp + tp_sl_margin * pnt, comment);
if (dir == OP_SELL) ticket = openTrade(Symbol(), dir, getLot(Symbol(), risk, size), sl + tp_sl_margin * pnt, tp - tp_sl_margin * pnt, comment);
Print("Trade opened");
signal = "";
}
void manageTrades()
{
bool result;
int cmd, total, error;
double profit;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
cmd = OrderType();
if (cmd != OP_BUY && cmd != OP_SELL) continue;
//if (OrderTakeProfit() == 0.0 || OrderStopLoss() == 0.0) continue;
if (OrderComment() != Symbol() + "_" + getPeriodName(Period())) continue;
// zamykaj zlecenie ktory dotarly do poziomu SOFT TP/SL
if ((cmd == OP_BUY && (Bid >= OrderTakeProfit() - tp_sl_margin * pnt || Bid < OrderStopLoss() + tp_sl_margin * pnt)) ||
(cmd == OP_SELL && (Ask <= OrderTakeProfit() + tp_sl_margin * pnt || Bid > OrderStopLoss() - tp_sl_margin * pnt)) ||
(Time[0] > OrderOpenTime() + (50 * Period() * 60) && OrderProfit() > 0)) {
if (cmd == OP_BUY) profit = closeTrade(OrderTicket(), Bid);
if (cmd == OP_SELL) profit = closeTrade(OrderTicket(), Ask);
// update
if (profit >= 0.0) {
Print("Soft TP. +$" + DoubleToStr(profit, 2));
//SendMail("TP (" + getBroker() + ")", "+$" + DoubleToStr(profit, 2));
}
else {
Print("Soft SL. -$" + DoubleToStr(MathAbs(profit), 2));
//SendMail("SL (" + getBroker() + ")", "-$" + DoubleToStr(MathAbs(profit), 2));
}
}
}
else {
Print("Error when order select ", GetLastError());
break;
}
}
}
double getLot(string pair, double risk, double size)
{
double amnt = cash * risk;
double dig = MarketInfo(pair, MODE_DIGITS);
if (dig >= 4) size *= 100000;
else if (dig >= 2) size *= 1000;
double lot = amnt / (size * MarketInfo(pair, MODE_TICKVALUE));
if (lot > maxLot) return (maxLot);
if (lot < minLot) return (minLot);
if (MarketInfo(Symbol(), MODE_LOTSTEP) == 0.01)
return (StrToDouble(DoubleToStr(lot, 2)));
if (MarketInfo(Symbol(), MODE_LOTSTEP) == 0.1)
return (StrToDouble(DoubleToStr(lot, 1)));
if (MarketInfo(Symbol(), MODE_LOTSTEP) == 1)
return (StrToDouble(DoubleToStr(lot, 0)));
}