-
Notifications
You must be signed in to change notification settings - Fork 1
/
openWithRisk.mq5
1689 lines (1594 loc) · 61 KB
/
openWithRisk.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//+------------------------------------------------------------------+
//| _fix_open.mq4 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#define pdxversion "1.00"
#property copyright "Klymenko Roman ([email protected])"
#property link "https://www.mql5.com/en/users/needtome"
#property version pdxversion
#property description "This advisor helps you to open a deal with a stop loss no more than the amount you specified."
#property description "That is, it determines the number of lots to which you want to make a deal, so that the stop loss does not exceed the indicated amount in dollars."
#property description "Keys:"
#property description "X - quit, S - send order and quit, U - min stop BUY, L - min stop SELL, Z - reset Open Price"
#property description "2 - 0.2% stop Long, 3 - 0.2% stop Short, 7 - CENT_STOP stop Long, 8 - CENT_STOP stop Short"
#property strict
double OPEN_PRICE=0; //Opening price (0 - by market)
double STOPLOSS_PRICE=0; //Stop loss Price
enum TYPESTOP
{
dollar,// $
percent,// % of deposit
};
enum TypeOfPos
{
MY_BUY,
MY_SELL,
MY_BUYSTOP,
MY_BUYLIMIT,
MY_SELLSTOP,
MY_SELLLIMIT,
MY_BUYSLTP,
MY_SELLSLTP,
};
enum TypeOfLang{
MY_ENG, // English
MY_RUS, // Russian
};
input TYPESTOP Type_STOP=dollar; //Stop Loss type
input double STOP_IN=10; //Stop loss as $ or %
input double CENT_STOP=0.001; //Stop loss in cents (keys 7 and 8)
input bool EXIT_IF_MORE=true; //No deal entry if with min. lot the risk is greater than specified
input uchar DEL_END_DAY=0; //Cancel limit order after, hours
input uchar TAKE_MULTIPLY=4; //Multiplier for take profit calculation
input string MY_COMMENT=""; //Comment
#ifdef __MQL5__
enum TypeOfFilling //Filling Mode
{
FOK,//ORDER_FILLING_FOK
RETURN,// ORDER_FILLING_RETURN
IOC,//ORDER_FILLING_IOC
};
input TypeOfFilling useORDER_FILLING_RETURN=FOK; //Order filling mode
#endif
input int EA_Magic=777; // Magic number for the first position
input TypeOfLang LANG=MY_RUS; // Language
int curMagic=0;
int lastOrder;
double open=OPEN_PRICE;
bool isLong=true;
double lot=0.01;
double profit=0;
double stopin_value;
double takein_value=0;
double takein_line1=0;
long chart_ID=0;
string symbols="";
int tmp_val=0;
double curPoint=0;
double curBid=0;
double curAsk=0;
double curSpread=0;
string exprefix="fixopen";
MqlRates rates[];
MqlTick lastme;
double maxLot=0;
MqlDateTime curDay;
datetime dfrom;
datetime dto;
MqlDateTime curEndTime;
MqlDateTime curStartTime;
string time_info="";
double draw_stop=0;
double draw_open=0;
string resval;
double curMinStop=0;
string currencyS;
bool isEndTime=false;
bool isClosed=false;
struct translate{
string err1;
string err2;
string err3;
string err4;
string err5;
string err6;
string err7;
string err8;
string err9;
string err64;
string err65;
string err128;
string err129;
string err130;
string err131;
string err132;
string err133;
string err134;
string err135;
string err136;
string err137;
string err138;
string err139;
string err140;
string err141;
string err145;
string err146;
string err147;
string err148;
string err0;
string CheckBox1;
string CheckBox2;
string CheckBox4;
string CheckBox5;
string CheckBox6;
string CheckBox7;
string Label1;
string Label1_min;
string Label1_freeze;
string Label1_spread;
string Label2;
string Label3;
string Label6;
string Label7;
string Label11;
string Label12;
string Button1;
string Button1_buy;
string Button1_sell;
string Button2;
string Label4;
string Label5;
string ComboBox1_item0;
string ComboBox1_item1;
string wrnType_STOP;
string wrnTAKE_MULTIPLY;
string wrnDEMO;
string wrnSTOPLOSS_PRICE;
string wrnTAKE_LEVEL1;
string wrnTAKE_LEVEL2;
string wrnTAKE_LEVEL3;
string wrnEXIT_IF_MORE1;
string wrnEXIT_IF_MORE2;
string wrnONLY_WRITE1;
string wrnONLY_WRITE2;
string wrnONLY_WRITE3;
string wrnONLY_WRITE4;
string wrnSTOPLOSS_PRICE2;
string wrnSTOP_IN;
string wndTITLE;
string wrnTAKEIN_LESS;
string retcode;
string retcode10004;
string retcode10006;
string retcode10007;
string retcode10010;
string retcode10011;
string retcode10012;
string retcode10013;
string retcode10014;
string retcode10015;
string retcode10016;
string retcode10017;
string retcode10018;
string retcode10019;
string retcode10020;
string retcode10021;
string retcode10022;
string retcode10023;
string retcode10024;
string retcode10025;
string retcode10026;
string retcode10027;
string retcode10028;
string retcode10029;
string retcode10030;
string retcode10031;
string retcode10032;
string retcode10033;
string retcode10034;
string retcode10035;
string retcode10036;
string retcode10038;
string retcode10039;
string retcode10040;
string retcode10041;
string retcode10042;
string retcode10043;
string retcode10044;
string wrnSYMBOL_TRADE_TICK_SIZE;
string wrnSYMBOL_TRADE_TICK_SIZE2;
string wrnSYMBOL_TRADE_TICK_SIZE_end;
string DEL_END_DAY_val1;
string DEL_END_DAY_val2;
string DEL_END_DAY_val3;
string DEL_END_DAY_val4;
string DEL_END_DAY_val5;
string lblMinSTOP;
string lblNo;
string lblDef;
string noMinStop;
string lblshow_SWAP;
string lbl_point;
string lblshow_TIME;
string lblshow_TIME2;
string lbl_min;
string lbl_hour;
string lbl_close;
string wrnMinVolume;
string wrnOnlyClose;
string wrnMaxLot;
string maxMarga;
string noBalance;
string maxCount;
string btnShowOpenLine;
string btnSpecSL;
};
translate langs;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
init_lang();
if(!SymbolInfoTick(_Symbol,lastme)){
Alert(GetLastError());
ExpertRemove();
}
curMagic=EA_Magic;
int cntMyPos=OrdersTotal();
stopin_value=STOP_IN;
maxLot=SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
if(SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN)==0){
Alert(langs.wrnMinVolume);
ExpertRemove();
}
if(SymbolInfoInteger(_Symbol, SYMBOL_TRADE_MODE)==SYMBOL_TRADE_MODE_DISABLED || SymbolInfoInteger(_Symbol, SYMBOL_TRADE_MODE)==SYMBOL_TRADE_MODE_CLOSEONLY ){
Alert(langs.wrnOnlyClose);
ExpertRemove();
}
isClosed=false;
// Get the current date
TimeToStruct(TimeCurrent(), curDay);
// Get symbol trading time for today
if(SymbolInfoSessionTrade(_Symbol, (ENUM_DAY_OF_WEEK) curDay.day_of_week, 0, dfrom, dto)){
time_info="";
TimeToStruct(dto, curEndTime);
TimeToStruct(dfrom, curStartTime);
isEndTime=true;
string tmpmsg="";
tmp_val=curEndTime.hour;
if(tmp_val<10){
StringAdd(tmpmsg, "0");
}
StringAdd(tmpmsg, (string) tmp_val+":");
tmp_val=curEndTime.min;
if(tmp_val<10){
StringAdd(tmpmsg, "0");
}
StringAdd(tmpmsg, (string) tmp_val);
if(curEndTime.hour==curDay.hour){
if(tmp_val>curDay.min){
}else{
isClosed=true;
}
}else{
if(curEndTime.hour==0){
}else{
if( curEndTime.hour>1 && (curDay.hour>curEndTime.hour || curDay.hour==0)){
StringAdd(time_info, " ("+langs.lbl_close+")");
isClosed=true;
}else if(curDay.hour<curStartTime.hour ){
StringAdd(time_info, " ("+langs.lbl_close+")");
isEndTime=false;
isClosed=true;
}else if(curDay.hour==curStartTime.hour && curDay.min<curStartTime.min ){
StringAdd(time_info, " ("+langs.lbl_close+")");
isEndTime=false;
isClosed=true;
}
}
}
if(isEndTime){
StringAdd(time_info, langs.lblshow_TIME+": "+tmpmsg+time_info);
}else{
StringAdd(time_info, langs.lblshow_TIME2+": "+tmpmsg+time_info);
}
}
ArraySetAsSeries(rates, true);
// the currency used for profit calculation for the current financial instrument
currencyS=SymbolInfoString(_Symbol, SYMBOL_CURRENCY_PROFIT);
// Symbol's point size
curPoint=SymbolInfoDouble(_Symbol, SYMBOL_POINT);
curMinStop=SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL)*SymbolInfoDouble(_Symbol, SYMBOL_POINT);
ObjectDelete(0, exprefix+"_line1");
symbols=SymbolName(0, false);
if(SymbolsTotal(false)>1){
StringAdd(symbols, ", "+SymbolName(1, false));
}
// if there are Stop Loss and open price lines on the chart
// add the appropriate prices to variables
if(ObjectFind(0, exprefix+"_stop")>=0){
draw_stop=ObjectGetDouble(0, exprefix+"_stop", OBJPROP_PRICE);
if(ObjectFind(0, exprefix+"_open")>=0){
draw_open=ObjectGetDouble(0, exprefix+"_open", OBJPROP_PRICE);
}
// otherwise create the entire Expert Advisor UI
}else{
draw_open=lastme.bid;
draw_stop=draw_open-(SymbolInfoInteger(_Symbol, SYMBOL_SPREAD)*curPoint);
ObjectCreate(0, exprefix+"_stop", OBJ_HLINE, 0, 0, draw_stop);
ObjectSetInteger(0,exprefix+"_stop",OBJPROP_SELECTABLE,1);
ObjectSetInteger(0,exprefix+"_stop",OBJPROP_SELECTED,1);
ObjectSetInteger(0,exprefix+"_stop",OBJPROP_STYLE,STYLE_DASHDOTDOT);
ObjectSetInteger(0,exprefix+"_stop",OBJPROP_ANCHOR,ANCHOR_TOP);
if(curMinStop>0){
ObjectCreate(0, exprefix+"_minstop_high", OBJ_HLINE, 0, 0, lastme.ask+curMinStop);
ObjectSetInteger(0,exprefix+"_minstop_high",OBJPROP_SELECTABLE,0);
ObjectSetInteger(0,exprefix+"_minstop_high",OBJPROP_SELECTED,0);
ObjectSetInteger(0,exprefix+"_minstop_high",OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,exprefix+"_minstop_high",OBJPROP_ANCHOR,ANCHOR_TOP);
ObjectSetInteger(0,exprefix+"_minstop_high",OBJPROP_COLOR,clrKhaki);
ObjectSetInteger(0,exprefix+"_minstop_high",OBJPROP_BACK,true);
ObjectCreate(0, exprefix+"_minstop_low", OBJ_HLINE, 0, 0, lastme.bid-curMinStop);
ObjectSetInteger(0,exprefix+"_minstop_low",OBJPROP_SELECTABLE,0);
ObjectSetInteger(0,exprefix+"_minstop_low",OBJPROP_SELECTED,0);
ObjectSetInteger(0,exprefix+"_minstop_low",OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,exprefix+"_minstop_low",OBJPROP_ANCHOR,ANCHOR_TOP);
ObjectSetInteger(0,exprefix+"_minstop_low",OBJPROP_COLOR,clrKhaki);
ObjectSetInteger(0,exprefix+"_minstop_low",OBJPROP_BACK,true);
}
// Creating button "Show (0) open price line"
if(ObjectFind(0, exprefix+"_openbtn")<0){
ObjectCreate(0, exprefix+"_openbtn", OBJ_BUTTON, 0, 0, 0);
ObjectSetInteger(0,exprefix+"_openbtn",OBJPROP_XDISTANCE,0);
ObjectSetInteger(0,exprefix+"_openbtn",OBJPROP_YDISTANCE,33);
ObjectSetString(0,exprefix+"_openbtn",OBJPROP_TEXT, langs.btnShowOpenLine);
ObjectSetInteger(0,exprefix+"_openbtn",OBJPROP_XSIZE,333);
ObjectSetInteger(0,exprefix+"_openbtn",OBJPROP_FONTSIZE, 8);
ObjectSetInteger(0,exprefix+"_openbtn",OBJPROP_YSIZE,25);
}
// Creating the Buy button
if(ObjectFind(0, exprefix+"_send")<0){
ObjectCreate(0, exprefix+"_send", OBJ_BUTTON, 0, 0, 0);
ObjectSetInteger(0,exprefix+"_send",OBJPROP_XDISTANCE,0);
ObjectSetInteger(0,exprefix+"_send",OBJPROP_YDISTANCE,58);
ObjectSetString(0,exprefix+"_send",OBJPROP_TEXT, langs.btnSpecSL);
ObjectSetInteger(0,exprefix+"_send",OBJPROP_XSIZE,333);
ObjectSetInteger(0,exprefix+"_send",OBJPROP_FONTSIZE, 8);
ObjectSetInteger(0,exprefix+"_send",OBJPROP_YSIZE,25);
}
}
getmespread();
//---
return(INIT_SUCCEEDED);
}
/* Show the open price line */
void showOpenLine(){
if(ObjectFind(0, exprefix+"_open")<0){
draw_open=lastme.bid;
ObjectCreate(0, exprefix+"_open", OBJ_HLINE, 0, 0, draw_open);
ObjectSetInteger(0,exprefix+"_open",OBJPROP_SELECTABLE,1);
ObjectSetInteger(0,exprefix+"_open",OBJPROP_SELECTED,1);
ObjectSetInteger(0,exprefix+"_open",OBJPROP_STYLE,STYLE_DASHDOTDOT);
ObjectSetInteger(0,exprefix+"_open",OBJPROP_ANCHOR,ANCHOR_TOP);
ObjectSetInteger(0,exprefix+"_open",OBJPROP_COLOR,clrGreen);
}
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(reason!=REASON_CHARTCHANGE){
ObjectsDeleteAll(0, exprefix);
Comment("");
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
SymbolInfoTick(_Symbol,lastme);
getmespread();
// movestopline(true);
// movestopline(false);
if(STOPLOSS_PRICE>0){
updatelots();
}
if( curMinStop>0 && ObjectFind(0, exprefix+"_minstop_high")>=0 ){
ObjectMove(0,exprefix+"_minstop_high",0,0,SymbolInfoDouble(_Symbol, SYMBOL_ASK)+curMinStop);
ObjectMove(0,exprefix+"_minstop_low",0,0,SymbolInfoDouble(_Symbol, SYMBOL_BID)-curMinStop);
}
}
void OnChartEvent(const int id, // event ID
const long& lparam, // event parameter of the long type
const double& dparam, // event parameter of the double type
const string& sparam) // event parameter of the string type
{
string text="";
double curprice=0;
switch(id){
case CHARTEVENT_OBJECT_CLICK:
if (sparam==exprefix+"_send"){
startPosition();
}else if (sparam==exprefix+"_openbtn"){
updateOpenLine();
}
break;
case CHARTEVENT_OBJECT_DRAG:
if(sparam==exprefix+"_stop"){
setstopbyline();
showOpenLine();
ObjectSetInteger(0,exprefix+"_openbtn",OBJPROP_STATE, true);
}else if(sparam==exprefix+"_open"){
curprice=ObjectGetDouble(0, exprefix+"_open", OBJPROP_PRICE);
if( curprice>0 && curprice != draw_open ){
double tmp_double=SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
if( tmp_double>0 && tmp_double!=1 ){
if(tmp_double<1){
resval=DoubleToString(curprice/tmp_double, 8);
if( StringFind(resval, ".00000000")>0 ){}else{
curprice=MathFloor(curprice)+MathFloor((curprice-MathFloor(curprice))/tmp_double)*tmp_double;
}
}else{
if( MathMod(curprice,tmp_double) ){
curprice= MathFloor(curprice/tmp_double)*tmp_double;
}
}
}
draw_open=open=OPEN_PRICE=curprice;
updatebuttontext();
ObjectSetString(0,exprefix+"Edit3",OBJPROP_TEXT,0, (string) NormalizeDouble(draw_open, _Digits));
ChartRedraw(0);
}
}
break;
case CHARTEVENT_KEYDOWN:
switch((int) sparam){
// Terminate EA operation without placing an order
case 45: //x
closeNotSave();
break;
// Place an order and complete EA operation
case 31: //s
startPosition();
break;
// Set minimum possible Stop Loss to open a Buy position
case 22: //u
setMinStopBuy();
break;
// Set minimum possible Stop Loss to open a Sell position
case 38: //l
setMinStopSell();
break;
// Cancel the set open price
case 44: //z
setZero();
ChartRedraw();
break;
// Set Stop Loss at 0.2% from the current price to open a Long position
case 3: //2
set02StopBuy();
break;
// Set Stop Loss at 0.2% from the current price to open a Short position
case 4: //3
set02StopSell();
break;
// Set Stop Loss to 7 cents from the current price (the CENT_STOP parameter)
// To open a Long position
case 8: //7
set7StopBuy();
break;
// Set Stop Loss to 7 cents from the current price (the CENT_STOP parameter)
// To open a Short position
case 9: //8
set7StopSell();
break;
}
break;
}
}
void updateOpenLine(){
setZero();
if( ObjectGetInteger(0,exprefix+"_openbtn",OBJPROP_SELECTED) == true ){
ObjectDelete(0, exprefix+"_open");
}else{
showOpenLine();
}
ChartRedraw();
}
/*
"Remembers" the Stop Loss levels for the future order
*/
void setstopbyline(){
// Receive the price in which the Stop Loss line is located
double curprice=ObjectGetDouble(0, exprefix+"_stop", OBJPROP_PRICE);
// If the price is different from the one in which the Stop Loss line was positioned at the EA launch,
if( curprice>0 && curprice != draw_stop ){
double tmp_double=SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
if( tmp_double>0 && tmp_double!=1 ){
if(tmp_double<1){
resval=DoubleToString(curprice/tmp_double, 8);
if( StringFind(resval, ".00000000")>0 ){}else{
curprice=MathFloor(curprice)+MathFloor((curprice-MathFloor(curprice))/tmp_double)*tmp_double;
}
}else{
if( MathMod(curprice,tmp_double) ){
curprice= MathFloor(curprice/tmp_double)*tmp_double;
}
}
}
draw_stop=STOPLOSS_PRICE=curprice;
updatebuttontext();
ChartRedraw(0);
}
}
void movestopline(bool direction, int type=0){
double tmp_price=0;
double new_price=0;
if(direction){
if(open>0){
tmp_price=open;
}else{
tmp_price=SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
switch(type){
case 0: // min stop
if(curMinStop>0){
new_price=tmp_price-(SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL)+1)*SymbolInfoDouble(_Symbol, SYMBOL_POINT);
}else{
Alert(langs.noMinStop);
return;
}
break;
case 1: // 0.2%
new_price=tmp_price-tmp_price*0.002;
break;
case 2: // 7 cents
new_price=tmp_price-CENT_STOP;
break;
}
ObjectMove(0,exprefix+"_stop",0,0,new_price);
}else{
if(open>0){
tmp_price=open;
}else{
tmp_price=SymbolInfoDouble(_Symbol, SYMBOL_ASK);
}
switch(type){
case 0: // min stop
if(curMinStop>0){
new_price=tmp_price+(SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL)+1)*SymbolInfoDouble(_Symbol, SYMBOL_POINT);
}else{
Alert(langs.noMinStop);
return;
}
break;
case 1: // 0.2%
new_price=tmp_price+tmp_price*0.002;
break;
case 2: // 7 cents
new_price=tmp_price+CENT_STOP;
break;
}
ObjectMove(0,exprefix+"_stop",0,0,new_price);
}
setstopbyline();
}
void updatebuttontext(){
double tmp_price=OPEN_PRICE;
if(tmp_price==0){
tmp_price=lastme.bid;
}
if(tmp_price>0 && STOPLOSS_PRICE>0){
if(tmp_price>STOPLOSS_PRICE){
// change the name for the Buy button
}else{
// change the name for the Sell button
}
}
updatelots();
}
/*
Calculate loss in case of Stop Loss
*/
double getMyProfit(double fPrice, double fSL, double fLot, bool forLong=true){
double fProfit=0;
fPrice=NormalizeDouble(fPrice,_Digits);
fSL=NormalizeDouble(fSL,_Digits);
#ifdef __MQL5__
if( forLong ){
if(OrderCalcProfit(ORDER_TYPE_BUY, _Symbol, fLot, fPrice, fSL, fProfit)){};
}else{
if(OrderCalcProfit(ORDER_TYPE_SELL, _Symbol, fLot, fPrice, fSL, fProfit)){};
}
#else
if( forLong ){
fProfit=(fPrice-fSL)*fLot* (1 / MarketInfo(_Symbol, MODE_POINT)) * MarketInfo(_Symbol, MODE_TICKVALUE);
}else{
fProfit=(fSL-fPrice)*fLot* (1 / MarketInfo(_Symbol, MODE_POINT)) * MarketInfo(_Symbol, MODE_TICKVALUE);
}
#endif
if( fProfit!=0 ){
fProfit=MathAbs(fProfit);
}
return fProfit;
}
void updatelots(){
double tmp_price=OPEN_PRICE;
if(tmp_price==0){
if(lastme.bid>STOPLOSS_PRICE){
tmp_price=SymbolInfoDouble(_Symbol, SYMBOL_ASK);
}else{
tmp_price=SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
}
lot=SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
if(lot==0){
Alert(langs.wrnMinVolume);
ExpertRemove();
}
if( Type_STOP==percent ){
stopin_value=(AccountInfoDouble(ACCOUNT_BALANCE)*STOP_IN)/100;
}
if(tmp_price>0 && STOPLOSS_PRICE>0){
if(tmp_price>STOPLOSS_PRICE){
//LONG
if( curMinStop>0 && curMinStop>=(tmp_price-STOPLOSS_PRICE) ){
if(ObjectFind(0, exprefix+"_take")>=0){
ObjectDelete(0, exprefix+"_take");
}
ObjectSetString(0,exprefix+"_send",OBJPROP_TEXT, langs.wrnSTOPLOSS_PRICE+" ("+DoubleToString(tmp_price-STOPLOSS_PRICE, _Digits)+" "+AccountInfoString(ACCOUNT_CURRENCY)+"). "+langs.Label1_min+": "+(string) curMinStop+" "+AccountInfoString(ACCOUNT_CURRENCY));
return;
}
if(TAKE_MULTIPLY>0){
takein_value=NormalizeDouble(tmp_price+(TAKE_MULTIPLY*(tmp_price-STOPLOSS_PRICE)),_Digits);
}else{
takein_value=0;
}
profit=getMyProfit(tmp_price, STOPLOSS_PRICE, lot);
if( profit!=0 ){
if( profit<stopin_value ){
lot*=(stopin_value/profit);
if( SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP)==0.01 ){
lot=(floor(lot*100))/100;
}else if( SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP)==0.1 ){
lot=(floor(lot*10))/10;
}else{
lot=floor(lot);
}
}
}
}else{
//SHORT
if( curMinStop>0 && curMinStop>=(STOPLOSS_PRICE-tmp_price) ){
if(ObjectFind(0, exprefix+"_take")>=0){
ObjectDelete(0, exprefix+"_take");
}
ObjectSetString(0,exprefix+"_send",OBJPROP_TEXT, langs.wrnSTOPLOSS_PRICE+" ("+DoubleToString(STOPLOSS_PRICE-tmp_price, _Digits)+" "+AccountInfoString(ACCOUNT_CURRENCY)+"). "+langs.Label1_min+": "+(string) curMinStop+" "+AccountInfoString(ACCOUNT_CURRENCY));
return;
}
if(TAKE_MULTIPLY>0){
takein_value=NormalizeDouble(tmp_price-(TAKE_MULTIPLY*(STOPLOSS_PRICE-tmp_price)),_Digits);
}else{
takein_value=0;
}
profit=getMyProfit(tmp_price, STOPLOSS_PRICE, lot, false);
if( profit!=0 ){
if( profit<stopin_value ){
lot*=(stopin_value/profit);
if( SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP)==0.01 ){
lot=(floor(lot*100))/100;
}else if( SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP)==0.1 ){
lot=(floor(lot*10))/10;
}else{
lot=floor(lot);
}
}
}
}
if(takein_value>0){
if(ObjectFind(0, exprefix+"_take")<0){
ObjectCreate(0, exprefix+"_take", OBJ_HLINE, 0, 0, takein_value);
ObjectSetInteger(0,exprefix+"_take",OBJPROP_STYLE,STYLE_DASHDOT);
ObjectSetInteger(0,exprefix+"_take",OBJPROP_COLOR,clrWhite);
ObjectSetInteger(0,exprefix+"_take",OBJPROP_SELECTABLE,true);
}else{
ObjectMove(0,exprefix+"_take",0,0,takein_value);
}
}
string direction="SELL: ";
if(tmp_price>STOPLOSS_PRICE){
direction="BUY: ";
}
double tmp_lot=SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
if(tmp_lot==0){
tmp_lot=0.01;
}
string wrnLot="";
if( maxLot>0 && lot > maxLot ){
wrnLot=". "+langs.wrnMaxLot+": "+(string) maxLot+"!";
}
ObjectSetString(0,exprefix+"_send",OBJPROP_TEXT, direction+langs.wrnONLY_WRITE1+": "+DoubleToString(lot, 2)+"; "+langs.wrnONLY_WRITE2+": "+(string) takein_value+"; "+langs.wrnONLY_WRITE3+": "+DoubleToString(profit*(lot/tmp_lot),2)+wrnLot);
}
}
void msgErr(int err, int retcode=0){
string curErr="";
switch(err){
case 1:
curErr=langs.err1;
break;
case 2:
curErr=langs.err2;
break;
case 3:
curErr=langs.err3;
break;
case 4:
curErr=langs.err4;
break;
case 5:
curErr=langs.err5;
break;
case 6:
curErr=langs.err6;
break;
case 7:
curErr=langs.err7;
break;
case 8:
curErr=langs.err8;
break;
case 9:
curErr=langs.err9;
break;
case 64:
curErr=langs.err64;
break;
case 65:
curErr=langs.err65;
break;
case 128:
curErr=langs.err128;
break;
case 129:
curErr=langs.err129;
break;
case 130:
curErr=langs.err130;
break;
case 131:
curErr=langs.err131;
break;
case 132:
curErr=langs.err132;
break;
case 133:
curErr=langs.err133;
break;
case 134:
curErr=langs.err134;
break;
case 135:
curErr=langs.err135;
break;
case 136:
curErr=langs.err136;
break;
case 137:
curErr=langs.err137;
break;
case 138:
curErr=langs.err138;
break;
case 139:
curErr=langs.err139;
break;
case 140:
curErr=langs.err140;
break;
case 141:
curErr=langs.err141;
break;
case 145:
curErr=langs.err145;
break;
case 146:
curErr=langs.err146;
break;
case 147:
curErr=langs.err147;
break;
case 148:
curErr=langs.err148;
break;
default:
curErr=langs.err0+": "+(string) err;
}
if(retcode>0){
curErr+=" ";
switch(retcode){
case 10004:
curErr+=langs.retcode10004;
break;
case 10006:
curErr+=langs.retcode10006;
break;
case 10007:
curErr+=langs.retcode10007;
break;
case 10010:
curErr+=langs.retcode10010;
break;
case 10011:
curErr+=langs.retcode10011;
break;
case 10012:
curErr+=langs.retcode10012;
break;
case 10013:
curErr+=langs.retcode10013;
break;
case 10014:
curErr+=langs.retcode10014;
break;
case 10015:
curErr+=langs.retcode10015;
break;
case 10016:
curErr+=langs.retcode10016;
break;
case 10017:
curErr+=langs.retcode10017;
break;
case 10018:
curErr+=langs.retcode10018;
break;
case 10019:
curErr+=langs.retcode10019;
break;
case 10020:
curErr+=langs.retcode10020;
break;
case 10021:
curErr+=langs.retcode10021;
break;
case 10022:
curErr+=langs.retcode10022;
break;
case 10023:
curErr+=langs.retcode10023;
break;
case 10024:
curErr+=langs.retcode10024;
break;
case 10025:
curErr+=langs.retcode10025;
break;
case 10026:
curErr+=langs.retcode10026;
break;
case 10027:
curErr+=langs.retcode10027;
break;
case 10028:
curErr+=langs.retcode10028;
break;
case 10029:
curErr+=langs.retcode10029;
break;
case 10030:
curErr+=langs.retcode10030;
break;
case 10031:
curErr+=langs.retcode10031;
break;
case 10032:
curErr+=langs.retcode10032;
break;
case 10033:
curErr+=langs.retcode10033;
break;
case 10034:
curErr+=langs.retcode10034;
break;
case 10035:
curErr+=langs.retcode10035;
break;
case 10036:
curErr+=langs.retcode10036;
break;
case 10038:
curErr+=langs.retcode10038;
break;
case 10039:
curErr+=langs.retcode10039;
break;
case 10040:
curErr+=langs.retcode10040;
break;
case 10041:
curErr+=langs.retcode10041;
break;
case 10042:
curErr+=langs.retcode10042;
break;
case 10043:
curErr+=langs.retcode10043;
break;
case 10044:
curErr+=langs.retcode10044;
break;
}
}
Alert(curErr);
}
/*
Showing data on spread and session closing time in a chart comment
*/
void getmespread(){
string msg="";
// Add the spread value in the symbol currency to the global variable
curSpread=lastme.ask-lastme.bid;
// If the market is not closed, show spread info
if( !isClosed ){
if(curSpread>0){
StringAdd(msg, langs.Label1_spread+": "+(string) DoubleToString(curSpread, (int) SymbolInfoInteger(_Symbol, SYMBOL_DIGITS))+" "+currencyS+" ("+DoubleToString(curSpread/curPoint, 0)+langs.lbl_point+")");
StringAdd(msg, "; "+DoubleToString(((curSpread)/lastme.bid)*100, 3)+"%");
}else{