Building a Custom Pivot Point Strategy in MetaTrader 4/5

Discover how to create a robust pivot point trading strategy in MT4/5, complete with custom indicator and EA implementation.

building-a-custom-pivot-point-strategy-in

Introduction

Pivot points are a powerful tool for traders, providing key support and resistance levels that can help in making informed trading decisions. In this article, we will delve into the world of pivot points, explore how to build a custom pivot point indicator in MetaTrader 4/5, and develop a trading strategy around it. We'll also discuss the pros, cons, and risks, and provide a practical example to illustrate the process.

Explanation of Pivot Points

Pivot points are calculated using the previous day's high, low, and closing prices. They serve as a central point of reference, with additional support and resistance levels derived from it. The formula for calculating pivot points is as follows:

  • Pivot Point (P): (High + Low + Close) / 3
  • First Support Level (S1): (2 * P) - High
  • Second Support Level (S2): P - (High - Low)
  • First Resistance Level (R1): (2 * P) - Low
  • Second Resistance Level (R2): P + (High - Low)

These levels can be used to identify potential areas where the price might reverse or continue its trend. Pivot points are particularly useful in intraday and swing trading, as they provide clear entry and exit points.

Practical Implementation in MetaTrader / MQL / EAs

Step 1: Creating the Custom Pivot Point Indicator

To create a custom pivot point indicator in MetaTrader, we will write an MQL4/MQL5 script. Here’s a basic example to get you started:


//+------------------------------------------------------------------+
//| Custom Pivot Point Indicator                                     |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 Blue
#property indicator_color4 Yellow
#property indicator_color5 Cyan

//--- input parameters
input int LookBackPeriod = 1;

//--- indicator buffers
double PivotBuffer[];
double S1Buffer[];
double S2Buffer[];
double R1Buffer[];
double R2Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- indicator buffers mapping
   SetIndexBuffer(0, PivotBuffer);
   SetIndexBuffer(1, S1Buffer);
   SetIndexBuffer(2, S2Buffer);
   SetIndexBuffer(3, R1Buffer);
   SetIndexBuffer(4, R2Buffer);

   //--- set indicator short name
   IndicatorShortName("Custom Pivot Points");

   //--- set indicator labels
   SetIndexLabel(0, "Pivot");
   SetIndexLabel(1, "S1");
   SetIndexLabel(2, "S2");
   SetIndexLabel(3, "R1");
   SetIndexLabel(4, "R2");

   //--- set indicator styles
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexStyle(4, DRAW_LINE);

   //--- set indicator levels
   SetIndexLevel(0, 0);
   SetIndexLevel(1, -1);
   SetIndexLevel(2, -2);
   SetIndexLevel(3, 1);
   SetIndexLevel(4, 2);

   //--- set indicator shift
   SetIndexShift(0, 0);
   SetIndexShift(1, 0);
   SetIndexShift(2, 0);
   SetIndexShift(3, 0);
   SetIndexShift(4, 0);

   //--- set indicator arrows
   SetIndexArrow(0, 0);
   SetIndexArrow(1, 0);
   SetIndexArrow(2, 0);
   SetIndexArrow(3, 0);
   SetIndexArrow(4, 0);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   //--- calculate pivot points
   double highPrev = iHigh(NULL, PERIOD_D1, 1);
   double lowPrev = iLow(NULL, PERIOD_D1, 1);
   double closePrev = iClose(NULL, PERIOD_D1, 1);

   double pivot = (highPrev + lowPrev + closePrev) / 3;
   double s1 = (2 * pivot) - highPrev;
   double s2 = pivot - (highPrev - lowPrev);
   double r1 = (2 * pivot) - lowPrev;
   double r2 = pivot + (highPrev - lowPrev);

   //--- set buffer values
   PivotBuffer[0] = pivot;
   S1Buffer[0] = s1;
   S2Buffer[0] = s2;
   R1Buffer[0] = r1;
   R2Buffer[0] = r2;

   return(rates_total);
  }

Step 2: Building the Trading Strategy

Now that we have our custom pivot point indicator, let's create a simple trading strategy. We will use the pivot points to identify potential buy and sell signals. Here’s a basic example of an Expert Advisor (EA) that uses the pivot points:


//+------------------------------------------------------------------+
//| Custom Pivot Point EA                                           |
//+------------------------------------------------------------------+
#property strict

//--- input parameters
input double LotSize = 0.1;
input int StopLoss = 50;
input int TakeProfit = 100;

//--- indicator handles
int pivotHandle;

//--- buffer for pivot points
double pivot, s1, s2, r1, r2;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- create indicator handle
   pivotHandle = iCustom(NULL, 0, "CustomPivotPoints", 1);

   if (pivotHandle == INVALID_HANDLE)
     {
      Print("Error creating custom pivot point indicator handle");
      return(INIT_FAILED);
     }

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   //--- release indicator handle
   IndicatorRelease(pivotHandle);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //--- get pivot points
   pivot = iCustom(NULL, 0, "CustomPivotPoints", 1, 0, 0);
   s1 = iCustom(NULL, 0, "CustomPivotPoints", 1, 1, 0);
   s2 = iCustom(NULL, 0, "CustomPivotPoints", 1, 2, 0);
   r1 = iCustom(NULL, 0, "CustomPivotPoints", 1, 3, 0);
   r2 = iCustom(NULL, 0, "CustomPivotPoints", 1, 4, 0);

   //--- check for buy signal
   if (Close[1] > r1 && Close[2] <= r1)
     {
      //--- open buy order
      int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, "Buy at R1", 0, 0, clrGreen);
      if (ticket < 0)
        {
         Print("Error opening buy order: ", GetLastError());
         return;
        }
      //--- set stop loss and take profit
      OrderModify(ticket, OrderOpenPrice(), Ask - StopLoss * Point, Ask + TakeProfit * Point, 0, clrGreen);
     }

   //--- check for sell signal
   if (Close[1] < s1 && Close[2] >= s1)
     {
      //--- open sell order
      int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, "Sell at S1", 0, 0, clrRed);
      if (ticket < 0)
        {
         Print("Error opening sell order: ", GetLastError());
         return;
        }
      //--- set stop loss and take profit
      OrderModify(ticket, OrderOpenPrice(), Bid + StopLoss * Point, Bid - TakeProfit * Point, 0, clrRed);
     }
  }

Pros, Cons, and Risks

Pros Cons Risks
Clear and objective entry and exit points. May not always align with market sentiment. False signals can lead to losses.
Useful for both intraday and swing trading. Can be complex for beginners. High-frequency trading requires robust risk management.
Can be combined with other indicators for better accuracy. May not perform well in sideways markets. Overfitting the strategy to historical data can lead to poor live performance.

Example Scenarios or a Worked Walkthrough

Scenario 1: Intraday Trading

Let's consider an intraday trading scenario using the pivot point strategy. Suppose you are trading the EUR/USD pair on a 15-minute chart. The pivot points for the day are as follows:

  • Pivot Point (P): 1.1000
  • First Support Level (S1): 1.0950
  • Second Support Level (S2): 1.0900
  • First Resistance Level (R1): 1.1050
  • Second Resistance Level (R2): 1.1100

During the morning session, the price moves above R1 (1.1050) and closes above it. This triggers a buy signal. You open a buy order at the current ask price, set a stop loss at 50 pips below the entry price, and a take profit at 100 pips above the entry price. The price continues to rise, and you close the position at the take profit level, realizing a profit.

Scenario 2: Swing Trading

Now, let's consider a swing trading scenario. You are trading the GBP/USD pair on a 4-hour chart. The pivot points for the week are as follows:

  • Pivot Point (P): 1.3000
  • First Support Level (S1): 1.2950
  • Second Support Level (S2): 1.2900
  • First Resistance Level (R1): 1.3050
  • Second Resistance Level (R2): 1.3100

During the week, the price drops below S1 (1.2950) and closes below it. This triggers a sell signal. You open a sell order at the current bid price, set a stop loss at 50 pips above the entry price, and a take profit at 100 pips below the entry price. The price continues to fall, and you close the position at the take profit level, realizing a profit.

Summary / Key Takeaways

Pivot points are a valuable tool for traders, providing clear and objective support and resistance levels. By creating a custom pivot point indicator in MetaTrader 4/5 and implementing a trading strategy around it, you can make more informed trading decisions. However, it's important to be aware of the pros, cons, and risks associated with pivot points. Combining pivot points with other indicators and robust risk management techniques can further enhance your trading performance.

Frequently Asked Questions

How do I install the custom pivot point indicator in MetaTrader 4/5?

To install the custom pivot point indicator, follow these steps: 1. Save the MQL4/MQL5 file with a .mq4 or .mq5 extension. 2. Place the file in the /MQL4/Indicators/ or /MQL5/Indicators/ folder. 3. Restart MetaTrader. 4. Navigate to the Navigator window, find the CustomPivotPoints indicator, and drag it to your chart.

Can I use pivot points for longer-term trading strategies?

Yes, pivot points can be used for longer-term trading strategies. They are particularly useful for identifying major support and resistance levels over weekly or monthly timeframes. Adjust the lookback period and timeframe settings in your indicator to suit your trading style.

What are some common pitfalls to avoid when using pivot points?

Some common pitfalls to avoid include overfitting the strategy to historical data, not using proper risk management, and relying solely on pivot points without considering other market factors. It's also important to be aware that pivot points may not always align with market sentiment, especially in sideways markets.

How can I optimize my pivot point strategy for better performance?

To optimize your pivot point strategy, consider the following steps: 1. Backtest your strategy on historical data using MetaTrader's Strategy Tester. 2. Adjust the lookback period and other input parameters to find the best settings for your market and timeframe. 3. Combine pivot points with other indicators, such as moving averages or oscillators, to filter signals and improve accuracy. 4. Implement robust risk management techniques, such as setting appropriate stop loss and take profit levels.

Want to build your own version?

Recreate similar entry logic, risk rules, and filters in TradingBotMaker—no MQL coding. Start free.

Community

Clap for the article and open comments only when you want to read them.

0 claps0 comments

Related articles