#property  copyright "Copyright © 2008, FAPTURBO.com Licensed till 2008.12.25"
#property  link "http://Fapturbo.com"

#import    "FapTurbo.dll"
   int fun1(int i1, int i2, double d1, double d2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12);
   bool fun2(double d1, int i1, double d2, double d3, int i2, int i3, double d4);
   bool fun3(double d1, int i1, double d2, double d3, int i2, int i3, double d4);
   bool fun4(double d1, int i1, int i2);
#import    "Kernel32.dll"
   int CreateFileA(string s1, int i1, int i2, int i3, int i4, int i5, int i6);
   bool ReadFile(int i1, int& ia1[], int i2, int& ia2[], int i3);
   bool WriteFile(int i1, string s1, int i2, int& ia1[], int i3);
   bool CloseHandle(int i1);
#import

extern   string   ____Scalper___________ = "--- Scalper EURCHF, EURGPB, GBPCHF or USDCAD M15 ---";
extern   bool     UseScalperStrategy = true;
extern   double   Scalper_Lots = 0.1;
extern   bool     Scalper_UseMM = false;
extern   double   Scalper_LotsRiskReductor = 1.5;
extern   double   Scalper_MaxLots = 10;
extern   int      Scalper_GMTOffset = 1;
extern   bool     Scalper_StealthMode = true;
extern   bool     Scalper_UseCustomLevels = true;
extern   int      Scalper_RelaxHours = 0;
extern   bool     Scalper_SimpleHeightFilter = true;
extern   bool     Scalper_TrendFilter = true;
extern   bool     Scalper_TradeFriday = false;
extern   int      Scalper_OneTrade = 0;
extern   bool     Scalper_UseFilterMA = false;
extern   int      Scalper_PeriodFilterMA = 100;
extern   int      Scalper_PriceFilterMA = 0;
extern   int      Scalper_MethodFilterMA = 0;
extern   string   Scalper_ExpertComment = "FapTurboScalper";
extern   string   ____FapTurbo__________ = "---------- FapTurbo EUR/USD M1 ----------";
extern   double   FapTurbo_Lots = 0;
extern   double   FapTurbo_LotsRiskReductor = 1;
extern   int      FapTurbo_MaxOrders = 5;
extern   int      FapTurbo_MaxLots = 10;
extern   int      FapTurbo_StopTime = 9;
extern   int      FapTurbo_aaa = 35;
extern   int      FapTurbo_bbb = 48;
extern   double   FapTurbo_TakeProfit = 140;
extern   double   FapTurbo_StopLoss = 500;
extern   double   FapTurbo_TrailingStop = 0;
extern   int      FapTurbo_DurationInHours = 0;
extern   int      FapTurbo_CloseAfterXmonths = 1;
extern   int      FapTurbo_PeriodMALarge = 100;
extern   int      FapTurbo_PeriodMASmall = 30;
extern   int      FapTurbo_PriceMA_0_6 = 0;
extern   int      FapTurbo_TypeMA_0_3 = 0;
extern   int      FapTurbo_ShiftMALarge = 3;
extern   int      FapTurbo_ShiftMASmall = 1;
extern   int      FapTurbo_LookForDays = 2;
extern   int      FapTurbo_CriticalDays = 45;
extern   int      FapTurbo_RelaxDays = 0;
extern   double   FapTurbo_HedgingLotReducer = 0;
extern   int      FapTurbo_Hedging = 0;
extern   int      FapTurbo_TradeNFP = 1;
extern   int      FapTurbo_TradeFriday = 1;
extern   int      FapTurbo_Prudent = 0;
extern   int      FapTurbo_SymAlligatorOnCritica = 1;
extern   int      FapTurbo_FixedDirection = 1;
extern   int      FapTurbo_ClsLsrOnMrktChnge = 0;
extern   int      FapTurbo_AlwaysTrade = 0;
extern   double   FapTurbo_LowLot = 0.1;
extern   int      FapTurbo_TooGoodToBeTrue = 50;
extern   int      FapTurbo_PrudentPeriod = 30;
extern   int      FapTurbo_StartWorkTimeHour = 0;
extern   int      FapTurbo_StartWorkTimeMin = 0;
extern   int      FapTurbo_EndWorkTimeHour = 0;
extern   int      FapTurbo_EndWorkTimeMin = 0;
extern   int      FapTurbo_OneTrade = 0;
extern   double   FapTurbo_SpanGator = 0.5;
extern   int      FapTurbo_SlipPage = 3;
extern   string   FapTurbo_ExpertComment = "FapTurboEA";
extern   string   ____Other_Parameters__ = "-----------------------------------------";
extern   int      MagicNumber = 231313;
extern   color    ColorBuy = Blue;
extern   color    ColorSell = Red;
extern   bool     WriteLog = true;
extern   bool     WriteDebugLog = false;
extern   bool     PrintLogOnChart = true;
extern   string   KEY = "012345";

int      var_420 = 6;
int      var_424 = 112;
int      var_428 = 25;
int      var_432 = 60;
int      var_436 = 3;
int      var_440 = 140;
int      var_444 = 2;
int      var_448 = 170;
int      var_452 = 0;
int      var_456 = 200;
int      var_460 = -1;
int      var_464 = 240;
int      var_468 = -4;
double   var_472 = 0.3;

int      var_480 = 5;
int      var_484 = 53;
int      var_488 = 25;
int      var_492 = 75;
int      var_496 = 3;
int      var_500 = 100;
int      var_504 = 2;
int      var_508 = 135;
int      var_512 = 0;
int      var_516 = 210;
int      var_520 = -1;
int      var_524 = 245;
int      var_528 = -4;
double   var_532 = 0.2;

int      var_540 = 10;
int      var_544 = 75;
int      var_548 = 75;
int      var_552 = 80;
int      var_556 = 3;
int      var_560 = 145;
int      var_564 = 2;
int      var_568 = 180;
int      var_572 = 0;
int      var_576 = 205;
int      var_580 = -1;
int      var_584 = 250;
int      var_588 = -4;
double   var_592 = 0.15;

int      var_600 = 10;
int      var_604 = 58;
int      var_608 = 23;
int      var_612 = 80;
int      var_616 = 3;
int      var_620 = 145;
int      var_624 = 2;
int      var_628 = 180;
int      var_632 = 0;
int      var_636 = 205;
int      var_640 = -1;
int      var_644 = 250;
int      var_648 = -4;
double   var_652 = 0.35;

int      var_660 = 21;
int      var_664 = 1;
int      var_668 = 50;
int      var_672 = 8;
int      var_676 = 6;
int      var_680 = 30;
int      var_684 = 20;
int      var_688 = 36;
int      var_692 = 20;
int      var_696 = 5;
int      var_700 = 0;
int      var_704 = 0;
int      var_708 = 4;
int      var_712 = 3;
int      var_716 = 150;
int      var_720 = 21;
int      var_724 = 40;
int      var_728 = 21;
int      Expert_ID;
bool     AllowTrade = true;
int      DIG;
double   POINT;
double   MaxLot;
double   MinLot;
double   LotStep;
double   ReqMargin;
string   SYMB;
bool     var_792 = true;
bool     var_796 = true;
int      var_800;
int      var_804;
int      var_808;
int      var_812;
int      var_816;
int      var_820;
int      var_824;
int      var_828;
int      var_832;
int      var_836;
int      var_840;
int      var_844;
int      var_848;
int      var_852;
int      var_856;
int      var_860;
int      var_864;
int      var_868;
int      var_872;
int      var_876;
int      var_880;
int      var_884;
int      var_888;
int      var_892;
int      var_896;
int      var_900;
int      var_904;
double   var_908;
double   var_916;
double   var_924;
double   var_932;
double   var_940;
double   var_948;
double   var_956;
double   var_964;
double   ScalperMaxSpread;
double   var_980;
double   Range;
bool     var_996;
bool     var_1000;
bool     var_1004;
bool     var_1008;
bool     var_1012;
bool     var_1016;
int      LastOpenedOrder;
int      var_1024;
int      PrevTime;
int      var_1032;
int      var_1036;
double   AlligatorJaw;
double   AlligatorLips;
double   AlligatorTeeth;
double   DIGITS;
double   var_1072;
double   FractalLower;
double   FractalUpper;
double   prevMALarge;
double   lastMALarge;
double   prevMASmall;
double   lastMASmall;
double   farMALarge;
double   farMASmall;
double   Demarker[];
double   WPR[];
string   var_1152;
string   var_1160;
string   StrategyInfo;
bool     FirstAlert = true;

#define def0 "C:\\WINDOWS\\System32\\"
#define def1 "cdmma6418.txt"

//+------------------------------------------------------------------+

