Tester Expert 1 TimeFrame Indicators





//+------------------------------------------------------------------+
//|                         Tester Expert 1 TimeFrame Indicators.mq4 |
//|                                                  Edi Dimitrovski |
//|                                              http://maktrade.org |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property show_inputs
//---- input
extern int TakeProfit = 100; 
extern int Stop = 1000; 
extern int Trailing = 40; 
extern int BreakOut = 1; 
extern int PlusSafetyPercent = 1000;
extern int magicNumber = 5;
extern int slip = 5;
extern bool safe = true;
extern int IndSignal=2;
extern bool IndSlow=false; 
extern double IndFilterPoints=5.0;
extern bool IndRepaint=true;
extern bool CloseOnlyWithProfit=false;
//---- var
int in[7] ;
double do[6];
//+------------------------------------------------------------------+

int init()
  {
   clear();
   Print("Starting Lots = ",Lots()*MarketInfo(Symbol(),23));
   if ( in[6] > TakeProfit || in[6] > Stop || in[6] > Trailing ) 
    Print("TakeProfit, Trailing or Stop is smaller than allowed, should be minimum = ",in[6]);
   return(0);
  }

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

int start()
  {
// common functions  

   bool up, dn, acc = (AccountFreeMargin()>0.0), test = IsTesting();  
   double fast0, slow0, fast1, fast1p, slow1, slow1p, fast2, fast2p, slow2, slow2p;
    
// new period selection

   in[1] = Time[0];
   do[1] = Close[0]; 
   if ( in[0] < in[1] ) clear(); // update new bar time
     
// order selection
   
    fast0 = iCustom(NULL,60,"Combined MA Signal",(IndSignal*3),IndSlow,IndFilterPoints,IndRepaint,2,0);
    slow0 = iCustom(NULL,60,"Combined MA Signal",(IndSignal*3),IndSlow,IndFilterPoints,IndRepaint,3,0);

   if(!test)
   {
    fast1 = iCustom(NULL,240,"Combined MA Signal",(IndSignal*2),IndSlow,IndFilterPoints,IndRepaint,0,0);
    fast1p= iCustom(NULL,240,"Combined MA Signal",(IndSignal*2),IndSlow,IndFilterPoints,IndRepaint,0,1);
    slow1 = iCustom(NULL,240,"Combined MA Signal",(IndSignal*2),IndSlow,IndFilterPoints,IndRepaint,1,0);
    slow1p = iCustom(NULL,240,"Combined MA Signal",(IndSignal*2),IndSlow,IndFilterPoints,IndRepaint,1,1);
    
    fast2 = iCustom(NULL,1440,"Combined MA Signal",IndSignal,IndSlow,IndFilterPoints,IndRepaint,0,0);
    fast2p= iCustom(NULL,1440,"Combined MA Signal",IndSignal,IndSlow,IndFilterPoints,IndRepaint,0,1);
    slow2 = iCustom(NULL,1440,"Combined MA Signal",IndSignal,IndSlow,IndFilterPoints,IndRepaint,1,0);
    slow2p = iCustom(NULL,1440,"Combined MA Signal",IndSignal,IndSlow,IndFilterPoints,IndRepaint,1,1);
   }
   
   in[4] = -1;
    // buy
   if ( fast0 > 0.0 )
   {
    up = ((slow1>slow1p || slow1>0.0) && fast1>fast1p+Point*IndFilterPoints && (fast1>0.0 || !IndSlow));
    if ( !safe || ( safe && ( test || up ) ) ) // buy condition H4
    {
     close(1); // close sell condition live or buy condition for testing
     if ( in[2] < 1 && // no opened buy possition on the current bar
       ( test || ( up && (slow2>slow2p || slow2>0.0) && fast2>fast2p+Point*IndFilterPoints && (fast2>0.0 || !IndSlow) ) ) ) // buy condition D1
     in[4] = 0 ; // order selection is OP_BUY
    }
   }
    // sell
   else if ( slow0 < 0.0 )
   {
    dn = ((slow1<slow1p || slow1<0.0) && fast1+Point*IndFilterPoints<fast1p && (fast1<0.0 || !IndSlow));
    if ( !safe || ( safe && ( test || dn ) ) ) // sell condition H4
    {
     close(0); // close buy condition live or sell condition for testing
     if ( in[3] < 1 && // no opened sell possition on the current bar
      ( test || ( dn && (slow2<slow2p || slow2<0.0) && fast2+Point*IndFilterPoints<fast2p && (fast2<0.0 || !IndSlow) ) ) ) // sell condition D1
     in[4] = 1 ; // order selection is OP_SELL
    }
   }

// orders part
   
   if ( acc )
   {
    if ( in[4] == 0 && do[2]<AccountFreeMargin()/MarketInfo(Symbol(),32) ) // buy condition met = send new buy order
       in[2] = OrderSend ( Symbol() , in[4] , Lots() * do[2] , Ask , slip , 
         Bid - Stop * Point , Bid + TakeProfit * Point , "" , magicNumber , 0 , Green ) ;

    else if ( in[4] == 1 && do[2]<AccountFreeMargin()/MarketInfo(Symbol(),32) ) // sell condition met =  = send new order
       in[3] = OrderSend ( Symbol() , in[4] , Lots() * do[2] , Bid , slip , 
         Ask + Stop * Point , Ask - TakeProfit * Point , "" , magicNumber , 0 , Green ) ;
   }

// check for trailing stop

  if ( Trailing > in[6] ) trail(); // if trailing is bigger than minimum stop level do trailing

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

double Lots()
  {
// volume lots section
   do[3] = MarketInfo(Symbol(),32);
   in[5] = AccountFreeMargin();
   do[5] = in[5]/do[3]/do[2];
   return(NormalizeDouble(MathMin(MathMax(in[5]/do[3]*in[5]/
     (( TakeProfit + Stop*safe )*0.01*(1+PlusSafetyPercent)
     *(in[5]/do[3])*do[4]+in[5]),
     do[2]),MarketInfo(Symbol(),25))/do[2],0));
  }
  
//+------------------------------------------------------------------+

void clear()
  {
// new period selection clear
   do[2] = MarketInfo(Symbol(),23);
   do[4] = MarketInfo(Symbol(),16);
   in[6] = MarketInfo(Symbol(),14);
   do[0] = do[1] ;
   in[0] = in[1] ;
   in[2] = 0 ;
   in[3] = 0 ;
   return;
  }
  
//+------------------------------------------------------------------+

void close(int type)
  {
   double price;
   for ( int i=0; i<OrdersTotal(); i++)
   {
    if ( OrderSelect(i,SELECT_BY_POS) )
    {
     if ( OrderMagicNumber()==magicNumber && Symbol()==OrderSymbol() && OrderType()==type && ( OrderProfit()>0.0 || !CloseOnlyWithProfit ) )
     {
      if ( IsTesting() )
      {
       if ( type == 1 ) price = Ask;
       else price = Bid;
      }
      else price = MarketInfo(Symbol(),9+type);
      OrderClose(OrderTicket(),OrderLots(),price,slip,CLR_NONE);
     }
    }
   }
   return;
  }

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

void trail()
  {
   int spread = MarketInfo(Symbol(),13), total = OrdersTotal();
   for(int cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))continue;
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber() == magicNumber)
        {
// buy possitions
          if(BreakOut>0 && OrderStopLoss()<OrderOpenPrice())
            {
              if(Bid-OrderOpenPrice() > Point*(in[6]+spread+BreakOut))
                OrderModify(OrderTicket(),0,Bid-Point*(in[6]+spread),OrderTakeProfit(),0,Green);
            }
          if(Trailing>in[6] && Bid-OrderOpenPrice()>Point*Trailing)
            {
              if(OrderStopLoss()<Bid-Point*Trailing)
                OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Trailing,OrderTakeProfit(),0,Green);
            }
        }
      else if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber() == magicNumber)
        {
// sell possitions
          if(BreakOut>0 && OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()<Point )
            {
              if(OrderOpenPrice()-Ask > Point*(in[6]+spread+BreakOut))
                OrderModify(OrderTicket(),0,Ask+Point*(in[6]+spread),OrderTakeProfit(),0,Green);
            }       
          if(Trailing>in[6] && OrderOpenPrice()-Ask>Point*Trailing)
            {
              if(OrderStopLoss()>Ask+Point*Trailing || OrderStopLoss()<Point)
                OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*Trailing,OrderTakeProfit(),0,Green);
            }
         }
      }
    return;
  }





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:




Custom Indicators Used:
Combined MA Signal

Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
It can change open orders parameters, due to possible stepping strategy

Other Features: