-
Notifications
You must be signed in to change notification settings - Fork 6
/
Position Size Calculator.mq4
293 lines (217 loc) · 11.1 KB
/
Position Size Calculator.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
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
//+------------------------------------------------------------------+
//| Position Size Calculator.mq4 |
//| Copyright © 2015 - 2017, Leonardo Ciaccio |
//| https://github.com/LeonardoCiaccio/Position-Size-Calculator |
//| |
//| Donate PayPal : [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015-2016, Leonardo Ciaccio"
#property link "https://github.com/LeonardoCiaccio/Position-Size-Calculator"
#property indicator_chart_window
#property description "Calculate the value of size in real-time with mode Money and Percentage."
#property strict // Extern/Input box trick ;) - http://docs.mql4.com/basis/preprosessor/compilation
enum __money{
FreeMargin, //Free Margin
Balance,
Equity
};
enum __position{
TopLeft = 0, // Top Left
TopRight = 1, // Top Right
BottomLeft = 2, // Bottom Left
BottomRight = 3 // Bottom Right
};
enum __mode{
Percentage = 1,
Money = 2
};
extern string Info = "--------------------------------------------------------"; // ------- INDICATOR INFORMATION
extern string Name = "Position Size Calculator";
extern string Version = "v.1.0.5";
extern string Contact = "[email protected]";
extern string Web = "https://github.com/LeonardoCiaccio/Position-Size-Calculator";
extern string Donate_Bitcoins = "1KHSR2S58y8WV6o3zRYeD5fBApvfTMtj8B"; // Donate Bitcoins
extern string Donate_PayPal = "[email protected]"; // Donate PayPal
extern string Setup = "--------------------------------------------------------"; // ------- SETUP INDICATOR
extern __money AccountMoney = Balance; // Money For The Calculation
extern __mode Mode = Percentage; // Mode For The Calculation
extern double StopLossPips = 30; // Stop Loss In Pips
extern double RiskPercentage = 5; // Risk In Percentage
extern double RiskMoney = 25; // Risk In Money
extern string Box = "--------------------------------------------------------"; // ------- SETUP BOX STYLE
extern color Color_BackGround = clrNONE; // Color Of Background Box
extern color Color_Lots = Red; // Color Of Size
extern color Color_Profit = LightBlue; // Color Of Profit
extern color Color_Tick = Orange; // Color Of Ticks
extern color Font_Color = LightBlue; // Color Of Common Fonts
extern int Font_Size = 12; // Font Size
extern string Font_Face = "Impact"; // Font Face
extern __position Position = TopRight; // Position
extern int Distance_X = 25; // Distance Of Box Horizontal
extern int Distance_Y = 15; // Distance Of Box Vertical
extern int BackGround_Size = 180; // Size Of Box ( ignore it )
double MyPoint = 0.0;
string MySymbol = "";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init(){
MyPoint = Point;
if( Digits == 3 || Digits == 5 )MyPoint = Point * 10;
MySymbol = Symbol();
return( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit(){
remove_box();
return( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start(){
create_box();
return( 0 );
}
//+------------------------------------------------------------------+
//| Current Spread for current pair |
//+------------------------------------------------------------------+
double current_spread(){
double spread;
if( Digits == 3 || Digits == 5 ){
spread = NormalizeDouble( MarketInfo( Symbol(), MODE_SPREAD ) / 10, 1 );
}else{
spread = NormalizeDouble( MarketInfo( Symbol(), MODE_SPREAD ), 0 );
}
return(spread);
}
//+------------------------------------------------------------------+
//| Create a box information |
//+------------------------------------------------------------------+
int create_box(){
string txtBG = "g";
string aName = "Account";
string bName = "unknown";
double size = 0.0;
switch( AccountMoney ){
case 0 :
size = AccountFreeMargin();
aName = "Free Margin";
break;
case 1 :
size = AccountBalance();
aName = "Balance";
break;
case 2 :
size = AccountEquity();
aName = "Equity";
break;
}
bName = DoubleToStr( size, 2 );
string myMode = ( Mode == Percentage ) ? "Percentage" : "Money";
double riskMoney = ( Mode == Percentage ) ? ( size / 100 ) * RiskPercentage : RiskMoney;
double riskPercentage = ( Mode == Percentage ) ? RiskPercentage : RiskMoney / ( size / 100 );
double unitCost = MarketInfo( Symbol(), MODE_TICKVALUE );
double tickSize = MarketInfo( Symbol(), MODE_TICKSIZE );
// Important for startup MT4, without generate an error
//if( unitCost == 0 )return( 0 );
double positionSize = ( unitCost > 0 ) ? riskMoney / ( ( ( NormalizeDouble( StopLossPips * MyPoint, Digits ) ) * unitCost ) / tickSize ) : 0 ;
// Background
ObjectCreate( "BoxBackground", OBJ_LABEL, 0, 0, 0, 0, 0);
ObjectSetText("BoxBackground", txtBG, BackGround_Size, "Webdings");
ObjectSet("BoxBackground", OBJPROP_CORNER, Position);
ObjectSet("BoxBackground", OBJPROP_BACK, false);
ObjectSet("BoxBackground", OBJPROP_XDISTANCE, 3 );
ObjectSet("BoxBackground", OBJPROP_YDISTANCE, 3 );
ObjectSet("BoxBackground", OBJPROP_COLOR, Color_BackGround );
ObjectCreate( "Spread", OBJ_LABEL, 0, 0, 0);
ObjectSet( "Spread", OBJPROP_CORNER, Position );
ObjectSet( "Spread", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "Spread", OBJPROP_YDISTANCE, Distance_Y );
ObjectSetText( "Spread", "SPREAD : " + DoubleToStr( current_spread(), 2 ) , Font_Size, Font_Face, Font_Color);
ObjectCreate("EntryLevel", OBJ_LABEL, 0, 0, 0);
ObjectSet( "EntryLevel", OBJPROP_CORNER, Position );
ObjectSet( "EntryLevel", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "EntryLevel", OBJPROP_YDISTANCE, Distance_Y * 3 );
ObjectSetText( "EntryLevel", "Level : " + DoubleToStr( Ask, Digits ), Font_Size, Font_Face, Font_Color);
ObjectCreate("AccountSize", OBJ_LABEL, 0, 0, 0);
ObjectSet( "AccountSize", OBJPROP_CORNER, Position );
ObjectSet( "AccountSize", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "AccountSize", OBJPROP_YDISTANCE, Distance_Y * 4 );
ObjectSetText( "AccountSize", aName + " : " + bName, Font_Size, Font_Face, Font_Color);
ObjectCreate("Mode", OBJ_LABEL, 0, 0, 0);
ObjectSet( "Mode", OBJPROP_CORNER, Position );
ObjectSet( "Mode", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "Mode", OBJPROP_YDISTANCE, Distance_Y * 5 );
ObjectSetText( "Mode", "Mode : " + myMode, Font_Size, Font_Face, Color_Tick);
ObjectCreate("StopLoss", OBJ_LABEL, 0, 0, 0);
ObjectSet( "StopLoss", OBJPROP_CORNER, Position );
ObjectSet( "StopLoss", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "StopLoss", OBJPROP_YDISTANCE, Distance_Y * 6 );
ObjectSetText( "StopLoss", "Stop Loss : " + DoubleToStr( StopLossPips, 2 ) , Font_Size, Font_Face, Font_Color);
ObjectCreate("Risk", OBJ_LABEL, 0, 0, 0);
ObjectSet( "Risk", OBJPROP_CORNER, Position );
ObjectSet( "Risk", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "Risk", OBJPROP_YDISTANCE, Distance_Y * 7 );
ObjectSetText( "Risk", "Risk : " + DoubleToStr( riskPercentage, 2 ) + " %" , Font_Size, Font_Face, Font_Color);
ObjectCreate("RiskM", OBJ_LABEL, 0, 0, 0);
ObjectSet( "RiskM", OBJPROP_CORNER, Position );
ObjectSet( "RiskM", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "RiskM", OBJPROP_YDISTANCE, Distance_Y * 8 );
ObjectSetText( "RiskM", "Money : " + DoubleToStr( riskMoney, 2 ) , Font_Size, Font_Face, Font_Color);
ObjectCreate("TickValue", OBJ_LABEL, 0, 0, 0);
ObjectSet( "TickValue", OBJPROP_CORNER, Position );
ObjectSet( "TickValue", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "TickValue", OBJPROP_YDISTANCE, Distance_Y * 10 );
ObjectSetText( "TickValue", "Tick Value : " + DoubleToStr( unitCost, 3 ) , Font_Size, Font_Face, Color_Tick );
ObjectCreate("TickSize", OBJ_LABEL, 0, 0, 0);
ObjectSet( "TickSize", OBJPROP_CORNER, Position );
ObjectSet( "TickSize", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "TickSize", OBJPROP_YDISTANCE, Distance_Y * 11 );
ObjectSetText( "TickSize", "Tick Size : " + DoubleToStr( tickSize, 3 ) , Font_Size, Font_Face, Color_Tick);
string pSize = ( positionSize > 0 ) ? DoubleToStr( positionSize, 3 ) : "loading ..." ;
ObjectCreate("PositionSize", OBJ_LABEL, 0, 0, 0);
ObjectSet( "PositionSize", OBJPROP_CORNER, Position );
ObjectSet( "PositionSize", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "PositionSize", OBJPROP_YDISTANCE, Distance_Y * 12 );
ObjectSetText( "PositionSize", "LOTS : " + pSize , Font_Size, Font_Face, Color_Lots);
ObjectCreate("Profit", OBJ_LABEL, 0, 0, 0);
ObjectSet( "Profit", OBJPROP_CORNER, Position );
ObjectSet( "Profit", OBJPROP_XDISTANCE, Distance_X);
ObjectSet( "Profit", OBJPROP_YDISTANCE, Distance_Y * 14 );
ObjectSetText( "Profit", "Profit : " + DoubleToStr( total_profit(), 2 ) , Font_Size, Font_Face, Color_Profit);
return( 0 );
}
//+------------------------------------------------------------------+
//| Remove a box information |
//+------------------------------------------------------------------+
void remove_box(){
ObjectDelete( "Spread" );
ObjectDelete( "EntryLevel" );
ObjectDelete( "StopLoss" );
ObjectDelete( "Risk" );
ObjectDelete( "RiskM" );
ObjectDelete( "AccountSize" );
ObjectDelete( "PositionSize" );
ObjectDelete( "TickValue" );
ObjectDelete( "TickSize" );
ObjectDelete( "Profit" );
ObjectDelete( "BoxBackground" );
ObjectDelete( "Mode" );
}
//+------------------------------------------------------------------+
//| Total profit |
//+------------------------------------------------------------------+
double total_profit(){
double tt_profit = 0.0;
for( int x = 0; x < OrdersTotal(); x++ ){
if( OrderSelect( x, SELECT_BY_POS, MODE_TRADES ) ){
if( OrderSymbol() == MySymbol ){
tt_profit += OrderProfit();
}
}
}
return(tt_profit);
}