int init()
{
string symb;

if (!IsDllsAllowed())
   {
   Comment("Warning: Set Parameter \"AllowDLL Imports\" ON in menu Tools -> Options -> ExpertAdvisors.");
   AllowTrade = false;
   return(0);
   }

SYMB = Symbol();
if (Digits < 4)
   {
   POINT = 0.01;
   DIG = 2;
   }
      else
   {
   POINT = 0.0001;
   DIG = 4;
   }

MaxLot = MarketInfo(SYMB,MODE_MAXLOT);
MinLot = MarketInfo(SYMB,MODE_MINLOT);
LotStep = MarketInfo(SYMB,MODE_LOTSTEP);
ReqMargin = MarketInfo(SYMB,MODE_MARGINREQUIRED) * LotStep;

if (!UseScalperStrategy)
   {
   if ((StringSubstr(SYMB,0,6) != "EURUSD") || (Period() != PERIOD_M1))
      {
      Alert("Use Fapturbo system only on EURUSD M1.");
      Print("Use Fapturbo system only on EURUSD M1.");
      Comment("Error: Wrong TimeFrame or Currency Pair! Read instructions!");
      AllowTrade = false;
      return(0);
      }
   if ((FapTurbo_StartWorkTimeHour < 0) || (FapTurbo_StartWorkTimeHour > 23)) FapTurbo_StartWorkTimeHour = 0;
   if ((FapTurbo_StartWorkTimeMin < 0) || (FapTurbo_StartWorkTimeMin > 59)) FapTurbo_StartWorkTimeMin = 0;
   if ((FapTurbo_EndWorkTimeHour < 0) || (FapTurbo_EndWorkTimeHour > 23)) FapTurbo_EndWorkTimeHour = 0;
   if ((FapTurbo_EndWorkTimeMin < 0) || (FapTurbo_EndWorkTimeMin > 59)) FapTurbo_EndWorkTimeMin = 0;
   if (FapTurbo_CloseAfterXmonths < 0) FapTurbo_CloseAfterXmonths = 0;
   if (FapTurbo_CloseAfterXmonths > 12) FapTurbo_CloseAfterXmonths = 12;
   }
      else
   {
   symb = StringSubstr(SYMB,0,6);
   if (((symb != "EURGBP") && (symb != "EURCHF") && (symb != "GBPCHF") && (symb != "USDCAD")) || (Period() != PERIOD_M15))
      {
      Alert("Use scalper system only on EURCHF, EURGPB, GBPCHF or USDCAD M15.");
      Print("Use scalper system only on EURCHF, EURGPB, GBPCHF or USDCAD M15.");
      Comment("Error: Wrong TimeFrame or Currency Pair! Read instructions!");
      AllowTrade = false;
      return(0);
      }
   if (symb == "EURGBP")
      {
      var_828 = var_420;
      var_832 = var_424;
      var_836 = var_428;
      var_908 = var_472;
      var_840 = var_432;
      var_848 = var_440;
      var_856 = var_448;
      var_864 = var_456;
      var_872 = var_464;
      var_844 = var_436;
      var_852 = var_444;
      var_860 = var_452;
      var_868 = var_460;
      var_876 = var_468;
      }
         else
      {
      if (symb == "EURCHF")
         {
         var_828 = var_480;
         var_832 = var_484;
         var_836 = var_488;
         var_908 = var_532;
         var_840 = var_492;
         var_848 = var_500;
         var_856 = var_508;
         var_864 = var_516;
         var_872 = var_524;
         var_844 = var_496;
         var_852 = var_504;
         var_860 = var_512;
         var_868 = var_520;
         var_876 = var_528;
         }
            else
         {
         if (symb == "GBPCHF")
            {
            var_828 = var_540;
            var_832 = var_544;
            var_836 = var_548;
            var_908 = var_592;
            var_840 = var_552;
            var_848 = var_560;
            var_856 = var_568;
            var_864 = var_576;
            var_872 = var_584;
            var_844 = var_556;
            var_852 = var_564;
            var_860 = var_572;
            var_868 = var_580;
            var_876 = var_588;
            }
               else
            {
            var_828 = var_600;
            var_832 = var_604;
            var_836 = var_608;
            var_908 = var_652;
            var_840 = var_612;
            var_848 = var_620;
            var_856 = var_628;
            var_864 = var_636;
            var_872 = var_644;
            var_844 = var_616;
            var_852 = var_624;
            var_860 = var_632;
            var_868 = var_640;
            var_876 = var_648;
            }
         }
      }
   var_924 = var_832 * (-1) * POINT;
   var_916 = var_828 * POINT;
   var_880 = var_840 * 60;
   var_884 = var_848 * 60;
   var_888 = var_856 * 60;
   var_892 = var_864 * 60;
   var_896 = var_872 * 60;
   var_932 = var_844 * POINT;
   var_940 = var_852 * POINT;
   var_948 = var_860 * POINT;
   var_956 = var_868 * POINT;
   var_964 = var_876 * POINT;
   Range = NormalizeDouble(var_836 * POINT,DIG);
   ScalperMaxSpread = (MarketInfo(SYMB,MODE_SPREAD) + 0.1) * Point;
   var_980 = NormalizeDouble(var_832 * POINT,DIG);
   var_812 = 100 - var_680;
   var_816 = 100 - var_688;
   var_820 = var_692 / 2 + 50;
   var_824 = 50 - var_692 / 2;
   var_900 = var_660 + Scalper_GMTOffset;
   var_904 = var_664 + Scalper_GMTOffset;
   while (true)
      {
      if (var_900 >= 24)
         {
         var_900 = var_900 - 24;
         continue;
         }
      if (var_900 >= 0) break;
      var_900 = var_900 + 24;
      }
   while (true)
      {
      if (var_904 >= 24)
         {
         var_904 = var_904 - 24;
         continue;
         }
      if (var_904 >= 0) break;
      var_904 = var_904 + 24;
      }
   if (Scalper_PeriodFilterMA <= 0) Scalper_UseFilterMA = false;
   if ((Scalper_PriceFilterMA < 0) || (Scalper_PriceFilterMA > 6)) Scalper_UseFilterMA = false;
   if ((Scalper_MethodFilterMA < 0) || (Scalper_MethodFilterMA > 3)) Scalper_UseFilterMA = false;
   }
}

//+------------------------------------------------------------------+

int deinit()
{
return(0);
}

//+------------------------------------------------------------------+

int start()
{
/*
if (!CheckKEY())
   {
   Alert("Your Key is not valid. Please activate your copy." + "\n" + " Your expert ID=" + Expert_ID + "\n" + " If ExpertID is 0 first Restart Metatrader! " + "If the problem is still here then 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 again");
   deinit();
   return;
   }
*/

if (!AllowTrade) return(0);

if (TimeCurrent() > D'2008.12.25')
   {
   Comment("FapTurbo EA has Expired." + "\n" + "Download new version in the member area.");
   if (FirstAlert) Alert("FapTurbo License has Expired. Download new version in the member area.");
   if (WriteLog) Print("FapTurbo License has Expired. Download new version in the member area.");
   FirstAlert = false;
   return(0);
   }

if (!UseScalperStrategy) FapTurbo(); else Scalper();
}

//+------------------------------------------------------------------+

void FapTurbo()
{
int    time;
bool   flag1;
bool   flag2;
bool   flag3;
bool   flag4;
int    flag5;
string msg;
int    i;
int    cnt;
int    profit;
double tp;
double sl;

DIGITS = MarketInfo(SYMB,MODE_DIGITS);
var_1152 = DoubleToStr(var_1072,DIGITS);
var_1072 = StrToDouble(var_1152);

if (!IsTradeAllowed()) return;
if (!IsConnected()) return;
if (IsStopped()) return;

if (IsTradeContextBusy())
   {
   Print("Trade context is busy!");
   return;
   }

if (!IsDateTimeEnabled(TimeCurrent())) return;
if (!CheckParams()) return;
if (!PrepareIndicators()) return;

time = iTime(NULL,PERIOD_D1,0);
if (PrevTime != time)
   {
   PrevTime = time;
   StrategyInfo = "";
   if (WriteLog) Print("---------------------- Start New Daily Bar: ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)," ----------------------");
   if (WriteDebugLog) PrintDebugLog();
   var_1000 = false;
   var_1004 = false;
   var_1008 = false;
   var_1012 = false;
   if (FapTurbo_FixedDirection != 0) var_1016 = true;
   flag1 = true;
   flag2 = true;
   flag3 = false;
   flag4 = true;
   flag5 = false;
   if (!Rule1(0))
      {
      msg = "Critical Day. Expected Cross in " + var_1032 + " days.";
      StrategyInfo = StrategyInfo + msg + "\n";
      if (WriteLog) Print(msg);
      flag1 = false;
      }
         else
      {
      if (!Relax1())
         {
         msg = "Relax Days Left = " + var_1036 + " (after Cross).";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         flag1 = false;
         }
            else
         {
         msg = "NO Expected Cross. -> Possible to trade.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         }
      }
   if (!Rule2(0))
      {
      if (FapTurbo_Prudent != 0)
         {
         msg = "Prudent Day. No Trading.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         flag2 = false;
         }
            else
         {
         msg = "Prudent Day Trading.";
         if (FapTurbo_SymAlligatorOnCritica != 0)
            {
            msg = msg + " Trading SymAlligator today.";
            var_1000 = true;
            }
               else
            {
            msg = msg + " Simple Trading today.";
            }
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         var_1004 = true;
         flag3 = true;
         }
      }
         else
      {
      if (!Relax2())
         {
         msg = "Relax Days Left = " + var_1036 + " (after Prudent).";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         flag2 = false;
         }
            else
         {
         msg = "NO Prudent Day. -> Possible to trade.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         }
      }
   if (!Rule3(0))
      {
      var_1016 = false;
      msg = "TooGoodToBeTrue Day (Start from " + TimeToStr(iTime(NULL,PERIOD_D1,var_1036),TIME_DATE) + ").";
      if (FapTurbo_Prudent != 0)
         {
         msg = msg + " No Trading.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         flag4 = false;
         }
            else
         {
         if (FapTurbo_SymAlligatorOnCritica != 0)
            {
            msg = msg + " Trading SymAlligator today.";
            var_1000 = true;
            }
               else
            {
            msg = msg + " Simple Trading today.";
            }
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         var_1004 = true;
         flag5 = true;
         }
      }
         else
      {
      if (!Relax3())
         {
         var_1016 = false;
         msg = "Prudent Period TooGoodToBeTrue day left = " + var_1036 + ".";
         if (FapTurbo_Prudent != 0)
            {
            msg = msg + " No Trading.";
            StrategyInfo = StrategyInfo + msg + "\n";
            if (WriteLog) Print(msg);
            flag4 = false;
            }
               else
            {
            if (FapTurbo_SymAlligatorOnCritica != 0)
               {
               msg = msg + " Trading SymAlligator today.";
               var_1000 = true;
               }
                  else
               {
               msg = msg + " Simple Trading today.";
               }
            StrategyInfo = StrategyInfo + msg + "\n";
            if (WriteLog) Print(msg);
            var_1004 = true;
            flag5 = true;
            }
         }
            else
         {
         msg = "NO TooGoodToBeTrue Day. -> Possible to trade.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         }
      }
   if (flag1 && flag2 && !flag3 && flag4 && !flag5)
      {
      msg = "Simple Trading Day.";
      StrategyInfo = StrategyInfo + msg + "\n";
      if (WriteLog) Print(msg);
      var_1004 = true;
      var_996 = true;
      var_1008 = false;
      }
         else
      {
      if (FapTurbo_AlwaysTrade != 0)
         {
         var_1008 = true;
         var_1000 = true;
         msg = "AlwaysTrade: Trading SymAlligator today with LowLot.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         }
      }
   if (flag3 || flag5) var_996 = true;
   if (!flag1 || !flag2 || !flag4) var_996 = false;
   if (FapTurbo_TradeNFP == 0)
      {
      if ((TimeDayOfWeek(time) == 5) && (TimeDay(time) <= 7))
         {
         msg = "Does not trade on the first Friday of month.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         var_1012 = true;
         }
      }
   if (FapTurbo_TradeFriday == 0)
      {
      if (TimeDayOfWeek(time) == 5)
         {
         msg = "Does not trade on the Friday.";
         StrategyInfo = StrategyInfo + msg + "\n";
         if (WriteLog) Print(msg);
         var_1012 = true;
         }
      }
   if (var_1012)
      {
      msg = "The final resolution: NO TRADE today.";
      }
         else
      {
      if (!var_996)
         {
         if (!var_1008)
            msg = "The final resolution: NO TRADE today.";
               else
            msg = "The final resolution: to TRADE WITH LOWLOT today.";
         }
            else
         {
         msg = "The final resolution: to TRADE today.";
         }
      }
   StrategyInfo = StrategyInfo + msg;
   if (WriteLog)
      {
      Print(msg);
      Print("------------------------------------------------------------------------------------------------");
      }
   }

if (PrintLogOnChart) Comment("" + "\n" + "FapTurbo" + "\n" + "------------------------------------------------" + "\n" + "BROKER INFORMATION:" + "\n" + "Broker Company:      " + AccountCompany() + "\n" + "------------------------------------------------" + "\n" + "ACCOUNT INFORMATION:" + "\n" + "Account Name:          " + AccountName() + "\n" + "Account Number:       " + AccountNumber() + "\n" + "Account Leverage:     " + DoubleToStr(AccountLeverage(),0) + "\n" + "Account Balance:       " + DoubleToStr(AccountBalance(),2) + "\n" + "Account Currency:     " + AccountCurrency() + "\n" + "Account Equity:         " + DoubleToStr(AccountEquity(),2) + "\n" + "------------------------------------------------" + "\n" + "MARGIN INFORMATION:" + "\n" + "Free Margin:              " + DoubleToStr(AccountFreeMargin(),2) + "\n" + "Used Margin:              " + DoubleToStr(AccountMargin(),2) + "\n" + "------------------------------------------------" + "\n" + "Actual Server Time     " + TimeToStr(TimeCurrent(),TIME_SECONDS) + "\n" + "------------------------------------------------" + "\n" + "STRATEGY INFORMATION:" + "\n" + StrategyInfo + "\n" + "------------------------------------------------" + "\n" + "DEBUG INFORMATION:" + "\n" + "lastMALarge = " + DoubleToStr(lastMALarge,DIG) + ",  prevMALarge = " + DoubleToStr(prevMALarge,DIG) + ",  farMALarge = " + DoubleToStr(farMALarge,DIG) + "\n" + "lastMASmall = " + DoubleToStr(lastMASmall,DIG) + ",  prevMASmall = " + DoubleToStr(prevMASmall,DIG) + ",  farMASmall = " + DoubleToStr(farMASmall,DIG));

cnt = CalculateCurrentOrders();
if (cnt > 0)
   {
   for (i = cnt - 1; i >= 0; i--)
      {
      if (GetOrderByPos(i))
         {
         if (OrderMagicNumber() == MagicNumber)
            {
            if (TradeSignalCloseOrder() || TradeSignalCloseOrderOnTime(i))
               {
               CloseOrder();
               }
                  else
               {
               if ((FapTurbo_TakeProfit != 0) && (NormalizeDouble(OrderTakeProfit(),DIG) == 0))
                  {
                  if (OrderType() == OP_BUY)
                     {
                     profit = NormalizeDouble((OrderClosePrice() - OrderOpenPrice()) / POINT,DIG);
                     tp = NormalizeDouble(OrderOpenPrice() + FapTurbo_TakeProfit * POINT,DIG);
                     }
                        else
                     {
                     profit = NormalizeDouble((OrderOpenPrice() - OrderClosePrice()) / POINT,DIG);
                     tp = NormalizeDouble(OrderOpenPrice() - FapTurbo_TakeProfit * POINT,DIG);
                     }
                  if (profit >= FapTurbo_TakeProfit)
                     {
                     var_1160 = "Price at level TakeProfit.";
                     CloseOrder();
                     return;
                     }
                  if (FapTurbo_TakeProfit * POINT > MarketInfo(SYMB,MODE_STOPLEVEL) * Point)
                     {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,0,Red);
                     }
                  }
               if ((FapTurbo_StopLoss != 0) && (NormalizeDouble(OrderStopLoss(),DIG) == 0))
                  {
                  if (OrderType() == OP_BUY)
                     {
                     profit = NormalizeDouble((OrderClosePrice() - OrderOpenPrice()) / POINT,DIG);
                     sl = NormalizeDouble(OrderOpenPrice() - FapTurbo_StopLoss * POINT,DIG);
                     }
                        else
                     {
                     profit = NormalizeDouble((OrderOpenPrice() - OrderClosePrice()) / POINT,DIG);
                     sl = NormalizeDouble(OrderOpenPrice() + FapTurbo_StopLoss * POINT,DIG);
                     }
                  if (profit <= FapTurbo_StopLoss * (-1.0))
                     {
                     var_1160 = "Price at level StopLoss.";
                     CloseOrder();
                     return;
                     }
                  if (FapTurbo_StopLoss * POINT > MarketInfo(SYMB,MODE_STOPLEVEL) * Point)
                     {
                     OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Red);
                     }
                  }
               if (FapTurbo_TrailingStop > 0.0) TrailOrderStop();
               }
            }
         }
      }
   if (cnt < FapTurbo_MaxOrders)
      {
      if ((TimeCurrent() - LastOpenedOrder) / 60 >= FapTurbo_StopTime) TrueOpen();
      }
   }
      else
   {
   TrueOpen();
   }
}

//+------------------------------------------------------------------+

int CalculateCurrentOrders()
{
int cnt;

for (int i = 0; i < OrdersTotal(); i++)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
   if ((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == SYMB)) cnt++;
   }
return(cnt);
}

//+------------------------------------------------------------------+

int TrueOpen()
{
int    dir;
int    maxcnt;
int    cnt;
double var_TrueOpen_12;
double minlot;
double lots;
int    var_TrueOpen_36;

if (!IsTradeTime(TimeCurrent())) return(0);

dir = TradeSignalOpenOrder();
if (dir == 0) return(0);

if (FapTurbo_OneTrade != 0)
   {
   if (HaveTrade())
      {
      if (WriteDebugLog) Print("Already have one trade inside this interval of time.");
      return(0);
      }
   }

if (FapTurbo_Hedging != 0)
   {
   maxcnt = FapTurbo_MaxOrders / 2;
   if (FapTurbo_MaxOrders % 2 != 0) maxcnt++;
   if (dir == 1)
      {
      cnt = HavePositions(OP_BUY);
      if (cnt >= maxcnt)
         {
         if (WriteLog) Print("Hedging: already there are open " + cnt + " orders. The signal to BUY is blocked.");
         return(0);
         }
      }
         else
      {
      cnt = HavePositions(OP_SELL);
      if (cnt >= maxcnt)
         {
         if (WriteLog) Print("Hedging: already there are open " + cnt + " orders. The signal to SELL is blocked.");
         return(0);
         }
      }
   }

if (FapTurbo_HedgingLotReducer != 0.0)
   {
   if (dir == 1)
      var_TrueOpen_12 = NormalizeDouble(OpenPosVol(OP_BUY) / FapTurbo_HedgingLotReducer,2);
         else
      var_TrueOpen_12 = NormalizeDouble(OpenPosVol(OP_SELL) / FapTurbo_HedgingLotReducer,2);
   if (var_TrueOpen_12 != 0.0)
      {
      minlot = MarketInfo(SYMB,MODE_MINLOT);
      if (var_TrueOpen_12 < minlot) var_TrueOpen_12 = minlot;
      if (!var_1008)
         {
         lots = var_TrueOpen_12;
         if (WriteLog) Print("HedgingLot: the order will be open by the reduced lots.");
         }
            else
         {
         if (var_TrueOpen_12 > FapTurbo_LowLot)
            {
            lots = FapTurbo_LowLot;
            if (WriteLog) Print("HedgingLot: the reduced lots more than LowLot, the order will be open by the LowLot lots.");
            }
               else
            {
            lots = var_TrueOpen_12;
            if (WriteLog)
               {
               Print("HedgingLot: the order will be open by the reduced lots.");
               }
            }
         }
      }
         else
      {
      if (!var_1008) lots = CalcLotsVolume(); else lots = FapTurbo_LowLot;
      }
   }
      else
   {
   if (!var_1008) lots = CalcLotsVolume(); else lots = FapTurbo_LowLot;
   }

if ((lots == 0) || !CheckAccount(DirectionOrderType(dir),lots)) return(0);

var_TrueOpen_36 = OpenOrder(dir,lots,FapTurbo_ExpertComment);
}

//+------------------------------------------------------------------+

bool CheckParams()
{
if (Bars < 100)
   {
   Print("Bars less than 100");
   return(false);
   }

if (FapTurbo_TakeProfit < 10.0)
   {
   Print("FapTurbo_TakeProfit is less than 10");
   return(false);
   }

if ((FapTurbo_Lots <= 0) && (FapTurbo_LotsRiskReductor < 0))
   {
   Print("FapTurbo_LotsRiskReductor is less than 0");
   return(false);
   }
return(true);
}

//+------------------------------------------------------------------+

bool CheckAccount(int cmd, double lots)
{
bool   result = true;
double freemargin = AccountFreeMarginCheck(SYMB,cmd,lots);

if (GetLastError() == 134) result = false;
if (!result) Print("No money to open more orders."," Lot=",lots," Free Margin = ",AccountFreeMargin()," Balance = ",AccountBalance());
return(result);
}

//+------------------------------------------------------------------+

double CalcLotsVolume()
{
double lots;

if (FapTurbo_Lots > 0.0)
   {
   lots = NormalizeLot(FapTurbo_Lots,false,"");
   }
      else
   {
   lots = NormalizeDouble(MathFloor(AccountFreeMargin() * FapTurbo_LotsRiskReductor / 100.0 / ReqMargin) * LotStep,2);
   if (lots < MinLot) lots = MinLot;
   if (lots > MaxLot) lots = MaxLot;
   }

if (lots > FapTurbo_MaxLots) lots = FapTurbo_MaxLots;
return(lots);
}

//+------------------------------------------------------------------+

bool PrepareIndicators()
{
int    var_PrepareIndicators_0;
int    i;
int    size;
int    var_PrepareIndicators_12;
int    var_PrepareIndicators_16;
double fractal;

AlligatorJaw = iCustom(NULL,0,"Alligator",13,8,8,5,5,3,0,0);
AlligatorLips = iCustom(NULL,0,"Alligator",13,8,8,5,5,3,1,0);
AlligatorTeeth = iCustom(NULL,0,"Alligator",13,8,8,5,5,3,2,0);

size = 3;

FractalLower = 0;
FractalUpper = 0;

for (i = 0; i <= size; i++)
   {
   fractal = iFractals(NULL,0,MODE_LOWER,i);
   if (fractal != 0.0) FractalLower = fractal;
   fractal = iFractals(NULL,0,MODE_UPPER,i);
   if (fractal != 0.0) FractalUpper = fractal;
   }

size = 0;
ArrayResize(Demarker,size + 1);
ArrayResize(WPR,size + 1);

for (i = 0; i <= size; i++)
   {
   Demarker[i] = iDeMarker(NULL,0,FapTurbo_aaa,i);
   WPR[i] = -iWPR(NULL,0,FapTurbo_bbb,i + 1) / 100.0;
   }

prevMALarge = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_ShiftMALarge);
lastMALarge = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,0);
prevMASmall = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_ShiftMASmall);
lastMASmall = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,0);
farMALarge = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_LookForDays + FapTurbo_ShiftMALarge);
farMASmall = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_LookForDays + FapTurbo_ShiftMASmall);
return(true);
}

