#property copyright "Bonansa Investment Co., Ltd." #property link "http://bonansa.blog47.fc2.com/" #include string gs_unused_76 = "著作権及び禁止止事項"; string gs_unused_84 = "当社の販売している自動売買プログラム(EA)についての著作権は、当社が有しています。"; string gs_unused_92 = "無断で転載・転用・複製等をすることを禁止します。"; string gs_unused_100 = "また当社のEAをリバースエンジニアリング、逆コンパイル、逆アセンブルすることを禁止します。"; string gs_unused_108 = "さらに当社のEAの技術的な制限を回避して使用することを禁止します。"; string gs_unused_116 = "上記の事項に違反した場合、違約金として違反件数に300万円を乗じた金額を当社に支払うものとします。"; extern bool SafetyMode = FALSE; double g_high_128; double g_low_136; bool gi_144 = TRUE; extern int SignalPeriod = 22; extern int HourToStart = 21; extern int TradingHours = 3; extern int MaxPosition = 3; int g_count_164 = 0; extern int TakeProfit = 25; extern int StopLoss = 70; extern int Slippage = 3; extern int OrderSendRetries = 5; extern int maxSpread = 6; bool gi_188 = FALSE; int gi_192 = 10; bool gi_196 = FALSE; extern bool MoneyManagement = FALSE; extern int Risk_percent = 10; extern double minLots = 0.1; extern double maxLots = 1.0; extern bool UseATRStop = FALSE; extern int ATRTimeframe = 30; extern int ATRPeriod = 10; double gd_236; extern double ATRStoplevel = 3.0; string gs_eurchf_252 = "EURCHF"; int g_magic_260 = 20081001; string g_comment_264 = "FN-EURCHF"; int gia_272[100][2] = {15829, 100, 18437, 100, 17537, 100, 201027023, 10, 76089, 10, 99016, 1, 24276, 1, 9999999, 1, 9999999, 1, 9999999, 1, 20191, 1, 125593, 1, 76386, 1, 18045, 1, 16757, 1, 76291, 1, 123620, 1, 30977, 1, 125600, 1, 126070, 1, 155025, 1, 71854, 1, 702744, 1, 16699, 1, 813718, 1, 10309130, 1, 85813, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1, 9999999, 1}; int gi_unused_276 = -1; int gi_unused_280 = -1; int gi_unused_284 = -1; bool gi_unused_288 = FALSE; extern bool ECNMode = FALSE; int g_stoplevel_296; int g_spread_300; int g_count_304; double gd_308; double gd_316; double g_minlot_324; double g_maxlot_332; double g_lotstep_340; double g_lotsize_348; double gd_356 = 0.0; int gi_364; double gd_368 = 1.0; bool gi_376 = FALSE; bool gi_380 = FALSE; bool gi_384 = FALSE; bool gi_388 = FALSE; bool gi_392 = FALSE; double g_high_396; double g_low_404; double g_high_412 = -1.0; double g_low_420 = -1.0; int gia_428[6] = {-1, -1, -1, -1, -1, -1}; bool gi_432 = FALSE; string g_str_concat_436; string gs_444; bool g_ord_close_452; bool g_bool_456; int g_ticket_460; int g_error_464; int gi_468 = -1; int gi_472 = -1; int gia_476[]; int gia_480[]; double gda_484[]; double gda_488[]; double gda_492[]; double gda_496[]; double gda_500[]; double gda_504[]; double gda_508[]; int gia_512[]; int gia_516[]; string gsa_520[]; int g_bars_524; int init() { HideTestIndicators(TRUE); if (StringSubstr(Symbol(), 0, 6) == gs_eurchf_252) gs_eurchf_252 = Symbol(); gs_444 = AccountCurrency(); g_stoplevel_296 = MarketInfo(gs_eurchf_252, MODE_STOPLEVEL); g_spread_300 = MarketInfo(gs_eurchf_252, MODE_SPREAD); g_minlot_324 = MarketInfo(gs_eurchf_252, MODE_MINLOT); g_maxlot_332 = MarketInfo(gs_eurchf_252, MODE_MAXLOT); g_lotstep_340 = MarketInfo(gs_eurchf_252, MODE_LOTSTEP); g_lotsize_348 = MarketInfo(gs_eurchf_252, MODE_LOTSIZE); gi_364 = StringLen(StringConcatenate(g_lotstep_340, "")) - 2; if (TradingHours > 6) TradingHours = 6; if (MaxPosition > TradingHours) MaxPosition = TradingHours; for (int l_index_0 = 0; l_index_0 < TradingHours; l_index_0++) { gia_428[l_index_0] = HourToStart + l_index_0; if (gia_428[l_index_0] > 23) gia_428[l_index_0] = gia_428[l_index_0] - 24; } if (Digits == 3 || Digits == 5) gd_368 = 10; gi_192 = MathMin(25, MathMax((g_stoplevel_296 - g_spread_300) / gd_368, gi_192)); if (TakeProfit > gi_192) gi_192 = TakeProfit; Risk_percent = MathMin(25, MathMax(1, Risk_percent)); OrderSendRetries = MathMin(25, MathMax(1, OrderSendRetries)); if (gs_444 == "JPY") g_lotsize_348 = 10000000; if (g_minlot_324 < 0.0 || g_maxlot_332 <= 0.0 || g_lotstep_340 <= 0.0) { logError("Init", "invalid MarketInfo() results [" + g_minlot_324 + "," + g_maxlot_332 + "," + g_lotstep_340 + "]"); return (-1); } gi_432 = FALSE; for (l_index_0 = 0; l_index_0 < ArraySize(gia_272) / 2; l_index_0++) { if (AccountNumber() == gia_272[l_index_0][0] || (StrToInteger(AccountName()) == gia_272[l_index_0][0] && StrToInteger(AccountName()) != 0)) { gd_356 = NormalizeDouble(gia_272[l_index_0][1] / g_lotstep_340, 0) * g_lotstep_340; gi_432 = TRUE; break; } } maxLots = NormalizeDouble(MathMin(gd_356, MathMax(g_minlot_324, maxLots)) / g_lotstep_340, 0) * g_lotstep_340; minLots = NormalizeDouble(MathMin(gd_356, MathMax(g_minlot_324, minLots)) / g_lotstep_340, 0) * g_lotstep_340; if (!gi_432) { g_str_concat_436 = StringConcatenate(AccountNumber(), " is not registered."); Comment(g_str_concat_436); Alert(g_str_concat_436); return (-1); } if (CheckExpiration()) return (-1); if (Symbol() != gs_eurchf_252) { g_str_concat_436 = StringConcatenate("Please attach this EA to ", gs_eurchf_252, " chart."); Comment(g_str_concat_436); Alert(g_str_concat_436); return (-1); } g_bars_524 = Bars; CheckPosition(); if (SafetyMode == TRUE) TPModify(); return (1); } int deinit() { return (1); } int start() { if (Symbol() != gs_eurchf_252) return (-1); if (!gi_432) return (-1); if (CheckExpiration()) return (-1); if (g_bars_524 != Bars) init(); HighLowSignal(); if (CheckPosition() == -1) { gi_472 = -1; CheckPosition(); } if (SafetyMode == TRUE && g_high_396 != g_high_412 || g_low_404 != g_low_420) { if (TPModify() != -1) { g_high_412 = g_high_396; g_low_420 = g_low_404; } } int l_hour_0 = Hour(); if (l_hour_0 != gia_428[0] && l_hour_0 != gia_428[1] && l_hour_0 != gia_428[2] && l_hour_0 != gia_428[3] && l_hour_0 != gia_428[4] && l_hour_0 != gia_428[5]) return (-1); if ((DayOfWeek() == 1 && l_hour_0 < 10) || DayOfWeek() < 1) return (-1); if ((DayOfWeek() == 5 && l_hour_0 > 10) || DayOfWeek() > 5) return (-1); if (Ask - Bid > maxSpread * gd_368 * Point) return (-1); PositionCount(); Function_Trade(); return (1); } bool CheckExpiration() { bool li_ret_0 = FALSE; g_str_concat_436 = ""; if (gi_432) { g_str_concat_436 = StringConcatenate(g_str_concat_436, "Registered to ", AccountNumber(), "\nRegistered Max. Lots: ", DoubleToStr(gd_356, gi_364)); } g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nTime to Entry: ", gia_428[0], ":00 - ", gia_428[TradingHours - 1], ":59"); if (ECNMode) { g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nECNMode: on, "); } else { g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nECNMode: off, "); } if (SafetyMode) { g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nSafetyMode: on"); } else { g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nSafetyMode: off"); } g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nSignalPeriod: ", SignalPeriod, ", MaxPosition: ", MaxPosition); if (!UseATRStop) { g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nTakeProfit: ", TakeProfit, ", StopLoss: ", StopLoss); } else { gd_236 = MathCeil(iATR(NULL, ATRTimeframe, ATRPeriod, 0) * ATRStoplevel * MathPow(10, Digits)); if (gd_236 < (g_stoplevel_296 + g_spread_300) / gd_368) gd_236 = (g_stoplevel_296 + g_spread_300) / gd_368; g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nTakeProfit: ", TakeProfit, ", ATRStopLoss: ", gd_236, "\nATRStop setting: ", ATRTimeframe, " - ", ATRPeriod, " - ", ATRStoplevel); } if (!MoneyManagement) { g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nMM: off, FixedLotSize: ", DoubleToStr(LotSize(), gi_364)); } else { g_str_concat_436 = StringConcatenate(g_str_concat_436, "\nMM: on, Risk%: ", Risk_percent, ", MMLotSize: ", DoubleToStr(LotSize(), gi_364), "\nMin. Lots: ", DoubleToStr(minLots, gi_364), ", Max. Lots: ", DoubleToStr(maxLots, gi_364)); } Comment(g_str_concat_436); return (li_ret_0); } int TPModify() { CacheOrders(); if (SafetyMode) { for (int l_index_0 = 0; l_index_0 < gi_468; l_index_0++) { if (gia_480[l_index_0] == 0) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); gd_308 = NormalizeDouble(g_high_128, Digits); if (gd_308 < NormalizeDouble(Bid + g_stoplevel_296 * Point, Digits)) gd_308 = NormalizeDouble(Bid + g_stoplevel_296 * Point, Digits); if (gd_308 >= NormalizeDouble(gda_496[l_index_0], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_456 = OrderModify(gia_476[l_index_0], gda_488[l_index_0], gda_492[l_index_0], NormalizeDouble(gd_308, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_456) { g_count_304 = OrderSendRetries; gda_496[l_index_0] = gd_308; } else { g_count_304++; g_error_464 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to modify LONG T/P - attempt #", g_count_304), g_error_464); Sleep(500); } } } else { if (gia_480[l_index_0] == 1) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); gd_308 = NormalizeDouble(g_low_136 + g_spread_300 * Point, Digits); if (gd_308 > NormalizeDouble(Ask - g_stoplevel_296 * Point, Digits)) gd_308 = NormalizeDouble(Ask - g_stoplevel_296 * Point, Digits); if (gd_308 <= NormalizeDouble(gda_496[l_index_0], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_456 = OrderModify(gia_476[l_index_0], gda_488[l_index_0], gda_492[l_index_0], NormalizeDouble(gd_308, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_456) { g_count_304 = OrderSendRetries; gda_496[l_index_0] = gd_308; } else { g_count_304++; g_error_464 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to modify SHORT T/P - attempt #", g_count_304), g_error_464); Sleep(500); } } } } } } return (1); } void HighLowSignal() { gi_376 = FALSE; gi_380 = FALSE; g_high_396 = High[iHighest(NULL, 0, MODE_HIGH, SignalPeriod, 1)]; g_low_404 = Low[iLowest(NULL, 0, MODE_LOW, SignalPeriod, 1)]; if (Bid <= g_low_404) gi_376 = TRUE; if (Bid >= g_high_396) gi_380 = TRUE; g_high_128 = g_high_396; g_low_136 = g_low_404; } void PositionCount() { g_count_164 = 0; gi_384 = FALSE; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderMagicNumber() == g_magic_260 && OrderSymbol() == Symbol()) g_count_164++; } if (g_count_164 < MaxPosition) gi_384 = TRUE; } int NoPos() { bool li_ret_0 = TRUE; for (int l_index_4 = 0; l_index_4 < gi_468; l_index_4++) { if (Hour() == TimeHour(gia_512[l_index_4])) { li_ret_0 = FALSE; break; } } return (li_ret_0); } int CheckPosition() { CacheOrders(); gi_388 = FALSE; gi_392 = FALSE; if (gi_144 == TRUE) { gi_388 = gi_380; gi_392 = gi_376; } if (!gi_188) { for (int l_index_0 = 0; l_index_0 < gi_468; l_index_0++) { if (gia_480[l_index_0] == 0 && gi_388 == TRUE) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_452 = OrderClose(gia_476[l_index_0], gda_484[l_index_0], NormalizeDouble(Bid, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_452) { g_count_304++; g_error_464 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close LONG order - attempt #", g_count_304), g_error_464); if (g_error_464 == 2/* COMMON_ERROR */) return (-1); if (g_error_464 == 4108/* INVALID_TICKET */) { g_count_304 = OrderSendRetries; gi_472 = -1; } else Sleep(500); } else { g_count_304 = OrderSendRetries; gi_472 = -1; } } } else { if (gia_480[l_index_0] == 1 && gi_392 == TRUE) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_452 = OrderClose(gia_476[l_index_0], gda_484[l_index_0], NormalizeDouble(Ask, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_452) { g_count_304++; g_error_464 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close SHORT order - attempt #", g_count_304), g_error_464); if (g_error_464 == 2/* COMMON_ERROR */) return (-1); if (g_error_464 == 4108/* INVALID_TICKET */) { g_count_304 = OrderSendRetries; gi_472 = -1; } else Sleep(500); } else { g_count_304 = OrderSendRetries; gi_472 = -1; } } } } } } if (gi_188) { for (int l_index_4 = 0; l_index_4 < gi_468; l_index_4++) { if (gia_480[l_index_4] == 0 && (Bid >= gda_488[l_index_4] + TakeProfit * gd_368 * Point && gda_496[l_index_4] != gda_488[l_index_4] + TakeProfit * gd_368 * Point) || gi_388 == TRUE) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_452 = OrderClose(gia_476[l_index_4], gda_484[l_index_4], NormalizeDouble(Bid, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_452) { g_count_304++; g_error_464 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close LONG order - attempt #", g_count_304), g_error_464); if (g_error_464 == 2/* COMMON_ERROR */) return (-1); if (g_error_464 == 4108/* INVALID_TICKET */) { g_count_304 = OrderSendRetries; gi_472 = -1; } else Sleep(500); } else { g_count_304 = OrderSendRetries; gi_472 = -1; } } } else { if (gia_480[l_index_4] == 1 && (Ask <= gda_488[l_index_4] - TakeProfit * gd_368 * Point && gda_496[l_index_4] != gda_488[l_index_4] - TakeProfit * gd_368 * Point) || gi_392 == TRUE) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_452 = OrderClose(gia_476[l_index_4], gda_484[l_index_4], NormalizeDouble(Ask, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_452) { g_count_304++; g_error_464 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close SHORT order - attempt #", g_count_304), g_error_464); if (g_error_464 == 2/* COMMON_ERROR */) return (-1); if (g_error_464 == 4108/* INVALID_TICKET */) { g_count_304 = OrderSendRetries; gi_472 = -1; } else Sleep(500); } else { g_count_304 = OrderSendRetries; gi_472 = -1; } } } else { if (gia_480[l_index_4] == 0 && gi_196 && gda_496[l_index_4] > NormalizeDouble(gda_488[l_index_4] + TakeProfit * gd_368 * Point, Digits) && Bid < gda_488[l_index_4]) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); gd_308 = NormalizeDouble(Ask + gi_192 * gd_368 * Point, Digits); if (gd_308 < NormalizeDouble(gda_488[l_index_4] + TakeProfit * gd_368 * Point, Digits)) gd_308 = NormalizeDouble(gda_488[l_index_4] + TakeProfit * gd_368 * Point, Digits); if (gd_308 >= NormalizeDouble(gda_496[l_index_4], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_456 = OrderModify(gia_476[l_index_4], gda_488[l_index_4], gda_492[l_index_4], NormalizeDouble(gd_308, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_456) { g_count_304 = OrderSendRetries; gda_496[l_index_4] = gd_308; } else { g_count_304++; logError("CheckPosition", StringConcatenate("failed to modify LONG T/P - attempt #", g_count_304), GetLastError()); Sleep(500); } } } else { if (gia_480[l_index_4] == 1 && gi_196 && gda_496[l_index_4] < NormalizeDouble(gda_488[l_index_4] - TakeProfit * gd_368 * Point, Digits) && Ask > gda_488[l_index_4]) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { RefreshRates(); gd_308 = NormalizeDouble(Bid - gi_192 * gd_368 * Point, Digits); if (gd_308 > NormalizeDouble(gda_488[l_index_4] - TakeProfit * gd_368 * Point, Digits)) gd_308 = NormalizeDouble(gda_488[l_index_4] - TakeProfit * gd_368 * Point, Digits); if (gd_308 <= NormalizeDouble(gda_496[l_index_4], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_456 = OrderModify(gia_476[l_index_4], gda_488[l_index_4], gda_492[l_index_4], NormalizeDouble(gd_308, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_456) { g_count_304 = OrderSendRetries; gda_496[l_index_4] = gd_308; } else { g_count_304++; logError("CheckPosition", StringConcatenate("failed to modify SHORT T/P - attempt #", g_count_304), GetLastError()); Sleep(500); } } } } } } } } return (1); } double LotSize() { double ld_ret_0; if (MoneyManagement) { if (AccountLeverage() <= 0) { logError("LotSize", StringConcatenate("CalculateVolume: invalid AccountLeverage() [", AccountLeverage(), "]")); return (-1); } ld_ret_0 = NormalizeDouble(100.0 * (AccountFreeMargin() * (NormalizeDouble(Risk_percent, 1) / 100.0)) / g_lotsize_348, 2); ld_ret_0 = NormalizeDouble(ld_ret_0 / g_lotstep_340, 0) * g_lotstep_340; ld_ret_0 = MathMin(maxLots, MathMax(minLots, ld_ret_0)); } else ld_ret_0 = minLots; return (ld_ret_0); } void Function_Trade() { if (gi_376 && gi_384 && NoPos()) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { if (TradeIsBusy() < 0) return; RefreshRates(); if (gi_188) gd_308 = Ask + gi_192 * gd_368 * Point; else gd_308 = Ask + TakeProfit * gd_368 * Point; if (StopLoss > (g_stoplevel_296 + g_spread_300) / gd_368 - 1.0) gd_316 = Ask - StopLoss * gd_368 * Point; else gd_316 = Ask - 60.0 * gd_368 * Point; if (UseATRStop) gd_316 = Ask - gd_236 * gd_368 * Point; if (ECNMode) { g_ticket_460 = OrderSend(gs_eurchf_252, OP_BUY, LotSize(), NormalizeDouble(Ask, Digits), 0, 0, 0, g_comment_264, g_magic_260, 0, Blue); if (g_ticket_460 > 0) { OrderSelect(g_ticket_460, SELECT_BY_TICKET); OrderModify(g_ticket_460, OrderOpenPrice(), NormalizeDouble(gd_316, Digits), NormalizeDouble(gd_308, Digits), 0, Red); } } else g_ticket_460 = OrderSend(gs_eurchf_252, OP_BUY, LotSize(), NormalizeDouble(Ask, Digits), Slippage, NormalizeDouble(gd_316, Digits), NormalizeDouble(gd_308, Digits), g_comment_264, g_magic_260, 0, Blue); TradeIsNotBusy(); if (g_ticket_460 == -1) { g_count_304++; logError("Function_Trade", StringConcatenate("could not open LONG order - attempt #", g_count_304)); Sleep(500); } else { g_count_304 = OrderSendRetries; gi_472 = -1; } } } if (gi_380 && gi_384 && NoPos()) { g_count_304 = 0; while (g_count_304 < OrderSendRetries) { if (TradeIsBusy() < 0) break; RefreshRates(); if (gi_188) gd_308 = Bid - gi_192 * gd_368 * Point; else gd_308 = Bid - TakeProfit * gd_368 * Point; if (StopLoss > (g_stoplevel_296 + g_spread_300) / gd_368 - 1.0) gd_316 = Bid + StopLoss * gd_368 * Point; else gd_316 = Bid + 60.0 * gd_368 * Point; if (UseATRStop) gd_316 = Bid + gd_236 * gd_368 * Point; if (ECNMode) { g_ticket_460 = OrderSend(gs_eurchf_252, OP_SELL, LotSize(), NormalizeDouble(Bid, Digits), 0, 0, 0, g_comment_264, g_magic_260, 0, Red); if (g_ticket_460 > 0) { OrderSelect(g_ticket_460, SELECT_BY_TICKET); OrderModify(g_ticket_460, OrderOpenPrice(), NormalizeDouble(gd_316, Digits), NormalizeDouble(gd_308, Digits), 0, Red); } } else g_ticket_460 = OrderSend(gs_eurchf_252, OP_SELL, LotSize(), NormalizeDouble(Bid, Digits), Slippage, NormalizeDouble(gd_316, Digits), NormalizeDouble(gd_308, Digits), g_comment_264, g_magic_260, 0, Red); TradeIsNotBusy(); if (g_ticket_460 == -1) { g_count_304++; logError("Function_Trade", StringConcatenate("could not open SHORT order - attempt #", g_count_304)); Sleep(500); } else { g_count_304 = OrderSendRetries; gi_472 = -1; } } } } void CacheOrders() { if (gi_472 != OrdersTotal()) InitializeOrders(g_magic_260); } void InitializeOrders(int ai_unused_0) { int li_unused_4 = 0; int l_ord_total_8 = OrdersTotal(); int li_12 = MathMax(l_ord_total_8, 1); ArrayResize(gia_476, li_12); ArrayResize(gia_480, li_12); ArrayResize(gda_484, li_12); ArrayResize(gda_488, li_12); ArrayResize(gda_492, li_12); ArrayResize(gda_496, li_12); ArrayResize(gia_512, li_12); ArrayResize(gda_500, li_12); ArrayResize(gda_504, li_12); ArrayResize(gda_508, li_12); ArrayResize(gsa_520, li_12); ArrayResize(gia_516, li_12); ArrayInitialize(gia_476, 0); ArrayInitialize(gia_480, 0); ArrayInitialize(gda_484, 0); ArrayInitialize(gda_488, 0); ArrayInitialize(gda_492, 0); ArrayInitialize(gda_496, 0); ArrayInitialize(gia_512, 0); ArrayInitialize(gda_500, 0); ArrayInitialize(gda_504, 0); ArrayInitialize(gda_508, 0); ArrayInitialize(gia_516, 0); gi_468 = 0; for (int l_pos_16 = l_ord_total_8 - 1; l_pos_16 >= 0; l_pos_16--) { if (!OrderSelect(l_pos_16, SELECT_BY_POS)) logError("InitializeOrders", "failed to select order", GetLastError()); else { if (OrderMagicNumber() == g_magic_260 && OrderSymbol() == gs_eurchf_252) { gia_476[gi_468] = OrderTicket(); gia_480[gi_468] = OrderType(); gda_484[gi_468] = NormalizeDouble(OrderLots(), gi_364); gda_488[gi_468] = NormalizeDouble(OrderOpenPrice(), Digits); gda_492[gi_468] = NormalizeDouble(OrderStopLoss(), Digits); gda_496[gi_468] = NormalizeDouble(OrderTakeProfit(), Digits); gia_512[gi_468] = OrderOpenTime(); gda_500[gi_468] = NormalizeDouble(OrderProfit(), 2); gda_504[gi_468] = NormalizeDouble(OrderSwap(), 2); gda_508[gi_468] = NormalizeDouble(OrderCommission(), 2); gsa_520[gi_468] = OrderComment(); gia_516[gi_468] = OrderExpiration(); gi_468++; } } } li_12 = MathMax(gi_468, 1); ArrayResize(gia_476, li_12); ArrayResize(gia_480, li_12); ArrayResize(gda_484, li_12); ArrayResize(gda_488, li_12); ArrayResize(gda_492, li_12); ArrayResize(gda_496, li_12); ArrayResize(gia_512, li_12); ArrayResize(gda_500, li_12); ArrayResize(gda_504, li_12); ArrayResize(gda_508, li_12); ArrayResize(gsa_520, li_12); ArrayResize(gia_516, li_12); gi_472 = l_ord_total_8; } int TradeIsBusy(int ai_0 = 30) { if (IsTesting()) return (1); int l_error_4 = 0; int li_8 = GetTickCount(); while (true) { if (IsStopped()) { logInfo("The expert was terminated by the user!"); return (-1); } if (GetTickCount() - li_8 > 1000 * ai_0) { logInfo("Waiting time (" + ai_0 + " sec) exceeded!"); return (-2); } if (GlobalVariableCheck("BNTradeIsBusy")) break; l_error_4 = GetLastError(); if (l_error_4 != 0/* NO_ERROR */) { logError("TradeIsBusy", "GlobalVariableCheck", l_error_4); Sleep(100); continue; } if (GlobalVariableSet("BNTradeIsBusy", 1.0) > 0) return (1); l_error_4 = GetLastError(); if (l_error_4 == 0/* NO_ERROR */) continue; logError("TradeIsBusy", "GlobalVariableSet", l_error_4); Sleep(100); } while (true) { if (IsStopped()) { logInfo("The expert was terminated by the user!"); return (-1); } if (GetTickCount() - li_8 > 1000 * ai_0) { logInfo("The waiting time (" + ai_0 + " sec) exceeded!"); return (-2); } if (GlobalVariableSetOnCondition("BNTradeIsBusy", 1.0, 0.0)) return (1); l_error_4 = GetLastError(); if (l_error_4 != 0/* NO_ERROR */) { logError("TradeIsBusy", "GlobalVariableSetOnCondition", l_error_4); continue; } logInfo("Wait until another expert finishes trading..."); Comment("Wait until another expert finishes trading..."); Sleep(1000); Comment(""); } return /*(WARN)*/; } void TradeIsNotBusy() { int l_error_0; if (IsTesting() == 0) { while (true) { if (IsStopped()) { logInfo("The expert was terminated by the user!"); return; } if (GlobalVariableSet("BNTradeIsBusy", 0.0) > 0) return; l_error_0 = GetLastError(); if (l_error_0 != 0/* NO_ERROR */) logError("TradeIsNotBusy", "GlobalVariableSet", l_error_0); Sleep(100); } } } void logError(string as_0, string as_8, int ai_16 = -1) { Print("ERROR: in " + as_0 + "()"); Print("ERROR: " + as_8); int l_error_20 = GetLastError(); if (ai_16 != -1) l_error_20 = ai_16; if (l_error_20 != 0/* NO_ERROR */) Print("ERROR: code=" + l_error_20 + " - " + ErrorDescription(l_error_20)); } void logInfo(string as_0) { Print("INFO: " + as_0); }