Gandalf





//+------------------------------------------------------------------+
//|                                                      Gandalf.mq4 |
//|                                                          budimir |
//|                                              tartar27@bigmir.net |
//+------------------------------------------------------------------+
#property copyright "budimir"
#property link      "tartar27@bigmir.net"

//---- input parameters
//ooooooooooooooooooooooooooooooooooooooooooooooooo
extern bool      In_BUY=true;
extern int       Count_buy=24;
extern double    w=0.18;
extern int       SL_buy=62;
//ooooooooooooooooooooooooooooooooooooooooooooooooo
extern bool      In_SELL=true;
extern int       Count_sell=24;
extern double    m=0.18;
extern int       SL_sell=62;
//ooooooooooooooooooooooooooooooooooooooooooooooooo
extern int       Risk=0;
//ooooooooooooooooooooooooooooooooooooooooooooooooo
//---- other parameters
   static int  prevtime=0;
          int    ticket=0;
          int Magic_BUY  =123;
          int Magic_SELL =321;
          int x=1;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
      if(Digits == 5) x=10;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//oooooooooooooooooooooooooooooooooooooooooooooooooooo
    if (Time[0] == prevtime) return(0); 
                             prevtime = Time[0];
    if (!IsTradeAllowed()) {
     prevtime=Time[1]; MathSrand(TimeCurrent());Sleep(30000 + MathRand());
                           }
//oooooooooooooooooooooooooooooooooooooooooooooooooooo
   if( In_BUY)Trade_BUY ( Magic_BUY, Count_buy,w, SL_buy);
   if(In_SELL)Trade_SELL(Magic_SELL,Count_sell,m,SL_sell);
//oooooooooooooooooooooooooooooooooooooooooooooooooooo
   return(0);
  }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void Trade_BUY(int mn,int num,double factor,int sl) { 

         int total=OrdersTotal();
         
    for (int i = 0; i < total; i++) {   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    
                if (OrderSymbol() == Symbol() && OrderMagicNumber() == mn) { return(0);
                                                                           } 
                                    }
 //ooooooooooooooooooooooooooooooooooooooooooooooooooo
      ticket = -1;  
      
      double target=Out(mn,factor);  
 
  if (target>(Bid+15*x*Point)  && IsTradeAllowed()) { 
  
    ticket= OrderSend(Symbol(), OP_BUY,lot(Risk),Ask,5,Bid-x*sl*Point,target,DoubleToStr(mn,0),mn,0,Blue);
        
   
                 RefreshRates();   
      
              if ( ticket < 0) { Sleep(30000);   prevtime = Time[1]; } 
                                           
                                           } //-- Exit ---

       return(0); } 
       
       
 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 void Trade_SELL(int mn,int num,double factor,int sl) { 
 
         int total=OrdersTotal();
         
    for (int i = 0; i < total; i++) {   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    
                if (OrderSymbol() == Symbol() && OrderMagicNumber() == mn) { return(0);
                                                                           } 
                                    }
 //ooooooooooooooooooooooooooooooooooooooooooooooooooo
      ticket = -1; 
       
      double target=Out(mn,factor);  
 
     if (target<(Ask-15*x*Point)  && IsTradeAllowed()) { 
     
     ticket= OrderSend(Symbol(),OP_SELL,lot(Risk),Bid,5,Ask+x*sl*Point,target,DoubleToStr(mn,0),mn,0, Red);
  
   
                 RefreshRates();   
      
              if ( ticket < 0) { Sleep(30000);   prevtime = Time[1]; } 
                                           
                                           } //-- Exit ---

       return(0); } 
       
       
 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 double Out(int n,double l) {    double t[120],
                                        s[120],
                                        lm=iMA(NULL,0,n,0,MODE_LWMA,PRICE_CLOSE,1),
                                        sm=iMA(NULL,0,n,0, MODE_SMA,PRICE_CLOSE,1); 
 //ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo  
        t[n]=(6*lm-6*sm)/(n-1);s[n]=4*sm-3*lm-t[n]; 
              for (int k = n-1; k>0; k--) {    
           s[k]=l*Close[k]+(1-l)*(s[k+1]+t[k+1]);
           t[k]=l*(s[k]-s[k+1])+(1-l)*t[k+1];
                                          }//--end--for-
   return  (NormalizeDouble(s[1]+t[1],MarketInfo(Symbol(),MODE_DIGITS)));}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
double lot(int R) { double minlot = MarketInfo(Symbol(), MODE_MINLOT);
                       int o = MathAbs(MathLog(minlot) *0.4343) + 0.5;  
                  double lot = minlot;
//ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
   lot = NormalizeDouble(AccountFreeMargin() * 0.00001*R, o);//---
   if (AccountFreeMargin() < lot * MarketInfo(Symbol(), MODE_MARGINREQUIRED)) {
   lot = NormalizeDouble(AccountFreeMargin() / MarketInfo(Symbol(), MODE_MARGINREQUIRED), o);
                                                                               }
//ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
   if(lot < minlot) lot = minlot;
   double maxlot =MarketInfo(Symbol(), MODE_MAXLOT);
   if(lot > maxlot) lot = maxlot;
   return(lot);    } 
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_end_film_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

 
  
      





Sample





Analysis



Market Information Used:

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


Indicator Curves created:


Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It automatically opens orders when conditions are reached

Other Features:

BackTest : EURUSD on H1

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

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

BackTest : GBPUSD on H1

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

BackTest : USDCAD on H1

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

BackTest : USDCHF on H1

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

BackTest : USDJPY on H1

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

Request Backtest for Gandalf


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

Pair: Period: