#property copyright "Copyright © 2009, www.forex-knightrider.com" #property link "www.forex-knightrider.com" #include int gi_76 = 17; int gi_80 = 9; int gi_84 = 7; int gi_88 = 8; int gi_92 = 14; int gi_96 = 16; extern double Lot = 0.1; extern int MY.Leverage = 20; extern int Broker.GMT = 2; bool gi_116 = FALSE; int gi_120 = 0; int gi_124 = 100; extern int StopLoss = 60; extern int TakeProfit = 6; extern int BrokerStopLoss = 100; extern int BrokerTakeProfit = 100; extern int MAGIC.NUMBER = 10031973; int g_period_148 = 14; int g_period_152 = 14; int g_slippage_156 = 3; double gd_160 = 1.0; int init() { SetDigits(); HideTestIndicators(TRUE); return (0); } int deinit() { return (0); } int start() { CheckForClose(); CheckForOpen(getLot()); Comments(); return (0); } void SetDigits() { if (Digits == 5) gd_160 = 10; } bool CheckTimeToTrade() { bool li_ret_0 = FALSE; int li_4 = gi_84 + Broker.GMT; if (li_4 >= 24) li_4 -= 24; if (li_4 < 0) li_4 += 24; int li_8 = gi_92 + Broker.GMT; if (li_8 >= 24) li_8 -= 24; if (li_8 < 0) li_8 += 24; int li_12 = gi_88 + Broker.GMT; if (li_12 >= 24) li_12 -= 24; if (li_12 < 0) li_12 += 24; if (li_12 == 0) li_12 = 24; int li_16 = gi_96 + Broker.GMT; if (li_16 >= 24) li_16 -= 24; if (li_16 < 0) li_16 += 24; if (li_16 == 0) li_16 = 24; if ((Hour() >= li_4 && Hour() <= li_12) || (Hour() >= li_8 && Hour() <= li_16)) li_ret_0 = TRUE; if (DayOfWeek() == 0 || (DayOfWeek() == 5 && Hour() > gi_76) || (DayOfWeek() == 1 && Hour() < gi_80)) li_ret_0 = FALSE; return (li_ret_0); } bool InTrade(string a_symbol_0) { bool li_ret_8 = FALSE; for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) { if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES) == FALSE) break; if (OrderMagicNumber() == MAGIC.NUMBER && OrderSymbol() == a_symbol_0 && OrderType() < OP_BUYLIMIT) { li_ret_8 = TRUE; break; } } return (li_ret_8); } double GetLastLot() { double l_ord_lots_0 = 0; for (int li_8 = 1; li_8 <= OrdersHistoryTotal(); li_8++) { OrderSelect(OrdersHistoryTotal() - li_8, SELECT_BY_POS, MODE_HISTORY); if (OrderMagicNumber() == MAGIC.NUMBER && OrderSymbol() == Symbol()) { l_ord_lots_0 = OrderLots(); break; } } return (l_ord_lots_0); } void CheckForClose() { int l_error_0; bool li_4; for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) { if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break; if (OrderMagicNumber() != MAGIC.NUMBER || OrderSymbol() != Symbol()) continue; RefreshRates(); switch (OrderType()) { case OP_SELL: if (Ask >= OrderOpenPrice() + StopLoss * Point * gd_160 || Ask <= OrderOpenPrice() - TakeProfit * Point * gd_160) { OrderClose(OrderTicket(), OrderLots(), Ask, 3, Orange); li_4 = TRUE; } if (TimeCurrent() - OrderOpenTime() > 60 * Period() * gi_124 && OrderOpenPrice() - OrderClosePrice() > 0.0) { OrderClose(OrderTicket(), OrderLots(), Ask, 3, Blue); li_4 = TRUE; } break; case OP_BUY: if (Bid <= OrderOpenPrice() - StopLoss * Point * gd_160 || Bid >= OrderOpenPrice() + TakeProfit * Point * gd_160) { OrderClose(OrderTicket(), OrderLots(), Bid, 3, Orange); li_4 = TRUE; } if (TimeCurrent() - OrderOpenTime() > 60 * Period() * gi_124 && OrderClosePrice() - OrderOpenPrice() > 0.0) { OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue); li_4 = TRUE; } } if (li_4 == FALSE) { l_error_0 = GetLastError(); if (l_error_0 > 0/* NO_ERROR */) Print("OrderDelete failed, error=", l_error_0, ": ", ErrorDescription(l_error_0)); } } } void CheckForOpen(double a_lots_0) { int l_ticket_16; int l_error_20; double l_icci_28; double l_irsi_36; double l_icci_44; double l_irsi_52; double l_icci_60; double l_irsi_68; double l_ima_76; double l_ima_84; RefreshRates(); double ld_8 = MarketInfo(Symbol(), MODE_SPREAD) / gd_160; if (InTrade(Symbol()) == 0) { if (CheckTimeToTrade()) { if (InLastClose() != 1) { l_icci_28 = iCCI(NULL, 0, g_period_152, PRICE_TYPICAL, 0); l_irsi_36 = iRSI(NULL, 0, g_period_148, PRICE_TYPICAL, 0); l_icci_44 = iCCI(NULL, 0, g_period_152, PRICE_TYPICAL, 1); l_irsi_52 = iRSI(NULL, 0, g_period_148, PRICE_TYPICAL, 1); l_icci_60 = iCCI(NULL, PERIOD_H1, g_period_152, PRICE_TYPICAL, 0); l_irsi_68 = iRSI(NULL, PERIOD_H1, g_period_148, PRICE_TYPICAL, 0); l_ima_76 = iMA(NULL, PERIOD_H1, 5, 0, MODE_EMA, PRICE_TYPICAL, 0); l_ima_84 = iMA(NULL, PERIOD_H1, 13, 0, MODE_EMA, PRICE_TYPICAL, 0); if (l_icci_44 < 90.0 && l_icci_28 > 90.0 && l_irsi_52 < 50.0 && l_irsi_36 > 50.0) { l_ticket_16 = OrderSend(Symbol(), OP_BUY, a_lots_0, Ask, g_slippage_156, Ask - BrokerStopLoss * Point * gd_160, Ask + BrokerTakeProfit * Point * gd_160, "BuyOrder", MAGIC.NUMBER, 0, Green); if (l_ticket_16 <= 0) { l_error_20 = GetLastError(); if (l_error_20 > 0/* NO_ERROR */) Print("BuyOrderSend failed: ", l_error_20, ": ", ErrorDescription(l_error_20)); } } if (l_icci_44 > -90.0 && l_icci_28 < -90.0 && l_irsi_52 > 50.0 && l_irsi_36 < 50.0) { l_ticket_16 = OrderSend(Symbol(), OP_SELL, a_lots_0, Bid, g_slippage_156, Bid + BrokerStopLoss * Point * gd_160, Bid - BrokerTakeProfit * Point * gd_160, "SellOrder", MAGIC.NUMBER, 0, Red); if (l_ticket_16 <= 0) { l_error_20 = GetLastError(); if (l_error_20 > 0/* NO_ERROR */) Print("SellOrderSend failed: ", l_error_20, ": ", ErrorDescription(l_error_20)); } } } } } } void Comments() { string ls_0 = ""; string ls_12 = "\n"; string ls_20 = "" + "\n" + " Forex Knight Rider ver 1.0 Ultimate © Build 01" + "\n" + "------------------------------------------------" + "\n" + "BROKER INFORMATION:" + "\n" + "Broker Company: " + AccountCompany() + "\n" + "------------------------------------------------" + "\n" + "ACCOUNT INFORMATION:" + "\n" + "Account Name: " + AccountName() + ls_12 + "Account Number: " + AccountNumber() + ls_12 + "Account Leverage: " + DoubleToStr(AccountLeverage(), 0) + ls_12 + "Account Balance: " + DoubleToStr(AccountBalance(), 2) + ls_12 + "Account Currency: " + AccountCurrency() + ls_12 + "Account Equity: " + DoubleToStr(AccountEquity(), 2) + "\n" + "------------------------------------------------" + "\n" + "MARGIN INFORMATION:" + "\n" + "Free Margin: " + DoubleToStr(AccountFreeMargin(), 2) + ls_12 + "Used Margin: " + DoubleToStr(AccountMargin(), 2) + "\n" + "------------------------------------------------" + "\n" + "ORDER INFORMATION:" + ls_12; ls_0 = Symbol() + ": Did not find any open order!!!"; for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) { if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break; if (OrderMagicNumber() == MAGIC.NUMBER && OrderSymbol() == Symbol() && OrderType() < OP_BUYLIMIT) ls_0 = "Order StopLoss " + StopLoss + ls_12 + "Order TakeProfit " + TakeProfit + ls_12 + "Order Lots " + DoubleToStr(OrderLots(), 2) + ls_12 + "Current.Broker.Spread " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD) / gd_160, 2) + ls_12 + "------------------------------------------------"; } for (l_pos_8 = 0; !IsStopped() && !IsConnected(); l_pos_8++) { Comment("Waiting for connection"); Sleep(150); } Comment(ls_20 + ls_0); } double getLot() { double ld_ret_0 = Lot; double l_maxlot_8 = MarketInfo(Symbol(), MODE_MAXLOT); double l_minlot_16 = MarketInfo(Symbol(), MODE_MINLOT); if (gi_116 == FALSE) { ld_ret_0 = NormalizeDouble(AccountBalance() * MY.Leverage / 100000.0, 1); Print("mylot ", ld_ret_0, " GetLastLot ", GetLastLot()); if (ld_ret_0 < GetLastLot()) ld_ret_0 = GetLastLot(); } if (gi_116 == TRUE) { for (int li_24 = 1; li_24 < OrdersHistoryTotal(); li_24++) { OrderSelect(OrdersHistoryTotal() - li_24, SELECT_BY_POS, MODE_HISTORY); if (OrderMagicNumber() != MAGIC.NUMBER || OrderSymbol() != Symbol()) continue; if (OrderProfit() < 0.0) { if (gi_120 > 0) { ld_ret_0 = OrderLots() * gi_120; break; } ld_ret_0 = OrderLots() * NormalizeDouble(StopLoss / TakeProfit, 0) + OrderLots(); break; } if (OrderProfit() > 0.0) { ld_ret_0 = Lot; break; } } } if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8; if (ld_ret_0 < l_minlot_16) ld_ret_0 = l_minlot_16; return (ld_ret_0); } int InLastClose() { bool li_ret_0 = FALSE; for (int li_4 = 1; li_4 < OrdersHistoryTotal(); li_4++) { OrderSelect(OrdersHistoryTotal() - li_4, SELECT_BY_POS, MODE_HISTORY); if (OrderMagicNumber() == MAGIC.NUMBER && OrderSymbol() == Symbol() && OrderType() < OP_BUYLIMIT) { if (TimeCurrent() - OrderCloseTime() < 60 * Period()) { li_ret_0 = TRUE; break; } li_ret_0 = FALSE; break; } } return (li_ret_0); }