-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximum-trade-volume.mq5
317 lines (252 loc) · 11.3 KB
/
maximum-trade-volume.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//+------------------------------------------------------------------+
//| Maximum Trade Volume |
//| Copyright 2024, phade |
//| https://www.fxcalculator.io|
//+------------------------------------------------------------------+
#include <Controls\Dialog.mqh>
#include <Controls\Label.mqh> // Include the Label class
#define INDENT_LEFT (11)
#define INDENT_TOP (11)
#define INDENT_RIGHT (11)
#define INDENT_BOTTOM (11)
#define CONTROLS_GAP_X (5)
#define CONTROLS_GAP_Y (5)
#define BUTTON_WIDTH (100)
#define BUTTON_HEIGHT (20)
#define EDIT_HEIGHT (20)
#define GROUP_WIDTH (150)
#define LIST_HEIGHT (179)
#define RADIO_HEIGHT (56)
#define CHECK_HEIGHT (93)
//+------------------------------------------------------------------+
//| Class CControlsDialog |
//+------------------------------------------------------------------+
class CControlsDialog : public CAppDialog
{
public:
CControlsDialog(void);
~CControlsDialog(void);
//--- create
virtual bool Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
protected:
//--- create dependent controls
bool CreatePanel(void);
private:
CLabel label_text; // Label for the main title
CLabel label_sell; // Label for "max lot for sell"
CLabel label_buy; // Label for "max lot for buy"
CLabel label_pending_buy; // Label for "max lot for pending buy"
CLabel label_pending_sell;// Label for "max lot for pending sell"
};
//+------------------------------------------------------------------+
//| Global Variables |
//+------------------------------------------------------------------+
CControlsDialog ExtDialog;
CPanel my_white_border; // object CPanel
bool pause = true; // true - pause
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CControlsDialog::CControlsDialog(void)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CControlsDialog::~CControlsDialog(void)
{
}
//+------------------------------------------------------------------+
//| Create |
//+------------------------------------------------------------------+
bool CControlsDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
{
if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))
return(false);
//--- create dependent controls
if(!CreatePanel())
return(false);
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Create the "CPanel" |
//+------------------------------------------------------------------+
bool CControlsDialog::CreatePanel(void)
{
//--- coordinates
int x1 = 5;
int y1 = 5;
int x2 = 230;
int y2 = 40;
//--- create panel
if(!my_white_border.Create(0, ExtDialog.Name() + "MyWhiteBorder", m_subwin, x1, y1, x2, y2))
return(false);
if(!my_white_border.ColorBackground(CONTROLS_DIALOG_COLOR_BG))
return(false);
if(!my_white_border.ColorBorder(CONTROLS_DIALOG_COLOR_BORDER_DARK))
return(false);
if(!ExtDialog.Add(my_white_border))
return(false);
my_white_border.Alignment(WND_ALIGN_CLIENT, 0, 0, 0, 0);
//--- create the main label
int label_x1 = INDENT_LEFT;
int label_y1 = y1 + CONTROLS_GAP_Y - 3; // Positioning label below the panel
int label_x2 = label_x1 + 200; // Label width
int label_y2 = label_y1 + 20; // Label height
if(!label_text.Create(0, "LabelText", m_subwin, label_x1, label_y1, label_x2, label_y2))
return(false);
label_text.Text("Max lot on " + _Symbol); // Set text for the label
label_text.Color(clrBlack); // Set text color
label_text.FontSize(18); // Set font size
if(!ExtDialog.Add(label_text))
return(false);
//--- create additional labels for "max lot for sell", "max lot for buy", etc.
int text_y_gap = 25; // Vertical gap between labels
// "max lot for sell"
int sell_y1 = label_y2 + CONTROLS_GAP_Y + text_y_gap;
int sell_y2 = sell_y1 + 20;
if(!label_sell.Create(0, "SellLabel", m_subwin, label_x1, sell_y1, label_x2, sell_y2))
return(false);
label_sell.Text("Max lot for sell: " + DoubleToString(LotCheckSell(), 2));
label_sell.Color(clrBlack);
label_sell.FontSize(12);
if(!ExtDialog.Add(label_sell))
return(false);
// "max lot for buy"
int buy_y1 = sell_y2 + CONTROLS_GAP_Y;
int buy_y2 = buy_y1 + 20;
if(!label_buy.Create(0, "BuyLabel", m_subwin, label_x1, buy_y1, label_x2, buy_y2))
return(false);
label_buy.Text("Max lot for buy: " + DoubleToString(LotCheckBuy(), 2));
label_buy.Color(clrBlack);
label_buy.FontSize(12);
if(!ExtDialog.Add(label_buy))
return(false);
// "max lot for pending buy"
int pending_buy_y1 = buy_y2 + CONTROLS_GAP_Y;
int pending_buy_y2 = pending_buy_y1 + 20;
if(!label_pending_buy.Create(0, "PendingBuyLabel", m_subwin, label_x1, pending_buy_y1, label_x2, pending_buy_y2))
return(false);
label_pending_buy.Text("Max lot for pending buy: " + DoubleToString(LotCheckPendingBuy(), 2));
label_pending_buy.Color(clrBlack);
label_pending_buy.FontSize(12);
if(!ExtDialog.Add(label_pending_buy))
return(false);
// "max lot for pending sell"
int pending_sell_y1 = pending_buy_y2 + CONTROLS_GAP_Y;
int pending_sell_y2 = pending_sell_y1 + 20;
if(!label_pending_sell.Create(0, "PendingSellLabel", m_subwin, label_x1, pending_sell_y1, label_x2, pending_sell_y2))
return(false);
label_pending_sell.Text("Max lot for pending sell: " + DoubleToString(LotCheckPendingSell(), 2));
label_pending_sell.Color(clrBlack);
label_pending_sell.FontSize(12);
if(!ExtDialog.Add(label_pending_sell))
return(false);
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
EventSetTimer(3);
pause = true;
if(!ExtDialog.Create(0, "Max lot checker", 0, 40, 40, 340, 250))
Print("Could not create the dialog");
ExtDialog.Run();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
}
//+------------------------------------------------------------------+
//| Handle Chart Events |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
ExtDialog.ChartEvent(id, lparam, dparam, sparam);
}
//+------------------------------------------------------------------+
//| Calculate Lot Size |
//+------------------------------------------------------------------+
double LotCheckBuy()
{
double margin_for_one_lot;
static double lotSize;
double currentMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
if(!OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, 1.0, SymbolInfoDouble(_Symbol, SYMBOL_ASK), margin_for_one_lot))
Print("Could not obtain the margin required to open 1 lot");
lotSize = currentMargin / margin_for_one_lot;
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lotSize = MathMax(minLot, MathFloor(lotSize / lotStep) * lotStep);
return lotSize;
}
double LotCheckSell()
{
double margin_for_one_lot;
static double lotSize;
double currentMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
if(!OrderCalcMargin(ORDER_TYPE_SELL, _Symbol, 1.0, SymbolInfoDouble(_Symbol, SYMBOL_BID), margin_for_one_lot))
Print("Could not obtain the margin required to open 1 lot");
lotSize = currentMargin / margin_for_one_lot;
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lotSize = MathMax(minLot, MathFloor(lotSize / lotStep) * lotStep);
return lotSize;
}
double LotCheckPendingBuy()
{
double margin_for_one_lot;
static double lotSize;
double currentMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
if(!OrderCalcMargin(ORDER_TYPE_BUY_STOP || ORDER_TYPE_BUY_LIMIT, _Symbol, 1.0, SymbolInfoDouble(_Symbol, SYMBOL_ASK), margin_for_one_lot))
Print("Could not obtain the margin required to open 1 lot");
lotSize = currentMargin / margin_for_one_lot;
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lotSize = MathMax(minLot, MathFloor(lotSize / lotStep) * lotStep);
return lotSize;
}
double LotCheckPendingSell()
{
double margin_for_one_lot;
static double lotSize;
double currentMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
if(!OrderCalcMargin(ORDER_TYPE_SELL_STOP || ORDER_TYPE_SELL_LIMIT, _Symbol, 1.0, SymbolInfoDouble(_Symbol, SYMBOL_BID), margin_for_one_lot))
Print("Could not obtain the margin required to open 1 lot");
lotSize = currentMargin / margin_for_one_lot;
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lotSize = MathMax(minLot, MathFloor(lotSize / lotStep) * lotStep);
return lotSize;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
EventKillTimer();
ExtDialog.Destroy(reason);
}
//+------------------------------------------------------------------+
//| Timer Event |
//+------------------------------------------------------------------+
void OnTimer()
{
pause = !pause;
}
//+------------------------------------------------------------------+