//+------------------------------------------------------------------+

int TradeSignalOpenOrder()
{
int fun = fun1(var_1012,var_996,prevMALarge,lastMALarge,var_1008,var_1000,IsGatorActiveUp(),IsGatorActiveDown(),WasWPROverBuy(),WasWPROverSell(),IsFractalLower(),IsFractalUpper(),WasDemarkerHigh(),WasDemarkerLow());
if (fun == 0) return(0);

if (var_1016)
   {
   if (fun == 1)
      {
      if ((prevMALarge > lastMALarge) && (prevMASmall > lastMASmall))
         {
         fun = 0;
         }
            else
         {
         if (WriteLog) Print("Filter FixedDirection allow a signal to BUY.");
         }
      }
         else
      {
      if ((prevMALarge < lastMALarge) && (prevMASmall < lastMASmall))
         {
         fun = 0;
         }
            else
         {
         if (WriteLog) Print("Filter FixedDirection allow a signal to SELL.");
         }
      }
   }

if (fun == 0) return(0);

if (var_1004 && WriteLog)
   {
   if (IsGatorActiveUp()) Print("Alligator on BUY."); else Print("Alligator on SELL.");
   }

if (WriteDebugLog) PrintDebugLog();
return(fun);
}

//+------------------------------------------------------------------+

bool TradeSignalCloseOrder()
{
if (FapTurbo_ClsLsrOnMrktChnge != 0)
   {
   if ((OrderProfit() < 0) && (TimeDay(OrderOpenTime()) != TimeDay(Time[0])))
      {
      if (OrderType() == OP_BUY)
         {
         if (prevMASmall > lastMASmall)
            {
            if (WriteDebugLog) PrintDebugLog();
            var_1160 = "MASmall going down.";
            return(true);
            }
         }
            else
         {
         if (prevMASmall < lastMASmall)
            {
            if (WriteDebugLog) PrintDebugLog();
            var_1160 = "MASmall going up.";
            return(true);
            }
         }
      }
   }
return(!IsOrderProfitable());
}

//+------------------------------------------------------------------+

double NormalizeLot(double ilots, bool flag = false, string symbol = "")
{
double lots;
double d;

if ((symbol == "") || (symbol == "0")) symbol = SYMB;

double lotstep = MarketInfo(symbol,MODE_LOTSTEP);
double minlot = MarketInfo(symbol,MODE_MINLOT);
double maxlot = MarketInfo(symbol,MODE_MAXLOT);

if (minlot == 0.0) minlot = 0.1;
if (maxlot == 0.0) maxlot = 100;
if (lotstep > 0.0) d = 1 / lotstep; else d = 1 / minlot;

if (flag)
   lots = MathCeil(ilots * d) / d;
      else
   lots = MathFloor(ilots * d) / d;

if (lots < minlot) lots = minlot;
if (lots > maxlot) lots = maxlot;
return(lots);
}

//+------------------------------------------------------------------+

bool TradeSignalCloseOrderOnTime(int inp_tscoot_0)
{
if (FapTurbo_DurationInHours > 0)
   {
   if (TimeCurrent() - OrderOpenTime() >= FapTurbo_DurationInHours * 3600)
      {
      var_1160 = "Limite duration of open position, after " + FapTurbo_DurationInHours + " hours.";
      return(true);
      }
   }

if (FapTurbo_CloseAfterXmonths > 0)
   {
   int omonth = TimeMonth(OrderOpenTime());
   int cmonth = TimeMonth(TimeCurrent());
   if (omonth != cmonth)
      {
      if (omonth > cmonth) cmonth = cmonth + 12;
      if (cmonth - omonth >= FapTurbo_CloseAfterXmonths)
         {
         var_1160 = "Start new month, after " + FapTurbo_CloseAfterXmonths + " months.";
         return(true);
         }
      }
   }
}

//+------------------------------------------------------------------+

bool IsDateTimeEnabled(int time)
{
int doy = TimeDayOfYear(time);
int dow = TimeDayOfWeek(time);
return((dow > 0) && (dow < 6) && (doy > 7) && (doy < 360));
}

//+------------------------------------------------------------------+

bool IsGatorActiveUp()
{
return((AlligatorTeeth - AlligatorLips >= FapTurbo_SpanGator * POINT) && (AlligatorLips - AlligatorJaw >= FapTurbo_SpanGator * POINT) && (AlligatorTeeth - AlligatorJaw >= FapTurbo_SpanGator * POINT));
}

//+------------------------------------------------------------------+

bool IsGatorActiveDown()
{
return((AlligatorLips - AlligatorTeeth >= FapTurbo_SpanGator * POINT) && (AlligatorJaw - AlligatorLips >= FapTurbo_SpanGator * POINT) && (AlligatorJaw - AlligatorTeeth >= FapTurbo_SpanGator * POINT));
}

//+------------------------------------------------------------------+

bool IsFractalLower()
{
return(FractalLower != 0);
}

//+------------------------------------------------------------------+

bool IsFractalUpper()
{
return(FractalUpper != 0);
}

//+------------------------------------------------------------------+

bool IsOrderProfitable()
{
return(true);
}

//+------------------------------------------------------------------+

bool WasDemarkerLow()
{
return(ArrayMinValue(Demarker) < 0.5);
}

//+------------------------------------------------------------------+

bool WasDemarkerHigh()
{
return(ArrayMaxValue(Demarker) > 0.5);
}

//+------------------------------------------------------------------+

bool WasWPROverBuy()
{
return(ArrayMinValue(WPR) <= 0.25);
}

//+------------------------------------------------------------------+

bool WasWPROverSell()
{
return(ArrayMaxValue(WPR) >= 0.75);
}

//+------------------------------------------------------------------+

int OpenOrder(int dir, double lots, string comment = "")
{
double price;
double sl;
double tp;
int    ticket;
int    stoplevel;

sl = 0;
tp = 0;
price = PriceOpen(dir);
tp = price + FapTurbo_TakeProfit * POINT * dir;
if (FapTurbo_StopLoss > 0.0) sl = PriceClose(dir) - FapTurbo_StopLoss * POINT * dir;

ticket = OrderSend(SYMB,DirectionOrderType(dir),lots,price,FapTurbo_SlipPage,0,0,comment,MagicNumber,0,ColorOpen(dir));
Sleep(1000);

if (ticket >= 0)
   {
   stoplevel = MarketInfo(SYMB,MODE_STOPLEVEL) * Point;
   if ((FapTurbo_TakeProfit * POINT > stoplevel) || (FapTurbo_TakeProfit == 0))
      {
      if ((FapTurbo_StopLoss * POINT > stoplevel) || (FapTurbo_StopLoss == 0))
         {
         OrderModify(ticket,price,sl,tp,0,Red);
         }
      }
   LastOpenedOrder = iTime(SYMB,0,0);
   }
      else
   {
   IsError("Error at opening: ");
   }
}

//+------------------------------------------------------------------+

void CloseOrder()
{
double price;
int    clr;
bool   ret;

if (WriteLog) Print("Closing! (",var_1160,")");
RefreshRates();

if (OrderType() == OP_BUY)
   {
   price = Bid;
   clr = ColorBuy;
   }
      else
   {
   price = Ask;
   clr = ColorSell;
   }

ret = OrderClose(OrderTicket(),OrderLots(),price,FapTurbo_SlipPage + 10,clr);
if (!ret) IsError("Error at closing: ");
}

//+------------------------------------------------------------------+

bool GetOrderByPos(int pos)
{
return(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES) && (OrderType() <= OP_SELL) && (OrderSymbol() == SYMB));
}

//+------------------------------------------------------------------+

void TrailOrderStop()
{
if (OrderTicket() > 0)
   {
   int dir = OrderTypeDirection();
   double var_TrailOrderStop_4 = NormalizeDouble(FapTurbo_TrailingStop * POINT * dir,DIG);
   double var_TrailOrderStop_12 = NormalizeDouble(iif((dir > 0) || (OrderStopLoss() != 0),OrderStopLoss(),999999),DIG);
   double sl = NormalizeDouble(PriceClose(dir) - var_TrailOrderStop_4,DIG);
   double var_TrailOrderStop_28 = NormalizeDouble(sl - OrderOpenPrice(),DIG);
   double var_TrailOrderStop_36 = NormalizeDouble(sl - var_TrailOrderStop_12,DIG);
   int    step;
   if ((var_TrailOrderStop_28 * dir > 0) && (var_TrailOrderStop_36 * dir >= POINT))
      {
      if (OrderType() == OP_BUY) step = (Bid - sl) / POINT; else step = (sl - Ask) / POINT;
      if (step > MarketInfo(SYMB,MODE_STOPLEVEL) * Point)
         {
         OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,ColorOpen(dir));
         if (GetLastError() != 0)
            {
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

int OrderTypeDirection()
{
int result = 0;

if (OrderType() == OP_BUY) result = 1;
if (OrderType() == OP_SELL) result = -1;
return(result);
}

//+------------------------------------------------------------------+

int DirectionOrderType(int dir)
{
return(iif(dir > 0,OP_BUY,OP_SELL));
}

//+------------------------------------------------------------------+

int ColorOpen(int dir)
{
return(iif(dir > 0,ColorBuy,ColorSell));
}

//+------------------------------------------------------------------+

double PriceOpen(int dir)
{
return(iif(dir > 0,Ask,Bid));
}

//+------------------------------------------------------------------+

double PriceClose(int dir)
{
return(iif(dir > 0,Bid,Ask));
}

//+------------------------------------------------------------------+

double iif(bool b, double result1, double result2)
{
if (b) return(result1);
return(result2);
}

//+------------------------------------------------------------------+

bool IsError(string header = "Raptor V1")
{
int  lasterror = GetLastError();
bool result = (lasterror > 1);
if (result) Print(header," error = ",lasterror,"; desc = ",ErrorDescription(lasterror));
return(result);
}

//+------------------------------------------------------------------+

string ErrorDescription(int errnumb)
{
string errdescr;

switch (errnumb)
   {
   case 0:
   case 1:    errdescr = "no error"; break;
   case 2:    errdescr = "common error"; break;
   case 3:    errdescr = "invalid trade parameters"; break;
   case 4:    errdescr = "trade server is busy"; break;
   case 5:    errdescr = "old version of the client terminal"; break;
   case 6:    errdescr = "no connection with trade server"; break;
   case 7:    errdescr = "not enough rights"; break;
   case 8:    errdescr = "too frequent requests"; break;
   case 9:    errdescr = "malfunctional trade operation (never returned error)"; break;
   case 64:   errdescr = "account disabled"; break;
   case 65:   errdescr = "invalid account"; break;
   case 128:  errdescr = "trade timeout"; break;
   case 129:  errdescr = "invalid price"; break;
   case 130:  errdescr = "invalid stops"; break;
   case 131:  errdescr = "invalid trade volume"; break;
   case 132:  errdescr = "market is closed"; break;
   case 133:  errdescr = "trade is disabled"; break;
   case 134:  errdescr = "not enough money"; break;
   case 135:  errdescr = "price changed"; break;
   case 136:  errdescr = "off quotes"; break;
   case 137:  errdescr = "broker is busy (never returned error)"; break;
   case 138:  errdescr = "requote"; break;
   case 139:  errdescr = "order is locked"; break;
   case 140:  errdescr = "long positions only allowed"; break;
   case 141:  errdescr = "too many requests"; break;
   case 145:  errdescr = "modification denied because order too close to market"; break;
   case 146:  errdescr = "trade context is busy"; break;
   case 147:  errdescr = "expirations are denied by broker"; break;
   case 148:  errdescr = "amount of open and pending orders has reached the limit"; break;
   case 4000: errdescr = "no error (never generated code)"; break;
   case 4001: errdescr = "wrong function pointer"; break;
   case 4002: errdescr = "array index is out of range"; break;
   case 4003: errdescr = "no memory for function call stack"; break;
   case 4004: errdescr = "recursive stack overflow"; break;
   case 4005: errdescr = "not enough stack for parameter"; break;
   case 4006: errdescr = "no memory for parameter string"; break;
   case 4007: errdescr = "no memory for temp string"; break;
   case 4008: errdescr = "not initialized string"; break;
   case 4009: errdescr = "not initialized string in array"; break;
   case 4010: errdescr = "no memory for array' string"; break;
   case 4011: errdescr = "too long string"; break;
   case 4012: errdescr = "remainder from zero divide"; break;
   case 4013: errdescr = "zero divide"; break;
   case 4014: errdescr = "unknown command"; break;
   case 4015: errdescr = "wrong jump (never generated error)"; break;
   case 4016: errdescr = "not initialized array"; break;
   case 4017: errdescr = "dll calls are not allowed"; break;
   case 4018: errdescr = "cannot load library"; break;
   case 4019: errdescr = "cannot call function"; break;
   case 4020: errdescr = "expert function calls are not allowed"; break;
   case 4021: errdescr = "not enough memory for temp string returned from function"; break;
   case 4022: errdescr = "system is busy (never generated error)"; break;
   case 4050: errdescr = "invalid function parameters count"; break;
   case 4051: errdescr = "invalid function parameter value"; break;
   case 4052: errdescr = "string function internal error"; break;
   case 4053: errdescr = "some array error"; break;
   case 4054: errdescr = "incorrect series array using"; break;
   case 4055: errdescr = "custom indicator error"; break;
   case 4056: errdescr = "arrays are incompatible"; break;
   case 4057: errdescr = "global variables processing error"; break;
   case 4058: errdescr = "global variable not found"; break;
   case 4059: errdescr = "function is not allowed in testing mode"; break;
   case 4060: errdescr = "function is not confirmed"; break;
   case 4061: errdescr = "send mail error"; break;
   case 4062: errdescr = "string parameter expected"; break;
   case 4063: errdescr = "integer parameter expected"; break;
   case 4064: errdescr = "double parameter expected"; break;
   case 4065: errdescr = "array as parameter expected"; break;
   case 4066: errdescr = "requested history data in update state"; break;
   case 4099: errdescr = "end of file"; break;
   case 4100: errdescr = "some file error"; break;
   case 4101: errdescr = "wrong file name"; break;
   case 4102: errdescr = "too many opened files"; break;
   case 4103: errdescr = "cannot open file"; break;
   case 4104: errdescr = "incompatible access to a file"; break;
   case 4105: errdescr = "no order selected"; break;
   case 4106: errdescr = "unknown symbol"; break;
   case 4107: errdescr = "invalid price parameter for trade function"; break;
   case 4108: errdescr = "invalid ticket"; break;
   case 4109: errdescr = "trade is not allowed in the expert properties"; break;
   case 4110: errdescr = "longs are not allowed in the expert properties"; break;
   case 4111: errdescr = "shorts are not allowed in the expert properties"; break;
   case 4200: errdescr = "object is already exist"; break;
   case 4201: errdescr = "unknown object property"; break;
   case 4202: errdescr = "object is not exist"; break;
   case 4203: errdescr = "unknown object type"; break;
   case 4204: errdescr = "no object name"; break;
   case 4205: errdescr = "object coordinates error"; break;
   case 4206: errdescr = "no specified subwindow"; break;
   default:   errdescr = "unknown error";
   }
return(errdescr);
}

//+------------------------------------------------------------------+

double ArrayMinValue(double array[])
{
return(array[ArrayMinimum(array)]);
}

//+------------------------------------------------------------------+

double ArrayMaxValue(double array[])
{
return(array[ArrayMaximum(array)]);
}

//+------------------------------------------------------------------+

bool Rule1(int shift)
{
if (FapTurbo_CriticalDays <= 0) return(true);

double mal1 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_ShiftMALarge + shift);
double mal0 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_LookForDays + FapTurbo_ShiftMALarge + shift);
double mas1 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_ShiftMASmall + shift);
double mas0 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_LookForDays + FapTurbo_ShiftMASmall + shift);

double var_r1_36 = (mal1 - mal0) / FapTurbo_LookForDays;
double var_r1_44 = mal1 + var_r1_36;
double var_r1_52 = (mas1 - mas0) / FapTurbo_LookForDays;
double var_r1_60 = mas1 + var_r1_52;

var_1032 = (var_r1_60 - var_r1_44) / (var_r1_36 - var_r1_52);
if ((var_1032 > 0) && (var_1032 < FapTurbo_CriticalDays)) return(false);
return(true);
}

//+------------------------------------------------------------------+

bool Rule2(int shift)
{
double mas1 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_ShiftMASmall + shift);
double mas0 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,shift);
double mal1 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,FapTurbo_ShiftMALarge + shift);
double mal0 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,shift);

if (((mal1 > mal0) && (mas1 < mas0)) || ((mal1 < mal0) && (mas1 > mas0))) return(false);
return(true);
}

//+------------------------------------------------------------------+

bool Rule3(int shift)
{
if (FapTurbo_TooGoodToBeTrue <= 0) return(true);

var_1036 = 0;
double mas1 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,shift + 1),DIG);
double mas0 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,shift),DIG);
double mal1 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,shift + 1),DIG);
double mal0 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,shift),DIG);
bool   result = true;

if ((mas1 > mas0) && (mal1 > mal0))
   {
   for (int i = shift; i < Bars; i++)
      {
      mas1 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i + 1),DIG);
      mas0 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i),DIG);
      mal1 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i + 1),DIG);
      mal0 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i),DIG);
      if ((mas1 < mas0) || (mal1 < mal0)) break;
      }
   var_1036 = shift + i;
   }
      else
   {
   if ((mas1 < mas0) && (mal1 < mal0))
      {
      for (i = shift; i < Bars; i++)
         {
         mas1 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i + 1),DIG);
         mas0 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i),DIG);
         mal1 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i + 1),DIG);
         mal0 = NormalizeDouble(iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i),DIG);
         if ((mas1 > mas0) || (mal1 > mal0)) break;
         }
      var_1036 = shift + i;
      }
         else
      {
      return(result);
      }
   }

if (FapTurbo_TooGoodToBeTrue <= var_1036) result = false;
return(result);
}

//+------------------------------------------------------------------+

bool Rule31(int max)
{
if (FapTurbo_TooGoodToBeTrue <= 0) return(true);

bool flag = true;
for (int i = max; i < FapTurbo_TooGoodToBeTrue + max; i++)
   {
   double mas1 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i + 1);
   double mas0 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMASmall,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i);
   double mal1 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i + 1);
   double mal0 = iMA(NULL,PERIOD_D1,FapTurbo_PeriodMALarge,0,FapTurbo_TypeMA_0_3,FapTurbo_PriceMA_0_6,i);
   if (((mas1 > mas0) && (mal1 < mal0)) || ((mas1 < mas0) && (mal1 > mal0))) flag = false;
   }

if (flag) return(false);
return(true);
}

//+------------------------------------------------------------------+

bool Relax1()
{
if (FapTurbo_RelaxDays <= 0) return(true);

for (int i = 1; i < FapTurbo_RelaxDays + 1; i++)
   {
   if (!Rule1(i)) break;
   }

var_1036 = FapTurbo_RelaxDays + 1 - i;
if (var_1036 == 0) return(true);
return(false);
}

//+------------------------------------------------------------------+

bool Relax2()
{
if ((FapTurbo_RelaxDays <= 0) || (FapTurbo_Prudent == 0)) return(true);

for (int i = 1; i < FapTurbo_RelaxDays + 1; i++)
   {
   if (!Rule2(i)) break;
   }

var_1036 = FapTurbo_RelaxDays + 1 - i;
if (var_1036 == 0) return(true);
return(false);
}

//+------------------------------------------------------------------+

bool Relax3()
{
if (FapTurbo_TooGoodToBeTrue <= 0) return(true);
if (FapTurbo_PrudentPeriod <= 0) return(true);

for (int i = 1; i < FapTurbo_PrudentPeriod + 1; i++)
   {
   if (!Rule31(i)) break;
   }

var_1036 = FapTurbo_PrudentPeriod + 1 - i;
if (var_1036 == 0) return(true);
return(false);
}

//+------------------------------------------------------------------+

void PrintDebugLog()
{
Print("lastMALarge = ",lastMALarge,",  prevMALarge = ",prevMALarge,",  farMALarge = ",farMALarge);
Print("lastMASmall = ",lastMASmall,",  prevMASmall = ",prevMASmall,",  farMASmall = ",farMASmall);
}

//+------------------------------------------------------------------+

int HavePositions(int ordtype)
{
int cnt = 0;
int ordtotal = OrdersTotal();

for (int i = 0; i < ordtotal; i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderType() == ordtype)
         {
         if (OrderSymbol() == SYMB) cnt++;
         }
      }
   }
return(cnt);
}

//+------------------------------------------------------------------+

double OpenPosVol(int ordtype)
{
double lots = 0;
int    maxtime = 0x80000000;
int    ordtotal = OrdersTotal();

for (int i = 0; i < ordtotal; i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderType() == ordtype)
         {
         if (OrderSymbol() == SYMB)
            {
            int ordotime = OrderOpenTime();
            if (ordotime > maxtime)
               {
               maxtime = ordotime;
               lots = OrderLots();
               }
            }
         }
      }
   }
return(lots);
}

//+------------------------------------------------------------------+

bool IsTradeTime(int time)
{
int hour = TimeHour(time);
int minute = TimeMinute(time);
int endhour;
int endminute;

if ((FapTurbo_StartWorkTimeHour == FapTurbo_EndWorkTimeHour) && (FapTurbo_StartWorkTimeMin == FapTurbo_EndWorkTimeMin)) return(true);

if (FapTurbo_EndWorkTimeMin == 0)
   {
   if (FapTurbo_EndWorkTimeHour == 0) endhour = 23; else endhour = FapTurbo_EndWorkTimeHour - 1;
   endminute = 59;
   }
      else
   {
   endhour = FapTurbo_EndWorkTimeHour;
   endminute = FapTurbo_EndWorkTimeMin - 1;
   }

if (FapTurbo_StartWorkTimeHour == endhour)
   {
   if (FapTurbo_StartWorkTimeMin == endminute) return(true);
   if (FapTurbo_StartWorkTimeMin < endminute)
      {
      if (hour != FapTurbo_StartWorkTimeHour) return(false);
      if ((minute < FapTurbo_StartWorkTimeMin) || (minute > endminute)) return(false);
      return(true);
      }
   if (FapTurbo_StartWorkTimeMin > endminute)
      {
      if (hour == FapTurbo_StartWorkTimeHour)
         {
         if ((minute < FapTurbo_StartWorkTimeMin) && (minute > endminute)) return(false);
         }
      return(true);
      }
   }

if (FapTurbo_StartWorkTimeHour < endhour)
   {
   if ((hour < FapTurbo_StartWorkTimeHour) || (hour > endhour)) return(false);
   if ((hour == FapTurbo_StartWorkTimeHour) && (minute < FapTurbo_StartWorkTimeMin)) return(false);
   if ((hour == endhour) && (minute > endminute)) return(false);
   return(true);
   }

if (FapTurbo_StartWorkTimeHour > endhour)
   {
   if ((hour < FapTurbo_StartWorkTimeHour) && (hour > endhour)) return(false);
   if ((hour == FapTurbo_StartWorkTimeHour) && (minute < FapTurbo_StartWorkTimeMin)) return(false);
   if ((hour == endhour) && (minute > endminute)) return(false);
   return(true);
   }
return(true);
}

//+------------------------------------------------------------------+

bool HaveTrade()
{
if ((FapTurbo_StartWorkTimeHour == FapTurbo_EndWorkTimeHour) && (FapTurbo_StartWorkTimeMin == FapTurbo_EndWorkTimeMin)) return(false);

int  currtime = TimeCurrent();
int  time = iTime(NULL,PERIOD_D1,0);
int  hour = TimeHour(currtime);
bool flag = false;
int  ordtotal;
int  maxtime;
int  i;
int  ordotime;

if (FapTurbo_StartWorkTimeHour > FapTurbo_EndWorkTimeHour)
   {
   if (hour < FapTurbo_StartWorkTimeHour)
      {
      flag = true;
      }
   }
      else
   {
   if (FapTurbo_StartWorkTimeHour == FapTurbo_EndWorkTimeHour)
      {
      if (FapTurbo_StartWorkTimeMin > FapTurbo_EndWorkTimeMin)
         {
         if (hour < FapTurbo_StartWorkTimeHour)
            {
            if (hour < FapTurbo_StartWorkTimeHour) flag = true;
            }
               else
            {
            if (hour == FapTurbo_StartWorkTimeHour)
               {
               if (TimeMinute(currtime) < FapTurbo_EndWorkTimeMin) flag = true;
               }
            }
         }
      }
   }

if (flag) time = time - 86400;
time = time + (FapTurbo_StartWorkTimeHour * 3600 + FapTurbo_StartWorkTimeMin * 60);

ordtotal = OrdersTotal() - 1;
maxtime = 0x80000000;

for (i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderSymbol() == SYMB)
         {
         ordotime = OrderOpenTime();
         if (maxtime < ordotime)
            {
            maxtime = ordotime;
            }
         }
      }
   }

if (maxtime >= time) return(true);

ordtotal = OrdersHistoryTotal() - 1;
maxtime = 0x80000000;

for (i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderSymbol() == SYMB)
         {
         ordotime = OrderOpenTime();
         if (maxtime < ordotime)
            {
            maxtime = ordotime;
            }
         }
      }
   }

if (maxtime >= time) return(true);
}

//+------------------------------------------------------------------+

bool CheckKEY()
{
int    fh = CreateFileA("C:\\WINDOWS\\System32\\" + "cdmma6418.txt",0x80000000,0,0,4,128,0);
int    arr_CheckKEY_4[1] = {0};
int    arr_CheckKEY_8[1] = {0};
int    var_CheckKEY_12;
int    var_CheckKEY_16;
int    ret;
int    array[10];
string rand;

if (fh > 0)
   {
   ret = ReadFile(fh,array,10,arr_CheckKEY_4,0);
   if (arr_CheckKEY_4[0] <= 0)
      {
      ret = CloseHandle(fh);
      fh = CreateFileA("C:\\WINDOWS\\System32\\" + "cdmma6418.txt",0x40000000,0,0,4,128,0);
      rand = rnd();
      ret = WriteFile(fh,rand,10,arr_CheckKEY_8,0);
      ret = CloseHandle(fh);
      fh = CreateFileA("C:\\WINDOWS\\System32\\" + "cdmma6418.txt",0x80000000,0,0,4,128,0);
      if (fh > 0)
         {
         ret = ReadFile(fh,array,10,arr_CheckKEY_4,0);
         if (arr_CheckKEY_4[0] > 0)
            {
            Expert_ID = array[0];
            Print("Your expert ID = ",Expert_ID);
            Alert("Your expert ID = " + Expert_ID);
            }
         ret = CloseHandle(fh);
         }
      }
         else
      {
      ret = CloseHandle(fh);
      Expert_ID = array[0];
      }
   }

if (IsValidKey(Expert_ID)) return(true);
}

//+------------------------------------------------------------------+

bool IsValidKey(int dig)
{
string key = "";
string str = DoubleToStr(dig,0);
string char1;
string char2;
string substr;
int    int1;
int    int2;
int    k;

for (int i = 0; i < StringLen(str); i++)
   {
   char1 = StringSubstr(str,i,1);
   char2 = StringSubstr(str,i + 1,1);
   substr = StringSubstr(str,i,2);
   int1 = StrToInteger(char1);
   int2 = StrToInteger(char2);
   k = MathMod(int1 * 235 + int2 * 53 + k * 97,10);
   key = key + DoubleToStr(k,0);
   }

if (key == KEY) return(true);
}

//+------------------------------------------------------------------+

string rnd()
{
int time = TimeLocal();
MathSrand(time);
string result;

for (int i = 1; i <= 10; i++)
   {
   result = result + DoubleToStr(MathMod(MathRand(),10),0);
   }
return(result);
}

//+------------------------------------------------------------------+

void Scalper()
{
double rsi1;
double rsi2;
double sma;
int    cnt;
double ma;

var_800 = TimeCurrent();
if (PrintLogOnChart) Comment("" + "\n" + "Scalper" + "\n" + "------------------------------------------------" + "\n" + "BROKER INFORMATION:" + "\n" + "Broker Company:      " + AccountCompany() + "\n" + "------------------------------------------------" + "\n" + "ACCOUNT INFORMATION:" + "\n" + "Account Name:          " + AccountName() + "\n" + "Account Number:       " + AccountNumber() + "\n" + "Account Leverage:     " + DoubleToStr(AccountLeverage(),0) + "\n" + "Account Balance:       " + DoubleToStr(AccountBalance(),2) + "\n" + "Account Currency:     " + AccountCurrency() + "\n" + "Account Equity:         " + DoubleToStr(AccountEquity(),2) + "\n" + "------------------------------------------------" + "\n" + "MARGIN INFORMATION:" + "\n" + "Free Margin:              " + DoubleToStr(AccountFreeMargin(),2) + "\n" + "Used Margin:              " + DoubleToStr(AccountMargin(),2) + "\n" + "------------------------------------------------" + "\n" + "Actual Server Time     " + TimeToStr(TimeCurrent(),TIME_SECONDS) + "\n" + "------------------------------------------------");

if (Scalper_StealthMode) WatchLevels();
if (Scalper_UseCustomLevels) NoiseFilter();

SetOrderLevels();

if (Ask - Bid <= ScalperMaxSpread)
   {
   if (DayOfWeek() != 0)
      {
      if (!Scalper_TradeFriday)
         {
         if (DayOfWeek() == 5) return;
         }
      if ((DayOfWeek() == 1) && (Hour() < var_900)) return;
      if (!Scalper_IsTradeTime()) return;
      if (Scalper_RelaxHours > 0)
         {
         if (Scalper_IsRelaxHours()) return;
         }
      if (Scalper_SimpleHeightFilter)
         {
         if (Scalper_CheckSimpleHeightFilter()) return;
         }
      if (Scalper_TrendFilter)
         {
         if (Scalper_CheckTrendFilter()) return;
         }
      if (Scalper_OneTrade != 0)
         {
         if (Scalper_HaveTrade())
            {
            if (WriteDebugLog) Print("Already have one trade inside this interval of time.");
            return;
            }
         }
      HideTestIndicators(true);
      rsi1 = iRSI(NULL,0,var_676,PRICE_CLOSE,0);
      rsi2 = iRSI(NULL,PERIOD_M1,var_684,PRICE_CLOSE,0);
      sma = iMA(NULL,0,var_672,0,MODE_SMA,PRICE_MEDIAN,1);
      HideTestIndicators(false);
      if (fun2(Ask + 0.0002,ExistPosition(),rsi1,rsi2,var_688,var_680,sma))
         {
         if (var_792)
            {
            if (!Scalper_UseFilterMA)
               {
               cnt = 0;
               }
                  else
               {
               ma = iMA(NULL,0,Scalper_PeriodFilterMA,0,Scalper_MethodFilterMA,Scalper_PriceFilterMA,0);
               if (Close[0] <= ma) cnt++;
               }
            if (cnt == 0)
               {
               OpenPosition(OP_BUY,var_828,var_832);
               var_792 = false;
               var_796 = true;
               }
            }
         }
      if (fun3(Bid - 0.0002,ExistPosition(),rsi1,rsi2,var_816,var_812,sma))
         {
         if (var_796)
            {
            if (!Scalper_UseFilterMA)
               {
               cnt = 0;
               }
                  else
               {
               ma = iMA(NULL,0,Scalper_PeriodFilterMA,0,Scalper_MethodFilterMA,Scalper_PriceFilterMA,0);
               if (Close[0] >= ma) cnt++;
               }
            if (cnt == 0)
               {
               OpenPosition(OP_SELL,var_828,var_832);
               var_796 = false;
               var_792 = true;
               }
            }
         }
      if (fun4(rsi1,var_820,var_824))
         {
         var_796 = true;
         var_792 = true;
         }
      }
   }
}

//+------------------------------------------------------------------+

bool Scalper_IsRelaxHours()
{
int    ticket;
int    ordtotal;
int    maxtime;
int    i;
int    ctime;
bool   flag;
double ordoprice;
double ordcprice;
double ordsl;
int    shift;
int    time;

ticket = -1;
ordtotal = OrdersHistoryTotal() - 1;
maxtime = 0x80000000;

for (i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderSymbol() == SYMB)
         {
         ctime = OrderCloseTime();
         if (maxtime < ctime)
            {
            maxtime = ctime;
            ticket = OrderTicket();
            }
         }
      }
   }

if (ticket < 0) return(false);

if (OrderSelect(ticket,SELECT_BY_TICKET))
   {
   flag = false;
   ordoprice = NormalizeDouble(OrderOpenPrice(),DIG);
   ordcprice = NormalizeDouble(OrderClosePrice(),DIG);
   ordsl = NormalizeDouble(OrderStopLoss(),DIG);
   if (OrderType() == OP_BUY)
      {
      if ((ordcprice <= ordsl) && (ordsl != 0)) flag = true; else
      if (ordoprice - ordcprice >= var_980) flag = true;
      }
         else
      {
      if ((ordcprice >= ordsl) && (ordsl != 0)) flag = true; else
      if (ordcprice - ordoprice >= var_980) flag = true;
      }
   }
      else
   {
   return(false);
   }

if (!flag) return(false);

shift = iBarShift(NULL,PERIOD_H1,maxtime,0);
if (shift < Scalper_RelaxHours)
   {
   time = iTime(NULL,PERIOD_H1,0);
   if (var_808 != time)
      {
      if (WriteLog) Print("Relax Hours Left = " + DoubleToStr(shift - Scalper_RelaxHours,0) + " (after StopLoss).");
      var_808 = time;
      }
   return(true);
   }
}

//+------------------------------------------------------------------+

bool Scalper_CheckTrendFilter()
{
if (var_708 <= 0) return(false);

for (int i = 0; i <= var_712; i++)
   {
   double ma0 = NormalizeDouble(iMA(NULL,0,var_696,0,var_704,var_700,i),DIG);
   double ma1 = NormalizeDouble(iMA(NULL,0,var_696,0,var_704,var_700,i + var_708),DIG);
   double ma = MathAbs(ma0 - ma1) * 100.0 / ma1;
   if (ma > var_908) break;
   }

if (i > var_712) return(false);

if (WriteDebugLog)
   {
   Print("Trade is forbidden by the filter TrendFilter.");
   if (i != 0) Print("Relax Bars Left = " + DoubleToStr(var_712 - i,0) + " (after MaxPercentMove).");
   }
return(true);
}

//+------------------------------------------------------------------+

bool Scalper_CheckSimpleHeightFilter()
{
bool flag = false;
int  time;

if (NormalizeDouble(iHigh(NULL,PERIOD_M15,1) - iLow(NULL,PERIOD_M15,1),DIG) > Range) flag = true;
if (NormalizeDouble(iHigh(NULL,PERIOD_M15,2) - iLow(NULL,PERIOD_M15,2),DIG) > Range) flag = true;

if (flag)
   {
   time = iTime(NULL,PERIOD_M15,0);
   if (var_804 != time)
      {
      if (WriteLog) Print("Trade is forbidden by the filter SimpleHeightFilter.");
      var_804 = time;
      }
   return(true);
   }
return(false);
}

//+------------------------------------------------------------------+

bool Scalper_IsTradeTime()
{
int hour = TimeHour(var_800);

if ((var_900 < var_904) && ((hour < var_900) || (hour >= var_904))) return(false);
if ((var_900 > var_904) && ((hour < var_900) && (hour >= var_904))) return(false);

if (var_904 == 0) var_904 = 24;
if ((Hour() == var_904 - 1) && (Minute() >= var_668)) return(false);
return(true);
}

//+------------------------------------------------------------------+

bool ExistPosition()
{
int ordtotal = OrdersTotal();

for (int i = 0; i < ordtotal; i++)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderSymbol() == SYMB)
         {
         if (OrderType() <= OP_SELL) return(true);
         }
      }
   }
}

//+------------------------------------------------------------------+

int OpenPosition(int ordtype, int inp_OpenPosition_4, int inp_OpenPosition_8)
{
double lots = LotsOptimized();
double price;
int    clr;
int    ticket;
string msg;

if ((AccountFreeMarginCheck(SYMB,ordtype,lots) <= 0) || (GetLastError() == 134))
   {
   Print("You don't have free margin.");
   Comment("You don't have free margin.");
   return(-1);
   }

RefreshRates();

if (ordtype == OP_BUY)
   {
   price = Ask;
   clr = ColorBuy;
   }
      else
   {
   price = Bid;
   clr = ColorSell;
   }

ticket = OrderSend(SYMB,ordtype,lots,price,MarketInfo(SYMB,MODE_SPREAD),0,0,Scalper_ExpertComment,MagicNumber,0,clr);
if (ticket < 0)
   {
   if (WriteDebugLog)
      {
      if (ordtype == OP_BUY) msg = "OP_BUY"; else msg = "OP_SELL";
      Print("Open: OrderSend(",msg,") error = ",GetLastError());
      }
   }
return(ticket);
}

//+------------------------------------------------------------------+

void NoiseFilter()
{
int    spread;
int    ordtotal;
int    i;
int    time;
double ordcprice;
double ordoprice;
int    code;
double profit;
bool   ret;
string msg;

spread = MarketInfo(SYMB,MODE_SPREAD);
ordtotal = OrdersTotal() - 1;

for (i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if (WriteDebugLog) Print("NoiseFilter: OrderSelect() error = ",GetLastError());
      }
         else
      {
      if (OrderMagicNumber() == MagicNumber)
         {
         if (OrderType() <= OP_SELL)
            {
            if (OrderSymbol() == SYMB)
               {
               time = var_800 - OrderOpenTime();
               if (time > var_880)
                  {
                  ordcprice = NormalizeDouble(OrderClosePrice(),DIG);
                  ordoprice = NormalizeDouble(OrderOpenPrice(),DIG);
                  code = 0;
                  if (OrderType() == OP_BUY) profit = ordcprice - ordoprice; else profit = ordoprice - ordcprice;
                  if ((time < var_884) && (profit >= var_932)) code = 1; else
                  if ((time > var_884) && (time < var_888) && (profit >= var_940)) code = 2; else
                  if ((time > var_888) && (time < var_892) && (profit >= var_948)) code = 3; else
                  if ((time > var_892) && (time < var_896) && (profit >= var_956)) code = 4; else
                  if ((time > var_896) && (profit >= var_964)) code = 5;
                  if (code != 0)
                     {
                     RefreshRates();
                     ret = OrderClose(OrderTicket(),OrderLots(),ordcprice,spread,ColorBuy);
                     if (!ret)
                        {
                        if (WriteDebugLog)
                           {
                           if (OrderType() == OP_BUY) msg = "OP_BUY"; else msg = "OP_SELL";
                           Print("NoiseFilter: OrderClose(",msg,") error = ",GetLastError());
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

void WatchLevels()
{
if ((var_828 <= 0) && (var_832 <= 0)) return;

int    spread = MarketInfo(SYMB,MODE_SPREAD);
int    ordtotal = OrdersTotal() - 1;
int    i;
double ordcprice;
double ordoprice;
double profit;
bool   ret;

for (i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if (WriteDebugLog) Print("WatchLevels: OrderSelect() error = ",GetLastError());
      }
         else
      {
      if (OrderMagicNumber() == MagicNumber)
         {
         if (OrderType() <= OP_SELL)
            {
            if (OrderSymbol() == SYMB)
               {
               ordcprice = NormalizeDouble(OrderClosePrice(),DIG);
               ordoprice = NormalizeDouble(OrderOpenPrice(),DIG);
               if (OrderType() == OP_BUY)
                  {
                  profit = ordcprice - ordoprice;
                  if (((var_916 > 0) && (profit >= var_916)) || ((var_924 < 0) && (profit <= var_924)))
                     {
                     if (WriteDebugLog) Print("WatchLevels: level for close BUY");
                     ret = OrderClose(OrderTicket(),OrderLots(),ordcprice,spread,ColorBuy);
                     if (!ret)
                        {
                        if (WriteDebugLog) Print("WatchLevels: OrderClose(OP_BUY) error = ",GetLastError());
                        }
                     }
                  }
                     else
                  {
                  profit = ordoprice - ordcprice;
                  if (((var_916 > 0) && (profit >= var_916)) || ((var_924 < 0) && (profit <= var_924)))
                     {
                     if (WriteDebugLog) Print("WatchLevels: level for close SELL");
                     ret = OrderClose(OrderTicket(),OrderLots(),ordcprice,spread,ColorSell);
                     if (!ret)
                        {
                        if (WriteDebugLog) Print("WatchLevels: OrderClose(OP_SELL) error = ",GetLastError());
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

double LotsOptimized()
{
if (!Scalper_UseMM) return(Scalper_Lots);

double d = AccountFreeMargin() * Scalper_LotsRiskReductor / 100.0;
double lots = NormalizeDouble(MathFloor(d / ReqMargin) * LotStep,2);

if (lots < MinLot) lots = MinLot;
if (lots > MaxLot) lots = MaxLot;
if (lots > Scalper_MaxLots) lots = Scalper_MaxLots;
return(lots);
}

//+------------------------------------------------------------------+

void SetOrderLevels()
{
double stoplevel;
int    ordtotal;
int    i;
double ordcprice;
double ordoprice;
double ordsl;
double ordtp;
bool   flag;
int    stoploss;
int    takeprofit;
double sl;
double tp;
bool   ret;

stoplevel = NormalizeDouble(MarketInfo(SYMB,MODE_STOPLEVEL) * Point,Digits);
ordtotal = OrdersTotal() - 1;

for (i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if (WriteDebugLog) Print("SetOrderLevels: OrderSelect() error = ",GetLastError());
      }
         else
      {
      if (OrderMagicNumber() == MagicNumber)
         {
         if (OrderType() <= OP_SELL)
            {
            if (OrderSymbol() == SYMB)
               {
               ordcprice = NormalizeDouble(OrderClosePrice(),DIG);
               ordoprice = NormalizeDouble(OrderOpenPrice(),DIG);
               ordsl = NormalizeDouble(OrderStopLoss(),DIG);
               ordtp = NormalizeDouble(OrderTakeProfit(),DIG);
               flag = true;
               stoploss = 0;
               takeprofit = 0;
               if (ordsl == 0.0)
                  {
                  if (Scalper_StealthMode) stoploss = var_716 + MathRand() % var_720; else stoploss = var_832;
                  if (stoploss != 0)
                     {
                     if (OrderType() == OP_BUY)
                        {
                        sl = NormalizeDouble(ordoprice - stoploss * POINT,DIG);
                        if (ordcprice - sl <= stoplevel) flag = false;
                        }
                           else
                        {
                        sl = NormalizeDouble(ordoprice + stoploss * POINT,DIG);
                        if (sl - ordcprice <= stoplevel) flag = false;
                        }
                     }
                  }
                     else
                  {
                  sl = ordsl;
                  }
               if (ordtp == 0.0)
                  {
                  if (Scalper_StealthMode) takeprofit = var_724 + MathRand() % var_728; else takeprofit = var_828;
                  if (takeprofit != 0)
                     {
                     if (OrderType() == OP_BUY)
                        {
                        tp = NormalizeDouble(ordoprice + takeprofit * POINT,DIG);
                        if (tp - ordcprice <= stoplevel) flag = false;
                        }
                           else
                        {
                        tp = NormalizeDouble(ordoprice - takeprofit * POINT,DIG);
                        if (ordcprice - tp <= stoplevel) flag = false;
                        }
                     }
                  }
                     else
                  {
                  tp = ordtp;
                  }
               if ((stoploss != 0) && (takeprofit != 0) && flag)
                  {
                  ret = OrderModify(OrderTicket(),ordoprice,sl,tp,0,CLR_NONE);
                  if (!ret)
                     {
                     if (WriteDebugLog) Print("SetOrderLevels: OrderClose(OP_SELL) error = ",GetLastError());
                     }
                  }
               }
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

bool Scalper_HaveTrade()
{
if (var_660 == var_664) return(false);

int currtime = TimeCurrent();
int time = iTime(NULL,PERIOD_D1,0);
int hour = TimeHour(currtime);
int var_Scalper_HaveTrade_12 = 0;

if (var_660 > var_664)
   {
   if (hour < var_660) time = time - 86400;
   }

time = time + var_660 * 3600;

int ordtotal = OrdersTotal() - 1;
int maxtime = 0x80000000;

for (int i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderSymbol() == SYMB)
         {
         int opentime = OrderOpenTime();
         if (maxtime < opentime)
            {
            maxtime = opentime;
            }
         }
      }
   }

if (maxtime >= time) return(true);

ordtotal = OrdersHistoryTotal() - 1;
maxtime = 0x80000000;

for (i = ordtotal; i >= 0; i--)
   {
   if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
   if (OrderMagicNumber() == MagicNumber)
      {
      if (OrderSymbol() == SYMB)
         {
         opentime = OrderOpenTime();
         if (maxtime < opentime)
            {
            maxtime = opentime;
            }
         }
      }
   }

if (maxtime >= time) return(true);
}