#property copyright "Stella Robinson email: stella.robinson@hi-netcapital.com" #property link "www.hi-netcapital.com" bool gi_76 = TRUE; int gia_80[2] = {999999, 5568670}; int gia_84[2] = {1244419200, 1240185600}; int gi_88 = 2; string gs_unused_92 = "=== Slippage Settings ==="; int g_slippage_100 = 5; int gi_104 = 5; extern string Expert_Name = "== Hi-Net Grid Strategy (hi-netcapital.com)=="; extern string Version = " v.3.1.6(Express)"; extern string EmergencyExit = "=== Emergency Exit ==="; extern bool ManualShutdown = FALSE; extern string GeneralParameters = "===Hi-Net Grid Strategy Parameters =="; extern int MagicNumber = 1234; int gi_148 = 45; bool gi_152; bool gi_156; string gs_unused_160 = "=== Trading Time Option ==="; bool gi_168 = FALSE; string gs_172 = "2009.04.17 00:30"; string gs_180 = "2009.04.17 18:00"; string gs_unused_188 = "== Automatic System Entry Option =="; bool gi_196 = TRUE; int g_timeframe_200 = PERIOD_H1; string gs_unused_204 = "=== Price Entry Options ==="; bool gi_212 = FALSE; double gd_216 = 1.06; double gd_224 = 1.0554; extern string LotSizeSettings = "=== Lot Size Settings ==="; extern double AnchorLots = 0.1; double gd_248 = 2.0; int gi_unused_256 = 200; int gi_260 = 0; int gi_264 = 0; extern string ProfitTargetSettings = "=== Profit Target Settings ==="; extern double OverAllProfitTarget = 4.0; double gd_284 = 0.0; double gd_292 = 0.0; double gd_300 = 0.0; extern bool StopTradingWhenTargetReached = FALSE; string gs_unused_312 = "==== EA look to close-all at Maxlevel ===="; bool gi_320 = TRUE; int gi_324 = 4; string gs_unused_328 = "=== Equity Level Alerts ==="; bool gi_336 = FALSE; int gi_340 = -200; bool gi_344 = TRUE; bool gi_348 = TRUE; string gs_unused_352 = "=== Broker & Display Settings ===="; bool gi_360 = FALSE; bool gi_364 = TRUE; bool gi_368 = TRUE; int gi_unused_372; int gi_unused_376; bool gi_380 = FALSE; double gd_384 = 0.0; datetime g_time_392; double gd_unused_396 = 0.0; double gd_404; double gd_412; bool gi_420 = FALSE; bool gi_424 = TRUE; int init() { g_time_392 = 0; if (Digits == 3 || Digits == 5) gi_360 = TRUE; if (gi_104 + 2 * g_slippage_100 > gi_148) gi_104 = 0; if (gi_368) { gi_unused_372 = 16711680; gi_unused_376 = 255; } else { gi_unused_372 = -1; gi_unused_376 = -1; } ObjectCreate("text_object", OBJ_LABEL, 0, 0, 0); ObjectSet("text_object", OBJPROP_XDISTANCE, 220); ObjectSet("text_object", OBJPROP_YDISTANCE, 0); ObjectSetText("text_object", "www.hi-netcapital.com eMail: cs@hi-netcapital.com ", 12, "Verdana", DodgerBlue); return (0); } int start() { double ld_24; if (!IsTesting() && AccountNumber() == 0) return (0); if (IsDemo() == FALSE && gi_76 == TRUE) { ManualShutdown = TRUE; Comment("Error: Expert Advisor will only run on a demo account.\n", "Program is disabled."); return (0); } if (gi_212) Obj_Label("100", "PriceEntry", MediumTurquoise, 680, 15); if (gi_196) Obj_Label("100", "SREntry", MediumTurquoise, 680, 15); gi_424 = TRUE; int li_unused_0 = 0; int li_4 = SecurityCheck(); if (li_4 == -1) { Alert("The account " + AccountNumber() + " is not registered to run this EA!\nPlease contact cs@hi-netcapital.com for details."); return (0); } if (li_4 == -2) { Alert("This EA has already expired!\nPlease contact cs@hi-netcapital.com for details."); return (0); } if (li_4 == 2) Alert("This EA is expiring soon. Please contact fcs@hi-netcapital.com for details."); else { if (li_4 == 0) { if (!IsDemo() && !IsTesting()) { Alert("This is a demo version. Please contact cs@hi-netcapital.com for details."); return (0); } } } if (gd_284 > 0.0 && (getProfit() >= gd_284 && !gi_380)) { gi_380 = TRUE; gd_384 = gd_284 - gd_292; return (gi_380); } if (gi_380 == TRUE && getProfit() - gd_384 > gd_292) { gd_384 = getProfit() - gd_292; return (gd_384); } if (gi_380 == TRUE && getProfit() <= gd_384) { gi_420 = TRUE; gd_384 = 0; gi_380 = FALSE; gi_152 = FALSE; gi_156 = FALSE; return; } double ld_8 = Point; if (gi_360 == TRUE) ld_8 = Point / 0.1; if (subTotalTrade() > 0) { if (subSellAnchorPrice() > 0.0) gd_404 = (subSellAnchorPrice() - Ask) / NormalizeDouble(ld_8, Digits); else gd_404 = 0; if (subBuyAnchorPrice() > 0.0) gd_412 = (Bid - subBuyAnchorPrice()) / NormalizeDouble(ld_8, Digits); else gd_412 = 0; g_time_392 = Time[0]; if (OverAllProfitTarget != 0.0 && OverAllProfitTarget < getProfit()) { Comment("OverAllProfitTarget has been reached"); gi_424 = FALSE; gi_420 = TRUE; gi_152 = FALSE; gi_156 = FALSE; } if (gd_300 != 0.0 && gd_300 < getLoss()) { Comment("EquityStopLoss has been reached"); gi_424 = FALSE; gi_420 = TRUE; gi_152 = FALSE; gi_156 = FALSE; } if (ManualShutdown) gi_420 = TRUE; } double ld_16 = MarketInfo(Symbol(), MODE_TICKVALUE) * AnchorLots; if (gi_320 == TRUE && getProfit() >= ld_16 && subTotalOpenBuy() >= gi_324 || subTotalOpenSell() >= gi_324 && gi_324 != 0) { Comment("Attempt at breakeven profits+losses has been executed"); gi_424 = FALSE; gi_420 = TRUE; } if (subTotalTrade() == 0) { gi_420 = FALSE; if (ManualShutdown) { Comment("ManualShutdown"); gi_424 = FALSE; return (0); } if (StopTradingWhenTargetReached && g_time_392 != 0) { Comment("Hibernating"); gi_424 = FALSE; return (0); } } if (gi_420) CloseAll(); if (subTotalTrade() == 0 && gi_420 == FALSE) { if (longSignal()) subOpenOrder(0, Ask, 0, gi_260, AnchorLots, "Anchor-" + Symbol() + MagicNumber); if (shortSignal()) subOpenOrder(1, Bid, 0, gi_264, AnchorLots, "Anchor-" + Symbol() + MagicNumber); } if (gi_420 == FALSE) { if (subBuyAnchorTotal() > 0) CheckBuyGrid(); if (subSellAnchorTotal() > 0) CheckSellGrid(); } if (gi_424 && gi_364 == FALSE) { Comment("\n", Expert_Name, " Magic#: ", MagicNumber + "\nTime: " + TimeToStr(TimeCurrent(), TIME_SECONDS) + "\nAccountBalance: $" + DoubleToStr(AccountBalance(), 2) + "\nOverAllProfitTarget: $" + OverAllProfitTarget + "\nProfit: $" + DoubleToStr(getProfit(), 2) + "\nOpen Buys: ", subTotalOpenBuy(), " Open Sells: ", subTotalOpenSell()); } if (gi_424 && gi_364 == TRUE) { Comment("\n", Expert_Name, " Magic#: ", MagicNumber, " Time: " + TimeToStr(TimeCurrent(), TIME_SECONDS) + "\nBalance: $" + DoubleToStr(AccountBalance(), 2), " Equity Target: $" + OverAllProfitTarget, " Profit: $" + DoubleToStr(getProfit(), 2) + "\nOpen Buys: ", subTotalOpenBuy(), " Open Sells: ", subTotalOpenSell() + "\nEquTrail: ", gd_384); } if (gi_336) { ld_24 = getProfit(); if (ld_24 >= gi_340) { if (gi_344 == TRUE) PlaySound("alert.wav"); if (gi_348 == TRUE) Alert("Equity Alert ", Symbol(), ": $", ld_24); } } return (0); } void CheckBuyGrid() { double ld_24; int li_32; int li_36; int li_40; double ld_44; double ld_0 = subBuyAnchorPrice(); double ld_8 = subBuyAncorLot(); double ld_16 = Point; if (gi_360 == TRUE) ld_16 = Point / 0.1; if (ld_0 > 0.0) { ld_24 = (ld_0 - Ask) / ld_16; if (ld_24 > 0.0) { li_32 = MathFloor(ld_24 / gi_148); li_36 = MathCeil(ld_24); li_40 = li_36 % gi_148; if (li_32 > 0 && li_40 <= gi_104) { ld_44 = NormalizeDouble(ld_0 - li_32 * gi_148 * ld_16, Digits); Print("pips=", ld_24, " level=", li_32, " iDist=", li_36, " nearpips=", li_40, " LevelPrice=", ld_44); if (!subIsBuyExistsAtPrice(ld_44)) { Print("Order not found"); if (Ask + gi_104 * ld_16 > ld_44) subOpenOrder(0, Ask, 0, gi_260, MathPow(gd_248, li_32) * ld_8, "Grid-" + Symbol() + MagicNumber); else if (Ask + g_slippage_100 * ld_16 > ld_44) subOpenOrder(0, ld_44, 0, gi_260, MathPow(gd_248, li_32) * ld_8, "Grid-" + Symbol() + MagicNumber); } } } } } void CheckSellGrid() { double ld_24; int li_32; int li_36; int li_40; double ld_44; double ld_0 = subSellAnchorPrice(); double ld_8 = subSellAncorLot(); double ld_16 = Point; if (gi_360 == TRUE) ld_16 = Point / 0.1; if (ld_0 > 0.0) { ld_24 = (Bid - ld_0) / ld_16; if (ld_24 > 0.0) { li_32 = MathFloor(ld_24 / gi_148); li_36 = MathCeil(ld_24); li_40 = li_36 % gi_148; if (li_32 > 0 && li_40 <= gi_104) { ld_44 = NormalizeDouble(ld_0 + li_32 * gi_148 * ld_16, Digits); if (!subIsSellExistsAtPrice(ld_44)) { if (Bid - gi_104 * ld_16 < ld_44) subOpenOrder(1, Bid, 0, gi_264, MathPow(gd_248, li_32) * ld_8, "Grid-" + Symbol() + MagicNumber); else if (Bid - g_slippage_100 * ld_16 < ld_44) subOpenOrder(1, ld_44, 0, gi_264, MathPow(gd_248, li_32) * ld_8, "Grid-" + Symbol() + MagicNumber); } } } } } int subTotalTrade() { int l_count_4 = 0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) l_count_4++; } return (l_count_4); } int subTotalOpenSell() { int l_count_4 = 0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber) l_count_4++; } return (l_count_4); } int subTotalOpenBuy() { int l_count_4 = 0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber) l_count_4++; } return (l_count_4); } double subBuyAncorLot() { double l_ord_lots_4 = 0.0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderComment() == "Anchor-" + Symbol() + MagicNumber && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT) l_ord_lots_4 = OrderLots(); } return (l_ord_lots_4); } double subSellAncorLot() { double l_ord_lots_4 = 0.0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderComment() == "Anchor-" + Symbol() + MagicNumber && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT) l_ord_lots_4 = OrderLots(); } return (l_ord_lots_4); } int subBuyAnchorTotal() { int l_count_4 = 0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderComment() == "Anchor-" + Symbol() + MagicNumber && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT) l_count_4++; } return (l_count_4); } bool subIsBuyExistsAtPrice(double ad_0) { double l_ord_open_price_12; double ld_20 = Point; if (gi_360 == TRUE) ld_20 = Point / 0.1; for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) { OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT) { l_ord_open_price_12 = OrderOpenPrice(); if (NormalizeDouble(l_ord_open_price_12 + (g_slippage_100 + gi_104) * ld_20, Digits) >= ad_0 && NormalizeDouble(l_ord_open_price_12 - g_slippage_100 * ld_20, Digits) <= ad_0) return (TRUE); } } return (FALSE); } bool subIsSellExistsAtPrice(double ad_0) { double l_ord_open_price_12; double ld_20 = Point; if (gi_360 == TRUE) ld_20 = Point / 0.1; for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) { OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT) { l_ord_open_price_12 = OrderOpenPrice(); if (NormalizeDouble(l_ord_open_price_12 - (g_slippage_100 + gi_104) * ld_20, Digits) <= ad_0 && NormalizeDouble(l_ord_open_price_12 + g_slippage_100 * ld_20, Digits) >= ad_0) return (TRUE); } } return (FALSE); } int subSellAnchorTotal() { int l_count_4 = 0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderComment() == "Anchor-" + Symbol() + MagicNumber && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT) l_count_4++; } return (l_count_4); } double subBuyAnchorPrice() { int li_unused_4 = 0; double l_ord_open_price_8 = 0.0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderComment() == "Anchor-" + Symbol() + MagicNumber && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) l_ord_open_price_8 = OrderOpenPrice(); } return (l_ord_open_price_8); } double subSellAnchorPrice() { int li_unused_4 = 0; double l_ord_open_price_8 = 0.0; for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) { OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES); if (OrderComment() == "Anchor-" + Symbol() + MagicNumber && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) l_ord_open_price_8 = OrderOpenPrice(); } return (l_ord_open_price_8); } int subOpenOrder(int ai_0, double a_price_4, int ai_12, int ai_16, double a_lots_20, string a_comment_28) { int l_ticket_36 = 0; int l_error_40 = 0; int l_count_44 = 0; double l_price_48 = 0; double l_price_56 = 0; double l_price_64 = 0; double l_price_72 = 0; double ld_80 = Point; if (gi_360 == TRUE) ld_80 = Point / 0.1; if (ai_12 != 0) { l_price_48 = NormalizeDouble(a_price_4 - ai_12 * NormalizeDouble(ld_80, Digits), 4); l_price_64 = NormalizeDouble(a_price_4 + ai_12 * NormalizeDouble(ld_80, Digits), 4); } if (ai_16 != 0) { l_price_56 = NormalizeDouble(a_price_4 + ai_16 * NormalizeDouble(ld_80, Digits), 4); l_price_72 = NormalizeDouble(a_price_4 - ai_16 * NormalizeDouble(ld_80, Digits), 4); } if (ai_0 == 0) { for (l_count_44 = 0; l_count_44 < 10; l_count_44++) { l_ticket_36 = OrderSend(Symbol(), OP_BUY, a_lots_20, a_price_4, g_slippage_100, l_price_48, l_price_56, a_comment_28, MagicNumber, 0, Green); l_error_40 = GetLastError(); if (l_error_40 == 0/* NO_ERROR */) { if (l_ticket_36 > 0) break; } else { if (l_error_40 == 0/* NO_ERROR */ || l_error_40 == 4/* SERVER_BUSY */ || l_error_40 == 136/* OFF_QUOTES */ || l_error_40 == 137/* BROKER_BUSY */ || l_error_40 == 138/* REQUOTE */ || l_error_40 == 146/* TRADE_CONTEXT_BUSY */) Sleep(5000); else if (l_ticket_36 > 0) break; } } } if (ai_0 == 1) { for (l_count_44 = 0; l_count_44 < 10; l_count_44++) { l_ticket_36 = OrderSend(Symbol(), OP_SELL, a_lots_20, a_price_4, g_slippage_100, l_price_64, l_price_72, a_comment_28, MagicNumber, 0, Red); l_error_40 = GetLastError(); if (l_error_40 == 0/* NO_ERROR */) { if (l_ticket_36 > 0) break; } else { if (l_error_40 == 0/* NO_ERROR */ || l_error_40 == 4/* SERVER_BUSY */ || l_error_40 == 136/* OFF_QUOTES */ || l_error_40 == 137/* BROKER_BUSY */ || l_error_40 == 138/* REQUOTE */ || l_error_40 == 146/* TRADE_CONTEXT_BUSY */) Sleep(5000); else if (l_ticket_36 > 0) break; } } } return (l_ticket_36); } bool timesAreValid() { if (!gi_168) return (TRUE); int l_datetime_0 = TimeCurrent(); if (StrToTime(gs_172) < l_datetime_0) if (StrToTime(gs_180) > l_datetime_0) return (TRUE); return (FALSE); } double getProfit() { double ld_ret_0 = 0; for (int l_ord_total_8 = OrdersTotal(); l_ord_total_8 >= 0; l_ord_total_8--) { if (OrderSelect(l_ord_total_8, SELECT_BY_POS, MODE_TRADES)) if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) ld_ret_0 = ld_ret_0 + OrderProfit() + OrderSwap(); } return (ld_ret_0); } double getLoss() { double ld_ret_0 = 0; for (int l_ord_total_8 = OrdersTotal(); l_ord_total_8 >= 0; l_ord_total_8--) { if (OrderSelect(l_ord_total_8, SELECT_BY_POS, MODE_TRADES)) if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) ld_ret_0 = ld_ret_0 - OrderProfit() - OrderSwap(); } return (ld_ret_0); } bool longSignal() { double l_ihigh_0 = iHigh(NULL, g_timeframe_200, 1); if (subTotalOpenSell() > 0) return (FALSE); if (timesAreValid()) { if (gi_212 == TRUE) { if (Ask == gd_216) { gi_152 = TRUE; gi_156 = FALSE; } if (!(gi_152 && !gi_156)) return (FALSE); return (TRUE); } if (gi_196 == TRUE) { if (Ask > l_ihigh_0) { gi_152 = TRUE; gi_156 = FALSE; } if (gi_152 && !gi_156) return (TRUE); } } return (FALSE); } bool shortSignal() { double l_ilow_0 = iLow(NULL, g_timeframe_200, 1); if (subTotalOpenBuy() > 0) return (FALSE); if (timesAreValid()) { if (gi_212 == TRUE) { if (Bid == gd_224) { gi_152 = FALSE; gi_156 = TRUE; } if (!gi_152 && gi_156) return (TRUE); } if (gi_196 == TRUE && Bid < l_ilow_0) { gi_152 = FALSE; gi_156 = TRUE; } if (!gi_152 && gi_156) return (TRUE); } return (FALSE); } void CloseAll() { int l_ord_total_0 = OrdersTotal(); for (int l_pos_4 = 0; l_pos_4 < l_ord_total_0; l_pos_4++) { OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, g_slippage_100, White); if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, g_slippage_100, White); if (OrderType() > OP_SELL) OrderDelete(OrderTicket()); } } } void Obj_Label(string a_name_0, string a_text_8, color a_color_16, int a_x_20, int a_y_24) { ObjectCreate(a_name_0, OBJ_LABEL, 0, 0, 0); ObjectSet(a_name_0, OBJPROP_XDISTANCE, a_x_20); ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_24); ObjectSetText(a_name_0, a_text_8, 10, "Verdana", a_color_16); } int SecurityCheck() { int li_0 = 1; if (!IsDemo()) { while (li_0 < ArraySize(gia_80)) { if (AccountNumber() == gia_80[li_0]) { if (TimeCurrent() < gia_84[li_0]) { if (TimeCurrent() > gia_84[li_0] - 86400 * gi_88) return (2); return (1); } return (-2); } li_0++; } return (-1); } if (TimeCurrent() < gia_84[0]) { if (TimeCurrent() > gia_84[0] - 86400 * gi_88) return (2); return (0); } return (-2); }