#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 gd_128; double gd_136; double gd_144 = -1.0; double gd_152 = -1.0; bool gi_160 = TRUE; extern int SignalPeriod = 42; extern int minEntrySignal = 7; extern int EntrySignalAdjust = -2; extern int ExitSignalAdjust = -1; extern int HourToStart = 20; extern int TradingHours = 3; extern int MaxPosition = 3; int g_count_192 = 0; extern int TakeProfit = 80; extern int StopLoss = 80; extern int Slippage = 3; extern int OrderSendRetries = 5; extern int maxSpread = 4; bool gi_216 = FALSE; int gi_220 = 20; bool gi_224 = FALSE; extern bool MoneyManagement = FALSE; extern int Risk_percent = 10; extern double minLots = 0.1; extern double maxLots = 1.0; bool gi_252 = FALSE; int g_timeframe_256 = PERIOD_M30; int g_period_260 = 10; double gd_264; double gd_272 = 3.0; string gs_usdchf_280 = "USDCHF"; int g_magic_288 = 20081218; string g_comment_292 = "GN-USDCHF"; int gia_300[100][2] = {15829, 100, 18437, 100, 17537, 100, 201027023, 10, 76089, 10, 20703, 10, 201105005, 1, 155070, 1, 24771, 1, 501459, 1, 9999999, 1, 201114041, 1, 201128004, 1, 123620, 1, 201211004, 1, 71694, 1, 123308, 1, 201107016, 1, 201121010, 1, 125600, 1, 126070, 1, 210115033, 1, 201113042, 1, 210120022, 1, 4553, 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, 9999999, 1}; int gi_unused_304 = -1; int gi_unused_308 = -1; int gi_unused_312 = -1; bool gi_unused_316 = FALSE; extern bool ECNMode = FALSE; int g_stoplevel_324; int g_spread_328; int g_count_332; double gd_336; double gd_344; double g_minlot_352; double g_maxlot_360; double g_lotstep_368; double g_lotsize_376; double gd_384 = 0.0; int gi_392; double gd_396 = 1.0; bool gi_unused_404 = FALSE; bool gi_unused_408 = FALSE; bool gi_412 = FALSE; bool gi_416 = FALSE; bool gi_420 = FALSE; bool gi_440 = FALSE; bool gi_444 = FALSE; bool gi_448 = FALSE; bool gi_452 = FALSE; double g_ibands_456; double g_ibands_464; double gd_472; int gia_480[5] = {-1, -1, -1, -1, -1, -1}; bool gi_484 = FALSE; string g_str_concat_488; string gs_496; bool g_ord_close_504; bool g_bool_508; int g_ticket_512; int g_error_516; int gi_520 = -1; int gi_524 = -1; int gia_528[]; int gia_532[]; double gda_536[]; double gda_540[]; double gda_544[]; double gda_548[]; double gda_552[]; double gda_556[]; double gda_560[]; int gia_564[]; int gia_568[]; string gsa_572[]; int g_bars_576; int init() { HideTestIndicators(TRUE); if (StringSubstr(Symbol(), 0, 6) == gs_usdchf_280) gs_usdchf_280 = Symbol(); gs_496 = AccountCurrency(); g_stoplevel_324 = MarketInfo(gs_usdchf_280, MODE_STOPLEVEL); g_spread_328 = MarketInfo(gs_usdchf_280, MODE_SPREAD); g_minlot_352 = MarketInfo(gs_usdchf_280, MODE_MINLOT); g_maxlot_360 = MarketInfo(gs_usdchf_280, MODE_MAXLOT); g_lotstep_368 = MarketInfo(gs_usdchf_280, MODE_LOTSTEP); g_lotsize_376 = MarketInfo(gs_usdchf_280, MODE_LOTSIZE); gi_392 = StringLen(StringConcatenate(g_lotstep_368, "")) - 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_480[l_index_0] = HourToStart + l_index_0; if (gia_480[l_index_0] > 23) gia_480[l_index_0] = gia_480[l_index_0] - 24; } if (Digits == 3 || Digits == 5) gd_396 = 10; gi_220 = MathMin(25, MathMax((g_stoplevel_324 - g_spread_328) / gd_396, gi_220)); if (TakeProfit > gi_220) gi_220 = TakeProfit; Risk_percent = MathMin(25, MathMax(1, Risk_percent)); OrderSendRetries = MathMin(25, MathMax(1, OrderSendRetries)); if (gs_496 == "JPY") g_lotsize_376 = 10000000; if (g_minlot_352 < 0.0 || g_maxlot_360 <= 0.0 || g_lotstep_368 <= 0.0) { logError("Init", "invalid MarketInfo() results [" + g_minlot_352 + "," + g_maxlot_360 + "," + g_lotstep_368 + "]"); return (-1); } gi_484 = FALSE; for (l_index_0 = 0; l_index_0 < ArraySize(gia_300) / 2; l_index_0++) { if (AccountNumber() == gia_300[l_index_0][0] || (StrToInteger(AccountName()) == gia_300[l_index_0][0] && StrToInteger(AccountName()) != 0)) { gd_384 = NormalizeDouble(gia_300[l_index_0][1] / g_lotstep_368, 0) * g_lotstep_368; gi_484 = TRUE; break; } } maxLots = NormalizeDouble(MathMin(gd_384, MathMax(g_minlot_352, maxLots)) / g_lotstep_368, 0) * g_lotstep_368; minLots = NormalizeDouble(MathMin(gd_384, MathMax(g_minlot_352, minLots)) / g_lotstep_368, 0) * g_lotstep_368; if (!gi_484) { g_str_concat_488 = StringConcatenate(AccountNumber(), " is not registered."); Comment(g_str_concat_488); Alert(g_str_concat_488); return (-1); } if (CheckExpiration()) return (-1); if (Symbol() != gs_usdchf_280) { g_str_concat_488 = StringConcatenate("Please attach this EA to ", gs_usdchf_280, " chart."); Comment(g_str_concat_488); Alert(g_str_concat_488); return (-1); } g_bars_576 = Bars; CheckPosition(); if (SafetyMode == TRUE) TPModify(); return (1); } int deinit() { return (1); } int start() { if (Symbol() != gs_usdchf_280) return (-1); if (!gi_484) return (-1); if (CheckExpiration()) return (-1); if (g_bars_576 != Bars) init(); BBandsSignal(); if (CheckPosition() == -1) { gi_524 = -1; CheckPosition(); } if (SafetyMode == TRUE && gd_128 != gd_144 || gd_136 != gd_152) { if (TPModify() != -1) { gd_144 = gd_128; gd_152 = gd_136; } } int l_hour_0 = Hour(); if (l_hour_0 != gia_480[0] && l_hour_0 != gia_480[1] && l_hour_0 != gia_480[2] && l_hour_0 != gia_480[3] && l_hour_0 != gia_480[4] && l_hour_0 != gia_480[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_396 * Point) return (-1); PositionCount(); Function_Trade(); return (1); } bool CheckExpiration() { bool li_ret_0 = FALSE; g_str_concat_488 = ""; if (gi_484) { g_str_concat_488 = StringConcatenate(g_str_concat_488, "Registered to ", AccountNumber(), "\nRegistered Max. Lots: ", DoubleToStr(gd_384, gi_392)); } g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nTime to Entry: ", gia_480[0], ":00 - ", gia_480[TradingHours - 1], ":59"); if (SafetyMode) { g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nSafetyMode: on, MaxPosition: ", MaxPosition); } else { g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nSafetyMode: off, MaxPosition: ", MaxPosition); } if (ECNMode) { g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nECNMode: on, "); } else { g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nECNMode: off, "); } g_str_concat_488 = StringConcatenate(g_str_concat_488, "SignalParameters: ", SignalPeriod, ", ", minEntrySignal, ", ", EntrySignalAdjust, ", ", ExitSignalAdjust); if (!gi_252) { g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nTakeProfit: ", TakeProfit, ", StopLoss: ", StopLoss); } else { gd_264 = MathCeil(iATR(NULL, g_timeframe_256, g_period_260, 0) * gd_272 * MathPow(10, Digits)); if (gd_264 < (g_stoplevel_324 + g_spread_328) / gd_396) gd_264 = (g_stoplevel_324 + g_spread_328) / gd_396; g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nTakeProfit: ", TakeProfit, ", ATRStopLoss: ", gd_264, "\nATRStop setting: ", g_timeframe_256, " - ", g_period_260, " - ", gd_272); } if (!MoneyManagement) { g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nMM: off, FixedLotSize: ", DoubleToStr(LotSize(), gi_392)); } else { g_str_concat_488 = StringConcatenate(g_str_concat_488, "\nMM: on, Risk%: ", Risk_percent, ", MMLotSize: ", DoubleToStr(LotSize(), gi_392), "\nMin. Lots: ", DoubleToStr(minLots, gi_392), ", Max. Lots: ", DoubleToStr(maxLots, gi_392)); } Comment(g_str_concat_488); return (li_ret_0); } int TPModify() { CacheOrders(); if (SafetyMode) { for (int l_index_0 = 0; l_index_0 < gi_520; l_index_0++) { if (gia_532[l_index_0] == 0) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); gd_336 = NormalizeDouble(gd_128, Digits); if (gd_336 < NormalizeDouble(Bid + g_stoplevel_324 * Point, Digits)) gd_336 = NormalizeDouble(Bid + g_stoplevel_324 * Point, Digits); if (gd_336 >= NormalizeDouble(gda_548[l_index_0], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_508 = OrderModify(gia_528[l_index_0], gda_540[l_index_0], gda_544[l_index_0], NormalizeDouble(gd_336, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_508) { g_count_332 = OrderSendRetries; gda_548[l_index_0] = gd_336; } else { g_count_332++; g_error_516 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to modify LONG T/P - attempt #", g_count_332), g_error_516); Sleep(500); } } } else { if (gia_532[l_index_0] == 1) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); gd_336 = NormalizeDouble(gd_136 + g_spread_328 * Point, Digits); if (gd_336 > NormalizeDouble(Ask - g_stoplevel_324 * Point, Digits)) gd_336 = NormalizeDouble(Ask - g_stoplevel_324 * Point, Digits); if (gd_336 <= NormalizeDouble(gda_548[l_index_0], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_508 = OrderModify(gia_528[l_index_0], gda_540[l_index_0], gda_544[l_index_0], NormalizeDouble(gd_336, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_508) { g_count_332 = OrderSendRetries; gda_548[l_index_0] = gd_336; } else { g_count_332++; g_error_516 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to modify SHORT T/P - attempt #", g_count_332), g_error_516); Sleep(500); } } } } } } return (0); } void BBandsSignal() { gi_440 = FALSE; gi_444 = FALSE; gi_448 = FALSE; gi_452 = FALSE; g_ibands_456 = iBands(NULL, 0, SignalPeriod, 2, 0, PRICE_CLOSE, MODE_LOWER, 1); g_ibands_464 = iBands(NULL, 0, SignalPeriod, 2, 0, PRICE_CLOSE, MODE_UPPER, 1); gd_472 = g_ibands_464 - g_ibands_456; if (Bid <= g_ibands_456 - EntrySignalAdjust * gd_396 * Point && gd_472 >= minEntrySignal * gd_396 * Point) gi_440 = TRUE; if (Bid >= g_ibands_464 + EntrySignalAdjust * gd_396 * Point && gd_472 >= minEntrySignal * gd_396 * Point) gi_444 = TRUE; if (Bid >= g_ibands_464 + ExitSignalAdjust * gd_396 * Point) gi_452 = TRUE; if (Bid <= g_ibands_456 - ExitSignalAdjust * gd_396 * Point) gi_448 = TRUE; gd_128 = g_ibands_464 + ExitSignalAdjust * gd_396 * Point; gd_136 = g_ibands_456 - ExitSignalAdjust * gd_396 * Point; } void PositionCount() { g_count_192 = 0; gi_412 = 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_288 && OrderSymbol() == Symbol()) g_count_192++; } if (g_count_192 < MaxPosition) gi_412 = TRUE; } int NoPos() { bool li_ret_0 = TRUE; for (int l_index_4 = 0; l_index_4 < gi_520; l_index_4++) { if (Hour() == TimeHour(gia_564[l_index_4])) { li_ret_0 = FALSE; break; } } return (li_ret_0); } int CheckPosition() { CacheOrders(); gi_416 = FALSE; gi_420 = FALSE; if (gi_160 == TRUE) { gi_416 = gi_452; gi_420 = gi_448; } if (!gi_216) { for (int l_index_0 = 0; l_index_0 < gi_520; l_index_0++) { if (gia_532[l_index_0] == 0 && gi_416 == TRUE) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_504 = OrderClose(gia_528[l_index_0], gda_536[l_index_0], NormalizeDouble(Bid, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_504) { g_count_332++; g_error_516 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close LONG order - attempt #", g_count_332), g_error_516); if (g_error_516 == 2/* COMMON_ERROR */) return (-1); if (g_error_516 == 4108/* INVALID_TICKET */) { g_count_332 = OrderSendRetries; gi_524 = -1; } else Sleep(500); } else { g_count_332 = OrderSendRetries; gi_524 = -1; } } } else { if (gia_532[l_index_0] == 1 && gi_420 == TRUE) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_504 = OrderClose(gia_528[l_index_0], gda_536[l_index_0], NormalizeDouble(Ask, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_504) { g_count_332++; g_error_516 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close SHORT order - attempt #", g_count_332), g_error_516); if (g_error_516 == 2/* COMMON_ERROR */) return (-1); if (g_error_516 == 4108/* INVALID_TICKET */) { g_count_332 = OrderSendRetries; gi_524 = -1; } else Sleep(500); } else { g_count_332 = OrderSendRetries; gi_524 = -1; } } } } } } if (gi_216) { for (int l_index_4 = 0; l_index_4 < gi_520; l_index_4++) { if (gia_532[l_index_4] == 0 && (Bid >= gda_540[l_index_4] + TakeProfit * gd_396 * Point && gda_548[l_index_4] != gda_540[l_index_4] + TakeProfit * gd_396 * Point) || gi_416 == TRUE) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_504 = OrderClose(gia_528[l_index_4], gda_536[l_index_4], NormalizeDouble(Bid, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_504) { g_count_332++; g_error_516 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close LONG order - attempt #", g_count_332), g_error_516); if (g_error_516 == 2/* COMMON_ERROR */) return (-1); if (g_error_516 == 4108/* INVALID_TICKET */) { g_count_332 = OrderSendRetries; gi_524 = -1; } else Sleep(500); } else { g_count_332 = OrderSendRetries; gi_524 = -1; } } } else { if (gia_532[l_index_4] == 1 && (Ask <= gda_540[l_index_4] - TakeProfit * gd_396 * Point && gda_548[l_index_4] != gda_540[l_index_4] - TakeProfit * gd_396 * Point) || gi_420 == TRUE) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); if (TradeIsBusy() < 0) return (-1); g_ord_close_504 = OrderClose(gia_528[l_index_4], gda_536[l_index_4], NormalizeDouble(Ask, Digits), Slippage, White); TradeIsNotBusy(); if (!g_ord_close_504) { g_count_332++; g_error_516 = GetLastError(); logError("CheckPosition", StringConcatenate("failed to close SHORT order - attempt #", g_count_332), g_error_516); if (g_error_516 == 2/* COMMON_ERROR */) return (-1); if (g_error_516 == 4108/* INVALID_TICKET */) { g_count_332 = OrderSendRetries; gi_524 = -1; } else Sleep(500); } else { g_count_332 = OrderSendRetries; gi_524 = -1; } } } else { if (gia_532[l_index_4] == 0 && gi_224 && gda_548[l_index_4] > NormalizeDouble(gda_540[l_index_4] + TakeProfit * gd_396 * Point, Digits) && Bid < gda_540[l_index_4]) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); gd_336 = NormalizeDouble(Ask + gi_220 * gd_396 * Point, Digits); if (gd_336 < NormalizeDouble(gda_540[l_index_4] + TakeProfit * gd_396 * Point, Digits)) gd_336 = NormalizeDouble(gda_540[l_index_4] + TakeProfit * gd_396 * Point, Digits); if (gd_336 >= NormalizeDouble(gda_548[l_index_4], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_508 = OrderModify(gia_528[l_index_4], gda_540[l_index_4], gda_544[l_index_4], NormalizeDouble(gd_336, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_508) { g_count_332 = OrderSendRetries; gda_548[l_index_4] = gd_336; } else { g_count_332++; logError("CheckPosition", StringConcatenate("failed to modify LONG T/P - attempt #", g_count_332), GetLastError()); Sleep(500); } } } else { if (gia_532[l_index_4] == 1 && gi_224 && gda_548[l_index_4] < NormalizeDouble(gda_540[l_index_4] - TakeProfit * gd_396 * Point, Digits) && Ask > gda_540[l_index_4]) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { RefreshRates(); gd_336 = NormalizeDouble(Bid - gi_220 * gd_396 * Point, Digits); if (gd_336 > NormalizeDouble(gda_540[l_index_4] - TakeProfit * gd_396 * Point, Digits)) gd_336 = NormalizeDouble(gda_540[l_index_4] - TakeProfit * gd_396 * Point, Digits); if (gd_336 <= NormalizeDouble(gda_548[l_index_4], Digits)) break; if (TradeIsBusy() < 0) return (-1); g_bool_508 = OrderModify(gia_528[l_index_4], gda_540[l_index_4], gda_544[l_index_4], NormalizeDouble(gd_336, Digits), 0, Blue); TradeIsNotBusy(); if (g_bool_508) { g_count_332 = OrderSendRetries; gda_548[l_index_4] = gd_336; } else { g_count_332++; logError("CheckPosition", StringConcatenate("failed to modify SHORT T/P - attempt #", g_count_332), 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_376, 2); ld_ret_0 = NormalizeDouble(ld_ret_0 / g_lotstep_368, 0) * g_lotstep_368; ld_ret_0 = MathMin(maxLots, MathMax(minLots, ld_ret_0)); } else ld_ret_0 = minLots; return (ld_ret_0); } void Function_Trade() { if (gi_440 && gi_412 && NoPos()) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { if (TradeIsBusy() < 0) return; RefreshRates(); if (gi_216) gd_336 = Ask + gi_220 * gd_396 * Point; else gd_336 = Ask + TakeProfit * gd_396 * Point; if (StopLoss > (g_stoplevel_324 + g_spread_328) / gd_396 - 1.0) gd_344 = Ask - StopLoss * gd_396 * Point; else gd_344 = Ask - 60.0 * gd_396 * Point; if (gi_252) gd_344 = Ask - gd_264 * gd_396 * Point; if (ECNMode) { g_ticket_512 = OrderSend(gs_usdchf_280, OP_BUY, LotSize(), NormalizeDouble(Ask, Digits), 0, 0, 0, g_comment_292, g_magic_288, 0, Blue); if (g_ticket_512 > 0) { OrderSelect(g_ticket_512, SELECT_BY_TICKET); OrderModify(g_ticket_512, OrderOpenPrice(), NormalizeDouble(gd_344, Digits), NormalizeDouble(gd_336, Digits), 0, Red); } } else g_ticket_512 = OrderSend(gs_usdchf_280, OP_BUY, LotSize(), NormalizeDouble(Ask, Digits), Slippage, NormalizeDouble(gd_344, Digits), NormalizeDouble(gd_336, Digits), g_comment_292, g_magic_288, 0, Blue); TradeIsNotBusy(); if (g_ticket_512 == -1) { g_count_332++; logError("Function_Trade", StringConcatenate("could not open LONG order - attempt #", g_count_332)); Sleep(500); } else { g_count_332 = OrderSendRetries; gi_524 = -1; } } } if (gi_444 && gi_412 && NoPos()) { g_count_332 = 0; while (g_count_332 < OrderSendRetries) { if (TradeIsBusy() < 0) break; RefreshRates(); if (gi_216) gd_336 = Bid - gi_220 * gd_396 * Point; else gd_336 = Bid - TakeProfit * gd_396 * Point; if (StopLoss > (g_stoplevel_324 + g_spread_328) / gd_396 - 1.0) gd_344 = Bid + StopLoss * gd_396 * Point; else gd_344 = Bid + 60.0 * gd_396 * Point; if (gi_252) gd_344 = Bid + gd_264 * gd_396 * Point; if (ECNMode) { g_ticket_512 = OrderSend(gs_usdchf_280, OP_SELL, LotSize(), NormalizeDouble(Bid, Digits), 0, 0, 0, g_comment_292, g_magic_288, 0, Red); if (g_ticket_512 > 0) { OrderSelect(g_ticket_512, SELECT_BY_TICKET); OrderModify(g_ticket_512, OrderOpenPrice(), NormalizeDouble(gd_344, Digits), NormalizeDouble(gd_336, Digits), 0, Red); } } else g_ticket_512 = OrderSend(gs_usdchf_280, OP_SELL, LotSize(), NormalizeDouble(Bid, Digits), Slippage, NormalizeDouble(gd_344, Digits), NormalizeDouble(gd_336, Digits), g_comment_292, g_magic_288, 0, Red); TradeIsNotBusy(); if (g_ticket_512 == -1) { g_count_332++; logError("Function_Trade", StringConcatenate("could not open SHORT order - attempt #", g_count_332)); Sleep(500); } else { g_count_332 = OrderSendRetries; gi_524 = -1; } } } } void CacheOrders() { if (gi_524 != OrdersTotal()) InitializeOrders(g_magic_288); } 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_528, li_12); ArrayResize(gia_532, li_12); ArrayResize(gda_536, li_12); ArrayResize(gda_540, li_12); ArrayResize(gda_544, li_12); ArrayResize(gda_548, li_12); ArrayResize(gia_564, li_12); ArrayResize(gda_552, li_12); ArrayResize(gda_556, li_12); ArrayResize(gda_560, li_12); ArrayResize(gsa_572, li_12); ArrayResize(gia_568, li_12); ArrayInitialize(gia_528, 0); ArrayInitialize(gia_532, 0); ArrayInitialize(gda_536, 0); ArrayInitialize(gda_540, 0); ArrayInitialize(gda_544, 0); ArrayInitialize(gda_548, 0); ArrayInitialize(gia_564, 0); ArrayInitialize(gda_552, 0); ArrayInitialize(gda_556, 0); ArrayInitialize(gda_560, 0); ArrayInitialize(gia_568, 0); gi_520 = 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_288 && OrderSymbol() == gs_usdchf_280) { gia_528[gi_520] = OrderTicket(); gia_532[gi_520] = OrderType(); gda_536[gi_520] = NormalizeDouble(OrderLots(), gi_392); gda_540[gi_520] = NormalizeDouble(OrderOpenPrice(), Digits); gda_544[gi_520] = NormalizeDouble(OrderStopLoss(), Digits); gda_548[gi_520] = NormalizeDouble(OrderTakeProfit(), Digits); gia_564[gi_520] = OrderOpenTime(); gda_552[gi_520] = NormalizeDouble(OrderProfit(), 2); gda_556[gi_520] = NormalizeDouble(OrderSwap(), 2); gda_560[gi_520] = NormalizeDouble(OrderCommission(), 2); gsa_572[gi_520] = OrderComment(); gia_568[gi_520] = OrderExpiration(); gi_520++; } } } li_12 = MathMax(gi_520, 1); ArrayResize(gia_528, li_12); ArrayResize(gia_532, li_12); ArrayResize(gda_536, li_12); ArrayResize(gda_540, li_12); ArrayResize(gda_544, li_12); ArrayResize(gda_548, li_12); ArrayResize(gia_564, li_12); ArrayResize(gda_552, li_12); ArrayResize(gda_556, li_12); ArrayResize(gda_560, li_12); ArrayResize(gsa_572, li_12); ArrayResize(gia_568, li_12); gi_524 = 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); }