MA cross with take profit levels 4





//+------------------------------------------------------------------+
//|                      MA Cross with Profit Targets - Version 1.00 |
//+------------------------------------------------------------------+
//|                                        Programming:  Ryan Klefas |
//|                                                rklefas@inbox.com |
//+------------------------------------------------------------------+
//|                                             Trade Strategy:  xxx |
//|                                                www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Ryan Klefas"
#property link      "rklefas@inbox.com"

//---- input parameters
extern string    str1 = " == Position Details == ";
extern int       TakeProfit=1000;
extern int       profitTarget1=21;
extern int       profitTarget2=34;
extern int       profitTarget3=55;
extern int       profitTarget4=89;
extern int       Stoploss=1000;
extern int       TrailingStop=100;
extern bool      TrailingStopOnlyProfit=true;
extern bool      TrailingStopRegular=false;
extern double    Lots=5;  // To avoid any possible errors, this value 
                          // is best set at some multiple of 5.  (Ex: 0.5, 5, 40)
extern string    str2 = " == Indicators == ";
extern double    emaOPENperiods=5;
extern double    emaCLOSEperiods=5;
extern string    str3 = " === EA Options === ";

//---- global variables
string commentString = "maCross";
int magicNum = 75399;

//+------------------------------------------------------------------+
//| expert initialization and deinitialization functions             |
//+------------------------------------------------------------------+
int init()
  { return(0); }

int deinit()
  { return(0); }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

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

   int cnt, ticket, total, OrdersPerSymbol;
   bool emaBuy = false, emaSell = false, candleIsNew = true;
   
   double emaOPEN=iMA(NULL,0,emaOPENperiods,0,MODE_EMA,PRICE_OPEN,0);
   double emaCLOSE=iMA(NULL,0,emaCLOSEperiods,0,MODE_EMA,PRICE_CLOSE,0);

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

   if (TrailingStopOnlyProfit == true && TrailingStopRegular == true)
      Alert("You have enabled both types of Trailing Stops.  /nOnly one may be enabled.");

   OrdersPerSymbol=0;
   for(cnt=OrdersTotal();cnt>=0;cnt--)
     {
        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
        if(OrderSymbol()==Symbol())
           OrdersPerSymbol++; 
     }
     
   total = OrdersPerSymbol; 
   
   if (TrailingStopRegular == true)
      regularTrailingStop();
   if (TrailingStopOnlyProfit == true)
      onlyProfitTrailingStop();

   if (emaOPEN < emaCLOSE)
      { emaBuy = true; }
   if (emaOPEN > emaCLOSE)
      { emaSell = true; }

/*   if (x)
      candleIsNew = true;  */
      
   if (total < 1) 
     {
       if( emaBuy == true && candleIsNew == true)
         { sendBuyOrder(); }
       if( emaSell == true && candleIsNew == true )
         { sendSellOrder(); }
     }

//+------------------------------------------------------------------+
//| closes lots at profit targets                                    |
//+------------------------------------------------------------------+ 

   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {if(OrderType()==OP_BUY)
           {if(  (Bid >= OrderOpenPrice()+profitTarget1*Point && OrderLots() == Lots) || 
                 (Bid >= OrderOpenPrice()+profitTarget2*Point && OrderLots() == ((Lots/5)*4) ) || 
                 (Bid >= OrderOpenPrice()+profitTarget3*Point && OrderLots() == ((Lots/5)*3) ) || 
                 (Bid >= OrderOpenPrice()+profitTarget4*Point && OrderLots() == ((Lots/5)*2) )  )
             { OrderClose(OrderTicket(),(Lots/5),Bid,3,CLR_NONE); }  }  }
        {if(OrderType()==OP_SELL)
           {if(  (Ask <= OrderOpenPrice()-profitTarget1*Point && OrderLots() == Lots) || 
                 (Ask <= OrderOpenPrice()-profitTarget2*Point && OrderLots() == ((Lots/5)*4) ) || 
                 (Ask <= OrderOpenPrice()-profitTarget3*Point && OrderLots() == ((Lots/5)*3) ) || 
                 (Ask <= OrderOpenPrice()-profitTarget4*Point && OrderLots() == ((Lots/5)*2) )  )
             { OrderClose(OrderTicket(),(Lots/5),Ask,3,CLR_NONE); }  }  }
     }

//+------------------------------------------------------------------+
  
}  

//+------------------------------------------------------------------+
//| end of expert start function                                     |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| modules, functions                                               |
//+------------------------------------------------------------------+

void sendBuyOrder()
   {
     int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-Stoploss*Point,Ask+TakeProfit*Point,commentString + Period(),magicNum,0,Green);
     if(ticket>0)
       {if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); }
     else 
        Print("Error opening BUY order : ",GetLastError()); 
   }

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

void sendSellOrder()
   {
      int ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+Stoploss*Point,Bid-TakeProfit*Point,commentString + Period(),magicNum,0,Red);
      if(ticket>0)
         {if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); }
      else 
         Print("Error opening SELL order : ",GetLastError()); 
   }

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

void onlyProfitTrailingStop()
   {
   
   int cnt, total = OrdersTotal();

   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {if(OrderType()==OP_BUY)
           {if(TrailingStop>0)  
              {if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {if(OrderStopLoss()<Bid-Point*TrailingStop)
                    { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); 
                        }}}}
          else
           {if(TrailingStop>0)  
              {if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); 
                        }}}}}}}
   
//+------------------------------------------------------------------+

void regularTrailingStop()
{
   int total = OrdersTotal(), cnt;
   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {if(OrderType()==OP_BUY)
           {if(TrailingStop>0)  
              {if(OrderStopLoss()<Bid-Point*TrailingStop)
                 { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green); 
                 }}}
         else
           {if(TrailingStop>0)  
              {if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                 { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); 
                 }}}}}
     }

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





Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

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

Other Features:

It issuies visual alerts to the screen

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : USDCAD on H1

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

BackTest : EURUSD on H1

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

BackTest : GBPUSD on H1

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

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.27 Total Net Profit:-10304.40

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:0.28 Total Net Profit:-9622.80

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-04-16 Profit Factor:0.07 Total Net Profit:-10098.00

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

Request Backtest for MA cross with take profit levels 4


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

Pair: Period: