System Trading 게시글

Creating the Easiest EA with the DeMarker Indicator for MetaTrader 4

첨부파일
32418.zip (1.46 KB, 다운로드 0회)

Hey fellow traders! If you’ve ever considered using an Expert Advisor (EA) to enhance your trading strategy, you’re in the right place. Today, I’m sharing my experience with a straightforward EA that I’ve developed, which I think you’ll find super useful. After reading through, I’d love for you to share your thoughts!

This EA is designed to operate on a single currency pair and comes with customizable settings for timeframe, lot size, stop loss, and take profit, all easily accessible in the menu properties.


extern ENUM_TIMEFRAMES TF = PERIOD_CURRENT;// Select Time Frame
extern int period = 8;// Period DeMarker
extern double lt = 0.01;// Lots
extern int sl = 100;// Stop Loss
extern int tp = 100;// Take Profit
extern double OB = 0.7;// Over Sold
extern double OS = 0.3;// Over Bought
extern bool OPENBAR = false;// Trading on new bar open price

Now, here’s a little secret: I break down the EA into three key parts:

  • Data | Timeframe
  • Order Management
  • Currency Pair
//+------------------------------------------------------------------+

//-- Time frame | Indicator
double dmrk[5];
int signal = -1;//-- 0.buy 1.sell
int hold = 0;

//-- Order Management
int ticket = 0;
double lot = 0.0;
int typ = -1;

//-- Currency Pair
datetime t1 = 0;
bool newbar = false;
bool entry = false;

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

In the OnInit() function, I set up the DeMarker indicator and check for the minimum lot size required by the broker. Here’s how it works:

//+------------------------------------------------------------------+
//| Initialization
void OnInit()
  {
   ArrayInitialize(dmrk,0.0);
  //---
      const double test_lot = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
      if(lt < test_lot) lt = test_lot;
  }

Now, let’s dive into the OnTick() function, where we calculate the DeMarker indicator and identify our buy and sell signals:

//---------------------------------------------------------------------------
   signal = -1;
//---------------------------------------------------------------------------

//--- Calculate
   for(int i = 0; i < ArraySize(dmrk); i++)
     {
      dmrk[i] = iDeMarker(Symbol(), TF, period, i);
     }
//---

   if(dmrk[1] > OB)
     {
      hold = 1;// Set hold
     }
   else
      if(dmrk[1] < OS)
        {
         hold = -1;// Set hold
        }
      else
        {
         hold = 0;// Reset hold
        }

   if(hold == 1)
     {
      if(dmrk[0] < OB && dmrk[1] > OB)
        {
           signal = OP_SELL;
        }
     }
   if(hold == -1)
     {
      if(dmrk[0] > OS && dmrk[1] < OS)
        {
           signal = OP_BUY;
        }
     }

To execute buy and sell orders, we use the following logic:

//---------------------------------------------------------------------------
   if(signal != -1)
      if(newbar == true)
         if(entry == false)// Door open
           {
            //---
            entry = true;// Set entry
            //---

            if(signal == OP_BUY)
              {
              ticket = OrderSend(Symbol(), OP_BUY, lt, Ask, (int)((Ask - Bid) / Point,
                      sl > 0? Bid - sl * Point: 0.0,
                      tp > 0? Bid + tp * Point: 0.0,
                      EAName + ":signal= " + IntegerToString(signal) + ":hold= " + IntegerToString(hold),
                      EANumber,
                      0,
                      clrBlue);
               signal = -1;
              //hold = 0;
              }// Reset
            else
              if(signal == OP_SELL)
                 {
                  ticket = OrderSend(Symbol(), OP_SELL, lt, Bid, (int)((Ask - Bid) / Point,
                      sl > 0? Ask + sl * Point: 0.0,
                      tp > 0? Ask - tp * Point: 0.0,
                      EAName + ":signal= " + IntegerToString(signal) + ":hold= " + IntegerToString(hold),
                      EANumber,
                      0,
                      clrRed);
                  signal = -1;
                  //hold = 0;
                  }// Reset signal

          }

And when it comes to closing orders...

   if(entry == true) // Closing
     {

      if(OrderSelect(ticket, SELECT_BY_TICKET))
        {
         if(OrderCloseTime() == 0)//-- Order is active
           {
            /* Condition to close */
            //entry = false;
           }
         // Else
            if(OrderCloseTime() != 0)//-- Close by manual, SL/TP, or EA
              {
               entry = false;// Reset entry
              }
        }
     }



연관 포스트

댓글 (0)