Ema_cross_contest_hedged_v1

Author: Coders Guru
Profit factor:
0.72
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicatorMACD Histogram
3 Views
0 Downloads
0 Favorites
Ema_cross_contest_hedged_v1
//+------------------------------------------------------------------+
//|                                     EMA_CROSS_CONTEST_HEDGED.mq4 |
//|                                                      Coders Guru |
//|                                         http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link      "http://www.forex-tsd.com"
//---- Trades limits
extern double    TakeProfit=150;
extern double    TrailingStop=40;
extern double    StopLoss=150;
extern int       HedgeLevel=6;
extern bool      UseClose=true;
extern bool      UseMACD=true;
extern double    Expiration=7200;
extern int       ShortEma=4;
extern int       LongEma=24;
extern int       CurrentBar=1;
extern double    Lots=0.10;
string           ExpertName="EMA_CROSS_CONTEST_HEDGED";
int              Magic=20060328;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------
/*bool NewBar(int bar)
{
   static datetime lastbar=0;
   datetime curbar = Time[bar];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }
} */
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Crossed()
  {
   double EmaLongPrevious=iMA(NULL,0,LongEma,0,MODE_EMA, PRICE_CLOSE, CurrentBar+1);
   double EmaLongCurrent=iMA(NULL,0,LongEma,0,MODE_EMA, PRICE_CLOSE, CurrentBar);
   double EmaShortPrevious=iMA(NULL,0,ShortEma,0,MODE_EMA, PRICE_CLOSE, CurrentBar+1);
   double EmaShortCurrent=iMA(NULL,0,ShortEma,0,MODE_EMA, PRICE_CLOSE, CurrentBar);
//----
   if (EmaShortPrevious<EmaLongPrevious && EmaShortCurrent>EmaLongCurrent)    return(1); //up trend
   if (EmaShortPrevious>EmaLongPrevious && EmaShortCurrent<EmaLongCurrent)    return(2); //down trend
//----
   return(0); //elsewhere
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool isNewSumbol(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)
         return(False);
     }
   return(True);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   int cnt, ticket, total;
   double SEma, LEma;
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
   total =OrdersTotal();
//----
   double macd=iMACD(NULL,0,4,24,12,PRICE_CLOSE,MODE_SIGNAL,CurrentBar);
   if(UseMACD==false) macd=0;
   if(total < 1 || isNewSumbol(Symbol()))
     {
      if(Crossed()==1 &&  macd>=0 )
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*MarketInfo(Symbol(),MODE_POINT),Ask+TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + 3600,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError());
//----
         OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+HedgeLevel*MarketInfo(Symbol(),MODE_POINT),3,(Ask+HedgeLevel*MarketInfo(Symbol(),MODE_POINT))-StopLoss*MarketInfo(Symbol(),MODE_POINT),(Ask+HedgeLevel*MarketInfo(Symbol(),MODE_POINT))+TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
         OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+(HedgeLevel+2)*MarketInfo(Symbol(),MODE_POINT),3,(Ask+(HedgeLevel+2)*MarketInfo(Symbol(),MODE_POINT))-StopLoss*MarketInfo(Symbol(),MODE_POINT),(Ask+(HedgeLevel+2)*MarketInfo(Symbol(),MODE_POINT))+TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
         OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+(HedgeLevel+3)*MarketInfo(Symbol(),MODE_POINT),3,(Ask+(HedgeLevel+3)*MarketInfo(Symbol(),MODE_POINT))-StopLoss*MarketInfo(Symbol(),MODE_POINT),(Ask+(HedgeLevel+3)*MarketInfo(Symbol(),MODE_POINT))+TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
         OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+(HedgeLevel+4)*MarketInfo(Symbol(),MODE_POINT),3,(Ask+(HedgeLevel+4)*MarketInfo(Symbol(),MODE_POINT))-StopLoss*MarketInfo(Symbol(),MODE_POINT),(Ask+(HedgeLevel+4)*MarketInfo(Symbol(),MODE_POINT))+TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
//----
         return(0);
        }
      if(Crossed ()==2 &&  macd<=0  )
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*MarketInfo(Symbol(),MODE_POINT),Bid-TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,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());
//----
         OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-HedgeLevel*MarketInfo(Symbol(),MODE_POINT),3,(Bid-HedgeLevel*MarketInfo(Symbol(),MODE_POINT))+StopLoss*MarketInfo(Symbol(),MODE_POINT),(Bid-HedgeLevel*MarketInfo(Symbol(),MODE_POINT))-TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
         OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-(HedgeLevel+1)*MarketInfo(Symbol(),MODE_POINT),3,(Bid-(HedgeLevel+2)*MarketInfo(Symbol(),MODE_POINT))+StopLoss*MarketInfo(Symbol(),MODE_POINT),(Bid-(HedgeLevel+2)*MarketInfo(Symbol(),MODE_POINT))-TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
         OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-(HedgeLevel+2)*MarketInfo(Symbol(),MODE_POINT),3,(Bid-(HedgeLevel+3)*MarketInfo(Symbol(),MODE_POINT))+StopLoss*MarketInfo(Symbol(),MODE_POINT),(Bid-(HedgeLevel+3)*MarketInfo(Symbol(),MODE_POINT))-TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
         OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-(HedgeLevel+3)*MarketInfo(Symbol(),MODE_POINT),3,(Bid-(HedgeLevel+4)*MarketInfo(Symbol(),MODE_POINT))+StopLoss*MarketInfo(Symbol(),MODE_POINT),(Bid-(HedgeLevel+4)*MarketInfo(Symbol(),MODE_POINT))-TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + Expiration,Blue);
         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(UseClose)
              {
               if(Crossed ()== 2)
                 {
                  OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                  return(0); // exit
                 }
              }
            // check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>MarketInfo(Symbol(),MODE_POINT)*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MarketInfo(Symbol(),MODE_POINT)*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-MarketInfo(Symbol(),MODE_POINT)*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            if(UseClose)
              {
               if(Crossed ()== 1)
                 {
                  OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
                  return(0); // exit
                 }
              }
            // check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(MarketInfo(Symbol(),MODE_POINT)*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MarketInfo(Symbol(),MODE_POINT)*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MarketInfo(Symbol(),MODE_POINT)*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+

Profitability Reports

USD/CHF Jan 2025 - Jul 2025
0.63
Total Trades 527
Won Trades 324
Lost trades 203
Win Rate 61.48 %
Expected payoff -2.13
Gross Profit 1883.33
Gross Loss -3004.42
Total Net Profit -1121.09
-100%
-50%
0%
50%
100%
USD/CAD Jan 2025 - Jul 2025
0.78
Total Trades 618
Won Trades 407
Lost trades 211
Win Rate 65.86 %
Expected payoff -0.75
Gross Profit 1596.32
Gross Loss -2059.06
Total Net Profit -462.74
-100%
-50%
0%
50%
100%
NZD/USD Jan 2025 - Jul 2025
0.67
Total Trades 262
Won Trades 177
Lost trades 85
Win Rate 67.56 %
Expected payoff -1.20
Gross Profit 645.30
Gross Loss -959.80
Total Net Profit -314.50
-100%
-50%
0%
50%
100%
GBP/USD Jan 2025 - Jul 2025
0.32
Total Trades 598
Won Trades 299
Lost trades 299
Win Rate 50.00 %
Expected payoff -4.77
Gross Profit 1352.30
Gross Loss -4203.90
Total Net Profit -2851.60
-100%
-50%
0%
50%
100%
GBP/CAD Jan 2025 - Jul 2025
0.11
Total Trades 1072
Won Trades 283
Lost trades 789
Win Rate 26.40 %
Expected payoff -7.06
Gross Profit 897.08
Gross Loss -8463.98
Total Net Profit -7566.90
-100%
-50%
0%
50%
100%
GBP/AUD Jan 2025 - Jul 2025
0.47
Total Trades 706
Won Trades 386
Lost trades 320
Win Rate 54.67 %
Expected payoff -2.32
Gross Profit 1429.97
Gross Loss -3065.60
Total Net Profit -1635.63
-100%
-50%
0%
50%
100%
EUR/USD Jan 2025 - Jul 2025
0.59
Total Trades 615
Won Trades 411
Lost trades 204
Win Rate 66.83 %
Expected payoff -1.93
Gross Profit 1716.10
Gross Loss -2903.00
Total Net Profit -1186.90
-100%
-50%
0%
50%
100%
USD/CAD Oct 2024 - Jan 2025
2.59
Total Trades 85
Won Trades 71
Lost trades 14
Win Rate 83.53 %
Expected payoff 2.35
Gross Profit 325.72
Gross Loss -125.86
Total Net Profit 199.86
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.37
Total Trades 207
Won Trades 109
Lost trades 98
Win Rate 52.66 %
Expected payoff -3.72
Gross Profit 457.10
Gross Loss -1227.90
Total Net Profit -770.80
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
0.84
Total Trades 428
Won Trades 296
Lost trades 132
Win Rate 69.16 %
Expected payoff -0.72
Gross Profit 1584.80
Gross Loss -1894.00
Total Net Profit -309.20
-100%
-50%
0%
50%
100%

Comments