MA cross with take profit levels 4

Author: Ryan Klefas
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
2 Views
0 Downloads
0 Favorites

Profitability Reports

NZD/USD Oct 2024 - Jan 2025
104.00 %
Total Trades 328
Won Trades 322
Lost trades 6
Win Rate 98.17 %
Expected payoff 2.42
Gross Profit 18484.00
Gross Loss -17690.00
Total Net Profit 794.00
-100%
-50%
0%
50%
100%
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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---