-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIcarus_2.2.mq4
3032 lines (2766 loc) · 235 KB
/
Icarus_2.2.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
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
// ------------------------------------------------------------------------------------------------
// Original by http://www.lifesdream.org
//
// Modified by MaPi & HoHe & fxtrue & FX1079
//
// HIGH RISK GRID TRADING EA, USE IT ON DEMO ONLY UNTIL YOU KNOW HOW TO HANDLE AND ARE WILLING TO ACCEPT THE INVOLED RISKS!!!
// ------------------------------------------------------------------------------------------------
//
// To change global vars (F3):
// - Switch Auto Trading OFF
// - Change vars by hit F3-key
// - Restart Ikarus
// - Switch Auto Trading ON
// - Restart Ikarus again
//
//#define DEMO 1 // to make it a Demo Version (work on demo account only), this line MUST NOT be comment
//#define DEBUG 1 // to switch off debug info of, make this line as comment
#define max_open_positions 100 // maximum number of open positions, was constant = 50 before
#include <stdlib.mqh>
#include <stderror.mqh>
#define versionNo "2.2"
#define versionBMI "Icarus " + versionNo
#define versionOld "based on Super Money Grid v1.41"
#property version versionNo
#property strict
string key="Icarus 2.2";
// ------------------------------------------------------------------------------------------------
// EXTERN VARS
// ------------------------------------------------------------------------------------------------
extern int magic=11235;
// ------------------------------------------------------------------------------------------------
int user_slippage=2;
extern int grid_size=20; // Grid_Size
extern int gs_progression=3; // GS_Progression
extern int take_profit=20; // Take_Profit
extern double profit_lock=0.3; // Profit_Lock
extern double min_lots=0.01; // Min_lots
extern double equity_warning=0.20; // Equity_Warning
extern double account_risk=1.00; // Account_Risk
extern int progression=3; // Progression
extern int max_positions=6; // Max_Position
extern int unbalance_control=0; // unbalanced_Control
extern int max_spread=100; // Max_Spread
extern int show_forecast=1; // Show_Forecast
//
// ------------------------------------------------------------------------------------------------
// GLOBAL VARS
// ------------------------------------------------------------------------------------------------
// Ticket
// #007: be able to deal with variable number of open positions
int buy_tickets[max_open_positions];
int sell_tickets[max_open_positions];
// Lots
double buy_lots[max_open_positions];
double sell_lots[max_open_positions];
// Current Profit
double buy_profit[max_open_positions];
double sell_profit[max_open_positions];
// Open Price
double buy_price[max_open_positions];
double sell_price[max_open_positions];
// Number of orders
int buys=0;
int sells=0;
// #020: show line, where the next line_buy /line_sell would be, if it would be opened
// value of lines:
double line_buy=0,line_sell=0,line_buy_tmp=0,line_sell_tmp=0,line_buy_next=0,line_sell_next=0,line_buy_ts=0,line_sell_ts=0,line_margincall=0;
// profits:
double total_buy_profit=0,total_sell_profit=0,total_buy_swap=0,total_sell_swap=0;
double buy_max_profit=0,buy_close_profit=0;
double sell_max_profit=0,sell_close_profit=0;
double total_buy_lots=0,total_sell_lots=0;
double relativeVolume=0;
// Colors:
// #001: eliminate all warnings:
//color c=Black;
int colInPlus=clrGreen;
int colInMinus=clrRed;
int colNeutral=clrGray;
int colFontLight=clrWhite;
int colFontDark=clrGray;
int colCodeGreen=clrGreen;
int colCodeYellow=clrGold;
int colCodeRed=clrRed;
int colPauseButtonPassive=clrBlue;
int panelCol=colNeutral; // fore color of neutral panel text
int instrumentCol=colNeutral; // panel color that changes depending on its value
//
// OrderReliable:
int slippage=0; // is fix; depending on chart: 2 or 20
int retry_attempts= 10;
double sleep_time = 4.0; // in seconds
int sleep_maximum=25; // in seconds
string OrderReliable_Fname="OrderReliable fname unset";
static int _OR_err=0;
string OrderReliableVersion="V1_1_1";
// #023: implement account state by 3 colored button
enum ACCOUNT_STATE{as_green,as_yellow,as_red};
int accountState=as_green;
// #025: use equity percentage instead of unpayable position
double max_equity=0; // maximum of equity ever reached, saved in global vars
double max_float=0; // minimum of equity ever reached, saved in global vars
//
// global flags:
// #019: new button: Stop Next Cyle, which trades normally, until cycle is closed
int stopNextCycle=0; // flag, if trading will be terminated after next successful cycle, trades normally until cyle is closed
int restAndRealize=0; // flag, if trading will be terminated after next successful cycle, does not open new positions
int stopAll=0; // flag, if stopAll must close all and stop trading or continue with trading
//
// #044: Add button to hide comment
int showComment=1; // flag for comment at left side
bool isFirstStartLoop=true; // flag, to do some things only one time after program start
//
// screen coordinates:
// #054: make size of buttons flexible
int btnWidth = 70; // width of smallest buttons
int btnHeight = 30; // height of all buttons
int btnGap= 10; // gap, between buttons
int btnLeftAxis = 200; // distance of button from left screen border
int btnTopAxis = 17; // distance of button from top screen border
int btnNextLeft=btnWidth+btnGap; // distance to next button
int btnNextTop=btnHeight+btnGap; // distance to next button
//
// debugging:
string debugCommentDyn="\n"; // will be added to regular Comment txt and updated each program loop
string debugCommentStat=""; // will be added only - no updates
string debugCommentCloseBuys=""; // show condition, when cycle will be closed
string debugCommentCloseSells="";
string codeRedMsg=""; // tell user, why account state is yellow or red
string codeYellowMsg="";
double ter_IkarusChannel=0; // line_buy - line_sell
string globalVarsID=Symbol()+"_"+(string)magic+"_"; //ID to specify the global vars from other charts
//
// values read from terminal:
double ter_priceBuy=0;
double ter_priceSell=0;
double ter_point=0;
int ter_digits=0;
double ter_tick_value=0;
double ter_tick_size =0;
double ter_spread=0;
datetime ter_timeNow=0; // date and time while actual loop
//
// calculate by values from terminal:
double ter_ticksPerGrid=0; // ticks of 1 min_lot per 1 grid size
int ter_chartMultiplier=1; // if digits = 3 or 5: chart multiplier = 10
string ter_currencySymbol="$"; // € if account is in Euro, $ for all other
double ter_MODE_MARGINHEDGED=MarketInfo(Symbol(),MODE_MARGINHEDGED);
double ter_MODE_MARGININIT=MarketInfo(Symbol(),MODE_MARGININIT);
double ter_MODE_MARGINMAINTENANCE=MarketInfo(Symbol(),MODE_MARGINMAINTENANCE);
double ter_MODE_MARGINREQUIRED=MarketInfo(Symbol(),MODE_MARGINREQUIRED);
// ------------------------------------------------------------------------------------------------
// START
// ------------------------------------------------------------------------------------------------
void OnTick()
{
#ifdef DEMO
// #049: add option to work with demo account only
if(!IsDemo())
{
stopAll=0; // force a dived by zero error, to stop this EA
MessageBox("Only on D E M O account please!","C A U T I O N !",MB_OK);
stopAll=1/stopAll; // divide by zero stops it all
return(0); //just in case ;o)
}
#endif
max_float = MathMin(max_float, AccountProfit());
// do this only one time after starting the program
if(isFirstStartLoop)
{
if(AccountCurrency()=="EUR")
ter_currencySymbol="€";
if(MarketInfo(Symbol(),MODE_DIGITS)==4 || MarketInfo(Symbol(),MODE_DIGITS)==2)
{
slippage=user_slippage;
ter_chartMultiplier=1;
}
else if(MarketInfo(Symbol(),MODE_DIGITS)==5 || MarketInfo(Symbol(),MODE_DIGITS)==3)
{
ter_chartMultiplier=10;
slippage=ter_chartMultiplier*user_slippage;
}
// do we have any data from previous session?
ReadIniData();
debugCommentStat+="\nNew program start at "+TimeToStr(TimeCurrent());
isFirstStartLoop=false;
}
if(IsTradeAllowed()==false)
{
Comment(versionBMI+"\n\nTrade not allowed.");
// #001: eliminate all warnings:
return;
}
ter_priceBuy=MarketInfo(Symbol(),MODE_ASK);
ter_priceSell=MarketInfo(Symbol(),MODE_BID);
ter_tick_value=MarketInfo(Symbol(),MODE_TICKVALUE);
ter_spread=MarketInfo(Symbol(),MODE_SPREAD);
ter_digits=(int)MarketInfo(Symbol(),MODE_DIGITS);
ter_tick_size=MarketInfo(Symbol(),MODE_TICKSIZE);
ter_point=MarketInfo(Symbol(),MODE_POINT);
if(slippage>user_slippage)ter_point=ter_point*10;
ter_timeNow=TimeCurrent();
ter_ticksPerGrid=-CalculateTicksByPrice(min_lots,CalculateSL(min_lots,1))-ter_spread*ter_tick_size;
// #025: use equity percentage instead of unpayable position
if(AccountEquity()>max_equity)
max_equity=AccountEquity();
// Updating current status:
InitVars();
UpdateVars();
SortByLots();
ShowData();
// #014: start new dynamic debug output here; will be shown at the end of comment string; will be updated each program loop
debugCommentDyn="\n";
ShowLines();
// #023: implement account state by 3 colored button
CheckAccountState();
// #010: implement button: Stop & Close
if(stopAll)
{
// Closing all open orders
SetButtonText("btnStopAll","Continue");
SetButtonColor("btnStopAll",colCodeRed,colFontLight);
CloseAllBuys();
CloseAllSells();
}
else
{
Robot();
}
return;
}
//+------------------------------------------------------------------+
//| WrtieIniData() |
// #017: deal with global vars to save and restore data, while chart is closed or must be restarted by other reason
//+------------------------------------------------------------------+
void WriteIniData()
{
// #016: Save status of buttons in global vars
if(!IsTesting())
{
// #010: implement button: Stop & Close
// #011 #018 #019: implement button: Stop On Next Cycle
GlobalVariableSet(globalVarsID+"stopNextCycle",stopNextCycle);
GlobalVariableSet(globalVarsID+"restAndRealize",restAndRealize);
GlobalVariableSet(globalVarsID+"stopAll",stopAll);
GlobalVariableSet(globalVarsID+"showComment",showComment); // #044: Add button to show or hide comment
GlobalVariableSet(globalVarsID+"max_equity",NormalizeDouble(max_equity,2)); // #037: save max equity at global vars
//
//GlobalVariableSet(globalVarsID + "",DoubleToStr());
//GlobalVariableSet(globalVarsID + "",);
}
}
//+------------------------------------------------------------------+
//| ReadIniData() |
// #017: deal with global vars to save and restore data, while chart is closed or must be restarted by other reason
//+------------------------------------------------------------------+
void ReadIniData()
{
// #016: read status of buttons from global vars
if(!IsTesting())
{
int count=GlobalVariablesTotal();
if(count>0)
{
// #011 #018 #019: implement button: Stop On Next Cycle
if(GlobalVariableCheck(globalVarsID+"stopNextCycle"))
stopNextCycle=(int)GlobalVariableGet(globalVarsID+"stopNextCycle");
if(GlobalVariableCheck(globalVarsID+"restAndRealize"))
restAndRealize=(int)GlobalVariableGet(globalVarsID+"restAndRealize");
// #010: implement button: Stop & Close
if(GlobalVariableCheck(globalVarsID+"stopAll"))
stopAll=(int)GlobalVariableGet(globalVarsID+"stopAll");
// #044: Add button to show or hide comment
if(GlobalVariableCheck(globalVarsID+"showComment"))
showComment=(int)GlobalVariableGet(globalVarsID+"showComment");
if(GlobalVariableCheck(globalVarsID+"max_equity")) // #037: save max equity at global vars
max_equity=NormalizeDouble(GlobalVariableGet(globalVarsID+"max_equity"),2);
//if(GlobalVariableCheck(globalVarsID + ""))
// =DoubleToString(GlobalVariableGet(globalVarsID + ""),0);
}
}
}
// ------------------------------------------------------------------------------------------------
// INIT VARS
// ------------------------------------------------------------------------------------------------
void InitVars()
{
// Reset number of buy/sell orders
buys=0;
sells=0;
// Reset arrays
for(int i=0; i<max_open_positions; i++)
{
buy_tickets[i]=0;
buy_lots[i]=0;
buy_profit[i]= 0;
buy_price[i] = 0;
sell_tickets[i]=0;
sell_lots[i]=0;
sell_profit[i]= 0;
sell_price[i] = 0;
}
// #021: new setting: max_open_positions
if(max_positions==0) // if not used, set it to maximum => no restriction
max_positions=max_open_positions;
// #030: disable equity and account risk by setting them to 0
// #025: use equity percentage instead of unpayable position
if(equity_warning==0)
equity_warning=1.0;
if(account_risk==0)
account_risk=1.0;
}
// ------------------------------------------------------------------------------------------------
// UPDATE VARS
// ------------------------------------------------------------------------------------------------
void UpdateVars()
{
int aux_buys=0,aux_sells=0;
double aux_total_buy_profit=0,aux_total_sell_profit=0;
double aux_total_buy_swap=0,aux_total_sell_swap=0;
double aux_total_buy_lots=0,aux_total_sell_lots=0;
// We are going to introduce data from opened orders in arrays
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_BUY)
{
buy_tickets[aux_buys]=OrderTicket();
buy_lots[aux_buys]=OrderLots();
buy_profit[aux_buys]= OrderProfit()+OrderCommission()+OrderSwap();
buy_price[aux_buys] = OrderOpenPrice();
aux_total_buy_profit= aux_total_buy_profit+buy_profit[aux_buys];
aux_total_buy_lots=aux_total_buy_lots+buy_lots[aux_buys];
aux_total_buy_swap+=OrderSwap();
aux_buys++;
}
if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic && OrderType()==OP_SELL)
{
sell_tickets[aux_sells]=OrderTicket();
sell_lots[aux_sells]=OrderLots();
sell_profit[aux_sells]= OrderProfit()+OrderCommission()+OrderSwap();
sell_price[aux_sells] = OrderOpenPrice();
aux_total_sell_profit = aux_total_sell_profit + sell_profit[aux_sells];
aux_total_sell_lots=aux_total_sell_lots+sell_lots[aux_sells];
aux_total_sell_swap+=OrderSwap();
aux_sells++;
}
}
}
// Update global vars
buys=aux_buys;
sells=aux_sells;
total_buy_profit=aux_total_buy_profit;
total_sell_profit=aux_total_sell_profit;
total_buy_lots=aux_total_buy_lots;
total_sell_lots=aux_total_sell_lots;
total_buy_swap=aux_total_buy_swap;
total_sell_swap=aux_total_sell_swap;
relativeVolume=MathAbs(total_buy_lots-total_sell_lots);
}
// ------------------------------------------------------------------------------------------------
// SORT BY LOTS
// ------------------------------------------------------------------------------------------------
void SortByLots()
{
int aux_tickets;
double aux_lots,aux_profit,aux_price;
// We are going to sort orders by volume
// m[0] smallest volume m[size-1] largest volume
// BUY ORDERS
for(int i=0; i<buys-1; i++)
{
for(int j=i+1; j<buys; j++)
{
if(buy_lots[i]>0 && buy_lots[j]>0)
{
// at least 2 orders
if(buy_lots[j]<buy_lots[i])
{
// sorting
// ...lots...
aux_lots=buy_lots[i];
buy_lots[i]=buy_lots[j];
buy_lots[j]=aux_lots;
// ...tickets...
aux_tickets=buy_tickets[i];
buy_tickets[i]=buy_tickets[j];
buy_tickets[j]=aux_tickets;
// ...profits...
aux_profit=buy_profit[i];
buy_profit[i]=buy_profit[j];
buy_profit[j]=aux_profit;
// ...and open price
aux_price=buy_price[i];
buy_price[i]=buy_price[j];
buy_price[j]=aux_price;
}
}
}
}
// SELL ORDERS
for(int i=0; i<sells-1; i++)
{
for(int j=i+1; j<sells; j++)
{
if(sell_lots[i]>0 && sell_lots[j]>0)
{
// at least 2 orders
if(sell_lots[j]<sell_lots[i])
{
// sorting...
// ...lots...
aux_lots=sell_lots[i];
sell_lots[i]=sell_lots[j];
sell_lots[j]=aux_lots;
// ...tickets...
aux_tickets=sell_tickets[i];
sell_tickets[i]=sell_tickets[j];
sell_tickets[j]=aux_tickets;
// ...profits...
aux_profit=sell_profit[i];
sell_profit[i]=sell_profit[j];
sell_profit[j]=aux_profit;
// ...and open price
aux_price=sell_price[i];
sell_price[i]=sell_price[j];
sell_price[j]=aux_price;
}
}
}
}
}
// ------------------------------------------------------------------------------------------------
// SHOW LINES
// ------------------------------------------------------------------------------------------------
void ShowLines()
{
double aux_tp_buy=0,aux_tp_sell=0; // CalculateTP(next positions) = take_profit * pipvalue
double buy_tar=0,sell_tar=0, // local results
diff_tar=0;
double buy_a=0, sell_a=0; // sum: # of total opened min_lots
double buy_b=0, sell_b=0; // sum: price payed for all positions
double buy_pip=0,sell_pip=0; // terminal value: tick_value / min_lots
double buy_v[max_open_positions], // array: # of min_lots of this index; if progression = 0, it is always 1; if prog. = 2 then: 1, 2, 4, 8, ...
sell_v[max_open_positions];
ArrayInitialize(buy_v, 0);
ArrayInitialize(sell_v, 0);
double swapDiff=0; // swap accumulated until actual date
int i;
double myVal=1,offset=0,spreadPart=0,gridSizePart=0;
// init all lines to 0 to make sure they will be removed, if not more active
line_buy=0;line_buy_tmp=0;line_buy_next=0;line_buy_ts=0;
line_sell=0;line_sell_tmp=0;line_sell_next=0;line_sell_ts=0;line_margincall=0;
if(buys<=1) aux_tp_buy=CalculateTP(buy_lots[0]);
else if(progression==0) aux_tp_buy = CalculateTP(buy_lots[0]);
else if(progression==1) aux_tp_buy = buys*CalculateTP(buy_lots[0]);
else if(progression==2) aux_tp_buy = CalculateTP(buy_lots[buys-1]);
else if(progression==3) aux_tp_buy = CalculateTP(buy_lots[buys-1]);
if(sells<=1) aux_tp_sell=CalculateTP(sell_lots[0]);
else if(progression==0) aux_tp_sell = CalculateTP(sell_lots[0]);
else if(progression==1) aux_tp_sell = sells*CalculateTP(sell_lots[0]);
else if(progression==2) aux_tp_sell = CalculateTP(sell_lots[sells-1]);
else if(progression==3) aux_tp_sell = CalculateTP(sell_lots[sells-1]);
double tp_buy=aux_tp_buy;
double tp_sell=aux_tp_sell;
//Print("progression = "+progression);
//Print(StringConcatenate("aux_tp_buy = ",aux_tp_buy));
if(buys>=1)
{
buy_pip=CalculatePipValue(buy_lots[0]);
for(i=0;i<max_open_positions;i++) buy_v[i]=0;
for(i=0;i<buys;i++)
{
buy_v[i]=MathRound(buy_lots[i]/buy_lots[0]);
//Print(StringConcatenate("buy_v[",i,"] = ",buy_v[i]));
}
for(i=0;i<buys;i++)
{
buy_a = buy_a + buy_v[i];
buy_b = buy_b + buy_price[i]*buy_v[i];
}
//Print(StringConcatenate("pip = ",buy_pip));
//Print(StringConcatenate("buy_a = ",buy_a));
//Print(StringConcatenate("buy_b = ",buy_b));
buy_tar=aux_tp_buy/(buy_pip/ter_point);
//Print(StringConcatenate("buy_tar 1: ",buy_tar));
buy_tar=buy_tar+buy_b;
//Print(StringConcatenate("buy_tar 2: ",buy_tar));
buy_tar=buy_tar/buy_a;
//Print(StringConcatenate("RESULT BUY: ",buy_tar));
swapDiff=MathAbs(CalculateTicksByPrice(total_buy_lots,total_buy_swap));
line_buy=buy_tar+swapDiff;
HorizontalLine(line_buy,"TakeProfit_buy",DodgerBlue,STYLE_SOLID,2);
//debugCommentDyn+="\nline_buy: "+DoubleToString(line_buy,3);
ter_IkarusChannel=buy_tar/ter_tick_size; // ter_IkarusChannel=line_buy - line_sell
//
// calculate trailing stop line
if(buy_close_profit>0)
{
buy_tar = buy_close_profit/(buy_pip/ter_point);
buy_tar = buy_tar + buy_b;
line_buy_ts=buy_tar/buy_a;
HorizontalLine(line_buy_ts,"ProfitLock_buy",DodgerBlue,STYLE_DASH,1);
}
// #027: extern option to hide forecast lines
// #029: hide forecast lines, if trailing stop is active
if(show_forecast && line_buy_ts==0)
{
// #022: show next line_buy/line_sell
// #045: Fine tuning lines buy/sell next based on profit instead of grid_size
if(gs_progression==0) line_buy_next=buy_price[buys-1]-ter_ticksPerGrid;
else if(gs_progression==1) line_buy_next=buy_price[buys-1]-buys*ter_ticksPerGrid;
else if(gs_progression==2) line_buy_next=buy_price[buys-1]+CalculateTicksByPrice(buy_lots[buys-1],CalculateSL(buy_lots[buys-1],buys));
else if(gs_progression==3) line_buy_next=buy_price[buys-1]+CalculateTicksByPrice(buy_lots[buys-1],CalculateSL(buy_lots[buys-1],buys));
HorizontalLine(line_buy_next,"Next_buy",DodgerBlue,STYLE_DASHDOT,1);
// #020: show line, where the next line_buy /line_sell would be, if it would be opened right now
if(accountState!=as_green && total_buy_profit<0)
{
myVal=MathRound(buy_lots[buys-1]/buy_lots[0]);
buy_a+=myVal;
buy_b=(buy_b+ter_priceSell*myVal);
buy_tar=aux_tp_buy/(buy_pip/ter_point);
line_buy_tmp=(buy_tar+buy_b)/buy_a+swapDiff;
if(line_buy_tmp>0)
HorizontalLine(line_buy_tmp,"NewTakeProfit_buy",clrDarkViolet,STYLE_DASHDOTDOT,1);
}
}
}
if(sells>=1)
{
sell_pip=CalculatePipValue(sell_lots[0]);
for(i=0;i<max_open_positions;i++) sell_v[i]=0;
for(i=0;i<sells;i++)
{
sell_v[i]=MathRound(sell_lots[i]/sell_lots[0]);
}
for(i=0;i<sells;i++)
{
sell_a = sell_a + sell_v[i];
sell_b = sell_b + sell_price[i]*sell_v[i];
}
sell_tar = -1*(aux_tp_sell/(sell_pip/ter_point));
sell_tar = sell_tar + sell_b;
sell_tar = sell_tar/sell_a;
swapDiff = MathAbs(CalculateTicksByPrice(total_sell_lots,total_sell_swap));
line_sell=sell_tar-swapDiff;
HorizontalLine(line_sell,"TakeProfit_sell",Tomato,STYLE_SOLID,2);
ter_IkarusChannel-=sell_tar/ter_tick_size; // ter_IkarusChannel=line_buy - line_sell
if(buys>0 && sells>0) // only valid, if both direction have positions
ter_IkarusChannel=MathAbs(ter_IkarusChannel);
else
ter_IkarusChannel=0;
// calculate trailing stop line
if(sell_close_profit>0)
{
sell_tar = -1*(sell_close_profit/(sell_pip/ter_point));
sell_tar = sell_tar + sell_b;
line_sell_ts=sell_tar/sell_a;
HorizontalLine(line_sell_ts,"ProfitLock_sell",Tomato,STYLE_DASH,1);
}
// #027: extern option to hide forecast lines
// #029: hide forecast lines, if trailing stop is active
if(show_forecast && line_sell_ts==0)
{
// #022: show next line_buy/line_sell
// line_sell_next=sell_price[sells-1]+CalculateVolume(sells)/min_lots*ter_ticksPerGrid;
// #045: Fine tuning lines buy/sell next based on profit instead of grid_size
if(gs_progression==0) line_sell_next=sell_price[sells-1]+ter_ticksPerGrid;
else if(gs_progression==1) line_sell_next=sell_price[sells-1]+sells*ter_ticksPerGrid;
else if(gs_progression==2) line_sell_next=sell_price[sells-1]-CalculateTicksByPrice(sell_lots[sells-1],CalculateSL(sell_lots[sells-1],sells));
else if(gs_progression==3) line_sell_next=sell_price[sells-1]-CalculateTicksByPrice(sell_lots[sells-1],CalculateSL(sell_lots[sells-1],sells));
HorizontalLine(line_sell_next,"Next_sell",Tomato,STYLE_DASHDOT,1);
// #020: show line, where the next line_buy /line_sell would be, if it would be opened at the actual price
if(accountState!=as_green && total_sell_profit<0)
{
myVal=MathRound(sell_lots[sells-1]/sell_lots[0]);
sell_a+=myVal;
sell_b=(sell_b+ter_priceBuy*myVal);
sell_tar=-1*(aux_tp_sell/(sell_pip/ter_point));
line_sell_tmp=(sell_b-(aux_tp_sell/(sell_pip/ter_point)))/sell_a-swapDiff;
if(line_sell_tmp>0)
HorizontalLine(line_sell_tmp,"NewTakeProfit_sell",clrDarkViolet,STYLE_DASHDOTDOT,1);
}
}
}
// #036: new line: margin call (free margin = 0)
// #039: fixing bug that Stop&Close buttons works only once: divide by zero, if total_buy/sell_lots = 0
line_margincall=0;
if(show_forecast && (accountState==as_yellow || accountState==as_red))
{
double freeMargin=AccountFreeMargin();
double maxLoss=freeMargin/ter_tick_value*ter_tick_size;
//debugCommentDyn+="\nmaxLoss: "+DoubleToString(maxLoss,3);
if(total_buy_profit<total_sell_profit) // calculate line_margincall for worse profit
{
// formular to transfer an account price to chart diff:
// profit (€) = tick_value * lot_size * chart diff (in ticks)
// 30€ = 0,76 * 0.08 Lot * 500 (0,500) for USDJPY
if(total_buy_lots>0)
line_margincall=ter_priceBuy-maxLoss/total_buy_lots;
//debugCommentDyn+="\nline_margincall buys: "+DoubleToString(line_margincall,3);
if(line_margincall>0)
HorizontalLine(line_margincall,"MarginCall",clrSilver,STYLE_SOLID,5);
}
else
{
if(total_sell_lots>0)
line_margincall=ter_priceSell+maxLoss/total_sell_lots;
//debugCommentDyn+="\nline_margincall sells: "+DoubleToString(line_margincall,3);
if(maxLoss<ter_priceSell)
HorizontalLine(line_margincall,"MarginCall",clrSilver,STYLE_SOLID,5);
}
}
// #028: make sure, all unused lines (value=0) will be hidden
if(line_buy==0)
ObjectDelete("TakeProfit_buy");
if(line_buy_next==0)
ObjectDelete("Next_buy");
if(line_buy_tmp==0)
ObjectDelete("NewTakeProfit_buy");
if(line_buy_ts==0)
ObjectDelete("ProfitLock_buy");
if(line_sell==0)
ObjectDelete("TakeProfit_sell");
if(line_sell_next==0)
ObjectDelete("Next_sell");
if(line_sell_tmp==0)
ObjectDelete("NewTakeProfit_sell");
if(line_sell_ts==0)
ObjectDelete("ProfitLock_sell");
if(line_margincall==0)
ObjectDelete("MarginCall");
}
// ------------------------------------------------------------------------------------------------
// SHOW DATA
// ------------------------------------------------------------------------------------------------
void ShowData()
{
string txt;
double aux_tp_buy=0,aux_tp_sell=0;
// #002: correct message of fibo progression
string info_money_management[4];
string info_activation[2];
info_money_management[0]="min_lots";
info_money_management[1]="D´Alembert";
info_money_management[2]="Martingale";
info_money_management[3]="Fibonacci";
info_activation[0]="Disabled";
info_activation[1]="Enabled";
if(buys<=1) aux_tp_buy=CalculateTP(buy_lots[0]);
else if(progression==0) aux_tp_buy = CalculateTP(buy_lots[0]);
else if(progression==1) aux_tp_buy = buys*CalculateTP(buy_lots[0]);
else if(progression==2) aux_tp_buy = CalculateTP(buy_lots[buys-1]);
else if(progression==3) aux_tp_buy = CalculateTP(buy_lots[buys-1]);
if(sells<=1) aux_tp_sell=CalculateTP(sell_lots[0]);
else if(progression==0) aux_tp_sell = CalculateTP(sell_lots[0]);
else if(progression==1) aux_tp_sell = sells*CalculateTP(sell_lots[0]);
else if(progression==2) aux_tp_sell = CalculateTP(sell_lots[sells-1]);
else if(progression==3) aux_tp_sell = CalculateTP(sell_lots[sells-1]);
// #008: use progression for grid size as well as volume
string info_gs_progression;
if(gs_progression==0)
info_gs_progression="\nGS progression: Disabled";
else
info_gs_progression="\nGS progression: "+info_money_management[gs_progression];
// #051: change info of panel and comment
txt="\n"+versionBMI+
"\n"+versionOld+
"\nServer Time: "+TimeToStr(ter_timeNow,TIME_DATE|TIME_SECONDS)+
"\n"+
"\nBUY ORDERS"+
"\nNumber of orders: "+(string)buys+
"\nTotal lots: "+DoubleToStr(total_buy_lots,2)+
"\nCurrent profit: "+DoubleToStr(total_buy_profit,2)+
"\nProfit goal: "+ter_currencySymbol+DoubleToStr(aux_tp_buy,2)+
"\nMaximum profit reached: "+ter_currencySymbol+DoubleToStr(buy_max_profit,2)+
"\nProfit locked: "+ter_currencySymbol+DoubleToStr(buy_close_profit,2)+
"\n"+
"\nSELL ORDERS"+
"\nNumber of orders: "+(string)sells+
"\nTotal lots: "+DoubleToStr(total_sell_lots,2)+
"\nCurrent profit: "+DoubleToStr(total_sell_profit,2)+
"\nProfit goal: "+ter_currencySymbol+DoubleToStr(aux_tp_sell,2)+
"\nMaximum profit reached: "+ter_currencySymbol+DoubleToStr(sell_max_profit,2)+
"\nProfit locked: "+ter_currencySymbol+DoubleToStr(sell_close_profit,2)+"\n";
if(line_margincall>0)
txt+="\nLine: \"margin call\": "+DoubleToString(line_margincall,3);
// #038: give user info, why account state is yellow or red
if(codeYellowMsg!="")
txt+="\n"+codeYellowMsg;
if(codeRedMsg!="")
txt+="\n"+codeRedMsg;
txt+=
"\n\nMax. Equity: "+DoubleToString(max_equity,2)+" "+ter_currencySymbol+
"\nCurrent drawdown: "+DoubleToString((max_equity-AccountEquity()),2)+" "+ter_currencySymbol+" ("+DoubleToString((max_equity-AccountEquity())/max_equity*100,2)+" %)"+
"\nMax. drawdown: "+DoubleToString(max_float,2)+" "+ter_currencySymbol+
"\n\nSETTINGS: "+
"\nGrid size: "+(string)grid_size+
info_gs_progression+
"\nTake profit: "+(string)take_profit+
"\nProfit locked: "+DoubleToStr(100*profit_lock,2)+"%"+
"\nMinimum lots: "+DoubleToStr(min_lots,2)+
"\nEquity warning: "+DoubleToStr(100*equity_warning,2)+"%"+
"\nAccount risk: "+DoubleToStr(100*account_risk,2)+"%"+
"\nProgression: "+info_money_management[progression]+
"\nMax Positions: "+(string)max_positions+
"\nUnbalance control: "+info_activation[unbalance_control]+
// #004 new setting: max_spread; trades only, if spread <= max spread:
"\nMax Spread: "+(string)max_spread+" pts; actual spread: "+(string)MarketInfo(Symbol(),MODE_SPREAD)+" pts"+
// #027: extern option to hide forecast lines
"\nShow Forecast: "+info_activation[show_forecast]+
"\nID for GlobalVariables: "+globalVarsID+"\n";
ObjectSetInteger(0,"btnShowComment",OBJPROP_STATE,0); // switch color back to not selected
if(showComment)
{
// #050: show/hide buttons together with comment
if(ObjectFind(0,"btnManualBuy")==-1)
{
DrawButton("btnManualBuy","Buy",btnLeftAxis,btnTopAxis,btnWidth,btnHeight,false,colNeutral,clrBlack);
DrawButton("btnManualSell","Sell",btnLeftAxis+btnNextLeft,btnTopAxis,btnWidth,btnHeight,false,colNeutral,clrBlack);
DrawButton("btnCloseLastBuy","Cl. Last B",btnLeftAxis,btnTopAxis+btnNextTop,btnWidth,btnHeight,false,colNeutral,clrBlack);
DrawButton("btnCloseLastSell","Cl. Last S",btnLeftAxis+btnNextLeft,btnTopAxis+btnNextTop,btnWidth,btnHeight,false,colNeutral,clrBlack);
DrawButton("btnCloseAllBuys","Cl. All Bs",btnLeftAxis,btnTopAxis+2*btnNextTop,btnWidth,btnHeight,false,colNeutral,clrBlack);
DrawButton("btnCloseAllSells","Cl. All Ss",btnLeftAxis+btnNextLeft,btnTopAxis+2*btnNextTop,btnWidth,btnHeight,false,colNeutral,clrBlack);
DrawButton("btnShowComment","Show/Hide Comment",5,btnTopAxis,btnWidth*2,btnHeight,false,colNeutral,colCodeYellow);
DrawButton("btnstopNextCycle","Stop Next Cycle",btnLeftAxis+2*btnNextLeft,btnTopAxis,(int)(btnWidth*1.5),btnHeight,false,colNeutral,clrBlack);
DrawButton("btnrestAndRealize","Rest & Realize",btnLeftAxis+2*btnNextLeft,btnTopAxis+btnNextTop,(int)(btnWidth*1.5),btnHeight,false,colNeutral,clrBlack);
DrawButton("btnStopAll","Stop & Close",btnLeftAxis+2*btnNextLeft,btnTopAxis+2*btnNextTop,(int)(btnWidth*1.5),btnHeight,false,colNeutral,clrBlack);
}
// set state off all buttons to: Not Selected
ObjectSetInteger(0,"btnManualBuy",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnManualSell",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnCloseLastBuy",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnCloseLastSell",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnCloseAllBuys",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnCloseAllSells",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnstopNextCycle",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnrestAndRealize",OBJPROP_STATE,0); // switch color back to not selected
ObjectSetInteger(0,"btnStopAll",OBJPROP_STATE,0); // switch color back to not selected
//
// #019: implement button: Stop On Next Cycle
if(stopNextCycle)
{
SetButtonText("btnstopNextCycle","Continue");
// set color to red, if everything is closed
if(sells+buys==0)
SetButtonColor("btnstopNextCycle",colCodeRed,colFontLight);
else
SetButtonColor("btnstopNextCycle",colCodeYellow,colFontDark);
}
else
{
SetButtonText("btnstopNextCycle","Stop Next Cycle");
SetButtonColor("btnstopNextCycle",colPauseButtonPassive,colFontLight);
}
// #011 #018: implement button: Stop On Next Cycle
if(restAndRealize)
{
SetButtonText("btnrestAndRealize","Continue");
if(sells+buys==0)
SetButtonColor("btnrestAndRealize",colCodeRed,colFontLight);
else
SetButtonColor("btnrestAndRealize",colCodeYellow,colFontDark);
}
else
{
SetButtonText("btnrestAndRealize","Rest & Realize");
SetButtonColor("btnrestAndRealize",colPauseButtonPassive,colFontLight);
}
// #010: implement button: Stop & Close
if(stopAll)
{
SetButtonText("btnStopAll","Continue");
SetButtonColor("btnStopAll",colCodeRed,colFontLight);
}
else
{
SetButtonText("btnStopAll","Stop & Close");
SetButtonColor("btnStopAll",colPauseButtonPassive,colFontLight);
}
}
else
{
DeleteButton("btnStopAll");
DeleteButton("btnrestAndRealize");
DeleteButton("btnstopNextCycle");
DeleteButton("btnManualBuy");
DeleteButton("btnManualSell");
DeleteButton("btnCloseLastBuy");
DeleteButton("btnCloseLastSell");
DeleteButton("btnCloseAllBuys");
DeleteButton("btnCloseAllSells");
}
#ifdef DEBUG
// #014: debug out at the end of comment string; will be updated each program loop
debugCommentDyn+=
"Ikarus Channel: "+DoubleToStr(ter_IkarusChannel,0)+
"\n"+
//"\nPrice Buy: "+DoubleToStr(ter_priceBuy,ter_digits)+
//"\nPrice Sell: "+DoubleToStr(ter_priceSell,ter_digits)+
"\nTick Value: "+DoubleToStr(ter_tick_value,ter_digits)+
"\nTick Size: "+DoubleToStr(ter_tick_size,ter_digits)+
"\nPoint: "+DoubleToStr(ter_point,ter_digits)+
"\nDigits: "+ter_digits+
"\n"+
"\nTicks/Grid Size: "+DoubleToStr(ter_ticksPerGrid,ter_digits)+
"\nRelative Volume: "+DoubleToString(relativeVolume,2)+" Lot"+
"\nAct. Price/Tick: "+DoubleToString(CalculatePriceByTickDiff(relativeVolume,ter_tick_size),ter_digits)+ter_currencySymbol+
"\nAct. Ticks/1,- "+ter_currencySymbol+": "+DoubleToString(CalculateTicksByPrice(relativeVolume,1),ter_digits)+
"\n"+
debugCommentCloseBuys+
debugCommentCloseSells+
//"\n"+
//"\n\nMargins for 1 Lot:"
//"\nMODE_MARGINHEDGED: "+DoubleToString(MarketInfo(Symbol(),MODE_MARGINHEDGED)/AccountLeverage(),2)+
//"\nMODE_MARGININIT: "+DoubleToString(MarketInfo(Symbol(),MODE_MARGININIT)/AccountLeverage(),2)+
//"\nMODE_MARGINMAINTENANCE: "+DoubleToString(MarketInfo(Symbol(),MODE_MARGINMAINTENANCE)/AccountLeverage(),2)+
//"\nMODE_MARGINREQUIRED: "+DoubleToString(MarketInfo(Symbol(),MODE_MARGINREQUIRED),2)+
//"\nAccountLeverage: "+DoubleToString(AccountLeverage(),2)+
//"\nBalance: "+DoubleToString(AccountBalance(),2)+
//"\nEquity: "+DoubleToString(AccountEquity(),2)+
//"\nMargin: "+DoubleToString(AccountMargin(),5)+
//"\nFree Margin: "+DoubleToString(AccountFreeMargin(),2)+
//"\nNext Margin: "+DoubleToString(CalculateNextMargin(),2)+
//"\n"+
//"\nLineBuyTmp: "+DoubleToString(line_buy_tmp,ter_digits)+
//"\nLineSellTmp: "+DoubleToString(line_sell_tmp,ter_digits)+
//"\nLineBuyNext: "+DoubleToString(line_buy_next,ter_digits)+
//"\nLineSellNext: "+DoubleToString(line_sell_next,ter_digits)+
//"\nLineBuyTS: "+DoubleToString(line_buy_ts,ter_digits)+
//"\nLineSellTS: "+DoubleToString(line_sell_ts,ter_digits)+
"";
//"\n: "++
//"\n: "+DoubleToString(,2)+
if(showComment)
Comment("\n\n"+txt+"\n\nDebug info (switch of in code):"+debugCommentStat+debugCommentDyn);
else
Comment("");
#else
if(showComment)
Comment("\n\n"+txt);
else
Comment("");
#endif
// #047: add panel right upper corner
if(show_forecast)
{
if(total_buy_profit+total_sell_profit>0)
instrumentCol=colInPlus;
else
instrumentCol=colInMinus;
Write("panel_1_01",ChartSymbol(0),5,22,"Arial",14,instrumentCol);
if(ter_spread>max_spread)
Write("panel_1_02","Spread: "+DoubleToString(ter_spread/10,1),5,42,"Arial",10,colCodeRed);
else
Write("panel_1_02","Spread: "+DoubleToString(ter_spread/10,1),5,42,"Arial",10,panelCol);
Write("panel_1_03",DoubleToString(CalculatePriceByTickDiff(relativeVolume,ter_tick_size*10),2)+" "+ter_currencySymbol+" / Pip",5,58,"Arial",10,panelCol);
Write("panel_1_04","Balance: "+DoubleToString(AccountBalance(),2)+" "+ter_currencySymbol,5,74,"Arial",10,panelCol);
Write("panel_1_05","Equity: "+DoubleToString(AccountEquity(),2)+" "+ter_currencySymbol,5,90,"Arial",10,panelCol);
Write("panel_1_06","Free Margin: "+DoubleToString(AccountFreeMargin(),2)+" "+ter_currencySymbol,5,106,"Arial",10,panelCol);
Write("panel_1_07","P/L Sym. "+DoubleToString(total_buy_profit+total_sell_profit,2)+" "+ter_currencySymbol,5,122,"Arial",14,instrumentCol);
if(total_buy_profit<0)
Write("panel_1_08","P/L Buy: "+DoubleToStr(total_buy_profit,2)+" "+ter_currencySymbol,5,144,"Arial",10,colInMinus);
else
Write("panel_1_08","P/L Buy: "+DoubleToStr(total_buy_profit,2)+" "+ter_currencySymbol,5,144,"Arial",10,colInPlus);
if(total_sell_profit<0)
Write("panel_1_09","P/L sell: "+DoubleToStr(total_sell_profit,2)+" "+ter_currencySymbol,5,160,"Arial",10,colInMinus);
else
Write("panel_1_09","P/L sell: "+DoubleToStr(total_sell_profit,2)+" "+ter_currencySymbol,5,160,"Arial",10,colInPlus);
double accountPL=AccountProfit();
if(accountPL<0)
Write("panel_1_10","P/L Acc. "+DoubleToString(accountPL,2)+" "+ter_currencySymbol,5,176,"Arial",10,colInMinus);
else
Write("panel_1_10","P/L Acc. "+DoubleToString(accountPL,2)+" "+ter_currencySymbol,5,176,"Arial",10,colInPlus);
//double pips2go_Buys = (line_buy-ter_priceSell)*ter_tick_size;
double pips2go_Buys = MathAbs(line_buy/ter_tick_size-ter_priceSell/ter_tick_size)/10;
double pips2go_Sells = MathAbs(line_sell/ter_tick_size-ter_priceBuy/ter_tick_size)/10;
Write("panel_1_11","Pips2Go B "+DoubleToString(pips2go_Buys,0)+" S "+DoubleToStr(pips2go_Sells,0),5,192,"Arial",10,panelCol);
}
}
//+------------------------------------------------------------------+
//| CheckAccountState
//+------------------------------------------------------------------+
void CheckAccountState()
{
accountState=as_green; // init state
codeYellowMsg=""; // #038: give user info, why account state is yellow or red
codeRedMsg="";
double myPercentage=0;
// check if max_positions is reached:
if(buys>=max_positions)
accountState=as_yellow;
if(sells>=max_positions)
accountState=as_yellow;
if(accountState==as_yellow)
codeYellowMsg="Code YELLOW: Max positions reached";
// #024: calculate, if margin of next Ikarus position can be paid
if(CalculateNextMargin()>AccountFreeMargin())
{
accountState=as_red;
codeRedMsg="Code RED: Next M. "+DoubleToString(CalculateNextMargin(),2)+" > Free M. "+DoubleToString(AccountFreeMargin(),2);
}
// #025: use equity percentage instead of unpayable position
if((100-(100*equity_warning))/100*max_equity>AccountEquity())
{
accountState=as_red;
if(codeRedMsg=="")
codeRedMsg="Code RED: Equ. "+DoubleToStr(AccountEquity(),2)+" < "+DoubleToStr((100*equity_warning),0)+"% of max. equ. "+DoubleToStr(max_equity,2);