Download Sure Fire Hedging Strategy EA with Raw Code
Forex Indicator
14,568 views
DOWNLOAD SUREFIRE HEDGING STRATEGY
Every Trader who are related with Forex Market want to find a profitable EA. Sure Fire Hedging Strategy is one of the profitable expert adviser. By use sure fire hedging strategy EA you can feel free to trade and get lot of money. So why not download this hedging strategies.
You may know details more which we write long time age. just click and read.
HERE IS THE Hedging Forex EA CODE :
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 | //+------------------------------------------------------------------+ //| __SureForexHedging__1_0_0.mq4 | //| KrT group | //| www.pipschart.com | //| info@pipschart.com | //| www.pipschart.com/forex | //| Modified & Collected from different sources | //+------------------------------------------------------------------+ #property copyright "KrT group" #property link "info@pipschart.com" #include <stderror.mqh> #define sec_wait_context 10 #define max_errors 30 #define wait_error 1000 #define busysleep 100 #define EB_NEXTTRY 0 #define EB_WAITTRY 1 #define EB_TERMINATE 2 #define StoplevelAdd 0 extern int initial_deal = 0 ; // Direction of initial deal: 0 - buy deal, 1 - sell deal, other values - new opening disabled extern double stoploss = 60 ; // Stoploss for all deals extern double takeprofit = 30 ; // takeprofit for all deals extern double fixed_lot = 0.1 ; // If > 0, the initial deal will be opened with this lot extern double deposit_lot = 0 ; // If fixed_lot is 0 and deposit_lot > 0, initial deal will be opened with volume Balance / deposit_lot lots extern double martin_koeff1 = 3 ; // Lot multiplier for first hedging deal extern double martin_koeff2 = 2 ; // Lot multiplier for second, third and further hedging deals extern int enable_daily_profit = 0 ; // Enable option to stop trading if a daily profit exceeds following values (it considers profit for ALL currencies) extern double daily_profit = 0 ; // Daily profit in the account currency extern double daily_profit_percent = 0 ; // Daily profit as a percent to Balance of start of the day extern double distance0 = 30 ; // If > 0, distance for placing 1-st hedging deal extern double distance1 = 0 ; // If > 0, distance for placing 2-nd hedging deal extern double distance2 = 0 ; // If > 0, distance for placing 3-rd hedging deal extern double distance3 = 0 ; // If > 0, distance for placing 4-th hedging deal extern double distance4 = 0 ; // If > 0, distance for placing 5-th hedging deal extern double distance5 = 0 ; // If > 0, distance for placing 6-th hedging deal extern double distance6 = 0 ; // If > 0, distance for placing 7-th hedging deal extern double distance7 = 0 ; // If > 0, distance for placing 8-th hedging deal extern double distance8 = 0 ; // If > 0, distance for placing 9-th hedging deal extern double distance9 = 0 ; // If > 0, distance for placing 10-th hedging deal extern double normal_spread = 0 ; // This value in pips is substracted from SELL takeprofit and added to SELL stoploss to consider the spread extern string sep0 = " common EA settings " ; extern int params_digits = 4 ; // If = 4, then stoploss, takeprofit and distances are specified in 4-digit points, if = 5, then in 5-digit pips extern int initial_magic = 123000 ; // Initial magic number for 1-st deal. Hedging deals add 1 to this number extern int market_execution = 0 ; // Set to 1 for ECN and STP accounts (stoploss and takeprofit are modified only after an order is sent) extern int command_attempts = 5 ; // Number of simultaneous attempts of sending broker requests (if some error occurs) extern int max_slippage = 10 ; // Maximum slippage int go = true ; double point, d_stoplevel, d_freezelevel, d_buystop, d_sellstop, d_buytake, d_selltake ; int errors = 0 ; int lotsdigits ; double dist [10] ; int init() { if (takeprofit <= 0) { go = false ; return ; } if (distance0 <= 0) { distance0 = takeprofit ; } point = MathPow (0.1, params_digits) ; d_freezelevel = MarketInfo (Symbol (), MODE_FREEZELEVEL) * Point ; d_buystop = NormalizeDouble (stoploss * point, Digits) ; d_selltake = NormalizeDouble ((takeprofit - normal_spread) * point, Digits) ; d_sellstop = NormalizeDouble ((stoploss + normal_spread) * point, Digits) ; d_buytake = NormalizeDouble (takeprofit * point, Digits) ; dist [0] = NormalizeDouble (distance0 * point, Digits) ; if (distance1 > 0) { dist [1] = NormalizeDouble (distance1 * point, Digits) ; } else { dist [1] = NormalizeDouble (distance0 * point, Digits) ; } if (distance2 > 0) { dist [2] = NormalizeDouble (distance2 * point, Digits) ; } else { dist [2] = NormalizeDouble (distance0 * point, Digits) ; } if (distance3 > 0) { dist [3] = NormalizeDouble (distance3 * point, Digits) ; } else { dist [3] = NormalizeDouble (distance0 * point, Digits) ; } if (distance4 > 0) { dist [4] = NormalizeDouble (distance4 * point, Digits) ; } else { dist [4] = NormalizeDouble (distance0 * point, Digits) ; } if (distance5 > 0) { dist [5] = NormalizeDouble (distance5 * point, Digits) ; } else { dist [5] = NormalizeDouble (distance0 * point, Digits) ; } if (distance6 > 0) { dist [6] = NormalizeDouble (distance6 * point, Digits) ; } else { dist [6] = NormalizeDouble (distance0 * point, Digits) ; } if (distance7 > 0) { dist [7] = NormalizeDouble (distance7 * point, Digits) ; } else { dist [7] = NormalizeDouble (distance0 * point, Digits) ; } if (distance8 > 0) { dist [8] = NormalizeDouble (distance8 * point, Digits) ; } else { dist [8] = NormalizeDouble (distance0 * point, Digits) ; } if (distance9 > 0) { dist [9] = NormalizeDouble (distance9 * point, Digits) ; } else { dist [9] = NormalizeDouble (distance0 * point, Digits) ; } lotsdigits = 0 ; if (MarketInfo (Symbol (), MODE_LOTSTEP) == 0.1) { lotsdigits = 1 ; } if (MarketInfo (Symbol (), MODE_LOTSTEP) == 0.01) { lotsdigits = 2 ; } if (MarketInfo (Symbol (), MODE_LOTSTEP) == 0.001) { lotsdigits = 3 ; } } int start() { if (! go) { return ; } if (errors >= max_errors) { Comment ( "Exceeded maximum count of rejects from broker" + "\n" + "See experts tab for the information" + "\n" + "Expert should be restarted to continue") ; return ; } if (_IsNotTradeContextBusy () < 0) { return ; } if (! IsTradeAllowed ()) { Comment ("Trade by experts is prohibited") ; return ; } if (! IsConnected () && ! IsTesting () && ! IsOptimization ()) { Comment ("No connection with the trade server") ; return ; }/* if (stoplevel_is_spread) { d_stoplevel = NormalizeDouble (Ask - Bid + StoplevelAdd * point, Digits) ; } else*/ { d_stoplevel = NormalizeDouble (MarketInfo (Symbol (), MODE_STOPLEVEL) * Point + StoplevelAdd * point, Digits) ; } for (int i = OrdersTotal () - 1; i >= 0; i --) { if (OrderSelect (i, SELECT_BY_POS) && OrderSymbol () == Symbol () && OrderMagicNumber () >= initial_magic && OrderMagicNumber () <= initial_magic + 100) { switch (OrderType ()) { case OP_BUYSTOP: case OP_SELLSTOP: int tick = OrderTicket () ; if (OrderSelect (StrToDouble (OrderComment ()), SELECT_BY_TICKET) && OrderCloseTime () > 0) { PendingRemove (tick) ; } break ; } } } if (MyOrders () == 0 && ! ProfitReached ()) // Open a new { switch (LastProfitDir ()) { case 0: MarketSend (OP_BUY, Lots (), d_buystop, d_buytake, initial_magic) ; break ; case 1: MarketSend (OP_SELL, Lots (), d_sellstop, d_selltake, initial_magic) ; } } for (i = OrdersTotal () - 1; i >= 0; i --) { if (OrderSelect (i, SELECT_BY_POS) && OrderSymbol () == Symbol () && OrderMagicNumber () >= initial_magic && OrderMagicNumber () <= initial_magic + 100) { switch (OrderType ()) { case OP_BUY: if (! HedgeIsSet (OrderMagicNumber () + 1)) { PendingSend (OP_SELLSTOP, Lots (OrderMagicNumber () - initial_magic + 1), OrderOpenPrice () - dist [(OrderMagicNumber () - initial_magic) % 10], d_sellstop, d_selltake, OrderMagicNumber () + 1, DoubleToStr (OrderTicket (), 0)) ; } break ; case OP_SELL: if (! HedgeIsSet (OrderMagicNumber () + 1)) { PendingSend (OP_BUYSTOP, Lots (OrderMagicNumber () - initial_magic + 1), OrderOpenPrice () + dist [(OrderMagicNumber () - initial_magic) % 10], d_buystop, d_buytake, OrderMagicNumber () + 1, DoubleToStr (OrderTicket (), 0)) ; } break ; } } } } int ErrorBlock (int err, int ms) { string str = "" ; int res = EB_TERMINATE ; switch (err) { case ERR_NO_ERROR: str = "No error" ; break ; case ERR_NO_RESULT: str = "Unknown result" ; break ; case ERR_COMMON_ERROR: str = "Common error. Probably the signal was rejected by user or trade direction is denied in expert settings" ; break ; case ERR_INVALID_TRADE_PARAMETERS: str = "Wrong trade function parameters" ; break ; case ERR_SERVER_BUSY: str = "Server is busy. Waiting..." ; res = EB_WAITTRY ; break ; case ERR_OLD_VERSION: str = "Old version of client terminal" ; break ; case ERR_NO_CONNECTION: str = "No connection with trade server. Waiting..." ; res = EB_WAITTRY ; break ; case ERR_NOT_ENOUGH_RIGHTS: str = "Not enough rights" ; break ; case ERR_TOO_MANY_REQUESTS: case ERR_TOO_FREQUENT_REQUESTS: str = "To many requests" ; break ; case ERR_MALFUNCTIONAL_TRADE: str = "Malfunctional trade" ; break ; case ERR_ACCOUNT_DISABLED: str = "The account is blocked" ; break ; case ERR_INVALID_ACCOUNT: str = "Invalid account" ; break ; case ERR_TRADE_TIMEOUT: str = "Trade timeout exceeded" ; res = EB_NEXTTRY ; break ; case ERR_INVALID_PRICE: str = "Wrong price" ; res = EB_NEXTTRY ; break ; case ERR_INVALID_STOPS: str = "Invalid stops" ; break ; case ERR_INVALID_TRADE_VOLUME: str = "Invalid trade volume" ; break ; case ERR_MARKET_CLOSED: str = "The market is closed" ; break ; case ERR_TRADE_DISABLED: str = "Trade is disabled" ; break ; case ERR_NOT_ENOUGH_MONEY: str = "Not enough money to complete operation" ; break ; case ERR_PRICE_CHANGED: str = "The price has changed, trying again" ; res = EB_NEXTTRY ; break ; case ERR_OFF_QUOTES: str = "The broker sent offquotes. Trying again" ; res = EB_NEXTTRY ; break ; case ERR_BROKER_BUSY: str = "The broker is budy. Waiting..." ; res = EB_WAITTRY ; break ; case ERR_REQUOTE: str = "The broker sent requote. Trying again" ; res = EB_WAITTRY ; break ; case ERR_ORDER_LOCKED: str = "The order is already processing" ; break ; case ERR_LONG_POSITIONS_ONLY_ALLOWED: str = "Only long positions allowed" ; break ; case ERR_TRADE_MODIFY_DENIED: str = "Modification is denied because the order is too close to the price" ; res = EB_WAITTRY ; break ; case ERR_TRADE_CONTEXT_BUSY: if (_IsNotTradeContextBusy () >= 0) { res = EB_NEXTTRY ; } return (res) ; case ERR_TRADE_EXPIRATION_DENIED: str = "Expiration date usage is denied by the broker" ; break ; case ERR_TRADE_TOO_MANY_ORDERS: str = "Too many orders" ; break ; case ERR_TRADE_HEDGE_PROHIBITED: str = "Hedging is denied by broker" ; break ; case ERR_TRADE_PROHIBITED_BY_FIFO: str = "Cannot close a deal because FIFO order is violated" ; break ; case 4107: str = "Wrong price for OrderSend function" ; break ; case 4051: str = "Wrong takeprofit for OrderModify function" ; default: str = "Unknown error ¹" + DoubleToStr (err, 0) ; } str = StringConcatenate (str, " (the request is processed by broker in ", DoubleToStr (GetTickCount () - ms, 0), " ms)") ; Print (str) ; return (res) ; } int _IsNotTradeContextBusy() { // ïðîâåðÿåì, ñâîáîäåí ëè òîðãîâûé ïîòîê if(IsTradeContextBusy()) { int StartWaitingTime = GetTickCount(); Print("Trade context is busy. Waiting..."); // áåñêîíå÷íûé öèêë while(true) { // åñëè îæèäàíèå äëèòñÿ äîëüøå âðåìåíè, óêàçàííîãî â ïåðåìåííîé // MaxWaiting_sec, òîæå ïðåêðàùàåì ðàáîòó if(GetTickCount() - StartWaitingTime > sec_wait_context * 1000) { Print("Waiting limit exceeded (" + sec_wait_context + " sec.)!"); return(-2); } // åñëè òîðãîâûé ïîòîê îñâîáîäèëñÿ, if(!IsTradeContextBusy()) { //Print("òîðãîâûé ïîòîê îñâîáîäèëñÿ"); return(0); } // åñëè íè îäíî èç óñëîâèé îñòàíîâêè öèêëà íå ñðàáîòàëî, // "æä¸ì" 0,1 ñåêóíäû è íà÷èíàåì ïðîâåðêó ñíà÷àëà Sleep( busysleep ); } } else { //Print("Òîðãîâûé ïîòîê ñâîáîäåí!"); return(1); } } int MarketModify (int tick, double stop, double take) { if (! OrderSelect (tick, SELECT_BY_TICKET) || OrderType () >= 2 || OrderCloseTime () > 0) { Print ("Wrong ticket for MarketModify function") ; return (-3) ; } double use_sl = 0, use_tp = 0 ; if (stop > 0) { if (OrderType () == OP_BUY) { use_sl = OrderOpenPrice () - stop ; } else { use_sl = OrderOpenPrice () + stop ; } } if (stop < 0) { use_sl = -stop ; } if (take > 0) { if (OrderType () == OP_BUY) { use_tp = OrderOpenPrice () + take ; } else { use_tp = OrderOpenPrice () - take ; } } if (take < 0) { use_tp = -take ; } if (MathAbs (OrderStopLoss () - use_sl) < Point && MathAbs (use_tp - OrderTakeProfit ()) < Point) { Print ("There was an attempt to modify market order without changes to TP and SL") ; return (-3) ; } int attempt = 1 ; bool result = false ; while (attempt <= command_attempts && ! result) {/* if ((OrderType () == OP_BUY && (MarketInfo (OrderSymbol (), MODE_BID) - use_sl < d_stoplevel || use_tp - MarketInfo (OrderSymbol (), MODE_BID) < d_stoplevel)) || (OrderType () == OP_SELL && (use_sl - MarketInfo (OrderSymbol (), MODE_ASK) < d_stoplevel || MarketInfo (OrderSymbol (), MODE_ASK) - use_tp < d_stoplevel))) { Print ("Couldn''t modify order ", tick, " because of broker stops level") ; return (-2) ; } if ((OrderType () == OP_BUY && (MarketInfo (OrderSymbol (), MODE_BID) - use_sl < d_freezelevel || use_tp - MarketInfo (OrderSymbol (), MODE_BID) < d_freezelevel)) || (OrderType () == OP_SELL && (use_sl - MarketInfo (OrderSymbol (), MODE_ASK) < d_freezelevel || MarketInfo (OrderSymbol (), MODE_ASK) - use_tp < d_freezelevel))) { Print ("Couldn''t modify order ", tick, " because its level is too close to the price") ; return (-2) ; }*/ int ms = GetTickCount () ; Print ("Attempt #", attempt, " to modify market order ", tick, " to stoploss ", DoubleToStr (use_sl, Digits), " and takeprofit ", DoubleToStr (use_tp, Digits)) ; result = OrderModify (tick, OrderOpenPrice (), use_sl, use_tp, 0) ; if (! result) { errors ++ ; int err = GetLastError () ; switch (ErrorBlock (err, ms)) { case 0: // continue break ; case 1: // wait and continue Sleep (wait_error) ; break ; case 2: // terminate return (-1) ; } } else { errors = 0 ; Print ("Order successfully modified in ", GetTickCount () - ms, " ms") ; } if (errors > max_errors) { Print ("Exceeded maximum errors count while trying to send trading command.") ; Print ("To continue you should restart terminal or the EA") ; return (-1) ; } attempt ++ ; if (attempt > command_attempts && ! result) { return (-1) ; } } return (1) ; } int PendingSend (int type, double lots, double price, double stop, double take, int magic_number, string comm) { if (type < 2 || type > 5) { Print ("Error in type of pending order!") ; return (-3) ; } bool price_err = false, sltp_err = false ; RefreshRates () ; double use_sl = 0, use_tp = 0 ; if (stop > 0 && stop < d_stoplevel) { Print ("Stoploss for sending pending order is lesser that stops level!") ; return (-3) ; } if (take > 0 && take < d_stoplevel) { Print ("Takeprofit for sending pending order is lesser that stops level!") ; return (-3) ; } switch (type) { case OP_BUYLIMIT: if (Ask - price < d_stoplevel) { price_err = true ; } if (stop != 0) { if (stop > 0) { use_sl = price - stop ; } else { use_sl = -stop ; if (price - use_sl < d_stoplevel) { Print ("Stoploss for sending pending order is lesser that stops level!") ; return (-3) ; } } } if (take != 0) { if (take > 0) { use_tp = price + take ; } else { use_tp = -take ; if (use_tp - price < d_stoplevel) { Print ("Takeprofit for sending pending order is lesser that stops level!") ; return (-3) ; } } } break ; case OP_SELLLIMIT: if (price - Bid < d_stoplevel) { price_err = true ; } if (stop != 0) { if (stop > 0) { use_sl = price + stop ; } else { use_sl = -stop ; if (use_sl - price < d_stoplevel) { Print ("Stoploss for sending pending order is lesser that stops level!") ; return (-3) ; } } } if (take != 0) { if (take > 0) { use_tp = price - take ; } else { use_tp = -take ; if (price - use_tp < d_stoplevel) { Print ("Takeprofit for sending pending order is lesser that stops level!") ; return (-3) ; } } } break ; case OP_BUYSTOP: if (price - Ask < d_stoplevel) { price_err = true ; } if (stop != 0) { if (stop > 0) { use_sl = price - stop ; } else { use_sl = -stop ; if (price - use_sl < d_stoplevel) { Print ("Stoploss for sending pending order is lesser that stops level!") ; return (-3) ; } } } if (take != 0) { if (take > 0) { use_tp = price + take ; } else { use_tp = -take ; if (use_tp - price < d_stoplevel) { Print ("Takeprofit for sending pending order is lesser that stops level!") ; return (-3) ; } } } break ; case OP_SELLSTOP: if (Bid - price < d_stoplevel) { price_err = true ; } if (stop != 0) { if (stop > 0) { use_sl = price + stop ; } else { use_sl = -stop ; if (use_sl - price < d_stoplevel) { Print ("Stoploss for sending pending order is lesser that stops level!") ; return (-3) ; } } } if (take != 0) { if (take > 0) { use_tp = price - take ; } else { use_tp = -take ; if (price - use_tp < d_stoplevel) { Print ("Takeprofit for sending pending order is lesser that stops level!") ; return (-3) ; } } } break ; } int attempt = 1 ; int tick = -1 ; while (attempt <= command_attempts && tick < 0) { RefreshRates () ; if ((type == OP_BUYLIMIT && Ask - price < d_stoplevel) || (type == OP_SELLLIMIT && price - Bid < d_stoplevel) || (type == OP_BUYSTOP && price - Ask < d_stoplevel) || (type == OP_SELLSTOP && Bid - price < d_stoplevel)) { Print ("Cannot set a pending orders due to minimal broker stops&limits level (", DoubleToStr (type, 0), " ", DoubleToStr (price, Digits), " ", DoubleToStr (use_sl, Digits), " ", DoubleToStr (use_tp, Digits), ") (", DoubleToStr (Ask, Digits), " ", DoubleToStr (Bid, Digits), ")") ; return (-2) ; } string str = StringConcatenate ("Attempt ", DoubleToStr (attempt, 0), " to set an order ") ; switch (type) { case OP_BUYLIMIT: str = StringConcatenate (str, "Buy Limit") ; break ; case OP_SELLLIMIT: str = StringConcatenate (str, "Sell Limit") ; break ; case OP_BUYSTOP: str = StringConcatenate (str, "Buy Stop") ; break ; case OP_SELLSTOP: str = StringConcatenate (str, "Sell Stop") ; } str = StringConcatenate (str, " at price ", DoubleToStr (price, Digits), " stop ", DoubleToStr (use_sl, Digits), " take ", DoubleToStr (use_tp, Digits)) ; Print (str) ; int ms = GetTickCount () ; tick = OrderSend (Symbol (), type, lots, price, 0, use_sl, use_tp, comm, magic_number) ; if (tick < 0) { errors ++ ; int err = GetLastError () ; switch (ErrorBlock (err, ms)) { case 0: // continue break ; case 1: // wait and continue Sleep (wait_error) ; break ; case 2: // terminate return (-1) ; } } else { errors = 0 ; Print ("Order is set in ", GetTickCount () - ms, " ms") ; } if (errors > max_errors) { Print ("Exceeded maximum errors count while trying to send trading command.") ; Print ("To continue you should restart terminal or the EA") ; return (-1) ; } attempt ++ ; if (attempt > command_attempts && tick < 0) { Print ("Exeeded maximum number of simultaneous attempts of setting an order") ; return (-1) ; } } return (tick) ; } int MarketSend (int dir, double lots, double stop, double take, int magic_number) { if (dir != OP_BUY && dir != OP_SELL) { Print ("Wrong order direction for function MarketSend") ; return (-3) ; } RefreshRates () ; double init_ask = Ask ; double init_bid = Bid ; if ((stop > 0 && stop < d_stoplevel) || (stop < 0 && ((dir == OP_BUY && Ask + stop < d_stoplevel) || (dir == OP_SELL && -stop - Bid < d_stoplevel)))) { Print ("Wrong stoploss value for function MarketSend. Sending w/o stop") ; return (-3) ; } if ((take > 0 && take < d_stoplevel) || (take < 0 && ((dir == OP_BUY && -take - Ask < d_stoplevel) || (dir == OP_SELL && Bid + take < d_stoplevel)))) { Print ("Wrong takeprofit value for function MarketSend. Sending w/o take") ; return (-3) ; } double use_sl, use_tp, price ; int attempt = 1, tick = -1 ; while (attempt <= command_attempts && tick < 0) { double _slp ; int use_slp ; if (dir == OP_BUY) { _slp = (Ask - init_ask) / Point ; use_slp = max_slippage - _slp ; } else { _slp = (init_bid - Bid) / Point ; use_slp = max_slippage - _slp ; } if (use_slp < 0) { Print ("Maximal slippage exceeded while trying to open market order") ; return (-2) ; } if (dir == OP_BUY) { price = Ask ; } else { price = Bid ; } use_sl = 0 ; use_tp = 0 ; if (market_execution == 0) { if (stop > 0) { if (dir == OP_BUY) { use_sl = Ask - stop ; } else { use_sl = Bid + stop ; } } if (stop < 0) { if (dir == OP_BUY) { if (Ask + stop < d_stoplevel) { Print ("Stoploss is under broker stop levels while sending BUY") ; return (-2) ; } else { use_sl = -stop ; } } else { if (-stop - Bid < d_stoplevel) { Print ("Stoploss is under broker stop levels while sending SELL") ; return (-2) ; } else { use_sl = -stop ; } } } if (take > 0) { if (dir == OP_BUY) { use_tp = Ask + take ; } else { use_tp = Bid - take ; } } if (take < 0) { if (dir == OP_BUY) { if (-take - Ask < d_stoplevel) { Print ("Takeprofit is under broker stop levels while sending BUY") ; return (-2) ; } else { use_tp = -take ; } } else { if (Bid + take < d_stoplevel) { Print ("Takeprofit is under broker stop levels while sending SELL") ; return (-2) ; } else { use_tp = -take ; } } } } string str = StringConcatenate ("Attempt ", DoubleToStr (attempt, 0), " of sending order") ; if (dir == OP_BUY) { str = StringConcatenate (str, " BUY ") ; } else { str = StringConcatenate (str, " SELL ") ; } str = StringConcatenate (str, "at price ", DoubleToStr (price, Digits), " stop ", DoubleToStr (use_sl, Digits), " take ", DoubleToStr (use_tp, Digits)) ; Print (str) ; int ms = GetTickCount () ; tick = OrderSend (Symbol (), dir, lots, price, use_slp, use_sl, use_tp, "", magic_number) ; if (tick < 0) { errors ++ ; int err = GetLastError () ; if (err == ERR_INVALID_STOPS) { if (market_execution == 0) { if (stop != 0 || take != 0) { Print ("Incorrect stops. Maybe broker increased stop levels") ; return (-2) ; } else { Print ("Incorrect stops while stop and take = 0. Error in expert logic") ; return (-3) ; } } else { Print ("Incorrect stops with Market Execution. Error in expert logic") ; return (-3) ; } } else { switch (ErrorBlock (err, ms)) { case 0: // continue break ; case 1: // wait and continue Sleep (wait_error) ; break ; case 2: // terminate return (-1) ; } } } else { errors = 0 ; OrderSelect (tick, SELECT_BY_TICKET) ; str = StringConcatenate ("Order was opened in ", DoubleToStr (GetTickCount () - ms, 0), " ms") ; double r_slp ; if (MathAbs (OrderOpenPrice () - price) >= Point) { if (OrderType () == OP_BUY) { r_slp = (OrderOpenPrice () - price) / Point ; } else { r_slp = (price - OrderOpenPrice ()) / Point ; } str = StringConcatenate (str, " with slippage ", DoubleToStr (MathAbs (r_slp), 0), " pips") ; if (r_slp < 0) { str = StringConcatenate (str, " in our benefit") ; } } Print (str) ; if (market_execution == 1 && (stop != 0 || take != 0)) { MarketModify (OrderTicket (), stop, take) ; } } if (errors > max_errors) { Print ("Exceeded maximum errors count while trying to send trading command.") ; Print ("To continue you should restart terminal or the EA") ; return (-1) ; } attempt ++ ; if (attempt > command_attempts && tick < 0) { Print ("Exeeded maximum number of simultaneous attempts of setting an order") ; return (-1) ; } RefreshRates () ; } return (tick) ; } int MyOrders () { int num = 0 ; for (int i = OrdersTotal () - 1; i >= 0; i --) { if (OrderSelect (i, SELECT_BY_POS) && OrderSymbol () == Symbol () && OrderMagicNumber () >= initial_magic && OrderMagicNumber () < initial_magic + 100) { num ++ ; } } return (num) ; } bool ProfitReached () { if (enable_daily_profit <= 0) { return (false) ; } double sum = 0 ; int pos = OrdersHistoryTotal () - 1 ; while (pos >= 0) { if (OrderSelect (pos, SELECT_BY_POS, MODE_HISTORY) && OrderMagicNumber () >= initial_magic && OrderMagicNumber () < initial_magic + 100 && OrderType () < 2) { if (OrderOpenTime () < TimeCurrent () - TimeCurrent () % 86400) { break ; } sum += OrderProfit () + OrderSwap () + OrderCommission () ; } pos -- ; } if (daily_profit > 0 && sum >= daily_profit) { return (true) ; } if (daily_profit_percent > 0 && sum / AccountBalance () * 100 >= daily_profit_percent) { return (true) ; } } int LastProfitDir () { int dir = initial_deal, pos = OrdersHistoryTotal () - 1, i = 0 ; while (i < 100 && pos >= 0) { if (OrderSelect (pos, SELECT_BY_POS, MODE_HISTORY) && OrderMagicNumber () >= initial_magic && OrderMagicNumber () < initial_magic + 100 && OrderType () < 2) { if (OrderOpenTime () < TimeCurrent () - TimeCurrent () % 86400) { break ; } if (OrderProfit () > 0 && TimeCurrent () - OrderCloseTime () < 300) { switch (OrderType ()) { case OP_BUY: dir = 0 ; break ; case OP_SELL: dir = 1 ; break ; } break ; } } i ++ ; pos -- ; } return (dir) ; } int PendingRemove (int tick) { if (! OrderSelect (tick, SELECT_BY_TICKET) || OrderType () < 2 || OrderType () > 5 || OrderCloseTime () > 0) { Print ("Wrong ticket for function PendingRemove") ; return (-3) ; } int attempt = 1 ; bool result = false ; while (attempt <= command_attempts && ! result) { switch (OrderType ()) { case OP_BUYLIMIT: if (Ask - OrderOpenPrice () < d_freezelevel) { Print ("Unable to delete order ", tick, " because it is too close to the market") ; return (-2) ; } break ; case OP_BUYSTOP: if (OrderOpenPrice () - Ask < d_freezelevel) { Print ("Unable to delete order ", tick, " because it is too close to the market") ; return (-2) ; } break ; case OP_SELLLIMIT: if (OrderOpenPrice () - Bid < d_freezelevel) { Print ("Unable to delete order ", tick, " because it is too close to the market") ; return (-2) ; } break ; case OP_SELLSTOP: if (Bid - OrderOpenPrice () < d_freezelevel) { Print ("Unable to delete order ", tick, " because it is too close to the market") ; return (-2) ; } break ; } int ms = GetTickCount () ; Print ("Attempt ", attempt, " to remove pending order ", tick) ; result = OrderDelete (tick) ; if (! result) { errors ++ ; int err = GetLastError () ; switch (ErrorBlock (err, ms)) { case 0: // continue break ; case 1: // wait and continue Sleep (wait_error) ; break ; case 2: // terminate return (-1) ; } } else { errors = 0 ; Print ("Order was removed in ", GetTickCount () - ms, " ms") ; } if (errors > max_errors) { Print ("Exceeded maximum errors count while trying to send trading command.") ; Print ("To continue you should restart terminal or the EA") ; return (-1) ; } attempt ++ ; if (attempt > command_attempts && tick < 0) { Print ("Exeeded maximum number of simultaneous attempts of setting an order") ; return (-1) ; } } return (1) ; } double Lots (int leg = 0) { double lot = 0 ; if (fixed_lot > 0) { lot = fixed_lot ; } else { if (deposit_lot > 0) { lot = AccountBalance () / deposit_lot ; } } for (int i = 1; i <= leg; i ++) { if (i == 1) { lot *= martin_koeff1 ; } else { lot *= martin_koeff2 ; } } return (NormalizeDouble (MathMax (MathMin (MarketInfo (Symbol (), MODE_MAXLOT), lot), MarketInfo (Symbol (), MODE_MINLOT)), lotsdigits)) ; } bool HedgeIsSet (int magic) { int tick = OrderTicket () ; for (int i = OrdersTotal () - 1; i >= 0; i --) { if (OrderSelect (i, SELECT_BY_POS) && OrderSymbol () == Symbol () && OrderMagicNumber () == magic) { return (true) ; } } OrderSelect (tick, SELECT_BY_TICKET) ; return (false) ; } |
How to use forex hedging strategy :
Tags Expert Adviser
- Previous Check out the best stock investing books
- Next Volume Histogram for Amibroker AFL (100% Work Fast)
You may stop it manually or use a programming language.
You may help the Forex EA programmers to make this. Thanks
Like
Maybe your broker has no option to trade with gold. contact with them.
anyone having for mt5 please??
May I please have the surefire EA that will work on metatrader 5
Right now its may not. Thanks
I need to use this on metatrader 5 please help me