#include extern string EAName = "rsi minute"; extern bool IsMicroAccount = FALSE; extern bool CompletedBars = TRUE; extern bool TradeOrAlert = TRUE; extern bool SendMailMode = FALSE; extern bool PlaySounds = FALSE; extern string MySound = "alert.wav"; extern bool UseTradingTime = FALSE; extern int TradingTimeStart = 9; extern int TradingTimeEnd = 16; extern bool OrderIncludeTPSL = TRUE; extern int MaxNumberofPositions = 1; extern int MaxOrderRetry = 100; int gi_136 = 32768; int gi_140 = 255; bool gi_144 = FALSE; extern bool UseMaximumPercentageatRisk = FALSE; extern double MaximumPercentageatRisk = 1.0; extern bool UseRiskRatio = FALSE; extern int RiskRatio = 3; extern bool UseLotManagement = FALSE; extern int LotManagementType = 2; extern double ReductionFactor = 5.0; extern int Slippage = 3; extern double S_Symbol_LS_0 = 0.1; extern double S_Symbol_TP_0 = 8.0; extern double S_Symbol_SL_0 = 15.0; extern double S_Symbol_TS_0 = 0.0; extern double B_Symbol_LS_0 = 0.1; extern double B_Symbol_TP_0 = 7.0; extern double B_Symbol_SL_0 = 15.0; extern double B_Symbol_TS_0 = 0.0; int gi_252 = 6658641; int gi_256 = 4167090; string gs_260 = "Go long 6658641"; string gs_268 = "Go short 4167090"; int g_bars_276 = 0; string g_str_concat_280 = ""; string g_str_concat_288 = "EA: "; int init() { Print("M-*** EA created with Molanis Strategy Builder 3.1 Beta *** "); Print("M-*** EA starts running at: Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), " Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS)); if (IsDemo()) Print("M-*** Running in a DEMO account *** "); else Print("M-*** Running in a REAL account *** "); if (IsTesting()) Print("M-*** Running in TESTING MODE *** "); Print("M-Account Equity=", AccountEquity(), ". Account Balance=", AccountBalance(), ". Account Free Margin = ", AccountFreeMargin(), ". Account Leverage=", AccountLeverage()); if (OrdersTotal() > 0) MessageBox("There are existing positions that could affect the logic of this EA. We strongly advise you to close all pending and open positions/orders before executing this EA.", "Molanis Warning: Close existing orders"); if (Bars < 100) { Print("M-*** CAN NOT TRADE *** Not enough historical information!"); MessageBox("*** CAN NOT TRADE *** Not enough historical information!", "Molanis Warning"); } if (IsTradeAllowed() == FALSE) { Print("M-*** CAN NOT TRADE *** Trading is not allowed! Please confirm that the checkbox -Allow Live Trading option- is checked and that you are able to connect to the server."); MessageBox("*** CAN NOT TRADE *** Trading is not allowed! Please confirm that the checkbox -Allow Live Trading option- is checked and that you are able to connect to the server.", "Molanis Warning"); } Print("M-Lot Information: Symbol=", Symbol(), ". MIN LOT ALLOWED=", MarketInfo(Symbol(), MODE_MINLOT), ". MAX LOT ALLOWED=", MarketInfo(Symbol(), MODE_MAXLOT), ". LOT SIZE IN BASE CURRENCY=", MarketInfo(Symbol(), MODE_LOTSIZE)); Print("M-Lot Information: Buying 1 lot in your Account is equivalent to buying ", MarketInfo(Symbol(), MODE_LOTSIZE), " of currency."); Print("M-Lot Information: Buying the minimum lot size of ", MarketInfo(Symbol(), MODE_MINLOT), " is equivalent to buying ", MarketInfo(Symbol(), MODE_LOTSIZE) * MarketInfo(Symbol(), MODE_MINLOT), " of currency."); RefreshRates(); return (0); } int deinit() { Print("M-*** EA is stopping !*** Done!"); return (0); } int start() { gi_144 = FALSE; string l_str_concat_0 = "THIS EA HAS BEEN GENERATED USING MOLANIS STRATEGY BUILDER 3.1 BETA -"; if (TradeOrAlert) l_str_concat_0 = StringConcatenate(l_str_concat_0, " Trading Mode -"); else l_str_concat_0 = StringConcatenate(l_str_concat_0, " Alarm Mode -"); if (UseTradingTime) { if (!(Hour() >= TradingTimeStart && Hour() <= TradingTimeEnd)) { l_str_concat_0 = StringConcatenate(l_str_concat_0, " Server Time=", Hour(), "h:", Minute(), "m. | Current server time is NOT between trading time hours. | Trading Time is from ", TradingTimeStart, " to ", TradingTimeEnd, " hours"); Comment(l_str_concat_0); return (0); } l_str_concat_0 = StringConcatenate(l_str_concat_0, " Server Time=", Hour(), "h:", Minute(), "m. | Current server time is between trading time hours. | Trading Time is from ", TradingTimeStart, " to ", TradingTimeEnd, " hours"); Comment(l_str_concat_0); } if (SendMailMode) { g_str_concat_288 = "EA: "; g_str_concat_288 = StringConcatenate(g_str_concat_288, EAName, " "); g_str_concat_280 = "email generated by Molanis Strategy Builder v3.1 BETA\n"; g_str_concat_280 = StringConcatenate(g_str_concat_280, "Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), ". Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"); } if (!IsConnected()) { Print("M-URGENT ACTION REQUIRED : There is no connection to the server!"); Alert("URGENT ACTION REQUIRED : There is no connection to the server!"); if (SendMailMode) { g_str_concat_288 = StringConcatenate(g_str_concat_288, "URGENT ACTION REQUIRED : There is no connection to the server"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "URGENT ACTION REQUIRED : There is no connection to the server. Please check your MT4 terminal.\n"); SendMail(g_str_concat_288, g_str_concat_280); } return (0); } if (IsStopped()) { Print("M-ERROR : EA was commanded to stop its operation!"); Alert("ERROR : EA was commanded to stop its operation!"); if (SendMailMode) { g_str_concat_288 = StringConcatenate(g_str_concat_288, "EA was commanded to stop its operation"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "EA was commanded to stop its operation!\n"); SendMail(g_str_concat_288, g_str_concat_280); } return (0); } if (!IsTradeAllowed() && TradeOrAlert) { Print("M-ERROR : Trading is not allowed!"); Alert("ERROR : Trading is not allowed!"); if (SendMailMode) { g_str_concat_288 = StringConcatenate(g_str_concat_288, "Trading is not allowed"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "Trading is not allowed at this moment. Please take action if needed\n"); SendMail(g_str_concat_288, g_str_concat_280); } return (0); } if (g_bars_276 == Bars && CompletedBars) return (0); if (TradeOrAlert) { if (iRSI(Symbol(), PERIOD_M15, 14, PRICE_TYPICAL, 1) <49.0) SELL(Symbol(), S_Symbol_LS_0, S_Symbol_TP_0, S_Symbol_SL_0, S_Symbol_TS_0, "if ( ( iRSI(Symbol(),PERIOD_M15, 14,PRICE_TYPICAL,1) < 49.00000 ) )"); if (iRSI(Symbol(), PERIOD_M15, 14, PRICE_TYPICAL, 1) > 51.0) BUY(Symbol(), B_Symbol_LS_0, B_Symbol_TP_0, B_Symbol_SL_0, B_Symbol_TS_0, "if ( ( iRSI(Symbol(),PERIOD_M15, 14,PRICE_TYPICAL,1) > 51.00000 ) )"); if (iRSI(Symbol(), PERIOD_M1, 3, PRICE_TYPICAL, 1) <30.0) SELL(Symbol(), S_Symbol_LS_0, S_Symbol_TP_0, S_Symbol_SL_0, S_Symbol_TS_0, "if ( ( iRSI(Symbol(),PERIOD_M1, 3,PRICE_TYPICAL,1) < 30.00000 ) )"); if (iRSI(Symbol(), PERIOD_M1, 3, PRICE_TYPICAL, 1) > 70.0) BUY(Symbol(), B_Symbol_LS_0, B_Symbol_TP_0, B_Symbol_SL_0, B_Symbol_TS_0, "if ( ( iRSI(Symbol(),PERIOD_M1, 3,PRICE_TYPICAL,1) > 70.00000 ) )"); CheckTrailingStop(Symbol(), 1, gi_256, S_Symbol_TS_0); CheckTrailingStop(Symbol(), 0, gi_252, B_Symbol_TS_0); } else { if (iRSI(Symbol(), PERIOD_M15, 14, PRICE_TYPICAL, 1) < 49.0) MolanisAlert("if ( ( iRSI(Symbol(),PERIOD_M15,4,PRICE_TYPICAL,1) < 49.00000 ) )", "SELL(Symbol(),0.1,8,15,0)", Symbol()); if (iRSI(Symbol(), PERIOD_M15, 14, PRICE_TYPICAL, 1) > 51.0) MolanisAlert("if ( ( iRSI(Symbol(),PERIOD_M15, 14,PRICE_TYPICAL,1) > 51.00000 ) )", "BUY(Symbol(),0.09999999999999999,7,15,0)", Symbol()); if (iRSI(Symbol(), PERIOD_M1, 3, PRICE_TYPICAL, 1) < 30.0) MolanisAlert("if ( ( iRSI(Symbol(),PERIOD_M1,3,PRICE_TYPICAL,1) < 30.00000 ) )", "SELL(Symbol(),0.1,8,15,0)", Symbol()); if (iRSI(Symbol(), PERIOD_M1, 3, PRICE_TYPICAL, 1) > 70.0) MolanisAlert("if ( ( iRSI(Symbol(),PERIOD_M1, 3,PRICE_TYPICAL,1) > 70.00000 ) )", "BUY(Symbol(),0.09999999999999999,7,15,0)", Symbol()); } g_bars_276 = Bars; return (0); } int MolanisAlert(string as_0, string as_8, string a_symbol_16) { double l_bid_24; double l_ask_32; if (SendMailMode) { l_bid_24 = MarketInfo(a_symbol_16, MODE_BID); l_ask_32 = MarketInfo(a_symbol_16, MODE_ASK); g_str_concat_288 = "EA Alert: "; g_str_concat_288 = StringConcatenate(g_str_concat_288, EAName, ". ", as_8); g_str_concat_280 = "email generated by Molanis Strategy Builder v3.1 BETA\n"; g_str_concat_280 = StringConcatenate(g_str_concat_280, "Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), ". Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "Alert: ", as_8, "\nForex Signal: ", as_0, "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "Price on : ", a_symbol_16, " BID=", l_bid_24, " ASK=", l_ask_32, "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "Price on current chart: ", Symbol(), " BID=", MarketInfo(Symbol(), MODE_BID), " ASK=", MarketInfo(Symbol(), MODE_ASK), "\n"); SendMail(g_str_concat_288, g_str_concat_280); } Print("M-Alert:", as_8, " Forex Signal: ", as_0); Alert("M-Alert:", as_8, " Forex Signal: ", as_0); if (PlaySounds) PlaySound(MySound); return (0); } int BUY(string as_0, double ad_8, double ad_16, double ad_24, double ad_unused_32, string as_40) { CloseAllPositions(as_0, OP_SELL, gi_256); if (OrderIncludeTPSL) { if (ExecuteOrder(OP_BUY, as_0, LotManagement(as_0, ad_8), ad_24, ad_16, gs_260, gi_252, gi_136, as_40) > 0) { } } else { if (ExecuteOrderinTwo(0, as_0, LotManagement(as_0, ad_8), ad_24, ad_16, gs_260, gi_252, gi_136, as_40) > 0) { } } return (0); } int SELL(string as_0, double ad_8, double ad_16, double ad_24, double ad_unused_32, string as_40) { CloseAllPositions(as_0, OP_BUY, gi_252); if (OrderIncludeTPSL) { if (ExecuteOrder(OP_SELL, as_0, LotManagement(as_0, ad_8), ad_24, ad_16, gs_268, gi_256, gi_140, as_40) > 0) { } } else { if (ExecuteOrderinTwo(1, as_0, LotManagement(as_0, ad_8), ad_24, ad_16, gs_268, gi_256, gi_140, as_40) > 0) { } } return (0); } int EnoughMoney(string as_0, double ad_8) { if (AccountFreeMarginCheck(as_0, OP_BUY, ad_8) < 10.0 || AccountFreeMarginCheck(as_0, OP_SELL, ad_8) < 10.0 || GetLastError() == 134/* NOT_ENOUGH_MONEY */) { Print("M-NOT ENOUGH MONEY TO TRADE. Free margin is insufficient to trade a lot size of ", ad_8, ". Current Free Margin=", AccountFreeMargin()); Alert("M-NOT ENOUGH MONEY TO TRADE. Free margin is insufficient to trade a lot size of ", ad_8, ". Current Free Margin=", AccountFreeMargin()); return (0); } return (1); } int ExecuteOrderinTwo(int ai_0, string a_symbol_4, double ad_12, double a_price_20, double a_price_28, string as_36, int ai_44, color a_color_48, string as_52) { double l_point_68; double l_digits_76; double ld_84; bool li_92; int l_error_60 = 0; Print("M-Executing order in Two Steps. Step 1 - Sending order without TP/SL."); int l_ticket_64 = ExecuteOrder(ai_0, a_symbol_4, ad_12, 0, 0, as_36, ai_44, a_color_48, as_52); if (l_ticket_64 > 0 && a_price_20 != 0.0 || a_price_28 != 0.0) { Print("M-Executing order in Two Steps. Step 2 - Adding TP/SL."); if (OrderSelect(l_ticket_64, SELECT_BY_TICKET)) { RefreshRates(); l_digits_76 = MarketInfo(a_symbol_4, MODE_DIGITS); l_point_68 = MarketInfo(a_symbol_4, MODE_POINT); a_price_20 = 10.0 * a_price_20; a_price_28 = 10.0 * a_price_28; if (UseRiskRatio) { a_price_20 = NormalizeDouble(a_price_28 / RiskRatio, l_digits_76); Print("M-Stop loss adjusted according to risk ratio, equation TakeProfit/StopLoss=", RiskRatio, ". SL=Price +/- ", a_price_20 * l_point_68, "."); } switch (ai_0) { case 0: li_92 = TRUE; break; case 1: li_92 = FALSE; break; default: Print("M-ERROR : Wrong order type. Can not add SL/TP ", ai_0); return (-1); } if (li_92) { ld_84 = NormalizeDouble(MarketInfo(a_symbol_4, MODE_ASK), l_digits_76); if (a_price_20 > 0.0) { a_price_20 = NormalizeDouble(ld_84 - a_price_20 * l_point_68, l_digits_76); a_price_20 = CheckStopLoss(a_symbol_4, ld_84, a_price_20); } if (a_price_28 > 0.0) { a_price_28 = NormalizeDouble(ld_84 + a_price_28 * l_point_68, l_digits_76); a_price_28 = CheckTakeProfit(a_symbol_4, ld_84, a_price_28); } } else { ld_84 = NormalizeDouble(MarketInfo(a_symbol_4, MODE_BID), l_digits_76); if (a_price_20 > 0.0) { a_price_20 = NormalizeDouble(ld_84 + a_price_20 * l_point_68, l_digits_76); a_price_20 = CheckStopLoss(a_symbol_4, ld_84, a_price_20); } if (a_price_28 > 0.0) { a_price_28 = NormalizeDouble(ld_84 - a_price_28 * l_point_68, l_digits_76); a_price_28 = CheckTakeProfit(a_symbol_4, ld_84, a_price_28); } } if (OrderModify(l_ticket_64, OrderOpenPrice(), a_price_20, a_price_28, 0, a_color_48)) Print("M-Step 2 - TP/SL added. SL=", a_price_20, " TP=", a_price_28); else { l_error_60 = GetLastError(); Print("M-Step 2 failed. Could not add SL/TP to order. Error:", l_error_60, " ", ErrorDescription(l_error_60)); } } else { l_error_60 = GetLastError(); Print("M-Step 2 failed. Could not select order. Error:", l_error_60, " ", ErrorDescription(l_error_60)); } } return (0); } int ExecuteOrder(int a_cmd_0, string a_symbol_4, double a_minlot_12, double a_price_20, double a_price_28, string a_comment_36, int a_magic_44, color a_color_48, string as_52) { double l_price_72; int l_ticket_88; bool li_92; int l_error_96; int li_100 = 1; a_price_20 = 10.0 * a_price_20; a_price_28 = 10.0 * a_price_28; switch (a_cmd_0) { case OP_BUY: li_92 = TRUE; break; case OP_SELL: li_92 = FALSE; break; default: Print("M-ERROR : Wrong order type ", a_cmd_0); return (-1); } if (CalculateOpenPositions(a_symbol_4, a_cmd_0, a_magic_44) >= MaxNumberofPositions) { Print("M-Warning : Can not execute new ", OrderTypetoString(a_cmd_0), " order for Symbol ", a_symbol_4, ". Maximum number of ", MaxNumberofPositions, " open positions reached."); return (0); } if (!EnoughMoney(a_symbol_4, a_minlot_12)) return (-1); int l_digits_60 = MarketInfo(a_symbol_4, MODE_DIGITS); double l_point_64 = MarketInfo(a_symbol_4, MODE_POINT); if (UseRiskRatio) { a_price_20 = NormalizeDouble(a_price_28 / RiskRatio, l_digits_60); Print("M-Stop loss adjusted according to risk ratio, equation TakeProfit/StopLoss=", RiskRatio, ". SL=Price +/- ", a_price_20 * l_point_64, "."); } int li_108 = 1; if (IsMicroAccount) li_108 = 2; double l_lots_112 = a_minlot_12; if (NormalizeDouble(AccountFreeMargin() * (MaximumPercentageatRisk / 100.0) / 1000.0, li_108) < a_minlot_12 && UseMaximumPercentageatRisk) { a_minlot_12 = NormalizeDouble(AccountFreeMargin() * (MaximumPercentageatRisk / 100.0) / 1000.0, li_108); if (a_minlot_12 < MarketInfo(a_symbol_4, MODE_MINLOT)) { a_minlot_12 = MarketInfo(a_symbol_4, MODE_MINLOT); Print("M-Lot adjusted from ", l_lots_112, " to minimum size allowed by the server of ", a_minlot_12, " but it DOES NOT comply with Maximum Risk condition. User interaction is required!"); } else Print("M-Lot adjusted from ", l_lots_112, " to ", a_minlot_12, " to comply with Maximum Risk condition. Each trade can risk only ", MaximumPercentageatRisk, "% of free margin."); } while (li_100 > 0) { RefreshRates(); if (li_92) { l_price_72 = NormalizeDouble(MarketInfo(a_symbol_4, MODE_ASK), l_digits_60); if (a_price_20 > 0.0) { a_price_20 = NormalizeDouble(l_price_72 - a_price_20 * l_point_64, l_digits_60); a_price_20 = CheckStopLoss(a_symbol_4, l_price_72, a_price_20); } if (a_price_28 > 0.0) { a_price_28 = NormalizeDouble(l_price_72 + a_price_28 * l_point_64, l_digits_60); a_price_28 = CheckTakeProfit(a_symbol_4, l_price_72, a_price_28); } Print("M-Preparing Buy order ", a_symbol_4, " triggered by ", as_52); } else { l_price_72 = NormalizeDouble(MarketInfo(a_symbol_4, MODE_BID), l_digits_60); if (a_price_20 > 0.0) { a_price_20 = NormalizeDouble(l_price_72 + a_price_20 * l_point_64, l_digits_60); a_price_20 = CheckStopLoss(a_symbol_4, l_price_72, a_price_20); } if (a_price_28 > 0.0) { a_price_28 = NormalizeDouble(l_price_72 - a_price_28 * l_point_64, l_digits_60); a_price_28 = CheckTakeProfit(a_symbol_4, l_price_72, a_price_28); } Print("M-Preparing Sell order ", a_symbol_4, " triggered by ", as_52); } if (!isTradingPossible()) Print("M-Warning: Trading may not be possible. Trying to open a new position."); l_ticket_88 = OrderSend(a_symbol_4, a_cmd_0, a_minlot_12, l_price_72, Slippage, a_price_20, a_price_28, a_comment_36, a_magic_44, 0, a_color_48); if (l_ticket_88 >= 0) { Sleep(250); if (!(OrderSelect(l_ticket_88, SELECT_BY_TICKET))) continue; Print("M-Order Executed Successfully! Order Type=", OrderTypetoString(a_cmd_0), ". Symbol=", a_symbol_4); if (SendMailMode) { g_str_concat_288 = "EA: "; g_str_concat_288 = StringConcatenate(g_str_concat_288, EAName, ". ", a_symbol_4, " ", OrderTypetoString(a_cmd_0), " order executed. Ticket:", l_ticket_88); g_str_concat_280 = "email generated by Molanis Strategy Builder v3.1 BETA\n"; g_str_concat_280 = StringConcatenate(g_str_concat_280, "Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), ". Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "M-Order Executed Successfully!\nOrder Info : \n Order Type=", OrderTypetoString(a_cmd_0), ".\n Symbol=", a_symbol_4, ".\n Lot Size=", a_minlot_12, ".\n Price=", l_price_72, ".\n FillPrice=", OrderOpenPrice(), ".\n Slippage=", OrderOpenPrice() - l_price_72, ".\n SL=", OrderStopLoss(), ".\n TP=", OrderTakeProfit(), ".\n Order time=", TimeToStr(OrderOpenTime()), "\n Ticket:", l_ticket_88, ".\n Maximum Number of open positions:", MaxNumberofPositions, ".\n Number of open positions:", CalculateOpenPositions(a_symbol_4, a_cmd_0, a_magic_44)); SendMail(g_str_concat_288, g_str_concat_280); } if (PlaySounds) PlaySound(MySound); return (l_ticket_88); } l_error_96 = GetLastError(); if (l_error_96 == 4/* SERVER_BUSY */ || l_error_96 == 146/* TRADE_CONTEXT_BUSY */ || l_error_96 == 137/* BROKER_BUSY */ || l_error_96 == 6/* NO_CONNECTION */ || l_error_96 == 128/* TRADE_TIMEOUT */ || l_error_96 == 129/* INVALID_PRICE */ || l_error_96 == 136/* OFF_QUOTES */ || l_error_96 == 135/* PRICE_CHANGED */ || l_error_96 == 138/* REQUOTE */) { Print("M-ERROR: Server error. Sending order to server again. Retry intent number = ", li_100); li_100++; if (li_100 > MaxOrderRetry + 1) { Print("M-ERROR: Could not open new position. Error=", l_error_96, " ", ErrorDescription(l_error_96), ". Order Info : Order Type=", OrderTypetoString(a_cmd_0), ". Symbol=", a_symbol_4, ". Lot Size=", a_minlot_12, ". Price=", l_price_72, ". SL=", a_price_20, ". TP=", a_price_28); Alert("M-ERROR: Could not open new position. Error=", l_error_96, " ", ErrorDescription(l_error_96), ". Order Info : Order Type=", OrderTypetoString(a_cmd_0), ". Symbol=", a_symbol_4, ". Lot Size=", a_minlot_12, ". Price=", l_price_72, ". SL=", a_price_20, ". TP=", a_price_28); if (SendMailMode) { g_str_concat_288 = "EA: "; g_str_concat_288 = StringConcatenate(g_str_concat_288, EAName, " ", a_symbol_4, " ", OrderTypetoString(a_cmd_0), " order FAILED"); g_str_concat_280 = "email generated by Molanis Strategy Builder v3.1 BETA\n"; g_str_concat_280 = StringConcatenate(g_str_concat_280, "Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), ". Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "WARNING: Could not open new position.\n Error=", l_error_96, "\n", ErrorDescription(l_error_96), ".\n Order Info :\nOrder Type=", OrderTypetoString(a_cmd_0), ".\n Symbol=", a_symbol_4, ".\n Lot Size=", a_minlot_12, ".\n Price=", l_price_72, ".\n SL=", a_price_20, ".\n TP=", a_price_28); SendMail(g_str_concat_288, g_str_concat_280); } return (-1); } Sleep(250); } else { Print("M-ERROR: Could not open new position. Error=", l_error_96, " ", ErrorDescription(l_error_96), ". Order Info : Order Type=", OrderTypetoString(a_cmd_0), ". Symbol=", a_symbol_4, ". Lot Size=", a_minlot_12, ". Price=", l_price_72, ". SL=", a_price_20, ". TP=", a_price_28); Alert("M-ERROR: Could not open new position. Error=", l_error_96, " ", ErrorDescription(l_error_96), ". Order Info : Order Type=", OrderTypetoString(a_cmd_0), ". Symbol=", a_symbol_4, ". Lot Size=", a_minlot_12, ". Price=", l_price_72, ". SL=", a_price_20, ". TP=", a_price_28); if (l_error_96 == 130/* INVALID_STOPS */) { } if (SendMailMode) { g_str_concat_288 = "EA: "; g_str_concat_288 = StringConcatenate(g_str_concat_288, EAName, " ", a_symbol_4, " ", OrderTypetoString(a_cmd_0), " order FAILED"); g_str_concat_280 = "email generated by Molanis Strategy Builder v3.1 BETA\n"; g_str_concat_280 = StringConcatenate(g_str_concat_280, "Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), ". Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "WARNING: Could not open new position.\n Error=", l_error_96, "\n", ErrorDescription(l_error_96), ".\n Order Info :\nOrder Type=", OrderTypetoString(a_cmd_0), ".\n Symbol=", a_symbol_4, ".\n Lot Size=", a_minlot_12, ".\n Price=", l_price_72, ".\n SL=", a_price_20, ".\n TP=", a_price_28); SendMail(g_str_concat_288, g_str_concat_280); } return (-1); } } return (0); } double LotManagement(string a_symbol_0, double ad_8) { double l_minlot_16 = ad_8; int l_hist_total_24 = OrdersHistoryTotal(); int l_count_28 = 0; int l_count_32 = 0; if (!UseLotManagement) { if (l_minlot_16 < MarketInfo(a_symbol_0, MODE_MINLOT)) { l_minlot_16 = MarketInfo(a_symbol_0, MODE_MINLOT); Print("M-Warning: Your lot size of ", ad_8, " is lower than the minimum size allowed by the server. Lot size changed to ", l_minlot_16); } if (l_minlot_16 > 100.0) { l_minlot_16 = 100; Print("M-Warning: Your lot size of ", ad_8, " is higher than the maximum size allowed by the server. Lot size changed to ", l_minlot_16); } return (l_minlot_16); } for (int l_pos_36 = l_hist_total_24 - 1; l_pos_36 >= 0; l_pos_36--) { if (OrderSelect(l_pos_36, SELECT_BY_POS, MODE_HISTORY) == FALSE) { Print("M-Error in history when running Lot Management!"); break; } if (OrderSymbol() != a_symbol_0 || OrderType() > OP_SELL) continue; if (OrderProfit() > 0.0) break; if (OrderProfit() < 0.0) l_count_28++; } for (l_pos_36 = l_hist_total_24 - 1; l_pos_36 >= 0; l_pos_36--) { if (OrderSelect(l_pos_36, SELECT_BY_POS, MODE_HISTORY) == FALSE) { Print("M-Error in history when running Lot Management!"); break; } if (OrderSymbol() != a_symbol_0 || OrderType() > OP_SELL) continue; if (OrderProfit() < 0.0) break; if (OrderProfit() > 0.0) l_count_32++; } int li_40 = 1; if (IsMicroAccount) li_40 = 2; if (LotManagementType == 1) { if (ReductionFactor > 0.0) { if (l_count_28 > 0) { l_minlot_16 = NormalizeDouble(l_minlot_16 - l_minlot_16 * l_count_28 / ReductionFactor, li_40); if (l_minlot_16 < MarketInfo(a_symbol_0, MODE_MINLOT)) { l_minlot_16 = MarketInfo(a_symbol_0, MODE_MINLOT); Print("M-MoneyManagementType=1. Loss number=", l_count_28, ". Lot adjusted to minimum size allowed by server of ", l_minlot_16, " from ", ad_8); } else Print("M-MoneyManagementType=1. Loss number=", l_count_28, ". Lot adjusted to ", l_minlot_16, " from ", ad_8); } } } else { if (LotManagementType == 2) { if (l_count_28 == 1) { l_minlot_16 = NormalizeDouble(0.75 * l_minlot_16, li_40); if (l_minlot_16 < MarketInfo(a_symbol_0, MODE_MINLOT)) { l_minlot_16 = MarketInfo(a_symbol_0, MODE_MINLOT); Print("M-MoneyManagementType=2. 1st loss, Lot adjusted to minimum size allowed by server of ", l_minlot_16, " from ", ad_8); } else Print("M-MoneyManagementType=2. 1st loss, Lot adjusted to 75% of lot =", l_minlot_16, " from ", ad_8); } if (l_count_28 == 2) { l_minlot_16 = NormalizeDouble(l_minlot_16 / 2.0, li_40); if (l_minlot_16 < MarketInfo(a_symbol_0, MODE_MINLOT)) { l_minlot_16 = MarketInfo(a_symbol_0, MODE_MINLOT); Print("M-MoneyManagementType=2. 2nd loss, Lot adjusted to minimum size allowed by server of ", l_minlot_16, " from ", ad_8); } else Print("M-MoneyManagementType=2. 2nd loss, Lot adjusted to 50% of lot =", l_minlot_16, " from ", ad_8); } if (l_count_28 >= 3) { l_minlot_16 = NormalizeDouble(l_minlot_16 / 4.0, li_40); if (l_minlot_16 < MarketInfo(a_symbol_0, MODE_MINLOT)) { l_minlot_16 = MarketInfo(a_symbol_0, MODE_MINLOT); Print("M-MoneyManagementType=2. 3rd loss, Lot adjusted to minimum size allowed by server of ", l_minlot_16, " from ", ad_8); } else Print("M-MoneyManagementType=2. 3rd loss, Lot adjusted to 25% of lot =", l_minlot_16, " from ", ad_8); } } } return (l_minlot_16); } int CalculateOpenPositions(string a_symbol_0, int ai_8, int a_magic_12) { int l_count_16 = 0; int l_count_20 = 0; bool li_24 = TRUE; if (!(ai_8 == 0) && !(ai_8 == 1)) { Print("M-Can not calculate open positions. Order type is wrong."); Alert("M-Can not calculate open positions. Order type is wrong."); return (-1); } int l_ord_total_28 = OrdersTotal(); for (int l_pos_32 = 0; l_pos_32 < l_ord_total_28; l_pos_32++) { if (OrderSelect(l_pos_32, SELECT_BY_POS, MODE_TRADES)) { if (a_magic_12 == 0) li_24 = TRUE; else { if (OrderMagicNumber() == a_magic_12) li_24 = TRUE; else li_24 = FALSE; } if (OrderSymbol() == a_symbol_0 && li_24) { if (OrderType() == OP_SELL) l_count_16++; if (OrderType() == OP_BUY) l_count_20++; } } } if (ai_8 == 1) return (l_count_16); if (ai_8 == 0) return (l_count_20); return (-1); } string OrderTypetoString(int ai_0) { if (ai_0 == 0) return ("OP_BUY"); if (ai_0 == 1) return ("OP_SELL"); if (ai_0 == 4) return ("OP_BUYSTOP"); if (ai_0 == 5) return ("OP_SELLSTOP"); if (ai_0 == 2) return ("OP_BUYLIMIT"); if (ai_0 == 3) return ("OP_SELLLIMIT"); return ("Unknow order type"); } int CloseAllPositions(string a_symbol_0, int a_cmd_8, int a_magic_12) { int l_error_20; double l_price_28; int l_count_24 = 0; bool li_36 = TRUE; if (!(a_cmd_8 == OP_BUY) && !(a_cmd_8 == OP_SELL)) { Print("M-Closing all positions failed. Order type is wrong."); Alert("M-Closing all positions failed. Order type is wrong."); return (-1); } int l_ord_total_40 = OrdersTotal(); bool li_44 = TRUE; while (li_44) { li_44 = FALSE; l_ord_total_40 = OrdersTotal(); for (int l_pos_16 = 0; l_pos_16 < l_ord_total_40; l_pos_16++) { if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) { if (a_magic_12 == 0) li_36 = TRUE; else { if (OrderMagicNumber() == a_magic_12) li_36 = TRUE; else li_36 = FALSE; } if (OrderType() == a_cmd_8 && OrderSymbol() == a_symbol_0 && li_36) { RefreshRates(); if (a_cmd_8 == OP_BUY) l_price_28 = MarketInfo(a_symbol_0, MODE_BID); else l_price_28 = MarketInfo(a_symbol_0, MODE_ASK); if (!isTradingPossible()) Print("M-Warning: Trading may not be possible. Trying to close orders."); if (OrderClose(OrderTicket(), OrderLots(), l_price_28, Slippage, Violet)) { Print("M-Order Closed. Symbol:", a_symbol_0, ". Lots:", OrderLots(), ". Ticket number:", OrderTicket(), ". Closed at price:", OrderClosePrice(), ". Position was opened at price:", OrderOpenPrice(), " Profit:", OrderProfit()); if (SendMailMode) { g_str_concat_288 = "EA: "; g_str_concat_288 = StringConcatenate(g_str_concat_288, EAName, ". ", a_symbol_0, " ", OrderTypetoString(a_cmd_8), " position was closed. ", "Ticket:", OrderTicket()); g_str_concat_280 = "email generated by Molanis Strategy Builder v3.1 BETA\n"; g_str_concat_280 = StringConcatenate(g_str_concat_280, "Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), ". Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "M-Order Closed\nOrder Info : \n Order Closed Type=", OrderTypetoString(a_cmd_8), ".\n Symbol=", a_symbol_0, ".\n Lot Size=", OrderLots(), ".\n Ticket:", OrderTicket(), ".\n Closed at price:", OrderClosePrice(), ".\n Position was opened at price:", OrderOpenPrice(), ".\n Profit:", OrderProfit()); SendMail(g_str_concat_288, g_str_concat_280); } li_44 = TRUE; l_count_24 = 1; break; } l_error_20 = GetLastError(); Print("M-Closing Order failed. Symbol:", a_symbol_0, ". Lots:", OrderLots(), ". Ticket number:", OrderTicket(), ". Error:", l_error_20, " ", ErrorDescription(l_error_20)); Alert("M-Closing Order failed. Symbol:", a_symbol_0, ". Lots:", OrderLots(), ". Ticket number:", OrderTicket(), ". Error:", l_error_20, " ", ErrorDescription(l_error_20)); if (SendMailMode) { g_str_concat_288 = "EA: "; g_str_concat_288 = StringConcatenate(g_str_concat_288, EAName, " WARNING:", a_symbol_0, " ", OrderTypetoString(a_cmd_8), " position could NOT be closed"); g_str_concat_280 = "email generated by Molanis Strategy Builder v3.1 BETA\n"; g_str_concat_280 = StringConcatenate(g_str_concat_280, "Server Time=", TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS), ". Terminal Time=", TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"); g_str_concat_280 = StringConcatenate(g_str_concat_280, "M-Closing Order failed.\nSymbol:", a_symbol_0, ".\n Lots:", OrderLots(), ".\n Ticket number:", OrderTicket(), ".\n Error:", l_error_20, "\n", ErrorDescription(l_error_20)); SendMail(g_str_concat_288, g_str_concat_280); } li_44 = TRUE; l_count_24++; break; } } } if (l_count_24 > MaxOrderRetry + 10) { Print("M-Stopping Closing all positions. Too many errors when closing positions. Symbol:", a_symbol_0); Print("M-URGENT:Please verify your Internet Connection and Server response."); Alert("M-Stopping Closing all positions. Too many errors when closing positions. Symbol:", a_symbol_0); Alert("M-URGENT:Please verify your Internet Connection and Server response."); return (-1); } } return (0); } bool isTradingPossible() { bool li_ret_0 = TRUE; if (!IsConnected()) { Print("M-URGENT ACTION REQUIRED : There is no connection to the server!"); Alert("URGENT ACTION REQUIRED : There is no connection to the server!"); li_ret_0 = FALSE; } if (IsStopped()) { Print("M-ERROR : EA was commanded to stop its operation!"); Alert("ERROR : EA was commanded to stop its operation!"); li_ret_0 = FALSE; } if (!IsTradeAllowed()) { Print("M-ERROR : Trading is not allowed!"); Alert("ERROR : Trading is not allowed!"); li_ret_0 = FALSE; } return (li_ret_0); } double CheckStopLoss(string a_symbol_0, double ad_8, double ad_16) { double ld_24; if (gi_144) { if (ad_16 == 0.0) return (ad_16); ld_24 = 3.0 * MarketInfo(a_symbol_0, MODE_STOPLEVEL) * MarketInfo(a_symbol_0, MODE_POINT); Print("M-Minimum stop loss allowed=", ld_24); if (MathAbs(ad_8 - ad_16) <= ld_24) { if (ad_8 > ad_16) ad_16 = ad_8 - ld_24; else { if (ad_8 < ad_16) ad_16 = ad_8 + ld_24; else { Print("M-ERROR: Could not adjust stop loss, SL=", ad_16); return (ad_16); } } ad_16 = NormalizeDouble(ad_16, MarketInfo(a_symbol_0, MODE_DIGITS)); Print("M-Stop Loss was too small. It was changed. New SL=", ad_16); } return (ad_16); } return (ad_16); } int CheckTrailingStop(string a_symbol_0, int ai_8, int a_magic_12, double ad_16) { bool li_44; if (ad_16 == 0.0) return (0); if (!(ai_8 == 0) && !(ai_8 == 1)) { Print("M-Check Trailing Stop failed. Order type is wrong."); return (-1); } double l_pips_24 = ad_16; double l_ord_stoploss_32 = 0; int l_error_40 = 0; if (a_magic_12 == 0) li_44 = TRUE; else { if (OrderMagicNumber() == a_magic_12) li_44 = TRUE; else li_44 = FALSE; } for (int l_pos_48 = 0; l_pos_48 < OrdersTotal(); l_pos_48++) { if (OrderSelect(l_pos_48, SELECT_BY_POS, MODE_TRADES) == FALSE) Print("M-Warning: Could not select order to ckeck change on trailing stop. Continuing..."); else { if (OrderSymbol() == a_symbol_0 && li_44) { RefreshRates(); if (ai_8 == 1) { if (l_pips_24 > 0.0) { RefreshRates(); if (OrderOpenPrice() - MarketInfo(a_symbol_0, MODE_ASK) > 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24)) { if (OrderStopLoss() > MarketInfo(a_symbol_0, MODE_ASK) + 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24) || OrderStopLoss() == 0.0) { l_ord_stoploss_32 = OrderStopLoss(); if (OrderModify(OrderTicket(), OrderOpenPrice(), MarketInfo(a_symbol_0, MODE_ASK) + 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24), OrderTakeProfit(), 0, Red)) Print("M-SL changed using trailing stop. Old SL=", l_ord_stoploss_32, " New SL=", MarketInfo(a_symbol_0, MODE_ASK) + 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24)); else { l_error_40 = GetLastError(); Print("M-Warning: Could not change SL value with trailing stop. Error:", l_error_40, " ", ErrorDescription(l_error_40)); } } } } } if (ai_8 == 0) { if (l_pips_24 > 0.0) { RefreshRates(); if (MarketInfo(a_symbol_0, MODE_BID) - OrderOpenPrice() > 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24)) { if (OrderStopLoss() < MarketInfo(a_symbol_0, MODE_BID) - 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24)) { l_ord_stoploss_32 = OrderStopLoss(); if (OrderModify(OrderTicket(), OrderOpenPrice(), MarketInfo(a_symbol_0, MODE_BID) - 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24), OrderTakeProfit(), 0, Green)) Print("M-SL changed using trailing stop. Old SL=", l_ord_stoploss_32, " New SL=", MarketInfo(a_symbol_0, MODE_BID) - 10.0 * (MarketInfo(a_symbol_0, MODE_POINT) * l_pips_24)); else { l_error_40 = GetLastError(); Print("M-Warning: Could not change SL value with trailing stop. Error:", l_error_40, " ", ErrorDescription(l_error_40)); } } } } } } } } return (1); } double CheckTakeProfit(string a_symbol_0, double ad_8, double ad_16) { double ld_24; if (gi_144) { if (ad_16 == 0.0) return (ad_16); ld_24 = 3.0 * MarketInfo(a_symbol_0, MODE_STOPLEVEL) * MarketInfo(a_symbol_0, MODE_POINT); Print("M-Minimum take profit allowed=", ld_24); if (MathAbs(ad_8 - ad_16) <= ld_24) { if (ad_8 < ad_16) ad_16 = ad_8 + ld_24; else { if (ad_8 > ad_16) ad_16 = ad_8 - ld_24; else { Print("M-ERROR: Could not adjust take profit, TP=", ad_16); return (ad_16); } } ad_16 = NormalizeDouble(ad_16, MarketInfo(a_symbol_0, MODE_DIGITS)); Print("M-Take Profit too small. It was changed. New TP=", ad_16); } return (ad_16); } return (ad_16); }