Introduction to Pivot Points and MQL4
What are Pivot Points and Why are They Important?
Pivot points are significant levels used in technical analysis to determine potential support and resistance areas. They are calculated based on the previous period’s high, low, and closing prices. Traders use pivot points to anticipate price movements and make informed trading decisions. Understanding these levels can provide insights into potential entry and exit points, as well as stop-loss placement.
Overview of MQL4 for Indicator Development
MQL4 (MetaQuotes Language 4) is a proprietary programming language used in the MetaTrader 4 platform for creating custom indicators, Expert Advisors (EAs), and scripts. It’s a C-like language, making it relatively accessible for programmers with experience in similar languages. While MQL5 offers more advanced features like object-oriented programming, MQL4 remains widely used due to the prevalence of the MT4 platform. This article will focus on MQL4 to create a pivot point indicator.
Objectives of the MQL4 Pivot Point Indicator
The goal is to develop an MQL4 indicator that automatically calculates and plots pivot points and their associated support and resistance levels on the MetaTrader 4 chart. The indicator should be customizable, allowing users to select different pivot point calculation methods and timeframes. It should also be efficient in terms of resource usage.
Understanding the Calculation of Pivot Points
Standard Pivot Point Calculation (Formula Breakdown)
The standard pivot point calculation is as follows:
- Pivot Point (PP) = (High + Low + Close) / 3
- Resistance 1 (R1) = (2 * PP) – Low
- Support 1 (S1) = (2 * PP) – High
- Resistance 2 (R2) = PP + (High – Low)
- Support 2 (S2) = PP – (High – Low)
Where:
- High is the previous period’s high price.
- Low is the previous period’s low price.
- Close is the previous period’s closing price.
Fibonacci Pivot Points
Fibonacci pivot points incorporate Fibonacci ratios to calculate support and resistance levels. The PP is calculated as above, then:
- Resistance 1 (R1) = PP + ((High – Low) * 0.382)
- Support 1 (S1) = PP – ((High – Low) * 0.382)
- Resistance 2 (R2) = PP + ((High – Low) * 0.618)
- Support 2 (S2) = PP – ((High – Low) * 0.618)
- Resistance 3 (R3) = PP + ((High – Low) * 1.000)
- Support 3 (S3) = PP – ((High – Low) * 1.000)
Camarilla Pivot Points
Camarilla pivot points use a different set of formulas, generating tighter support and resistance levels:
- PP = (High + Low + Close) / 3
- R1 = Close + ((High – Low) * 1.1/12)
- S1 = Close – ((High – Low) * 1.1/12)
- R2 = Close + ((High – Low) * 1.1/6)
- S2 = Close – ((High – Low) * 1.1/6)
- R3 = Close + ((High – Low) * 1.1/4)
- S3 = Close – ((High – Low) * 1.1/4)
- R4 = Close + ((High – Low) * 1.1/2)
- S4 = Close – ((High – Low) * 1.1/2)
Woodie Pivot Points
Woodie’s pivot points calculation places more weight on the closing price:
- PP = (High + Low + (2 * Close)) / 4
- R1 = (2 * PP) – Low
- S1 = (2 * PP) – High
- R2 = PP + (High – Low)
- S2 = PP – (High – Low)
Developing the MQL4 Pivot Point Indicator
Setting up the MQL4 Indicator Template
- Open MetaEditor (in MT4, press F4).
- Go to File -> New.
- Select ‘Custom Indicator’ and click ‘Next’.
- Enter a name for the indicator (e.g., “PivotPointIndicator”) and click ‘Next’.
- Add input parameters (we’ll define these later) and click ‘Next’.
- Click ‘Finish’.
This creates a basic template with three essential functions:
OnInit(): Called once when the indicator is loaded or reinitialized.OnDeinit(): Called when the indicator is removed or the chart is closed.OnCalculate(): Called for each tick to calculate and draw the indicator.
Coding the Pivot Point Calculation Logic in MQL4
First, define input parameters for customization. Add these outside the OnInit() function:
extern string PivotType = "Standard"; // Pivot Point Type
extern int TimeFrame = PERIOD_D1; // Timeframe for calculation
Inside the OnCalculate() function, retrieve the High, Low, and Close prices from the specified timeframe. Note that MQL4 requires fetching historical data:
double High[], Low[], Close[];
ArraySetAsSeries(High, true);
ArraySetAsSeries(Low, true);
ArraySetAsSeries(Close, true);
CopyHigh(Symbol(), TimeFrame, 1, 1, High);
CopyLow(Symbol(), TimeFrame, 1, 1, Low);
CopyClose(Symbol(), TimeFrame, 1, 1, Close);
double PP, R1, S1, R2, S2;
double _High = High[0];
double _Low = Low[0];
double _Close = Close[0];
if(PivotType == "Standard")
{
PP = (_High + _Low + _Close) / 3;
R1 = (2 * PP) - _Low;
S1 = (2 * PP) - _High;
R2 = PP + (_High - _Low);
S2 = PP - (_High - _Low);
}
// Add other pivot point calculations here (Fibonacci, Camarilla, Woodie)
Plotting Pivot Points and Support/Resistance Levels on the Chart
Use ObjectCreate() to draw horizontal lines for each level. Add this code inside the OnCalculate() function, after the pivot point calculations:
string prefix = "Pivot";
ObjectCreate(prefix + "PP", OBJ_HLINE, 0, 0, PP);
ObjectSetString(prefix + "PP", OBJPROP_COLOR, clrBlue);
ObjectSetInteger(prefix + "PP", OBJPROP_STYLE, STYLE_SOLID);
ObjectCreate(prefix + "R1", OBJ_HLINE, 0, 0, R1);
ObjectSetString(prefix + "R1", OBJPROP_COLOR, clrRed);
ObjectSetInteger(prefix + "R1", OBJPROP_STYLE, STYLE_DASH);
ObjectCreate(prefix + "S1", OBJ_HLINE, 0, 0, S1);
ObjectSetString(prefix + "S1", OBJPROP_COLOR, clrGreen);
ObjectSetInteger(prefix + "S1", OBJPROP_STYLE, STYLE_DASH);
ObjectCreate(prefix + "R2", OBJ_HLINE, 0, 0, R2);
ObjectSetString(prefix + "R2", OBJPROP_COLOR, clrRed);
ObjectSetInteger(prefix + "R2", OBJPROP_STYLE, STYLE_DOT);
ObjectCreate(prefix + "S2", OBJ_HLINE, 0, 0, S2);
ObjectSetString(prefix + "S2", OBJPROP_COLOR, clrGreen);
ObjectSetInteger(prefix + "S2", OBJPROP_STYLE, STYLE_DOT);
// Repeat for R3, S3 etc., depending on pivot type
Don’t forget to delete the objects in the OnDeinit() function:
void OnDeinit(const int reason)
{
ObjectDelete(0, "PivotPP");
ObjectDelete(0, "PivotR1");
ObjectDelete(0, "PivotS1");
ObjectDelete(0, "PivotR2");
ObjectDelete(0, "PivotS2");
// Repeat for R3, S3 etc.
}
Adding Input Parameters for Customization (Pivot Point Type, Timeframe)
We already defined PivotType and TimeFrame as input parameters using the extern keyword. Users can modify these values in the indicator’s properties window in MetaTrader 4.
Advanced Features and Customization
Adding Alerts for Price Approaching Pivot Levels
To add alerts, check if the current price is within a certain range of the pivot levels within OnCalculate(). Use Alert() or PlaySound() functions:
double AlertRange = 20 * Point; // 20 pips range
double Ask = MarketInfo(Symbol(), MODE_ASK);
if(MathAbs(Ask - PP) < AlertRange)
{
Alert("Price approaching Pivot Point!");
}
//Add similar alerts for R1, S1, etc.
Implementing Multi-Timeframe Pivot Points
To calculate and display pivot points from multiple timeframes, create additional input parameters for each timeframe. Fetch the data for each timeframe and plot the corresponding levels.
extern int TimeFrame2 = PERIOD_W1;
//Copy data and calculate pivot points for TimeFrame2
Be mindful of the chart’s readability when displaying levels from too many timeframes.
Optimizing Indicator Performance
- Minimize Data Copying: Avoid unnecessary copying of price data. Copy only the required bars.
- Efficient Calculations: Use optimized formulas and avoid redundant calculations.
- Object Management: Efficiently create and delete graphical objects. Deleting and recreating objects on every tick is inefficient. Consider updating the object’s price level instead.
- Event Handling: Utilize
OnChartEvent()for event-driven updates instead of continuous calculations, if applicable (requires MQL5).
Testing and Using the MQL4 Pivot Point Indicator
Compiling and Running the Indicator in MetaTrader 4
- In MetaEditor, click ‘Compile’ (or press F7). Address any errors that appear in the ‘Errors’ tab.
- In MetaTrader 4, navigate to ‘Insert’ -> ‘Indicators’ -> ‘Custom’ and select your indicator.
- Adjust the input parameters as needed and click ‘OK’.
Interpreting Pivot Points for Trading Decisions
- Potential Support and Resistance: Pivot points and their associated levels can act as support and resistance.
- Breakout Confirmation: A breakout above a resistance level or below a support level can signal a continuation of the trend.
- Trend Direction: The relationship between the price and the pivot point can provide clues about the trend’s direction. Price above the pivot point suggests an upward trend, while price below suggests a downward trend.
Combining Pivot Points with Other Technical Indicators
Pivot points can be combined with other indicators like Moving Averages, RSI, and MACD to confirm signals and improve trading accuracy. For instance, a bullish signal might be stronger if the price breaks above a resistance level while the RSI is also above 50.
Troubleshooting Common Issues
- Indicator Not Displaying: Check the ‘Experts’ tab in the Terminal window for errors. Ensure that ‘Allow DLL imports’ is enabled if the indicator uses external libraries (generally not needed for pivot point indicators).
- Incorrect Pivot Point Levels: Double-check the formulas and ensure the data is being copied correctly from the specified timeframe. Verify the data using cross-validation tools.
- Performance Issues: Optimize the code as described in the ‘Optimizing Indicator Performance’ section.
While this article primarily focused on MQL4, the fundamental concepts of pivot point calculation and interpretation remain the same in MQL5. MQL5 allows for more sophisticated indicator development using object-oriented programming and event-driven architecture, opening new possibilities for advanced features and improved performance. However, the simplicity and widespread adoption of MQL4 still make it a valuable tool for creating effective trading indicators like the pivot point indicator described here.