#property copyright "Copyright © 2009, Gridbot" #property link "http://www.forexgridbot.com/" #include #import "wininet.dll" int InternetOpenA(string a0, int a1, string a2, string a3, int a4); int InternetOpenUrlA(int a0, string a1, string a2, int a3, int a4, int a5); int InternetReadFile(int a0, string a1, int a2, int& a3[]); int InternetCloseHandle(int a0); #import bool gi_unused_76 = FALSE; int gi_80; int gi_84; int gi_88 = 0; int gi_92 = 1; int gi_unused_96 = 3; int gi_100 = 13; int gi_104 = 10; double gd_108 = 4.0; double gd_116 = 25.0; string gs_unused_124 = "OrderReliable fname unset"; int g_error_132 = 0/* NO_ERROR */; int gi_136 = 2; extern double PerOrderLotsize = 0.2; int gi_148 = 20; extern int GridMaxOrders = 3; int gi_156 = 20; int gi_160 = 19; extern double RestartProfit = 100.0; int gi_172 = 1; extern double SafetyNet = -500.0; extern string TradingDaysOfWeek = "1,2,3,4"; extern int StartHour = 16; extern int EndHour = 2; int gi_200 = 2; bool gi_unused_204 = FALSE; extern bool StopNow = FALSE; int g_slippage_212 = 30; double gd_216 = 2.73; string gs_224 = ""; string gs_unused_232 = ""; bool gi_unused_240 = FALSE; int gi_244 = 5; int g_count_248 = 0; int g_index_252 = 0; double gd_unused_256 = 0.0; double gd_unused_264 = 0.0; int gia_272[500]; double gd_276 = 0.0; double gd_284 = 0.0; string gs_292 = ""; bool gi_300 = FALSE; double gd_unused_304 = 1.3; double gd_312 = 0.0; double gd_unused_320 = 0.0; int g_magic_328 = 0; double gd_332 = 0.0; double gd_340 = 0.0; double g_point_348 = 0.0; double gd_unused_356 = 0.0; double gd_unused_364 = 0.0; double gd_372 = 0.0; double g_spread_380 = 0.0; double gd_unused_388 = 0.0; double gd_396; double gd_404 = 0.0; int g_day_of_week_412; int g_hour_416 = 0; bool gi_420 = FALSE; int gi_unused_424 = 14; double gd_428; double gd_436 = 0.0; double gd_unused_444; double g_lotsize_452; double g_lotstep_460; double g_point_468; double g_ticksize_476; double g_tickvalue_484 = 0.0; int init() { Print("Brokerage: " + AccountCompany()); if (AccountCompany() == "CMS Forex") { StartHour++; EndHour++; StartHour %= 24; EndHour %= 24; } if (Point == 0.001) g_point_348 = 0.01; else { if (Point == 0.00001) g_point_348 = 0.0001; else g_point_348 = Point; } gd_unused_444 = AccountLeverage(); g_lotsize_452 = MarketInfo(Symbol(), MODE_LOTSIZE); g_lotstep_460 = MarketInfo(Symbol(), MODE_LOTSTEP); g_ticksize_476 = MarketInfo(Symbol(), MODE_TICKSIZE); g_tickvalue_484 = MarketInfo(Symbol(), MODE_TICKVALUE); g_point_468 = MarketInfo(Symbol(), MODE_POINT); if (!IsTesting() && !IsDemo()) { gi_420 = ConfirmLicense(); if (!gi_420) Alert("This account number is not licensed for live trading. Please contact customer support."); } else gi_420 = TRUE; InitGrid(); return (0); } int deinit() { return (0); } int start() { string ls_0 = ""; int li_8 = 0; int li_12 = 0; int li_16 = 0; li_8 = CheckAllowEntry(); if (!gi_420) { if (!gi_420) { Alert("This account number is not licensed for live trading. Please contact customer support."); return (0); } } if (StopNow) { if (gi_300) { GridStop(); Reset(); } else { if (StartHour != EndHour && Hour() != StartHour) { Reset(); StopNow = FALSE; } } return (0); } if (gi_300) { if (ExitTest()) { GridStop(); Reset(); } else { li_16 = IsLimitReached(); if (li_16 != 0) { li_12 = OpenMarketOrder(li_16); UpdateLimits(li_12, li_16); } if (!GridMonitor()) { if (gi_172 > 0 && g_count_248 == gi_172) { StopNow = TRUE; return (0); } gs_224 = Month() + "/" + Day() + "/" + Year(); } else { if (gd_276 < SafetyNet) { GridStop(); StopNow = TRUE; Alert("SAFETY NET DRAWDOWN HIT! GridTrader has been disabled."); } } } } else if (EntryTest() && li_8) GridStart(3); if (gd_276 < gd_396) { gd_396 = gd_276; g_day_of_week_412 = DayOfWeek(); g_hour_416 = Hour(); } if (AccountMargin() > gd_404) gd_404 = AccountMargin(); ls_0 = "UpperLimit: " + gd_332 + " LowerLimit: " + gd_340 + " Orders: " + g_index_252 + "\n" + "Close Profit: " + gd_284 + " Open Profit: " + gd_276 + " Runs Closed: " + g_count_248 + "\n"; if (gi_300 == TRUE) ls_0 = ls_0 + "Grid is currently active and looking for trades. \nGrid Version=" + DoubleToStr(gd_216, 2); else ls_0 = ls_0 + "Grid is not looking for trades. \nHour = " + Hour() + " Trading Start Time is = " + StartHour + " \nGrid Version = " + DoubleToStr(gd_216, 2); Comment(ls_0); return (0); } void UpdateLimits(int a_ticket_0, int ai_4) { if (OrderSelect(a_ticket_0, SELECT_BY_TICKET, MODE_TRADES)) { gia_272[g_index_252] = a_ticket_0; g_index_252++; if (ai_4 == 1) { gd_332 = OrderOpenPrice() + NormalizeDouble(gi_148 * g_point_348, Digits); return; } if (ai_4 == 2) gd_340 = OrderOpenPrice() - NormalizeDouble(gi_148 * g_point_348, Digits); } } int IsLimitReached() { int li_ret_0 = 0; if (g_index_252 < GridMaxOrders) { if (gi_136 == 1) { if (Ask >= gd_332) li_ret_0 = 1; else if (Bid <= gd_340) li_ret_0 = 2; } else { if (gi_136 == 2) { if (Bid >= gd_332) li_ret_0 = 1; else if (Ask <= gd_340) li_ret_0 = 2; } } } return (li_ret_0); } int OpenMarketOrder(int ai_0) { int l_ticket_4 = 0; int l_cmd_8 = 0; double l_price_12 = 0.0; if (gi_136 == 1) { if (ai_0 == 1) { l_cmd_8 = 0; l_price_12 = gd_332; } else { if (ai_0 == 2) { l_cmd_8 = 1; l_price_12 = gd_340; } } } else { if (gi_136 == 2) { if (ai_0 == 1) { l_cmd_8 = 1; l_price_12 = gd_332; } else { if (ai_0 == 2) { l_cmd_8 = 0; l_price_12 = gd_340; } } } } if (IsTesting()) l_ticket_4 = OrderSend(Symbol(), l_cmd_8, PerOrderLotsize, l_price_12, g_slippage_212, 0, 0, "GridOrder", g_magic_328, 0, Green); else l_ticket_4 = OrderSendReliableMKT(Symbol(), l_cmd_8, PerOrderLotsize, l_price_12, g_slippage_212, 0, 0, "GridOrder", g_magic_328, 0, Green); return (l_ticket_4); } int EntryTest() { bool li_ret_0 = TRUE; return (li_ret_0); } bool ExitTest() { bool li_ret_0 = FALSE; if (StartHour != EndHour && (Hour() >= EndHour && StartHour < EndHour) || (EndHour < StartHour && Hour() >= EndHour && Hour() < StartHour)) { if (gi_200 == 2) { StopNow = TRUE; li_ret_0 = TRUE; } } return (li_ret_0); } void Reset() { g_count_248 = 0; } int CheckAllowEntry() { bool li_ret_0 = FALSE; string ls_4 = ""; for (int li_12 = 0; li_12 < StringLen(TradingDaysOfWeek); li_12++) { ls_4 = StringSubstr(TradingDaysOfWeek, li_12, 1); if (ls_4 != ",") if (StrToInteger(ls_4) == DayOfWeek()) li_ret_0 = TRUE; } if (li_ret_0) { if (StartHour == EndHour) li_ret_0 = TRUE; else { if (Hour() == StartHour) li_ret_0 = TRUE; else { if (g_count_248 != 0 && StartHour != EndHour && (Hour() < EndHour && Hour() >= StartHour && StartHour < EndHour) || (EndHour < StartHour && Hour() >= StartHour || Hour() < EndHour)) li_ret_0 = TRUE; else li_ret_0 = FALSE; } } } return (li_ret_0); } void InitGrid() { g_magic_328 = GetMagicNumber(0); gd_312 = NormalizeDouble(MarketInfo(Symbol(), MODE_TICKVALUE), 2); gd_unused_320 = PerOrderLotsize; g_spread_380 = MarketInfo(Symbol(), MODE_SPREAD); if (GridCurrentPositions()) gi_300 = TRUE; } void GridStart(int ai_unused_0 = 3) { int li_unused_28; double ld_unused_20 = 0.0; int li_unused_32 = 0; double ld_unused_44 = 0.0; gi_300 = TRUE; if (gi_136 == 1) { li_unused_28 = 4; li_unused_32 = 5; gd_332 = Ask + NormalizeDouble(gi_156 * g_point_348, Digits); gd_340 = Bid - NormalizeDouble(gi_156 * g_point_348, Digits); } else { li_unused_28 = 3; li_unused_32 = 2; gd_332 = Bid + NormalizeDouble(gi_156 * g_point_348, Digits); gd_340 = Ask - NormalizeDouble(gi_156 * g_point_348, Digits); } gd_unused_356 = Bid; gd_unused_364 = Ask; } void GridStop() { bool li_4 = TRUE; while (li_4) { li_4 = FALSE; for (int l_pos_0 = OrdersTotal() - 1; li_4 == FALSE && l_pos_0 >= 0; l_pos_0--) { if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == g_magic_328) { if (OrderType() == OP_BUY) OrderCloseReliable(OrderTicket(), OrderLots(), Bid, g_slippage_212, CLR_NONE); else { if (OrderType() == OP_SELL) OrderCloseReliable(OrderTicket(), OrderLots(), Ask, g_slippage_212, CLR_NONE); else OrderDeleteReliable(OrderTicket()); } li_4 = TRUE; } } } } ArrayInitialize(gia_272, 0); gd_276 = 0.0; gd_284 = 0.0; gd_unused_256 = 0.0; gd_unused_264 = 0.0; g_count_248++; g_index_252 = 0; gi_300 = FALSE; } bool GridMonitor() { int l_ticket_0; double l_price_24; double l_ord_open_price_80; double l_ord_open_price_88; int l_count_4 = 0; int li_unused_20 = 0; double ld_unused_40 = 0.0; double ld_unused_56 = 0.0; int li_unused_68 = 0; double ld_72 = 0.0; double ld_unused_96 = 0.0; int li_104 = 0; int li_unused_108 = 0; bool li_112 = FALSE; bool li_ret_116 = TRUE; int li_120 = 0; gd_276 = 0.0; ArraySort(gia_272, WHOLE_ARRAY, 0, MODE_DESCEND); li_120 = g_index_252; for (int l_index_64 = 0; l_index_64 < li_120; l_index_64++) { if (gia_272[l_index_64] == 0) break; l_ticket_0 = gia_272[l_index_64]; if (OrderSelect(l_ticket_0, SELECT_BY_TICKET, MODE_TRADES)) { if (OrderCloseTime() != 0) { gd_284 += OrderProfit() + OrderSwap(); if (gi_300) { if (gi_136 == 1) { if (OrderType() == OP_BUY) gd_332 = OrderOpenPrice(); else if (OrderType() == OP_SELL) gd_340 = OrderOpenPrice(); } else { if (OrderType() == OP_BUY) gd_340 = OrderOpenPrice(); else if (OrderType() == OP_SELL) gd_332 = OrderOpenPrice(); } } gia_272[l_index_64] = 0; g_index_252--; } else { gd_276 += OrderProfit() + OrderSwap(); if (gi_136 == 1) { if (OrderType() == OP_BUY) { if (li_104 == 0) li_104 = 1; else if (li_104 == 2) li_104 = 3; l_price_24 = OrderOpenPrice() + NormalizeDouble(gi_160 * g_point_348, Digits); if (l_price_24 > gd_428) l_price_24 = gd_428; if (OrderTakeProfit() == 0.0) { if (IsTesting()) OrderModify(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); else OrderModifyReliable(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); } l_count_4++; } else { if (OrderType() == OP_SELL) { if (li_104 == 0) li_104 = 2; else if (li_104 == 1) li_104 = 3; l_price_24 = OrderOpenPrice() - NormalizeDouble(gi_160 * g_point_348, Digits); if (l_price_24 < gd_436) l_price_24 = gd_436; if (OrderTakeProfit() == 0.0) { if (IsTesting()) OrderModify(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); else OrderModifyReliable(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); } l_count_4++; } } } else { if (gi_136 == 2) { if (OrderType() == OP_BUY || OrderType() == OP_SELL) { if (OrderOpenPrice() > l_ord_open_price_80) l_ord_open_price_80 = OrderOpenPrice(); if (OrderOpenPrice() < l_ord_open_price_88 || l_ord_open_price_88 == 0.0) l_ord_open_price_88 = OrderOpenPrice(); } if (OrderType() == OP_BUY) { if (li_104 == 0) li_104 = 2; else if (li_104 == 1) li_104 = 3; l_price_24 = OrderOpenPrice() + NormalizeDouble(gi_160 * g_point_348, Digits); if (OrderTakeProfit() == 0.0) { if (IsTesting()) OrderModify(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); else OrderModifyReliable(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); } l_count_4++; } else { if (OrderType() == OP_SELL) { if (li_104 == 0) li_104 = 1; else if (li_104 == 2) li_104 = 3; l_price_24 = OrderOpenPrice() - NormalizeDouble(gi_160 * g_point_348, Digits); if (OrderTakeProfit() == 0.0) { if (IsTesting()) OrderModify(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); else OrderModifyReliable(OrderTicket(), OrderOpenPrice(), 0, l_price_24, 0, CLR_NONE); } l_count_4++; } } } } } } } ld_72 = RestartProfit; if (NormalizeDouble(gd_276 + gd_284, 2) >= NormalizeDouble(ld_72, 2)) li_112 = TRUE; if (li_112) { GridStop(); li_ret_116 = FALSE; } gs_292 = "Grid Disposition: " + gi_136 + " Grid Spacing: " + gi_148 + " Grid Orders: " + GridMaxOrders + " Grid Lot Start: " + gd_372 + " Grid Growth: " + gi_244 + " Take profit pips: " + gi_160 + "\n" + "Run Limit: " + gi_172 + " Runs Closed: " + g_count_248 + "\n" + "Restart Profit: " + ld_72 + " Current Profit: " + ((gd_284 + gd_276)) + " (Closed: " + gd_284 + " + Open: " + gd_276 + ")\n" + "Direction: " + li_104 + "\n" + "Position High: " + l_ord_open_price_80 + " Position Low: " + l_ord_open_price_88; return (li_ret_116); } bool GridCurrentPositions() { bool li_ret_0 = FALSE; double l_ord_lots_4 = 0.0; for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) { if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == g_magic_328) { g_index_252++; gia_272[g_index_252 - 1] = OrderTicket(); if (l_ord_lots_4 == 0.0) l_ord_lots_4 = OrderLots(); else if (OrderLots() < l_ord_lots_4) l_ord_lots_4 = OrderLots(); g_index_252++; } } } if (g_index_252 > 0) { g_index_252 /= 2; GridMaxOrders = g_index_252 / 2; li_ret_0 = TRUE; } return (li_ret_0); } int OrderSendReliableMKT(string a_symbol_0, int a_cmd_8, double a_lots_12, double ad_20, int ai_28, double a_price_32, double a_price_40, string a_comment_48, int a_magic_56, int a_datetime_60 = 0, color a_color_64 = -1) { double l_price_88; int l_slippage_96; gs_unused_124 = "OrderSendReliableMKT"; OrderReliablePrint(" attempted " + OrderReliable_CommandString(a_cmd_8) + " " + a_lots_12 + " lots @" + ad_20 + " sl:" + a_price_32 + " tp:" + a_price_40); if (a_cmd_8 != OP_BUY && a_cmd_8 != OP_SELL) { OrderReliablePrint("Improper non market-order command passed. Nothing done."); g_error_132 = 9; return (-1); } if (!IsConnected()) { OrderReliablePrint("error: IsConnected() == false"); g_error_132 = 6; return (-1); } if (IsStopped()) { OrderReliablePrint("error: IsStopped() == true"); g_error_132 = 2; return (-1); } for (int l_count_68 = 0; !IsTradeAllowed() && l_count_68 < gi_104; l_count_68++) OrderReliable_SleepRandomTime(gd_108, gd_116); if (!IsTradeAllowed()) { OrderReliablePrint("error: no operation possible because IsTradeAllowed()==false, even after retries."); g_error_132 = 146; return (-1); } int l_digits_72 = MarketInfo(a_symbol_0, MODE_DIGITS); if (l_digits_72 > 0) { ad_20 = NormalizeDouble(ad_20, l_digits_72); a_price_32 = NormalizeDouble(a_price_32, l_digits_72); a_price_40 = NormalizeDouble(a_price_40, l_digits_72); } if (a_price_32 != 0.0) OrderReliable_EnsureValidStop(a_symbol_0, ad_20, a_price_32); int l_error_76 = GetLastError(); l_error_76 = 0; g_error_132 = 0; bool li_80 = FALSE; int l_ticket_84 = -1; l_error_76 = GetLastError(); l_error_76 = 0; g_error_132 = 0; l_ticket_84 = -1; if (a_cmd_8 == OP_BUY || a_cmd_8 == OP_SELL) { l_count_68 = 0; while (!li_80) { if (IsTradeAllowed()) { l_price_88 = ad_20; l_slippage_96 = ai_28; if (a_cmd_8 == OP_BUY) { l_price_88 = NormalizeDouble(MarketInfo(a_symbol_0, MODE_ASK), MarketInfo(a_symbol_0, MODE_DIGITS)); if (l_price_88 > ad_20) l_slippage_96 = ai_28 - (l_price_88 - ad_20) / Point; } else { if (a_cmd_8 == OP_SELL) { l_price_88 = NormalizeDouble(MarketInfo(a_symbol_0, MODE_BID), MarketInfo(a_symbol_0, MODE_DIGITS)); if (l_price_88 < ad_20) l_slippage_96 = ai_28 - (ad_20 - l_price_88) / Point; } } if (l_slippage_96 > ai_28) l_slippage_96 = ai_28; if (l_slippage_96 >= 0) { l_ticket_84 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, l_price_88, l_slippage_96, a_price_32, a_price_40, a_comment_48, a_magic_56, a_datetime_60, a_color_64); l_error_76 = GetLastError(); g_error_132 = l_error_76; } else { l_error_76 = 129; g_error_132 = l_error_76; } } else l_count_68++; switch (l_error_76) { case 135/* PRICE_CHANGED */: continue; case 138/* REQUOTE */: continue; case 0/* NO_ERROR */: li_80 = TRUE; break; case 4/* SERVER_BUSY */: case 6/* NO_CONNECTION */: case 129/* INVALID_PRICE */: case 136/* OFF_QUOTES */: case 137/* BROKER_BUSY */: case 146/* TRADE_CONTEXT_BUSY */: l_count_68++; break; default: li_80 = TRUE; } if (l_count_68 > gi_104) li_80 = TRUE; if (!li_80) { OrderReliablePrint("retryable error (" + l_count_68 + "/" + gi_104 + "): " + OrderReliableErrTxt(l_error_76)); OrderReliable_SleepRandomTime(gd_108, gd_116); } if (li_80) { if (l_error_76 != 0/* NO_ERROR */) OrderReliablePrint("non-retryable error: " + OrderReliableErrTxt(l_error_76)); if (l_count_68 > gi_104) OrderReliablePrint("retry attempts maxed at " + gi_104); } } if (l_error_76 == 0/* NO_ERROR */) { OrderReliablePrint("apparently successful OP_BUY or OP_SELL order placed, details follow."); OrderSelect(l_ticket_84, SELECT_BY_TICKET, MODE_TRADES); OrderPrint(); return (l_ticket_84); } OrderReliablePrint("failed to execute OP_BUY/OP_SELL, after " + l_count_68 + " retries"); OrderReliablePrint("failed trade: " + OrderReliable_CommandString(a_cmd_8) + " " + a_symbol_0 + "@" + ad_20 + " tp@" + a_price_40 + " sl@" + a_price_32); OrderReliablePrint("last error: " + OrderReliableErrTxt(l_error_76)); return (-1); } return (0); } int OrderModifyReliable(int a_ticket_0, double a_price_4, double a_price_12, double a_price_20, int a_datetime_28, color a_color_32 = -1) { string ls_40; gs_unused_124 = "OrderModifyReliable"; OrderReliablePrint(" attempted modify of #" + a_ticket_0 + " price:" + a_price_4 + " sl:" + a_price_12 + " tp:" + a_price_20); if (!IsConnected()) { OrderReliablePrint("error: IsConnected() == false"); g_error_132 = 6; return (0); } if (IsStopped()) { OrderReliablePrint("error: IsStopped() == true"); return (0); } for (int l_count_36 = 0; !IsTradeAllowed() && l_count_36 < gi_104; l_count_36++) OrderReliable_SleepRandomTime(gd_108, gd_116); if (!IsTradeAllowed()) { OrderReliablePrint("error: no operation possible because IsTradeAllowed()==false, even after retries."); g_error_132 = 146; return (0); } int l_error_52 = GetLastError(); l_error_52 = 0; g_error_132 = 0; bool li_56 = FALSE; l_count_36 = 0; bool l_bool_60 = FALSE; while (!li_56) { if (IsTradeAllowed()) { l_bool_60 = OrderModify(a_ticket_0, a_price_4, a_price_12, a_price_20, a_datetime_28, a_color_32); l_error_52 = GetLastError(); g_error_132 = l_error_52; } else l_count_36++; if (l_bool_60 == TRUE) li_56 = TRUE; switch (l_error_52) { case 0/* NO_ERROR */: li_56 = TRUE; break; case 1/* NO_RESULT */: li_56 = TRUE; break; case 4/* SERVER_BUSY */: case 6/* NO_CONNECTION */: case 129/* INVALID_PRICE */: case 136/* OFF_QUOTES */: case 137/* BROKER_BUSY */: case 146/* TRADE_CONTEXT_BUSY */: case 128/* TRADE_TIMEOUT */: l_count_36++; break; case 135/* PRICE_CHANGED */: case 138/* REQUOTE */: RefreshRates(); continue; break; default: li_56 = TRUE; } if (l_count_36 > gi_104) li_56 = TRUE; if (!li_56) { OrderReliablePrint("retryable error (" + l_count_36 + "/" + gi_104 + "): " + OrderReliableErrTxt(l_error_52)); OrderReliable_SleepRandomTime(gd_108, gd_116); RefreshRates(); } if (li_56) { if (l_error_52 != 0/* NO_ERROR */ && l_error_52 != 1/* NO_RESULT */) OrderReliablePrint("non-retryable error: " + OrderReliableErrTxt(l_error_52)); if (l_count_36 > gi_104) OrderReliablePrint("retry attempts maxed at " + gi_104); } } if (l_bool_60 == TRUE || l_error_52 == 0/* NO_ERROR */) { OrderReliablePrint("apparently successful modification order, updated trade details follow."); OrderSelect(a_ticket_0, SELECT_BY_TICKET, MODE_TRADES); OrderPrint(); return (1); } if (l_error_52 == 1/* NO_RESULT */) { OrderReliablePrint("Server reported modify order did not actually change parameters."); OrderReliablePrint("redundant modification: " + a_ticket_0 + " " + ls_40 + "@" + a_price_4 + " tp@" + a_price_20 + " sl@" + a_price_12); OrderReliablePrint("Suggest modifying code logic to avoid."); return (1); } OrderReliablePrint("failed to execute modify after " + l_count_36 + " retries"); OrderReliablePrint("failed modification: " + a_ticket_0 + " " + ls_40 + "@" + a_price_4 + " tp@" + a_price_20 + " sl@" + a_price_12); OrderReliablePrint("last error: " + OrderReliableErrTxt(l_error_52)); return (0); } int OrderCloseReliable(int a_ticket_0, double a_lots_4, double a_price_12, int a_slippage_20, color a_color_24 = -1) { gs_unused_124 = "OrderCloseReliable"; OrderReliablePrint(" attempted close of #" + a_ticket_0 + " price:" + a_price_12 + " lots:" + a_lots_4 + " slippage:" + a_slippage_20); if (!OrderSelect(a_ticket_0, SELECT_BY_TICKET)) { g_error_132 = GetLastError(); OrderReliablePrint("error: " + ErrorDescription(g_error_132)); return (0); } int l_cmd_28 = OrderType(); string l_symbol_32 = Symbol(); if (l_cmd_28 != OP_BUY && l_cmd_28 != OP_SELL) { g_error_132 = 4108; OrderReliablePrint("error: trying to close ticket #" + a_ticket_0 + ", which is " + OrderReliable_CommandString(l_cmd_28) + ", not OP_BUY or OP_SELL"); return (0); } if (!IsConnected()) { OrderReliablePrint("error: IsConnected() == false"); g_error_132 = 6; return (0); } if (IsStopped()) { OrderReliablePrint("error: IsStopped() == true"); return (0); } int l_count_40 = 0; int l_error_44 = GetLastError(); l_error_44 = 0; g_error_132 = 0; bool li_48 = FALSE; l_count_40 = 0; bool l_ord_close_52 = FALSE; while (!li_48) { if (IsTradeAllowed()) { l_ord_close_52 = OrderClose(a_ticket_0, a_lots_4, a_price_12, a_slippage_20, a_color_24); l_error_44 = GetLastError(); g_error_132 = l_error_44; } else l_count_40++; if (l_ord_close_52 == 1) li_48 = TRUE; switch (l_error_44) { case 135/* PRICE_CHANGED */: continue; case 138/* REQUOTE */: continue; case 0/* NO_ERROR */: li_48 = TRUE; break; case 4/* SERVER_BUSY */: case 6/* NO_CONNECTION */: case 129/* INVALID_PRICE */: case 136/* OFF_QUOTES */: case 137/* BROKER_BUSY */: case 146/* TRADE_CONTEXT_BUSY */: case 128/* TRADE_TIMEOUT */: l_count_40++; break; default: li_48 = TRUE; } if (l_count_40 > gi_104) li_48 = TRUE; if (!li_48) { OrderReliablePrint("retryable error (" + l_count_40 + "/" + gi_104 + "): " + OrderReliableErrTxt(l_error_44)); OrderReliable_SleepRandomTime(gd_108, gd_116); if (l_cmd_28 == OP_BUY) a_price_12 = NormalizeDouble(MarketInfo(l_symbol_32, MODE_BID), MarketInfo(l_symbol_32, MODE_DIGITS)); if (l_cmd_28 == OP_SELL) a_price_12 = NormalizeDouble(MarketInfo(l_symbol_32, MODE_ASK), MarketInfo(l_symbol_32, MODE_DIGITS)); } if (li_48) { if (l_error_44 != 0/* NO_ERROR */ && l_error_44 != 1/* NO_RESULT */) OrderReliablePrint("non-retryable error: " + OrderReliableErrTxt(l_error_44)); if (l_count_40 > gi_104) OrderReliablePrint("retry attempts maxed at " + gi_104); } } if (l_ord_close_52 == 1 || l_error_44 == 0/* NO_ERROR */) { OrderReliablePrint("apparently successful close order."); return (1); } OrderReliablePrint("failed to execute close after " + l_count_40 + " retries"); OrderReliablePrint("failed close: Ticket #" + a_ticket_0 + ", Price: " + a_price_12 + ", Slippage: " + a_slippage_20); OrderReliablePrint("last error: " + OrderReliableErrTxt(l_error_44)); return (0); } int OrderDeleteReliable(int a_ticket_0) { gs_unused_124 = "OrderDeleteReliable"; OrderReliablePrint(" attempted deletion of pending order, #" + a_ticket_0); if (!OrderSelect(a_ticket_0, SELECT_BY_TICKET)) { g_error_132 = GetLastError(); OrderReliablePrint("error: " + ErrorDescription(g_error_132)); return (0); } int l_cmd_4 = OrderType(); string ls_8 = Symbol(); if (l_cmd_4 == OP_BUY || l_cmd_4 == OP_SELL) { g_error_132 = 4108; OrderReliablePrint("error: trying to close ticket #" + a_ticket_0 + ", which is " + OrderReliable_CommandString(l_cmd_4) + ", not OP_BUYSTOP, OP_SELLSTOP, OP_BUYLIMIT, or OP_SELLLIMIT"); return (0); } if (!IsConnected()) { OrderReliablePrint("error: IsConnected() == false"); g_error_132 = 6; return (0); } if (IsStopped()) { OrderReliablePrint("error: IsStopped() == true"); return (0); } int l_count_16 = 0; int l_error_20 = GetLastError(); l_error_20 = 0; g_error_132 = 0; bool li_24 = FALSE; l_count_16 = 0; bool l_ord_delete_28 = FALSE; while (!li_24) { if (IsTradeAllowed()) { l_ord_delete_28 = OrderDelete(a_ticket_0); l_error_20 = GetLastError(); g_error_132 = l_error_20; } else l_count_16++; if (l_ord_delete_28 == 1) li_24 = TRUE; switch (l_error_20) { case 135/* PRICE_CHANGED */: continue; case 138/* REQUOTE */: continue; case 0/* NO_ERROR */: li_24 = TRUE; break; case 4/* SERVER_BUSY */: case 6/* NO_CONNECTION */: case 129/* INVALID_PRICE */: case 136/* OFF_QUOTES */: case 137/* BROKER_BUSY */: case 146/* TRADE_CONTEXT_BUSY */: case 128/* TRADE_TIMEOUT */: l_count_16++; break; default: li_24 = TRUE; } if (l_count_16 > gi_104) li_24 = TRUE; if (!li_24) { OrderReliablePrint("retryable error (" + l_count_16 + "/" + gi_104 + "): " + OrderReliableErrTxt(l_error_20)); OrderReliable_SleepRandomTime(gd_108, gd_116); } if (li_24) { if (l_error_20 != 0/* NO_ERROR */ && l_error_20 != 1/* NO_RESULT */) OrderReliablePrint("non-retryable error: " + OrderReliableErrTxt(l_error_20)); if (l_count_16 > gi_104) OrderReliablePrint("retry attempts maxed at " + gi_104); } } if (l_ord_delete_28 == 1 || l_error_20 == 0/* NO_ERROR */) { OrderReliablePrint("apparently successful deletion order."); return (1); } OrderReliablePrint("failed to execute delete after " + l_count_16 + " retries"); OrderReliablePrint("failed deletion: Ticket #" + a_ticket_0); OrderReliablePrint("last error: " + OrderReliableErrTxt(l_error_20)); return (0); } string OrderReliableErrTxt(int ai_0) { return ("" + ai_0 + ":" + ErrorDescription(ai_0)); } void OrderReliablePrint(string as_unused_0) { } string OrderReliable_CommandString(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 ("(CMD==" + ai_0 + ")"); } void OrderReliable_EnsureValidStop(string a_symbol_0, double ad_8, double &ad_16) { double ld_24; if (ad_16 != 0.0) { ld_24 = MarketInfo(a_symbol_0, MODE_STOPLEVEL) * MarketInfo(a_symbol_0, MODE_POINT); 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 OrderReliablePrint("EnsureValidStop: error, passed in price == sl, cannot adjust"); } ad_16 = NormalizeDouble(ad_16, MarketInfo(a_symbol_0, MODE_DIGITS)); } } } void OrderReliable_SleepRandomTime(double ad_0, double ad_8) { double ld_16; int li_24; double ld_28; if (IsTesting() == 0) { ld_16 = MathCeil(ad_0 / 0.1); if (ld_16 > 0.0) { li_24 = MathRound(ad_8 / 0.1); ld_28 = 1.0 - 1.0 / ld_16; Sleep(100); for (int l_count_36 = 0; l_count_36 < li_24; l_count_36++) { if (MathRand() > 32768.0 * ld_28) break; Sleep(100); } } } } int GetSymbolCode() { bool li_ret_0 = FALSE; string ls_4 = ""; for (int li_12 = 0; li_12 < StringLen(Symbol()); li_12++) ls_4 = ls_4 + "" + StringGetChar(Symbol(), li_12); li_ret_0 = MathAbs(StrToInteger(ls_4)); return (li_ret_0); } int GetMagicNumber(int ai_unused_0) { int li_4 = 0; li_4 = GetSymbolCode(); return (li_4); } int ConfirmLicense() { string ls_12; bool li_ret_0 = FALSE; string ls_20 = ""; int li_28 = 0; string ls_4 = "http://www.forexgridrobot.com/lic/confirm.php?account_num=" + AccountNumber(); if (GrabWeb(ls_4, ls_12)) { ls_20 = GetString(ls_12, "VALID", li_28); if (ls_20 == "TRUE") li_ret_0 = TRUE; } return (li_ret_0); } int hSession(bool ai_0) { string ls_4; if (gi_80 == 0) { ls_4 = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)"; gi_80 = InternetOpenA(ls_4, gi_88, "0", "0", 0); gi_84 = InternetOpenA(ls_4, gi_92, "0", "0", 0); } if (ai_0) return (gi_84); return (gi_80); } bool GrabWeb(string as_0, string &as_8) { int lia_24[] = {1}; string ls_28 = "x"; int li_16 = InternetOpenUrlA(hSession(0), as_0, "0", 0, -2080374528, 0); if (li_16 == 0) return (FALSE); Print("Reading URL: " + as_0); int li_20 = InternetReadFile(li_16, ls_28, gi_100, lia_24); int li_36 = lia_24[0]; for (as_8 = StringSubstr(ls_28, 0, lia_24[0]); lia_24[0] != 0; as_8 = as_8 + StringSubstr(ls_28, 0, lia_24[0])) { li_20 = InternetReadFile(li_16, ls_28, gi_100, lia_24); if (lia_24[0] == 0) break; li_36 += lia_24[0]; } Print("Closing URL web connection"); li_20 = InternetCloseHandle(li_16); if (li_20 == 0) return (FALSE); return (TRUE); } string GetString(string as_0, string as_8, int &ai_16) { int li_20 = 0; string ls_ret_24 = ""; ai_16 = StringFind(as_0, "<" + as_8 + ">", ai_16); if (ai_16 != -1) { ai_16 += StringLen("<" + as_8 + ">"); li_20 = StringFind(as_0, "", ai_16); ls_ret_24 = StringSubstr(as_0, ai_16, li_20 - ai_16); ai_16 = li_20 + StringLen(""); } return (ls_ret_24); }