TEST-EA





//---- Trades limits
extern   double    Lots = 1;
extern   double    TakeProfit = 300;
extern   double    TrailingStop = 100; 
extern   double    StopLoss = 50;
extern   int       Slippage = 5;
extern   double    MaximumRisk        = 0.3;
extern   bool      UseMM=false;

//--- Global variables
extern int      MagicNumber = 34567;
string   ExpertComment = "TEST EA";
int      NumberOfTries = 5;

//+------------------------------------------------------------------
int init()
{
   return(0);
}

int deinit() 
{
   return(0);
}
//+------------------------------------------------------------------

bool isNewSymbol(string current_symbol)
  {
   //loop through all the opened order and compare the symbols
   int total  = OrdersTotal();
   for(int cnt = 0 ; cnt < total ; cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      string selected_symbol = OrderSymbol();
      if (current_symbol == selected_symbol && OrderMagicNumber()==MagicNumber)
      return (False);
    }
    return (True);
}

//+------------------------------------------------------------------+
int start()
  {

   
   int cnt, ticket, total,n,trend,signal;
   bool BuyCondition , SellCondition,CloseBuyCondition ,CloseSellCondition  ; 
   bool up1, up2, down1, down2;
   if(Bars<50) {Print("bars less than 50"); return(0);}

   //Symbo settings:
   RefreshRates();
   Slippage = MarketInfo(Symbol(),MODE_SPREAD);
       
      double O=iOpen(NULL,PERIOD_D1,0);
      
      double pricenow=iMA(NULL,0,2,0,MODE_EMA,PRICE_OPEN,1);

      double tsvbear=iCustom(NULL,0,"T.S.V._Bullish & Bearish",3,3000,1,1);
      double tsvbull=iCustom(NULL,0,"T.S.V._Bullish & Bearish",3,3000,2,1);
      
      double mom=iMomentum(NULL,0,24,PRICE_CLOSE,1);

      if (signal==1) BuyCondition=false;
      if (signal==-1) SellCondition=false;
      
      if ((pricenow>O && mom>100 && tsvbull>0) && signal!=1) 
      {signal=1; BuyCondition=true; CloseSellCondition=true;}
      
      if ((pricenow<O && mom<100 && tsvbear>0) && signal!=-1) 
      {signal=-1;  SellCondition=true; CloseBuyCondition=true;} 
                
      
   
   total  = OrdersTotal();
   
   if (UseMM)
   Lots=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
   
      
   if(total < 1 || isNewSymbol(Symbol())) 
     {
       if(BuyCondition) //<-- BUY condition
         {
           ticket = OpenOrder(OP_BUY); //<-- Open BUY order
            return(0);
         }
         if(SellCondition) //<-- SELL condition
         {
            ticket = OpenOrder(OP_SELL); //<-- Open SELL order
            return(0);
         }
         return(0);
     }
     
      for(cnt=0;cnt<total;cnt++) 
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

         if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
         {
            if(OrderType()==OP_BUY)   //<-- Long position is opened
            {
                  if(CloseBuyCondition) //<-- Close the order and exit! 
                  {
                     CloseOrder(OrderType()); 
                  } 
               
               TrailOrder(OrderType()); //<-- Trailling the order
            }
            if(OrderType()==OP_SELL) //<-- Go to short position
            {
                 if(CloseSellCondition) //<-- Close the order and exit! 
                  {
                     CloseOrder(OP_SELL);
                  } 
               TrailOrder(OrderType()); //<-- Trailling the order
            }
         }
      }

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

int OpenOrder(int type)
{
   int ticket=0;
   int err=0;
   int c = 0;
   
   if(type==OP_BUY)
   {
      for(c = 0 ; c < NumberOfTries ; c++)
      {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,ExpertComment,MagicNumber,0,Green);
         err=GetLastError();
         if(err==0)
         { 
            break;
         }
         else
         {
            if(err==4 || err==137 ||err==146 || err==136) //Busy errors
            {
               Sleep(5000);
               continue;
            }
            else //normal error
            {
               break;
            }  
         }
      }   
   }
   if(type==OP_SELL)
   {   
      for(c = 0 ; c < NumberOfTries ; c++)
      {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,ExpertComment,MagicNumber,0,Red);
         err=GetLastError();
         if(err==0)
         { 
            break;
         }
         else
         {
            if(err==4 || err==137 ||err==146 || err==136) //Busy errors
            {
               Sleep(5000);
               continue;
            }
            else //normal error
            {
               break;
            }  
         }
      }   
   }  
   return(ticket);
}

bool CloseOrder(int type)
{
   if(OrderMagicNumber() == MagicNumber)
   {
      if(type==OP_BUY)
         return (OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet));
      if(type==OP_SELL)   
         return (OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet));
   }
}
void TrailOrder(int type)
{
   if(TrailingStop>0)
   {
      if(OrderMagicNumber() == MagicNumber)
      {
         if(type==OP_BUY)
         {
            if(Bid-OrderOpenPrice()>Point*TrailingStop)
            {
               if(OrderStopLoss()<Bid-Point*TrailingStop)
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
               }
            }
         }
         if(type==OP_SELL)
         {
            if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
            {
               if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
               }
            }
         }
      }
   }
}






Sample





Analysis



Market Information Used:

Series array that contains open prices of each bar


Indicator Curves created:


Indicators Used:

Moving average indicator

Momentum indicator


Custom Indicators Used:
T.S.V._Bullish & Bearish

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 : EURUSD on H1

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

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-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.39 Total Net Profit:17339.00

BackTest : USDCAD on H1

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

BackTest : USDJPY on H1

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

Request Backtest for TEST-EA


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

Pair: Period: