#property copyright "Copyright © 2007-2009, ForexAutoPilot.com 2.3" #property link "http://ForexAutoPilot.com" #import "Kernel32.dll" int CreateFileA(string a0, int a1, int a2, int a3, int a4, int a5, int a6); bool ReadFile(int a0, int& a1[], int a2, int& a3[], int a4); bool WriteFile(int a0, string a1, int a2, int& a3[], int a4); bool CloseHandle(int a0); #import extern double Lots = 1.0; extern double LotsRiskReductor = 1.0; extern int MaxOrders = 1; extern int MaxLots = 100; extern int StopTime = 5; extern int aaa = 13; extern int bbb = 14; extern double TakeProfit = 20.0; extern double StopLoss = 0.0; extern double TrailingStop = 0.0; extern int UseMAControl = 1; extern int PeriodMA = 100; extern int PriceMA_0_6 = 0; extern int TypeMA_0_3 = 0; extern double SpanGator = 0.5; extern int SlipPage = 3; extern int OrderMagic = 231313; extern double SafetyGapDemarker = 0.2; extern double SafetyGapWPR = 0.0; extern int StartWorkTimeHour = 0; extern int StartWorkTimeMin = 0; extern int EndWorkTimeHour = 0; extern int EndWorkTimeMin = 0; extern int OneTrade = 0; extern bool WriteDebugLog = FALSE; extern string KEY = "012345"; int gi_216; double g_icustom_220; double g_icustom_228; double g_icustom_236; double g_digits_244; double g_str2dbl_252; double g_ifractals_260; double g_ifractals_268; double g_ima_276; double g_ima_284; string g_dbl2str_292; double gda_300[]; double gda_304[]; int g_datetime_308; bool gi_316 = TRUE; int init() { if (StartWorkTimeHour < 0 || StartWorkTimeHour > 23) StartWorkTimeHour = 0; if (StartWorkTimeMin < 0 || StartWorkTimeMin > 59) StartWorkTimeMin = 0; if (EndWorkTimeHour < 0 || EndWorkTimeHour > 23) EndWorkTimeHour = 0; if (EndWorkTimeMin < 0 || EndWorkTimeMin > 59) EndWorkTimeMin = 0; return (0); } int deinit() { return (0); } int start() { int li_8; if (!CheckKEY()) { Alert("Your Key is not valid. Please activate your copy." + "\n" + " Your expert ID=" + gi_216 + "\n" + " If ExpertID is 0 first check the EA settings." + "\n" + " Turn on ALLOW DLL IMPORTS and " + "\n" + "remove CONFIRM DLL FUNCTION CALLS there." + "\n" + " Also check the global parameters of the Metatrader:" + "\n" + " Tools -> Options -> Expert Advisors. " + "\n" + "Turn on Allow Dll imports" + "\n" + " and turn off Confirm DLL function calls" + "\n" + "Then restart the Metatrader"); deinit(); return; } if (TimeCurrent() > 1320105600) { if (gi_316) Alert("FAP EA Has Expired. Please download the fresh version from ForexAutoPilot.com."); if (WriteDebugLog) Print("FAP EA Has Expired. Please download the fresh version from ForexAutoPilot.com."); gi_316 = FALSE; return (0); } g_digits_244 = MarketInfo(Symbol(), MODE_DIGITS); g_dbl2str_292 = DoubleToStr(g_str2dbl_252, g_digits_244); g_str2dbl_252 = StrToDouble(g_dbl2str_292); if (!IsTradeAllowed()) return (0); if (!IsConnected()) return (0); if (IsStopped()) return (0); if (IsTradeContextBusy()) { Print("Trade context is busy!"); return (0); } if (!IsDateTimeEnabled(TimeCurrent())) return (0); if (!CheckParams()) return (0); if (!PrepareIndicators()) return (0); int li_4 = CalculateCurrentOrders(); if (li_4 > 0) { for (int li_0 = li_4 - 1; li_0 >= 0; li_0--) { if (GetOrderByPos(li_0)) { if (OrderMagicNumber() == OrderMagic) { if (TradeSignalCloseOrder() || TradeSignalCloseOrderOnTime(li_0)) CloseOrder(); else { if (OrderTakeProfit() == 0.0) { if (OrderType() == OP_BUY) li_8 = (Bid - OrderOpenPrice()) / Point; else li_8 = (OrderOpenPrice() - Ask) / Point; if (li_8 >= TakeProfit && TakeProfit != 0.0) { CloseOrder(); return (0); } if (TakeProfit > MarketInfo(Symbol(), MODE_STOPLEVEL)) ModifyOrder(); } if (TrailingStop > 0.0) TrailOrderStop(); } } } } if (li_4 < MaxOrders) if ((TimeCurrent() - g_datetime_308) / 60 >= StopTime) TrueOpen(); return (0); } TrueOpen(); return (0); } int CalculateCurrentOrders() { int li_ret_0; for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) { if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES)) if (OrderMagicNumber() == OrderMagic && OrderSymbol() == Symbol()) li_ret_0++; } return (li_ret_0); } int TrueOpen() { if (!IsTradeTime(TimeCurrent())) return (0); int li_0 = TradeSignalOpenOrder(); if (li_0 == 0) return (0); if (OneTrade != 0) { if (HaveTrade()) { if (WriteDebugLog) Print("Already have one trade inside this interval of time."); return (0); } } double ld_4 = CalcLotsVolume(); if (ld_4 == 0.0 || !CheckAccount(DirectionOrderType(li_0), ld_4)) return (0); int li_12 = OpenOrder(li_0, ld_4); return (0); } bool CheckParams() { if (Bars < 100) { Print("Bars less than 100"); return (FALSE); } if (TakeProfit < 10.0) { Print("TakeProfit is less than 10"); return (FALSE); } if (Lots == 0.0 && LotsRiskReductor < 1.0) { Print("LotsRiskReductor is less than 1"); return (FALSE); } return (TRUE); } bool CheckAccount(int a_cmd_0, double ad_4) { bool li_ret_12 = TRUE; double ld_16 = AccountFreeMarginCheck(Symbol(), a_cmd_0, ad_4); if (GetLastError() == 134/* NOT_ENOUGH_MONEY */) li_ret_12 = FALSE; if (!li_ret_12) Print("No money to open more orders.", " Lot=", ad_4, " Free Margin = ", AccountFreeMargin(), " Balance = ", AccountBalance()); return (li_ret_12); } double CalcLotsVolume() { double ld_ret_0; double l_maxlot_8; double l_minlot_16; double l_lotstep_24; if (Lots > 0.0) ld_ret_0 = NormalizeLot(Lots, 0, ""); else { l_maxlot_8 = MarketInfo(Symbol(), MODE_MAXLOT); l_minlot_16 = MarketInfo(Symbol(), MODE_MINLOT); l_lotstep_24 = MarketInfo(Symbol(), MODE_LOTSTEP); ld_ret_0 = NormalizeDouble(MathFloor(AccountFreeMargin() * LotsRiskReductor / 100.0 / (MarketInfo(Symbol(), MODE_MARGINREQUIRED) * l_lotstep_24)) * l_lotstep_24, 2); if (ld_ret_0 < l_minlot_16) ld_ret_0 = l_minlot_16; if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8; } if (ld_ret_0 > MaxLots) ld_ret_0 = MaxLots; return (ld_ret_0); } bool PrepareIndicators() { double l_ifractals_20; g_icustom_220 = iCustom(NULL, 0, "Alligator", 13, 8, 8, 5, 5, 3, 0, 0); g_icustom_228 = iCustom(NULL, 0, "Alligator", 13, 8, 8, 5, 5, 3, 1, 0); g_icustom_236 = iCustom(NULL, 0, "Alligator", 13, 8, 8, 5, 5, 3, 2, 0); int li_8 = 3; g_ifractals_260 = 0; g_ifractals_268 = 0; for (int li_4 = 0; li_4 <= li_8; li_4++) { l_ifractals_20 = iFractals(NULL, 0, MODE_LOWER, li_4); if (l_ifractals_20 != 0.0) g_ifractals_260 = l_ifractals_20; l_ifractals_20 = iFractals(NULL, 0, MODE_UPPER, li_4); if (l_ifractals_20 != 0.0) g_ifractals_268 = l_ifractals_20; } li_8 = 0; ArrayResize(gda_300, li_8 + 1); ArrayResize(gda_304, li_8 + 1); for (li_4 = 0; li_4 <= li_8; li_4++) { gda_300[li_4] = iDeMarker(NULL, 0, aaa, li_4); gda_304[li_4] = (-iWPR(NULL, 0, bbb, li_4 + 1)) / 100.0; } if (UseMAControl != 0) { g_ima_276 = iMA(NULL, PERIOD_D1, PeriodMA, 0, TypeMA_0_3, PriceMA_0_6, 1); g_ima_284 = iMA(NULL, PERIOD_D1, PeriodMA, 0, TypeMA_0_3, PriceMA_0_6, 0); } return (TRUE); } int TradeSignalOpenOrder() { if (UseMAControl == 0) if (!IsGatorActiveUp()) return (0); if (g_ima_276 < g_ima_284) if (!IsGatorActiveUp()) return (0); if (g_ima_276 > g_ima_284) if (!IsGatorActiveDown()) return (0); if (!IsGatorActiveUp() && !IsGatorActiveDown()) return (0); if (WasWPROverBuy() || WasWPROverSell()) return (0); if (IsFractalLower() && WasDemarkerHigh()) return (1); if (IsFractalUpper() && WasDemarkerLow()) return (-1); return (0); } int TradeSignalCloseOrder() { return (!IsOrderProfitable()); } double NormalizeLot(double ad_0, bool ai_8 = FALSE, string a_symbol_12 = "") { double ld_ret_20; double ld_28; if (a_symbol_12 == "" || a_symbol_12 == "0") a_symbol_12 = Symbol(); double l_lotstep_36 = MarketInfo(a_symbol_12, MODE_LOTSTEP); double l_minlot_44 = MarketInfo(a_symbol_12, MODE_MINLOT); double l_maxlot_52 = MarketInfo(a_symbol_12, MODE_MAXLOT); if (l_minlot_44 == 0.0) l_minlot_44 = 0.1; if (l_maxlot_52 == 0.0) l_maxlot_52 = 100; if (l_lotstep_36 > 0.0) ld_28 = 1 / l_lotstep_36; else ld_28 = 1 / l_minlot_44; if (ai_8) ld_ret_20 = MathCeil(ad_0 * ld_28) / ld_28; else ld_ret_20 = MathFloor(ad_0 * ld_28) / ld_28; if (ld_ret_20 < l_minlot_44) ld_ret_20 = l_minlot_44; if (ld_ret_20 > l_maxlot_52) ld_ret_20 = l_maxlot_52; return (ld_ret_20); } int ModifyOrder() { int li_8 = OrderTypeDirection(); Print("TakeProfit value is not set due to server connection error. Please modify the order and set it manually"); double l_price_0 = g_str2dbl_252 + TakeProfit * Point * li_8; OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), l_price_0, 0, Red); return (0); } int TradeSignalCloseOrderOnTime(int ai_unused_0) { if (TimeMonth(OrderOpenTime()) != TimeMonth(TimeCurrent())) return (1); return (0); } bool IsDateTimeEnabled(int ai_0) { int l_day_of_year_4 = TimeDayOfYear(ai_0); int l_day_of_week_8 = TimeDayOfWeek(ai_0); return (l_day_of_week_8 > 0 && l_day_of_week_8 < 6 && l_day_of_year_4 > 7 && l_day_of_year_4 < 360); } bool IsGatorActiveUp() { return (g_icustom_236 - g_icustom_228 >= SpanGator * Point && g_icustom_228 - g_icustom_220 >= SpanGator * Point && g_icustom_236 - g_icustom_220 >= SpanGator * Point); } bool IsGatorActiveDown() { return (g_icustom_228 - g_icustom_236 >= SpanGator * Point && g_icustom_220 - g_icustom_228 >= SpanGator * Point && g_icustom_220 - g_icustom_236 >= SpanGator * Point); } int IsFractalLower() { return (g_ifractals_260 != 0.0); } int IsFractalUpper() { return (g_ifractals_268 != 0.0); } bool IsOrderProfitable() { return (TRUE); } int WasDemarkerLow() { return (ArrayMinValue(gda_300) < 0.5); } int WasDemarkerHigh() { return (ArrayMaxValue(gda_300) > 0.5); } int WasWPROverBuy() { return (ArrayMinValue(gda_304) <= 0.25); } int WasWPROverSell() { return (ArrayMaxValue(gda_304) >= 0.75); } int OpenOrder(int ai_0, double a_lots_4, string a_comment_12 = "") { int li_48; double l_price_28 = 0; double l_price_36 = 0; double l_price_20 = PriceOpen(ai_0); l_price_36 = l_price_20 + TakeProfit * Point * ai_0; if (StopLoss > 0.0) l_price_28 = PriceClose(ai_0) - StopLoss * Point * ai_0; int l_ticket_44 = OrderSend(Symbol(), DirectionOrderType(ai_0), a_lots_4, l_price_20, SlipPage, 0, 0, a_comment_12, OrderMagic, 0, ColorOpen(ai_0)); Sleep(1000); if (l_ticket_44 > 0) { li_48 = MarketInfo(Symbol(), MODE_STOPLEVEL); if (TakeProfit > li_48 || TakeProfit == 0.0) if (StopLoss > li_48 || StopLoss == 0.0) OrderModify(l_ticket_44, l_price_20, l_price_28, l_price_36, 0, Red); } g_datetime_308 = iTime(Symbol(), 0, 0); return (0); } void CloseOrder() { Print("Closing!"); OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SlipPage, Red); } bool GetOrderByPos(int a_pos_0) { return (OrderSelect(a_pos_0, SELECT_BY_POS, MODE_TRADES) && OrderType() <= OP_SELL && OrderSymbol() == Symbol()); } void TrailOrderStop() { int li_0; double ld_4; double ld_12; double l_price_20; double ld_28; double ld_36; int li_44; if (OrderTicket() > 0) { li_0 = OrderTypeDirection(); ld_4 = NormalizeDouble(TrailingStop * Point * li_0, Digits); ld_12 = NormalizeDouble(iif(li_0 > 0 || OrderStopLoss() != 0.0, OrderStopLoss(), 999999), Digits); l_price_20 = NormalizeDouble(PriceClose(li_0) - ld_4, Digits); ld_28 = NormalizeDouble(l_price_20 - OrderOpenPrice(), Digits); ld_36 = NormalizeDouble(l_price_20 - ld_12, Digits); if (ld_28 * li_0 > 0.0 && ld_36 * li_0 >= Point) { if (OrderType() == OP_BUY) li_44 = (Bid - l_price_20) / Point; else li_44 = (l_price_20 - Ask) / Point; if (li_44 > MarketInfo(Symbol(), MODE_STOPLEVEL)) { OrderModify(OrderTicket(), OrderOpenPrice(), l_price_20, OrderTakeProfit(), 0, ColorOpen(li_0)); if (GetLastError() != 0/* NO_ERROR */) { } } } } } int OrderTypeDirection() { int li_ret_0 = 0; if (OrderType() == OP_BUY) li_ret_0 = 1; if (OrderType() == OP_SELL) li_ret_0 = -1; return (li_ret_0); } int DirectionOrderType(int ai_0) { return (iif(ai_0 > 0, 0, 1)); } int ColorOpen(int ai_0) { return (iif(ai_0 > 0, 32768, 255)); } double PriceOpen(int ai_0) { return (iif(ai_0 > 0, Ask, Bid)); } double PriceClose(int ai_0) { return (iif(ai_0 > 0, Bid, Ask)); } double iif(bool ai_0, double ad_4, double ad_12) { if (ai_0) return (ad_4); return (ad_12); } double ArrayMinValue(double ada_0[]) { return (ada_0[ArrayMinimum(ada_0)]); } double ArrayMaxValue(double ada_0[]) { return (ada_0[ArrayMaximum(ada_0)]); } bool IsTradeTime(int ai_0) { int li_12; int li_16; int l_hour_4 = TimeHour(ai_0); int l_minute_8 = TimeMinute(ai_0); if (StartWorkTimeHour == EndWorkTimeHour && StartWorkTimeMin == EndWorkTimeMin) return (TRUE); if (EndWorkTimeMin == 0) { if (EndWorkTimeHour == 0) li_12 = 23; else li_12 = EndWorkTimeHour - 1; li_16 = 59; } else { li_12 = EndWorkTimeHour; li_16 = EndWorkTimeMin - 1; } if (StartWorkTimeHour == li_12) { if (StartWorkTimeMin == li_16) return (TRUE); if (StartWorkTimeMin < li_16) { if (l_hour_4 != StartWorkTimeHour) return (FALSE); if (l_minute_8 < StartWorkTimeMin || l_minute_8 > li_16) return (FALSE); return (TRUE); } if (StartWorkTimeMin > li_16) { if (l_hour_4 == StartWorkTimeHour) if (l_minute_8 < StartWorkTimeMin && l_minute_8 > li_16) return (FALSE); return (TRUE); } } if (StartWorkTimeHour < li_12) { if (l_hour_4 < StartWorkTimeHour || l_hour_4 > li_12) return (FALSE); if (l_hour_4 == StartWorkTimeHour && l_minute_8 < StartWorkTimeMin) return (FALSE); if (l_hour_4 == li_12 && l_minute_8 > li_16) return (FALSE); return (TRUE); } if (StartWorkTimeHour > li_12) { if (l_hour_4 < StartWorkTimeHour && l_hour_4 > li_12) return (FALSE); if (l_hour_4 == StartWorkTimeHour && l_minute_8 < StartWorkTimeMin) return (FALSE); if (l_hour_4 == li_12 && l_minute_8 > li_16) return (FALSE); return (TRUE); } return (TRUE); } bool HaveTrade() { int l_datetime_28; if (StartWorkTimeHour == EndWorkTimeHour && StartWorkTimeMin == EndWorkTimeMin) return (FALSE); int l_datetime_0 = TimeCurrent(); int li_4 = iTime(NULL, PERIOD_D1, 0); int l_hour_8 = TimeHour(l_datetime_0); bool li_12 = FALSE; if (StartWorkTimeHour > EndWorkTimeHour) { if (l_hour_8 < StartWorkTimeHour) li_12 = TRUE; } else { if (StartWorkTimeHour == EndWorkTimeHour) { if (StartWorkTimeMin > EndWorkTimeMin) { if (l_hour_8 < StartWorkTimeHour) { if (l_hour_8 < StartWorkTimeHour) li_12 = TRUE; } else { if (l_hour_8 == StartWorkTimeHour) if (TimeMinute(l_datetime_0) < EndWorkTimeMin) li_12 = TRUE; } } } } if (li_12) li_4 -= 86400; li_4 += 3600 * StartWorkTimeHour + 60 * StartWorkTimeMin; int li_16 = OrdersTotal() - 1; int l_datetime_20 = -2147483648; for (int l_pos_24 = li_16; l_pos_24 >= 0; l_pos_24--) { if (OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == OrderMagic) { if (OrderSymbol() == Symbol()) { l_datetime_28 = OrderOpenTime(); if (l_datetime_20 < l_datetime_28) l_datetime_20 = l_datetime_28; } } } } if (l_datetime_20 >= li_4) return (TRUE); li_16 = OrdersHistoryTotal() - 1; l_datetime_20 = -2147483648; for (l_pos_24 = li_16; l_pos_24 >= 0; l_pos_24--) { if (OrderSelect(l_pos_24, SELECT_BY_POS, MODE_HISTORY)) { if (OrderMagicNumber() == OrderMagic) { if (OrderSymbol() == Symbol()) { l_datetime_28 = OrderOpenTime(); if (l_datetime_20 < l_datetime_28) l_datetime_20 = l_datetime_28; } } } } if (l_datetime_20 >= li_4) return (TRUE); return (FALSE); } bool CheckKEY() { int li_20; int lia_24[10]; string ls_28; int li_0 = CreateFileA("C:\\WINDOWS\\System32\\" + "cdmma6418.txt", -2147483648, 0, 0, 4, 128, 0); int lia_4[1] = {0}; int lia_8[1] = {0}; if (li_0 > 0) { li_20 = ReadFile(li_0, lia_24, 10, lia_4, 0); if (lia_4[0] <= 0) { li_20 = CloseHandle(li_0); li_0 = CreateFileA("C:\\WINDOWS\\System32\\" + "cdmma6418.txt", D'10.01.2004 17:07:04', 0, 0, 4, 128, 0); ls_28 = rnd(); li_20 = WriteFile(li_0, ls_28, 10, lia_8, 0); li_20 = CloseHandle(li_0); li_0 = CreateFileA("C:\\WINDOWS\\System32\\" + "cdmma6418.txt", -2147483648, 0, 0, 4, 128, 0); if (li_0 > 0) { li_20 = ReadFile(li_0, lia_24, 10, lia_4, 0); if (lia_4[0] > 0) { gi_216 = lia_24[0]; Print("Your expert ID = ", gi_216); Alert("Your expert ID = " + gi_216); } li_20 = CloseHandle(li_0); } } else { li_20 = CloseHandle(li_0); gi_216 = lia_24[0]; } } if (IsValidKey(gi_216)) return (TRUE); return (FALSE); } bool IsValidKey(int ai_0) { string ls_20; string ls_28; string ls_36; int l_str2int_44; int l_str2int_48; int li_52; string ls_4 = ""; string l_dbl2str_12 = DoubleToStr(ai_0, 0); for (int li_56 = 0; li_56 < StringLen(l_dbl2str_12); li_56++) { ls_20 = StringSubstr(l_dbl2str_12, li_56, 1); ls_28 = StringSubstr(l_dbl2str_12, li_56 + 1, 1); ls_36 = StringSubstr(l_dbl2str_12, li_56, 2); l_str2int_44 = StrToInteger(ls_20); l_str2int_48 = StrToInteger(ls_28); li_52 = MathMod(235 * l_str2int_44 + 53 * l_str2int_48 + 97 * li_52, 10); ls_4 = ls_4 + DoubleToStr(li_52, 0); } if (ls_4 == KEY) return (TRUE); return (FALSE); } string rnd() { string ls_ret_4; int l_datetime_0 = TimeLocal(); MathSrand(l_datetime_0); for (int li_12 = 1; li_12 <= 10; li_12++) ls_ret_4 = ls_ret_4 + DoubleToStr(MathMod(MathRand(), 10), 0); return (ls_ret_4); }