-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expert.mq4
211 lines (204 loc) · 6.67 KB
/
Expert.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
//+------------------------------------------------------------------+
//| pamTest.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
enum onoff {
on = 1, //On
off = 0 //Off
};
//--- input parameters
input onoff Buy=on;
input onoff Sell=on;
input double LotBuy=0.1;
input double LotSell=0.1;
input double stoploss=0.0;
input double TPBuy=0.001;
input double TPSell=0.001;
input double StepBuy=0.0005;
input double StepSell=0.0005;
double point = 0;
int buy;
int sell;
int count;
int prev_reason = -1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
if (prev_reason != 3 && prev_reason != 9 && prev_reason != 7) {
int total = OrdersTotal();
bool can_buy = true;
bool can_sell = true;
for (int i = 0; i < total; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) continue;
if (OrderType() == OP_BUY && OrderSymbol() == Symbol()) {
double diff = Ask - OrderOpenPrice();
if (diff <= StepBuy && diff >= (0 - StepBuy)) {
can_buy = false;
}
}
if (OrderType() == OP_SELL && OrderSymbol() == Symbol()) {
double diff = Bid - OrderOpenPrice();
if (diff <= StepSell && diff >= (0 - StepSell)) {
can_sell = false;
}
}
}
buy = Buy;
sell = Sell;
count = 0;
if (buy == 1 && can_buy) {
setOrder("B");
}
if (sell == 1 && can_sell) {
setOrder("S");
}
point = Bid;
}
PutButton("B",10,30,"BUY", buy);
PutButton("S",100,30,"SELL", sell);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
prev_reason = reason;
if (reason != 3 && reason != 9 && reason != 7) {
ObjectDelete("B");
ObjectDelete("S");
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
int total = OrdersTotal();
bool can_buy = true;
bool can_sell = true;
for (int i = 0; i < total; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) continue;
if (OrderType() == OP_BUY && OrderSymbol() == Symbol()) {
double diff = NormalizeDouble(Ask - OrderOpenPrice(), 4);
if (diff <= StepBuy && diff >= (0 - StepBuy)) {
can_buy = false;
}
}
if (OrderType() == OP_SELL && OrderSymbol() == Symbol()) {
double diff = NormalizeDouble(Bid - OrderOpenPrice(), 4);
if (diff <= StepSell && diff >= (0 - StepSell)) {
can_sell = false;
}
}
}
if (buy == 1 && can_buy == true) {
if (point == 0 || NormalizeDouble((Bid - point), 4) >= StepBuy){
setOrder("B");
}
}
if (sell == 1 && can_sell == true) {
if (point == 0 || NormalizeDouble((point - Bid), 4) >= StepSell){
setOrder("S");
}
}
if ((Bid - point) >= StepBuy || (point - Bid) >= StepSell){
point = Bid;
}
}
//+------------------------------------------------------------------+
void PutButton(string name,int x,int y,string text, int type)
{
long chart_id = ChartID();
ObjectCreate(chart_id, name,OBJ_BUTTON,0,0,0);
//--- óñòàíîâèì êîîðäèíàòû êíîïêè
ObjectSetInteger(chart_id, name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_id, name,OBJPROP_YDISTANCE,y);
//--- óñòàíîâèì ðàçìåð êíîïêè
ObjectSetInteger(chart_id, name,OBJPROP_XSIZE,80);
ObjectSetInteger(chart_id, name,OBJPROP_YSIZE,30);
//--- óñòàíîâèì óãîë ãðàôèêà, îòíîñèòåëüíî êîòîðîãî áóäóò îïðåäåëÿòüñÿ êîîðäèíàòû òî÷êè
ObjectSetInteger(chart_id, name,OBJPROP_CORNER,4);
//--- óñòàíîâèì òåêñò
ObjectSetString(chart_id, name,OBJPROP_TEXT,text);
//--- óñòàíîâèì øðèôò òåêñòà
ObjectSetString(chart_id, name,OBJPROP_FONT,"Arial");
//--- óñòàíîâèì ðàçìåð øðèôòà
ObjectSetInteger(chart_id, name,OBJPROP_FONTSIZE,12);
//--- óñòàíîâèì öâåò òåêñòà
ObjectSetInteger(chart_id, name,OBJPROP_COLOR,White);
//--- óñòàíîâèì öâåò ôîíà
if (type == 1)
ObjectSetInteger(chart_id, name,OBJPROP_BGCOLOR,Green);
else if (type == 0)
ObjectSetInteger(chart_id, name,OBJPROP_BGCOLOR,Red);
//--- óñòàíîâèì öâåò ãðàíèöû
ObjectSetInteger(chart_id, name,OBJPROP_BORDER_COLOR,Blue);
}
void setOrder(string type) {
double SLB = Ask - stoploss;
double SLS = Bid + stoploss;
double TPB = Ask + TPBuy;
double TPS = Bid - TPSell;
if (stoploss == 0){
SLB = 0;
SLS = 0;
}
if (TPBuy == 0)
TPB = 0;
if (TPSell == 0)
TPS = 0;
if (type == "B")
int order = OrderSend(Symbol(),OP_BUY,LotBuy, Ask, 50, SLB, TPB,NULL, 1111);
else if (type == "S")
int order = OrderSend(Symbol(),OP_SELL,LotSell, Bid, 50, SLS, TPS,NULL, 1111);
count++;
}
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
long chart_id = ChartID();
//--- ïðîâåðèì ñîáûòèå íà íàæàòèå êíîïêè ìûøêè
if(id==CHARTEVENT_OBJECT_CLICK)
{
string clickedChartObject=sparam;
//--- åñëè íàæàòèå íà îáúåêòå ñ èìåíåì "B"
if(clickedChartObject=="B")
{
if (buy == 1) {
buy = 0;
ObjectSetInteger(chart_id, "B",OBJPROP_BGCOLOR,Red);
}
else if (buy == 0) {
buy = 1;
ObjectSetInteger(chart_id, "B",OBJPROP_BGCOLOR,Green);
}
}
//--- åñëè íàæàòèå íà îáúåêòå ñ èìåíåì "S"
if(clickedChartObject=="S")
{
if (sell == 1) {
sell = 0;
ObjectSetInteger(chart_id, "S",OBJPROP_BGCOLOR,Red);
}
else if (sell == 0) {
sell = 1;
ObjectSetInteger(chart_id, "S",OBJPROP_BGCOLOR,Green);
}
}
ChartRedraw();// ïðèíóäèòåëüíî ïåðåðèñóåì âñå îáúåêòû íà ãðàôèêå
}
}