MA cross with take profit levels 4

Author: Ryan Klefas
Profit factor:
0.63
10 Views
0 Downloads
0 Favorites
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); 
                 }}}}}
     }

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

Comments