Auto_RXD





//+------------------------------------------------------------------+
//|                                                    Auto_RXD.mq4  |
//|                                            Copyright Robbie Ruan |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2009 Robbie Ruan Ver 0.01"
#property link      "robbie.ruan@gmail.com"

//---- input parameters

extern int          MagicNumberAI = 888;
extern int          MagicNumberAIReverse = 222;
extern int          MagicNumberGrid = 999;

extern string       NoteMode = "0 Indicator mode, 1 BTS Mode, 2 AI Short, 3 AI Long, 4 AI Filter and Normal";
extern int          Mode = 0;

extern bool         NewOrderAllowed = true;

extern bool         EnableHourTrade = true;
extern int          HourTradeStartTime  = 18;
extern int          HourTradeStopTime   = 23;

extern double       Lots = 0.1;
extern double       MaxLots = 0;

extern bool         FreeMarginControlLots = false;
extern double       BaseLots = 0.1;
extern double       BaseFreeMargin = 2000;   // example, evry 2000 USD make 0.1 Lot
extern int          Slippage = 1;

extern bool         EnableIndicatorManager  = true;
extern bool         EnableOrderCloseManager = true;
extern bool         EnableTrailingStopManager = false;
extern double       ATRLongStopRatio = 100;
extern double       ATRShortStopRatio = 100;


extern double       BTS_TP = 50;             //TP = 30 - 150
extern double       BTS_SL = 50;             //SL = 10 - 100
extern int          BTS_p  = 5;              //p  = 1  - 5

//Mode = 2
extern int          Short_x1 = 100;          //x  = 1  - 200
extern int          Short_x2 = 100;          //x  = 1  - 200
extern int          Short_x3 = 100;          //x  = 1  - 200
extern int          Short_x4 = 100;          //x  = 1  - 200
extern int          Short_MA = 1;            //MA = 1  - 5     
extern double       Short_TP = 1000;         //TP = 30 - 150
extern double       Short_SL = 1000;         //SL = 10 - 100
extern int          Short_p  = 5;            //p  = 1  - 5
extern int          Short_Threshold = 100;   //Threshold = 80 - 120

//Mode = 3
extern int          Long_x1 = 100;           //x  = 1  - 200 
extern int          Long_x2 = 100;           //x  = 1  - 200
extern int          Long_x3 = 100;           //x  = 1  - 200
extern int          Long_x4 = 100;           //x  = 1  - 200
extern int          Long_MA = 1;             //MA = 1  - 5        
extern double       Long_TP = 1000;          //TP = 30 - 150
extern double       Long_SL = 1000;          //SL = 10 - 100
extern int          Long_p  = 5;             //p  = 1  - 5
extern int          Long_Threshold = 100;    //Threshold = 80 - 120

//Mode = 4
extern int          Supervisor_x1 = 100;     //x  = 1 - 200
extern int          Supervisor_x2 = 100;     //x  = 1 - 200
extern int          Supervisor_x3 = 100;     //x  = 1 - 200
extern int          Supervisor_x4 = 100;     //x  = 1 - 200
extern int          Supervisor_MA = 1;       //MA = 1 - 5
extern int          Supervisor_p  = 5;       //p  = 1 - 5
extern int          Supervisor_Threshold = 100;  //Threshold = 80 - 120

extern bool         ADXControl      = true;             // when adx0 > adx1, and above 20, can orders
extern int          ADXPeriod       = 14;
extern double       ADXThreshold    = 21;  
 
extern bool         SARControl      = true;
extern bool         SARCrossNeeded  = false;            //need enter when just SAR crossing
extern bool         MTF_SARControl  = false;            // multi time fram SAR cotrol
extern double       SAR_Step        = 0.02;
extern double       SAR_Maximum     = 0.2;

extern bool         MACDControl     = true;            // macd0,1,2 above 0 make long, vice visa
extern double       MACD_FastEMA    = 12;
extern double       MACD_SlowEMA    = 26;
extern double       MACD_SMA        = 9;
extern bool         MTF_MACDControl = false;

extern bool         MovingFiboControl = true;
extern int          MovingFiboPeriod  = 9;
extern double       MovingFiboRetrace = 0.30;
static int          MovingFiboOld     = 0;

extern bool         MAFiboControl    = true;   
extern int          MAFiboPeriod     = 21;
extern int          MAFiboShift      = 9;
//extern int        MAFiboMA_Mothod  = 3;          //"MODE_LWMA";
//extern int        MAFiboApplied_Price  = 6;      //"PRICE_WEIGHTED";
extern double       MAFiboRetrace    = 0.30;
static int          MAFiboOld        = 0;


//extern bool         OsMAControl     = false;
//extern bool         MTF_OsMAControl = false;

extern bool         MAControl       = true;
extern int          SMAPeriod       = 21;
extern int          LWMAPeriod      = 21;
extern int          MAThreshold     = 1;


extern bool         ATR_TpSl_Enable = true;
extern int          ATR_Period      = 14;

extern bool         CandleControl   = true;           // make long only after new high, short after new low

extern bool         FractalsControl = false;

extern bool         AOControl       = false;

extern bool         ACControl       = false;

extern bool         XOControl       = true;
extern bool         XOCrossNeeded   = false;
extern double       XOBoxSize       = 20;


extern bool         OsEURControl    = false;
extern int          MA_Period       = 12;
extern int          MA_Shift        = 5;
extern int          Price           = 6;              //Weighted close price, (high+low+close+close)/4
extern int          MAMode          = 3;              //Linear weighted moving average.

extern bool         IchimokuControl = false;
extern int          Tenkan_Sen       = 9;
extern int          Kijun_Sen        = 26;
extern int          SenkouSpan_B     = 52;

static int          prevtime = 0;
static double       SL = 10;
static double       TP = 10;

//#define             LONGDIRECTION 1
//#define             SHORTDIRECTION -1

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   int i,total;
   double ATR;
   
   if (Time[0] == prevtime)
   {
      return(0);
   }
   
   prevtime = Time[0];
   
   if (! IsTradeAllowed() )
   {
      again();
      return(0);
   }
   
   if ( NewOrderAllowed == false )
   {
      return(0);
   }
   
   
   if ( EnableTrailingStopManager == true )
   {
      TrailingStopManager();
   }
   
   
   if ( EnableHourTrade == true )
   {
      if ( HourTradeStartTime < HourTradeStopTime )
      {
         //example, trade just in 19->20->21->22
         if (Hour() < HourTradeStartTime || Hour() > HourTradeStopTime )
         {
            return(0);
         }
      }
      
      else if ( HourTradeStartTime > HourTradeStopTime )
      {
         //example, trade just in 22->23->0->1
         if ( Hour() < HourTradeStartTime && Hour() > HourTradeStopTime )
         {
            return(0);
         }
         
      }
      
      else if ( HourTradeStartTime == HourTradeStopTime )
      {
         return(0);
      }
   }
   
   
   if ( EnableOrderCloseManager == true )
   {
      OrderCloseManager();
   }
   
   
   //initial TP/SL set to BTS_TP/BTS_SL
   
   if(ATR_TpSl_Enable == true)
   {
      ATR = iATR(NULL,0,ATR_Period,0);
      TP  = NormalizeDouble(4.0*ATR*BTS_TP/100.0,Digits);
      SL  = NormalizeDouble(3.0*ATR*BTS_SL/100.0, Digits);
   }
   
   else
   {
      TP = BTS_TP*Point;
      SL = BTS_SL*Point;
   }
   
   int ticket = -1;
   
   RefreshRates();
   
   double NewLots = Lots;
   if(FreeMarginControlLots == true)
   {
     NewLots = GetNewLots();
   }
   
   double Supervisor = Supervisor();
   if (Supervisor > 0)
   {
      
      //close reversed orders when doing Grid
      total = OrdersTotal();
      for (i = 0; i < total; i++)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberGrid)
         {
            if ( OrderType() == OP_SELL)
            {
               //OrderClose(OrderTicket(),OrderLots(),Ask,1,CLR_NONE);
            }
         }
      }
      
      // if one long positions already exist, do not make new orders
      total = OrdersTotal();
      for (i = 0; i < total; i++)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberAI)
         {
            if ( OrderType() == OP_BUY)
            return(0);
         }
      }
      
      //run here, it means no open long positions, so create one
      
      ticket = OrderSend(Symbol(), OP_BUY, NewLots, Ask, Slippage, Bid - SL, Bid + TP, WindowExpertName(), MagicNumberAI, 0, Blue); 
      if (ticket < 0)
      {
         again();      
      }
   }
   
   else if (Supervisor < 0)
   {
      //close reversed orders when doing Grid
      total = OrdersTotal();
      for (i = 0; i < total; i++)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberGrid)
         {
            if ( OrderType() == OP_BUY)
            {
               //OrderClose(OrderTicket(),OrderLots(),Bid,1,CLR_NONE);
            }
         }
      }
      
      // if short positions already exist, do not make new orders
      total = OrdersTotal();
      for (i = 0; i < total; i++)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberAI)
         {
            if ( OrderType() == OP_SELL)
            return(0);
         }
      }
      
      //run here, it means no open long positions, so create one
      
      ticket = OrderSend(Symbol(), OP_SELL, NewLots, Bid, Slippage, Ask + SL, Ask - TP, WindowExpertName(), MagicNumberAI, 0, Red); 
      if (ticket < 0)
      {
         again();
      }
   }
   
   else if (Supervisor == 0)
   {
      BasicTradingSystem(); // BTS, make both long and short orders at the same time
   }
   
//-- Exit --
   return(0);
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//Supervisor filter
double Supervisor()
{
   double ATR;
   if (Mode == 4)
   {
      if (Supervisor_Perceptron() > 0)
      {
         // Perceptron Long
         if ( Long_Perceptron() > 0 && ( !EnableIndicatorManager || IndicatorManager(1) == true) )
         {
            if(ATR_TpSl_Enable == true)
            {
               ATR = iATR(NULL,0,ATR_Period,0);
               TP  = NormalizeDouble(4.0*ATR*Long_TP/100.0,Digits);
               SL  = NormalizeDouble(3.0*ATR*Long_SL/100.0, Digits);
            }
            
            else
            {
               TP = Long_TP*Point;
               SL = Long_SL*Point;
            }
            
            return(1);
         }
      }
      
      // Perceptron Short
      else if ( Short_Perceptron() < 0 && ( !EnableIndicatorManager || IndicatorManager(-1) == true) )
         {
            if(ATR_TpSl_Enable == true)
            {
               ATR = iATR(NULL,0,ATR_Period,0);
               TP  = NormalizeDouble(4.0*ATR*Short_TP/100.0,Digits);
               SL  = NormalizeDouble(3.0*ATR*Short_SL/100.0, Digits);
            }
            
            else
            {
               TP = Short_TP*Point;
               SL = Short_SL*Point;
            }
            
            return(-1);
         }
      
      else
         return(0);
   }


   if (Mode == 3)
   {
      if ( Long_Perceptron() > 0 && ( !EnableIndicatorManager || IndicatorManager(1) == true) )
      {
         if(ATR_TpSl_Enable == true)
            {
               ATR = iATR(NULL,0,ATR_Period,0);
               TP  = NormalizeDouble(4.0*ATR*Long_TP/100.0,Digits);
               SL  = NormalizeDouble(3.0*ATR*Long_SL/100.0, Digits);
            }
            
            else
            {
               TP = Long_TP*Point;
               SL = Long_SL*Point;
            }
            
            return(1);
      }
      
      else
         return(0);
   }


   if (Mode == 2)
   {
      if ( Short_Perceptron() < 0 && ( !EnableIndicatorManager || IndicatorManager(-1) == true) )
      {
         if(ATR_TpSl_Enable == true)
            {
               ATR = iATR(NULL,0,ATR_Period,0);
               TP  = NormalizeDouble(4.0*ATR*Short_TP/100.0,Digits);
               SL  = NormalizeDouble(3.0*ATR*Short_SL/100.0, Digits);
            }
            
            else
            {
               TP = Short_TP*Point;
               SL = Short_SL*Point;
            }
            
            return(-1);
      }
      
      else
         return(0);
   }
   
   if (Mode == 1)
   {
      return(0);
   }

   //Mode = 0 in none AI mode,that means just use indicators to determine buy or sell
   if (Mode == 0)
   {
      if ( ( EnableIndicatorManager == true && IndicatorManager(1) == true) )
      {
         if(ATR_TpSl_Enable == true)
            {
               ATR = iATR(NULL,0,ATR_Period,0);
               TP  = NormalizeDouble(4.0*ATR*Long_TP/100.0,Digits);
               SL  = NormalizeDouble(3.0*ATR*Long_SL/100.0, Digits);
            }
            
            else
            {
               TP = Long_TP*Point;
               SL = Long_SL*Point;
            }
            
            return(1);
      }

      else if ( ( EnableIndicatorManager == true && IndicatorManager(-1) == true) )
         {
            if(ATR_TpSl_Enable == true)
            {
               ATR = iATR(NULL,0,ATR_Period,0);
               TP  = NormalizeDouble(4.0*ATR*Short_TP/100.0,Digits);
               SL  = NormalizeDouble(3.0*ATR*Short_SL/100.0, Digits);
            }
            
            else
            {
               TP = Short_TP*Point;
               SL = Short_SL*Point;
            }
            
            return(-1);
         }
         
      return(0);  
   }   
   
   return(0);  // Mode nothing, just return 0 by default
   
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//short training
double Short_Perceptron()
{
   double   w1 = Short_x1 - 100;
   double   w2 = Short_x2 - 100;
   double   w3 = Short_x3 - 100;
   double   w4 = Short_x4 - 100;
   
   double   a1 = iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_CLOSE,   0         ) - iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_WEIGHTED,Short_p   );
   double   a2 = iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_WEIGHTED,Short_p   ) - iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_WEIGHTED,Short_p*2 );
   double   a3 = iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_WEIGHTED,Short_p*2 ) - iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_WEIGHTED,Short_p*3 );
   double   a4 = iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_WEIGHTED,Short_p*3 ) - iMA( NULL,0,Short_MA,0,MODE_LWMA,PRICE_WEIGHTED,Short_p*4 );
   
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4 + Short_Threshold - 100 );
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//long training1
double Long_Perceptron()
{
   double   w1 = Long_x1 - 100;
   double   w2 = Long_x2 - 100;
   double   w3 = Long_x3 - 100;
   double   w4 = Long_x4 - 100;
   
   double   a1 = iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_CLOSE,   0        ) - iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_WEIGHTED,Long_p   );
   double   a2 = iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_WEIGHTED,Long_p   ) - iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_WEIGHTED,Long_p*2 );
   double   a3 = iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_WEIGHTED,Long_p*2 ) - iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_WEIGHTED,Long_p*3 );
   double   a4 = iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_WEIGHTED,Long_p*3 ) - iMA( NULL,0,Long_MA,0,MODE_LWMA,PRICE_WEIGHTED,Long_p*4 );
   
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4 + Long_Threshold - 100 );
   
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//long training2
double Supervisor_Perceptron()
{
   double   w1 = Supervisor_x1 - 100;
   double   w2 = Supervisor_x2 - 100;
   double   w3 = Supervisor_x3 - 100;
   double   w4 = Supervisor_x4 - 100;
   
   double   a1 = iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_CLOSE,   0              ) - iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_WEIGHTED,Supervisor_p   );
   double   a2 = iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_WEIGHTED,Supervisor_p   ) - iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_WEIGHTED,Supervisor_p*2 );
   double   a3 = iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_WEIGHTED,Supervisor_p*2 ) - iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_WEIGHTED,Supervisor_p*3 );
   double   a4 = iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_WEIGHTED,Supervisor_p*3 ) - iMA( NULL,0,Supervisor_MA,0,MODE_LWMA,PRICE_WEIGHTED,Supervisor_p*4 );
   
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4 + Supervisor_Threshold - 100 );

}

//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//BTS
double BasicTradingSystem()
{
   //return(iCCI(Symbol(), 0, BTS_p, PRICE_OPEN, 0));
   bool Flag_Long = false;
   bool Flag_Short = false;
   int  total = OrdersTotal();
  
   for (int i = 0; i < total; i++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberGrid)
      {
         if ( OrderType() == OP_BUY)
         {
            Flag_Long = true;
         }
         
         if (OrderType() == OP_SELL)
         {
            Flag_Short = true;
         }
      }
   }
   
   if (Flag_Long == false)
   {
      //OrderSend(Symbol(), OP_BUY, lots, Ask, Slippage, Bid - SL * Point, Bid + TP * Point, WindowExpertName(), MagicNumberGrid, 0, Blue);
   }

   if (Flag_Short == false)
   {
      //OrderSend(Symbol(), OP_SELL, lots, Bid, Slippage, Ask + SL * Point, Ask - TP * Point, WindowExpertName(), MagicNumberGrid, 0, Red); 
   }
   
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//try again
void again()
{
   prevtime = Time[1];
   Sleep(30000);
}

//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//Control All Inidicator results
bool IndicatorManager(int Direction)
{
   if (Direction == 1)
   {
      return( (!IchimokuControl     || IchimokuConfirm()    == 1) && 
              (!ADXControl          || ADXConfirm()         == 1) && 
              (!MACDControl         || MACDConfirm()        == 1) && 
              (!SARControl          || SARConfirm()         == 1) &&
              (!MovingFiboControl   || MovingFiboConfirm()  == 1) &&
              (!MAFiboControl       || MAFiboConfirm()      == 1) && 
              (!XOControl           || XOConfirm()          == 1) &&
              (!CandleControl       || CandleConfirm(1)     == 1) &&
              (!MAControl           || MAConfirm()          == 1) &&
              (!FractalsControl     || FractalsConfirm(1)   == 1) &&
              (!AOControl           || AOConfirm()          == 1) &&
              (!ACControl           || ACConfirm()          == 1) &&
//            (!OsMAControl         || OsMAConfirm()        == 1) &&
              (!OsEURControl        || OsEURConfirm()       == 1) );
   }
   
   if (Direction == -1)
   {
      return( (!IchimokuControl     || IchimokuConfirm()    == -1) && 
              (!ADXControl          || ADXConfirm()         == -1) && 
              (!MACDControl         || MACDConfirm()        == -1) && 
              (!SARControl          || SARConfirm()         == -1) &&
              (!MovingFiboControl   || MovingFiboConfirm()  == -1) && 
              (!MAFiboControl       || MAFiboConfirm()      == -1) && 
              (!XOControl           || XOConfirm()          == -1) &&
              (!CandleControl       || CandleConfirm(-1)    == -1) &&
              (!MAControl           || MAConfirm()          == -1) &&
              (!FractalsControl     || FractalsConfirm(-1)  == -1) &&
              (!AOControl           || AOConfirm()          == -1) &&
              (!ACControl           || ACConfirm()          == -1) &&
//            (!OsMAControl         || OsMAConfirm()        == -1) &&
              (!OsEURControl        || OsEURConfirm()       == -1) );
   }
   
   return(false);
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//ADX filter
int ADXConfirm()
{
// add this if for speed up test
if (ADXControl == true)
{
   double ADX0 = iADX(NULL,0,ADXPeriod,PRICE_OPEN,MODE_MAIN,0);
// double ADX1 = iADX(NULL,0,ADXPeriod,PRICE_OPEN,MODE_MAIN,1);
   
   double ADX_PLUSDI0  = iADX(NULL,0,ADXPeriod,PRICE_OPEN,MODE_PLUSDI,0);
   double ADX_MINUSDI0 = iADX(NULL,0,ADXPeriod,PRICE_OPEN,MODE_MINUSDI,0);
   
   if ( (ADX0 >= ADXThreshold) && (ADX_PLUSDI0 > ADX_MINUSDI0) )// && (ADX0 >= ADX1) )
   {
      return(1);
   }
   
   if ( (ADX0 >= ADXThreshold) && (ADX_PLUSDI0 < ADX_MINUSDI0) )// && (ADX0 >= ADX1) )
   {
      return(-1);
   }
   
}
   return(0);
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//MACD filter
int MACDConfirm()
{
// add this if for speed up test
if (MACDControl == true)
{
   double MACD0, MACD1, SIGNAL0, MACD0_H4, SIGNAL0_H4;

   MACD0   = iMACD(NULL,0,MACD_FastEMA,MACD_SlowEMA,MACD_SMA,PRICE_OPEN,MODE_MAIN,0);
   MACD1   = iMACD(NULL,0,MACD_FastEMA,MACD_SlowEMA,MACD_SMA,PRICE_OPEN,MODE_MAIN,1);
   SIGNAL0 = iMACD(NULL,0,MACD_FastEMA,MACD_SlowEMA,MACD_SMA,PRICE_OPEN,MODE_SIGNAL,0);
   
   MACD0_H4   = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_OPEN,MODE_MAIN,0);
   SIGNAL0_H4 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_OPEN,MODE_SIGNAL,0);
   
   if ( MACD0 > MACD1 && MACD0 > SIGNAL0 && MACD0 > 0 && (!MTF_MACDControl || MACD0_H4 > SIGNAL0_H4) )
   {
      return(1);
   }
   
   if ( MACD0 < MACD1 && MACD0 < SIGNAL0 && MACD0 < 0 && (!MTF_MACDControl || MACD0_H4 < SIGNAL0_H4) )
   {
      return(-1);
   }

}

   return(0);
}

//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//OsMA filter
//OsMA is actually iMACD(NULL,0,12,26,9,PRICE_OPEN,MODE_MAIN,0)-iMACD(NULL,0,12,26,9,PRICE_OPEN,MODE_SIGNAL,0);;
//int OsMAConfirm()
//{
// add this if for speed up test
//if (OsMAControl == true)
//{
//
//   double OsMA0    = iOsMA(NULL,0,12,26,9,PRICE_OPEN,0);
//   
//   double OsMA0_H4 = iOsMA(NULL,PERIOD_H4,12,26,9,PRICE_OPEN,0);
//   
//   if ( OsMA0 > 0 && (!MTF_OsMAControl || OsMA0_H4 > 0 ) )
//   {
//      return(1);
//   }
//   
//   if ( OsMA0 < 0 && (!MTF_OsMAControl || OsMA0_H4 < 0 ) )
//   {
//      return(-1);
//   }
//
//}
//   
//   return(0);
//}

//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//SAR filter
int SARConfirm()
{ 

// add this if for speed up test
if (SARControl == true)
{
   double SAR0    = iSAR(NULL,0,SAR_Step,SAR_Maximum,0);
   double SAR1    = iSAR(NULL,0,SAR_Step,SAR_Maximum,1);
   double SAR2    = iSAR(NULL,0,SAR_Step,SAR_Maximum,2);
   
   double SAR0_H4 = iSAR(NULL,PERIOD_H4,SAR_Step,SAR_Maximum,0);
   
   //indicate long
   double Low0_H4 = iLow(NULL,PERIOD_H4,0); 
   
   if( SAR0 < Low[0] && SAR1 < Close[1] && (!SARCrossNeeded || SAR2 > High[2]) && (!MTF_SARControl || SAR0_H4 < Low0_H4) )
   {
      return(1);
   }
   
   // indicate short
   double High0_H4 = iHigh(NULL,PERIOD_H4,0);
   
   if( SAR0 > High[0] && SAR1 > Close[1] && (!SARCrossNeeded || SAR2 < Low[2]) && (!MTF_SARControl || SAR0_H4 >High0_H4) )
   {
      return(-1);
   }
   
}
   
   return(0);
   
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//Candle filter
int CandleConfirm(int Direction)
{

// add this if for speed up test
if (CandleControl == true)
{
 
  if (Direction == 1 )
   {
      if ( High[1] > High[2] )// && Close[1] > Open[1] )// && Low1 > Low2 )
      {
         return(1);
      }
   }
   
  if (Direction == -1 )
   {
      if ( Low[1] < Low[2] )// && Close[1] < Open[1] )// && High1 < High2 )
      {
         return(-1);
      }
   }
   
}
   
   return(0);
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//MA filter
int MAConfirm()
{

// add this if for speed up test
if (MAControl == true)
{
   double LWMA  = iMA(NULL,0,LWMAPeriod,0,MODE_LWMA,PRICE_WEIGHTED,1);
   double SMA   = iMA(NULL,0,SMAPeriod,0,MODE_SMA,PRICE_CLOSE,1);
   double Delta = LWMA - SMA;
   
   if ( Delta > MAThreshold*Point )
   {
      return(1);
   }
   
   if ( (Delta < 0) && (MathAbs(Delta) > MAThreshold*Point) )
   {
      return(-1);
   }
   
}
   
   return(0);
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//Fractal filter
int FractalsConfirm(int Direction)
{

// add this if for speed up test
if (FractalsControl == true)
{

   if ( Direction == 1 )
   {
      double FractalsUpper = iFractals(NULL,0,MODE_UPPER,3);
      
      if ( FractalsUpper == 0 || High[0] > FractalsUpper )
      {
         return(1);
      }
   }
   
   if ( Direction == -1 )
   {
      double FractalsLower = iFractals(NULL,0,MODE_LOWER,3);
      
      if ( FractalsLower == 0 || Low[0] < FractalsLower )
      {
         return(-1);
      }
   }
   
}

   return(0);
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//AO filter
//AO is actually iMACD(NULL,0,5,34,5,PRICE_MEDIAN,MODE_MAIN,0);
int AOConfirm()
{

// add this if for speed up test
if (AOControl == true)
{

   double AO0 = iAO(NULL,0,0);
   double AO1 = iAO(NULL,0,1);
   
   if ( AO0 > 0 )// && AO0 > AO1)
   {
      return(1);
   }
   
   if ( AO0 < 0 )// && AO0 < AO1)
   {
      return(-1);
   }
   
}   

   return(0);
}

//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//AC filter
//AC is actually AO-SMA(AO, 5), so it equals to iMACD(NULL,0,5,34,5,PRICE_MEDIAN,MODE_MAIN,0)-iMACD(NULL,0,5,34,5,PRICE_MEDIAN,MODE_SIGNAL,0);;
int ACConfirm()
{

// add this if for speed up test
if (ACControl == true)
{

   double AC0 = iAC(NULL,0,0);
   //double AC1 = iAC(NULL,0,1);
   
   if (AC0 > 0) //&& AC0 > AC1)
   {
      return(1);
   }
   
   if (AC0 < 0) //&& AC0 < AC1)
   {
      return(-1);
   }
   
}
   
   return(0);
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//Ichimoku filter
int IchimokuConfirm()
{

// add this if for speed up test
if (IchimokuControl == true)
{
      //the BaseLine ( KIJUN SEN ) is a calculation of the ( highest high + lowest low )/2 over 26(Kijun_Sen) periods.
   
   double BaseLine       = iIchimoku(NULL,0,Tenkan_Sen,Kijun_Sen,SenkouSpan_B,MODE_KIJUNSEN,   0);
   
      //the ConversionLine is the same calc, ( highest high + lowest low )/2 but over 9(Tenkan_Sen) periods.
   
   double ConversionLine = iIchimoku(NULL,0,Tenkan_Sen,Kijun_Sen,SenkouSpan_B,MODE_TENKANSEN,  0);
   
      //the DelayLine is the closing price 26(Kijun_Sen) periods behind the current period.
   
   double DelayLine      = iIchimoku(NULL,0,Tenkan_Sen,Kijun_Sen,SenkouSpan_B,MODE_CHINKOUSPAN,Kijun_Sen);
   
      //the LeadingSpanA is the ( ConversionLine + BaseLine )/2 with the results displaced forward by 26(Kijun_Sen) periods
   
   double LeadingSpanA   = iIchimoku(NULL,0,Tenkan_Sen,Kijun_Sen,SenkouSpan_B,MODE_SENKOUSPANA,0);
   
      //the LeadingSpanB is the ( highest high + lowest low )/2 over the past 52(SenkouSpan_B) periods and displaced forward 26(Kijun_Sen) periods.
   
   double LeadingSpanB   = iIchimoku(NULL,0,Tenkan_Sen,Kijun_Sen,SenkouSpan_B,MODE_SENKOUSPANB,0);
   
   if ( (ConversionLine > BaseLine) )// && ( (Close[0] > LeadingSpanA) && (Close[0] > LeadingSpanB) ) && (DelayLine >= Close[Kijun_Sen]) )
   {
      return(1);
   }
   
   if ( (ConversionLine < BaseLine) )// && ( (Close[0] < LeadingSpanA) && (Close[0] < LeadingSpanB) ) && (DelayLine <= Close[Kijun_Sen]) )
   {
      return(-1);
   }
   
}

   return(0);
   
}
//---------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------
//OsEUR filter
int OsEURConfirm()
{

// add this if for speed up test
if (OsEURControl == true)
{

   int i = 0;
   double EURIndex;
   EURIndex = (iMA("EURUSD",0,MA_Period,0,MAMode,Price,i) - iMA("EURUSD",0,MA_Period,0,MAMode,Price,i+MA_Shift)) * MathPow(10,MarketInfo("EURUSD",MODE_DIGITS)) +
              (iMA("EURGBP",0,MA_Period,0,MAMode,Price,i) - iMA("EURGBP",0,MA_Period,0,MAMode,Price,i+MA_Shift)) * MathPow(10,MarketInfo("EURGBP",MODE_DIGITS)) +
              (iMA("EURCHF",0,MA_Period,0,MAMode,Price,i) - iMA("EURCHF",0,MA_Period,0,MAMode,Price,i+MA_Shift)) * MathPow(10,MarketInfo("EURCHF",MODE_DIGITS)) +
              (iMA("EURJPY",0,MA_Period,0,MAMode,Price,i) - iMA("EURJPY",0,MA_Period,0,MAMode,Price,i+MA_Shift)) * MathPow(10,MarketInfo("EURJPY",MODE_DIGITS)) +
              (iMA("EURCAD",0,MA_Period,0,MAMode,Price,i) - iMA("EURCAD",0,MA_Period,0,MAMode,Price,i+MA_Shift)) * MathPow(10,MarketInfo("EURCAD",MODE_DIGITS)) +
              (iMA("EURAUD",0,MA_Period,0,MAMode,Price,i) - iMA("EURAUD",0,MA_Period,0,MAMode,Price,i+MA_Shift)) * MathPow(10,MarketInfo("EURAUD",MODE_DIGITS)) ;
             
   if (EURIndex > 0 )
   {
      return(1);
   }
   
   if (EURIndex < 0 )
   {
      return(-1);
   }
   
}
   
   return(0);   
}
//---------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------
//XO filter
int XOConfirm()
{
//for speed up test
if( XOControl == true )
{
   int CurrentBar;
   int Counter = 0;
   double X_High,O_Low,CurrentPrice;
   double XO_UpFlag,XO_DownFlag,XO_UpFlag2,XO_DownFlag2;
   
   XO_UpFlag = 0;
   XO_DownFlag = 0;
   
   XO_UpFlag2 = 0;
   XO_DownFlag2 = 0;
   
   for(int i=200;i>=1;i--)
   {
      XO_UpFlag2 = XO_UpFlag;
      XO_DownFlag2 = XO_DownFlag;
      
      CurrentBar = i;
      
      if (Counter<1)
      {
         X_High=Close[CurrentBar];
         O_Low =Close[CurrentBar];
         Counter=1;
      }
      
      CurrentPrice=Close[CurrentBar];
      
      if (CurrentPrice>(X_High+XOBoxSize*Point))
      {
         Counter +=1;
         X_High = CurrentPrice;
         O_Low  = CurrentPrice-XOBoxSize*Point;
         XO_UpFlag = 1;
         XO_DownFlag = 0;
      }
      
      if (CurrentPrice<(O_Low-XOBoxSize*Point))
      {
         Counter += 1;
         O_Low  = CurrentPrice;
         X_High = CurrentPrice+XOBoxSize*Point;
         XO_UpFlag = 0;
         XO_DownFlag = 1;
      }
      
   }
   
   if((XO_UpFlag == 1) && ( !XOCrossNeeded || XO_DownFlag2 == 1 ) )
   {
      return(1);
   }
   
   if((XO_DownFlag == 1) && ( !XOCrossNeeded || XO_UpFlag2 == 1 ) )
   {
      return(-1);
   }

}
   return(0);
   
}
//---------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------
// Moving Fibonacci filter
int MovingFiboConfirm()
{  

//for speed up test
if ( MovingFiboControl == true )
{
   int MovingFiboNew;
   
   double MaxHigh = High[iHighest(NULL,0,MODE_HIGH,MovingFiboPeriod,1)];
   double MaxLow  = Low[iLowest(NULL,0,MODE_LOW,MovingFiboPeriod,1)];    
   
   double Risistance = MaxHigh - (MaxHigh-MaxLow)*MovingFiboRetrace;    // = High - 30%
   double Support    = MaxLow  + (MaxHigh-MaxLow)*MovingFiboRetrace;    // = Low  + 30%   
   
   MovingFiboNew = MovingFiboOld;   // if no new assign, MovingFiboNew remains the same with the old one.

   if (Close[1] > Risistance )
   {
      MovingFiboNew = 1;
   }
   
	if (Close[1] < Support )
	{
	  MovingFiboNew = -1;
	}
	
	MovingFiboOld = MovingFiboNew;   // store MovingFiboNew value to MovingFiboOld.
	
	return(MovingFiboNew);

}

   return(0);

}
//---------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------
// Moving Average Fibonacci filter
int MAFiboConfirm()
{  

//for speed up test
if ( MAFiboControl == true )
{
   int i, MAFiboNew;
   double tmp, MAMaxLow, MAMaxHigh;
   
   tmp = iMA(Symbol(),0,MAFiboPeriod,0,MODE_LWMA,PRICE_WEIGHTED,1);
   MAMaxHigh = tmp;
   MAMaxLow  = tmp;
   
   for (i=1; i<=MAFiboShift; i++ )
   {
      tmp = iMA(Symbol(),0,MAFiboPeriod,0,MODE_LWMA,PRICE_WEIGHTED,i);
      
      if ( tmp > MAMaxHigh )
      {
         MAMaxHigh = tmp;
      }
      
      if ( tmp < MAMaxLow )
      {
         MAMaxLow = tmp;
      }
      
   }
    
   double MARisistance = MAMaxHigh - (MAMaxHigh-MAMaxLow)*MAFiboRetrace;    // = MAHigh - 30%
   double MASupport    = MAMaxLow  + (MAMaxHigh-MAMaxLow)*MAFiboRetrace;    // = MALow  + 30%   
   
   MAFiboNew = MAFiboOld;   // if no new assign, MAFiboNew remains the same with the old one.

   if (Close[1] > MARisistance )
   {
      MAFiboNew = 1;
   }
   
	if (Close[1] < MASupport )
	{
	  MAFiboNew = -1;
	}
	
	MAFiboOld = MAFiboNew;   // store MAFiboNew value to MovingFiboOld.
	
	return(MAFiboNew);

}

   return(0);

}
//---------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------
//GetNewLots function
double GetNewLots()
{
   double NewLots = BaseLots;
   double FreeMarginPercentage = AccountFreeMargin()/AccountBalance();
   int FreeMarginTimes = MathFloor( (AccountFreeMargin()/BaseFreeMargin) * FreeMarginPercentage );  // how many time is present FreeMargin times BaseFreeMargin
   
   if(FreeMarginTimes >= 1)
   {
      NewLots = BaseLots*FreeMarginTimes;
   }
   
   if ( MaxLots > 0 && NewLots > MaxLots )
   {
      NewLots = MaxLots;
   }
         
   return(NewLots);
   
}
//---------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------
//OrderCloseManager function
int OrderCloseManager()
{
   int i, Flag;
   
   double SAR0 = iSAR(NULL,0,SAR_Step,SAR_Maximum,0);
   
   bool CloseSELLReason = ( SAR0 < Low[0] ) ;// || ( Low[1] > Low[3] && Low[2] > Low[3] );
   
   bool CloseBUYReason  = ( SAR0 > High[0] ) ;// || ( High[1] < High[3] && High[2] < High[3] );
   
   if ( CloseSELLReason == true)
   {
      int total = OrdersTotal();
      for (i = 0; i < total; i++)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberAI)
         {
            if ( OrderType() == OP_SELL)
            {
               OrderClose(OrderTicket(),OrderLots(),Ask,1,CLR_NONE);
               Flag = 1;
            }
         }
      }   
   }
   
   if (CloseBUYReason == true )
   {
      total = OrdersTotal();
      for (i = 0; i < total; i++)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberAI)
         {
            if ( OrderType() == OP_BUY)
            {
               OrderClose(OrderTicket(),OrderLots(),Bid,1,CLR_NONE);
               Flag = -1;
            }
         }
      }   
   }
   
   return(Flag);
   
}
//---------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------
// Trailing Stop function: trailing by ATR
int TrailingStopManager()
{
   int Counter = 0;
   
   for(int i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS);
      
      // long trailing stop
      if(OrderType() == OP_BUY)
      {
         int shifttime = iBarShift(Symbol(),0,OrderOpenTime(),0);
         
         //Print("Longshifttime:::::::::::",shifttime);
         
         double NewMaxHigh = High[iHighest(NULL,0,MODE_HIGH,shifttime,0)];
         double NewLongStopLoss = NewMaxHigh - iATR(NULL,0,14,0)*3.0*ATRLongStopRatio/100.0;
         
         //Print("NewMaxHigh::::::::::::::::",NewMaxHigh);
         //Print("NewLongStopLoss::::::::::::::::::::::",NewLongStopLoss);
         //Print("NormalizeDouble(NewLongStopLoss - OrderStopLoss(), Digits):::::::::::::::::::",NormalizeDouble(NewLongStopLoss - OrderStopLoss(), Digits));
         
         if(NormalizeDouble(NewLongStopLoss - OrderOpenPrice(), Digits) > 0)
         {
            if(NormalizeDouble(NewLongStopLoss - OrderStopLoss(), Digits) > 0)
            {
               Print("................................");
               OrderModify(OrderTicket(),OrderOpenPrice(),NewLongStopLoss,OrderTakeProfit(),OrderExpiration(),CLR_NONE);
               Counter += 1;
            }
         }
         
      } // Long orders trailing stop end here
      
      // short trailing stop
      if(OrderType() == OP_SELL)
      {
         shifttime = iBarShift(Symbol(),0,OrderOpenTime(),0);
         
         //Print("Shortshifttime:::::::::::",shifttime);
         
         double NewMaxLow = Low[iLowest(NULL,0,MODE_LOW,shifttime,0)];
         double NewShortStopLoss = NewMaxLow + iATR(NULL,0,14,0)*3.0*ATRShortStopRatio/100.0;
         
         //Print("NewMaxLow::::::::::::::::",NewMaxLow);
         //Print("NewsShortStopLoss::::::::::::::::::::::",NewShortStopLoss);
         //Print("NormalizeDouble(NewShortStopLoss - OrderStopLoss(), Digits):::::::::::::::::::",NormalizeDouble(NewShortStopLoss - OrderStopLoss(), Digits));
         
         if(NormalizeDouble(NewShortStopLoss - OrderOpenPrice(), Digits) < 0)
         {
            if(NormalizeDouble(NewShortStopLoss - OrderStopLoss(), Digits) < 0)
            {
               Print("................................");
               OrderModify(OrderTicket(),OrderOpenPrice(),NewShortStopLoss,OrderTakeProfit(),OrderExpiration(),CLR_NONE);
               Counter += 1;
            }
         }
         
      } // sell orders trailing stop end here
   
      
   }// for end here
   
   return(Counter);
}  
//---------------------------------------------------------------------------------------------------------------



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains the lowest prices of each bar
Series array that contains close prices for each bar
Series array that contains the highest prices of each bar


Indicator Curves created:


Indicators Used:

Indicator of the average true range
Moving average indicator
Movement directional index
MACD Histogram
Parabolic Stop and Reverse system
Fractals
Bill Williams Awesome oscillator
Bill Williams Accelerator/Decelerator oscillator
Ichimoku Kinko Hyo


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It automatically opens orders when conditions are reached
It Closes Orders by itself
It can change open orders parameters, due to possible stepping strategy

Other Features:

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCHF on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

Request Backtest for Auto_RXD


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: