#property copyright "Copyright © 2009, Gary White" #property link "http://www.forex-ai.com" #import "forex-ai.dll" int AuthorizeDLL(string a0, string a1); string ReadLicFile(string a0); int Account1No(); int Account2No(); int Account3No(); int Account4No(); int Account5No(); int Account6No(); string ExpiryDate(); double GetNNOutput(double a0, double a1, double a2, double a3, double a4, double a5, double a6, double a7); double GetLotSizeX(string a0, double a1, double a2, double a3, int a4, int a5); #import extern string ModeHelp = "A-Auto, F-Fixed Lot"; extern string Mode = "A"; extern bool UseMicroLots = FALSE; extern double RiskPerTradePercent = 3.0; extern double FixedSizeLots = 0.1; extern string SLModeHelp = "S/L Mode P-Periodical, T-Trail, S-Smart"; extern string SLMode = "T"; extern int StopLoss = 90; extern string SmartSLHelp = "SmartSL is used in Smart SL and TP mode"; extern int SmartSL = 10; extern string TPModeHelp = "T/P Mode C-Classic, S-Smart"; extern string TPMode = "C"; extern int TakeProfit = 820; extern int SmartTP = 10; extern bool ClosePositionsOnly = FALSE; extern int alpha = 114; extern int beta = 132; extern int gamma = 156; extern int delta = 199; extern string alt_symbol = "AUDUSD"; extern int alt_alpha = 200; extern int alt_beta = 42; extern int alt_gamma = 81; extern int alt_delta = 160; extern int MagicNumber = 543; extern int ReOrderDelaySec = 10; string gs_forex_ai_220 = "Forex-Ai"; string gs_228 = ""; int gi_236 = 0; int gi_240 = 0; int gi_unused_244 = 3; bool gi_248 = FALSE; string gs_252; string gs_260; double gd_268 = 0.0; double gd_276 = 0.0; double g_point_284; double g_lots_292 = 0.1; int init() { if (Point == 0.00001) g_point_284 = 0.0001; else { if (Point == 0.001) g_point_284 = 0.01; else g_point_284 = Point; } gs_260 = "1WDDFEsWcn qVklKpWxA5 QeaNKlSoF 6kKeQXcBmqw 8$dsqLefnx PdwaLdvYwq )d#4daw.jJ KwLlsaqK*3 #d1!pfdefa Lo)0983asr Sc^k3*(jlW"; gs_252 = ReadLicFile(TerminalPath() + "\\experts\\forex-ai.lic"); gi_236 = AuthorizeDLL(gs_252, gs_260); if (gi_236 != 1) { Comment("Your copy of Forex-AI Autotrader is not licensed to use!"); return (0); } Comment("Forex-AI is waiting for ticks... It will trade " + DoubleToStr(GetLotSize(), 2) + " lots."); gi_248 = FALSE; gd_268 = 0; gd_276 = 0; return (0); } int deinit() { return (0); } int start() { int l_ord_total_4; int l_ticket_24; if (gi_236 != 1) { Comment("Your copy of Forex-AI Autotrader is not licensed to use!"); return (0); } int l_str2time_0 = StrToTime(ExpiryDate()); if (TimeCurrent() >= l_str2time_0) { Comment("The grace period has been expired! If you have purchased the licensed copy, email: support@forex-ai.com"); return (0); } if (!(Symbol() == "EURUSD" || Symbol() == "EURUSDm" || Symbol() == "EURUSD-fx")) { } if (Period() != PERIOD_H4) { } if (gs_forex_ai_220 != "Forex-Ai") { Comment("Wrong password!"); return (0); } if (!(AccountNumber() == Account1No() || AccountNumber() == Account2No() || AccountNumber() == Account3No() || AccountNumber() == Account4No() || AccountNumber() == Account5No() || Account6No() == 615243)) { Comment("This account is not authorized to use Forex AI"); return (0); } gs_228 = "Trade Size " + DoubleToStr(GetLotSize(), 2) + " lots."; gd_268 = Fperceptron(); gd_276 = Aperceptron(); gs_228 = gs_228 + " F:" + Fperceptron(); gs_228 = gs_228 + " A:" + Aperceptron(); if (gd_268 > 0.0 && gd_276 > 0.0) gs_228 = gs_228 + " Long Bias"; else { if (gd_268 < 0.0 && gd_276 < 0.0) gs_228 = gs_228 + " Short Bias"; else gs_228 = gs_228 + " Neutral"; } if (ClosePositionsOnly) gs_228 = "\nThe system will only close existing positions"; gs_228 = gs_228 + "\nSLMode:" + SLMode + " SL:" + StopLoss + " SmartSL:" + SmartSL; gs_228 = gs_228 + " TPMode:" + TPMode + " TP:" + TakeProfit + " SmartTP:" + SmartTP; if (gi_248) { gs_228 = gs_228 + "\nSmartExitTriggered: true"; } else { gs_228 = gs_228 + "\nSmartExitTriggered: false"; } Comment(gs_228); if (Time[0] == gi_240) { l_ord_total_4 = OrdersTotal(); for (int l_pos_8 = 0; l_pos_8 < l_ord_total_4; l_pos_8++) { OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if (OrderType() == OP_BUY) { if (!(gd_268 > 0.0 && gd_276 > 0.0)) gi_248 = TRUE; } else if (!(gd_268 < 0.0 && gd_276 < 0.0)) gi_248 = TRUE; if (SLMode == "T") { if (OrderType() == OP_BUY) { if (OrderStopLoss() < Bid - StopLoss * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * g_point_284, OrderTakeProfit(), 0, Red); } else if (OrderStopLoss() > Ask + StopLoss * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * g_point_284, OrderTakeProfit(), 0, Blue); } if (SLMode == "S") { if (gi_248) { if (OrderType() == OP_BUY) { if (OrderStopLoss() < Bid - SmartSL * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), Bid - SmartSL * g_point_284, OrderTakeProfit(), 0, Red); } else if (OrderStopLoss() > Ask + SmartSL * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), Ask + SmartSL * g_point_284, OrderTakeProfit(), 0, Blue); } else { if (OrderType() == OP_BUY) { if (OrderStopLoss() < Bid - StopLoss * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * g_point_284, OrderTakeProfit(), 0, Red); } else if (OrderStopLoss() > Ask + StopLoss * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * g_point_284, OrderTakeProfit(), 0, Blue); } } if (OrderStopLoss() == 0.0) { if (OrderType() == OP_BUY) { if (SLMode == "S") OrderModify(OrderTicket(), OrderOpenPrice(), Bid - SmartSL * g_point_284, OrderTakeProfit(), 0, Red); else OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * g_point_284, OrderTakeProfit(), 0, Red); } else { if (SLMode == "S") OrderModify(OrderTicket(), OrderOpenPrice(), Ask + SmartSL * g_point_284, OrderTakeProfit(), 0, Blue); else OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * g_point_284, OrderTakeProfit(), 0, Blue); } } if (TakeProfit > 0) { if (OrderTakeProfit() == 0.0) { if (OrderType() == OP_BUY) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() + TakeProfit * g_point_284, 0, Yellow); else OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() - TakeProfit * g_point_284, 0, Yellow); } if (gi_248) { if (TPMode == "S") { if (OrderType() == OP_BUY) { if (OrderTakeProfit() > Bid + SmartTP * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Bid + SmartTP * g_point_284, 0, Yellow); } else if (OrderTakeProfit() < Ask - SmartTP * g_point_284) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Ask - SmartTP * g_point_284, 0, Yellow); } } } return (0); } gi_248 = FALSE; } gi_248 = FALSE; return (0); } gi_240 = Time[0]; if (IsTradeAllowed()) { RefreshRates(); gi_unused_244 = MarketInfo(Symbol(), MODE_SPREAD); } else { gi_240 = Time[1]; return (0); } int l_ticket_12 = -1; int l_ord_total_16 = OrdersTotal(); for (int l_pos_20 = 0; l_pos_20 < l_ord_total_16; l_pos_20++) { OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { l_ticket_24 = OrderTicket(); if (OrderType() == OP_BUY) { if (gd_268 < 0.0 && gd_276 < 0.0) { OrderClose(l_ticket_24, OrderLots(), Bid, 3, Yellow); Sleep(1000 * ReOrderDelaySec); if (!ClosePositionsOnly) { g_lots_292 = GetLotSize(); l_ticket_12 = OrderSend(Symbol(), OP_SELL, g_lots_292, Bid, 3, 0, 0, "AI", MagicNumber, 0, Red); Sleep(1000 * ReOrderDelaySec); if (l_ticket_12 < 0) { Sleep(1000 * ReOrderDelaySec); gi_240 = Time[1]; } else ModifyShortOrder(l_ticket_12); } } else { if (OrderStopLoss() < Bid - StopLoss * g_point_284) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * g_point_284, OrderTakeProfit(), 0, Red)) { Sleep(1000 * ReOrderDelaySec); gi_240 = Time[1]; } } } } else { if (gd_268 > 0.0 && gd_276 > 0.0) { OrderClose(l_ticket_24, OrderLots(), Ask, 3, Yellow); Sleep(1000 * ReOrderDelaySec); if (!ClosePositionsOnly) { g_lots_292 = GetLotSize(); l_ticket_12 = OrderSend(Symbol(), OP_BUY, g_lots_292, Ask, 3, 0, 0, "AI", MagicNumber, 0, Blue); Sleep(1000 * ReOrderDelaySec); if (l_ticket_12 < 0) { Sleep(1000 * ReOrderDelaySec); gi_240 = Time[1]; } else ModifyLongOrder(l_ticket_12); } } else { if (OrderStopLoss() > Ask + StopLoss * g_point_284) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * g_point_284, OrderTakeProfit(), 0, Blue)) { Sleep(1000 * ReOrderDelaySec); gi_240 = Time[1]; } } } } return (0); } } if (!ClosePositionsOnly) { if (gd_268 > 0.0 && gd_276 > 0.0) { g_lots_292 = GetLotSize(); l_ticket_12 = OrderSend(Symbol(), OP_BUY, g_lots_292, Ask, 3, 0, 0, "AI", MagicNumber, 0, Green); Sleep(1000 * ReOrderDelaySec); if (l_ticket_12 < 0) { Sleep(1000 * ReOrderDelaySec); gi_240 = Time[1]; } else ModifyLongOrder(l_ticket_12); } else { if (gd_268 < 0.0 && gd_276 < 0.0) { g_lots_292 = GetLotSize(); l_ticket_12 = OrderSend(Symbol(), OP_SELL, g_lots_292, Bid, 3, 0, 0, "AI", MagicNumber, 0, Yellow); Sleep(1000 * ReOrderDelaySec); if (l_ticket_12 < 0) { Sleep(1000 * ReOrderDelaySec); gi_240 = Time[1]; } else ModifyShortOrder(l_ticket_12); } } } return (0); } void ModifyLongOrder(int a_ticket_0) { if (TakeProfit > 0) { if (SLMode == "S") OrderModify(a_ticket_0, OrderOpenPrice(), Bid - SmartSL * g_point_284, OrderOpenPrice() + TakeProfit * g_point_284, 0, Green); else OrderModify(a_ticket_0, OrderOpenPrice(), Bid - StopLoss * g_point_284, OrderOpenPrice() + TakeProfit * g_point_284, 0, Green); } else { if (SLMode == "S") OrderModify(a_ticket_0, OrderOpenPrice(), Bid - SmartSL * g_point_284, 0, 0, Yellow); else OrderModify(a_ticket_0, OrderOpenPrice(), Bid - StopLoss * g_point_284, 0, 0, Yellow); } Sleep(1000 * ReOrderDelaySec); } void ModifyShortOrder(int a_ticket_0) { if (TakeProfit > 0) { if (SLMode == "S") OrderModify(a_ticket_0, OrderOpenPrice(), Ask + SmartSL * g_point_284, OrderOpenPrice() - TakeProfit * g_point_284, 0, Yellow); else OrderModify(a_ticket_0, OrderOpenPrice(), Ask + StopLoss * g_point_284, OrderOpenPrice() - TakeProfit * g_point_284, 0, Yellow); } else { if (SLMode == "S") OrderModify(a_ticket_0, OrderOpenPrice(), Ask + SmartSL * g_point_284, 0, 0, Yellow); else OrderModify(a_ticket_0, OrderOpenPrice(), Ask + StopLoss * g_point_284, 0, 0, Yellow); } Sleep(1000 * ReOrderDelaySec); } double Fperceptron() { double l_iac_0 = iAC(Symbol(), 0, 0); double l_iac_8 = iAC(Symbol(), 0, 7); double l_iac_16 = iAC(Symbol(), 0, 14); double l_iac_24 = iAC(Symbol(), 0, 21); return (GetNNOutput(alpha, beta, gamma, delta, l_iac_0, l_iac_8, l_iac_16, l_iac_24)); } double Aperceptron() { double l_iac_0 = iAC(alt_symbol, 0, 0); double l_iac_8 = iAC(alt_symbol, 0, 7); double l_iac_16 = iAC(alt_symbol, 0, 14); double l_iac_24 = iAC(alt_symbol, 0, 21); return (GetNNOutput(alt_alpha, alt_beta, alt_gamma, alt_delta, l_iac_0, l_iac_8, l_iac_16, l_iac_24)); } double GetLotSize() { return (GetLotSizeX(Mode, FixedSizeLots, RiskPerTradePercent, AccountFreeMargin(), StopLoss, UseMicroLots)); }