//+------------------------------------------------------------------+
//| OmigosFX Williams_EMA_Crossover.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright OmigosFX"
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow // Williams %R
#property indicator_color2 Red // EMA
#property indicator_color3 Lime // Buy Signal
#property indicator_color4 Red // Sell Signal
// Input parameters
extern int WilliamsPeriod = 11; // Williams Percent Range period
extern int EMAPeriod = 13; // EMA period
extern ENUM_MA_METHOD MAMethod = MODE_EMA; // Moving Average method
extern bool ShowWilliamsInSeparateWindow = false; // Show in separate window or
main chart
extern int WilliamsUpperLevel = -20; // Williams %R upper level
extern int WilliamsLowerLevel = -80; // Williams %R lower level
// Signal display parameters
extern bool ShowBuySignals = true; // Show buy signals
extern bool ShowSellSignals = true; // Show sell signals
extern int SignalOffset = 10; // Offset for signals (in points)
extern int BuyArrowCode = 233; // Buy arrow code (233=Wing up, 241=Up
arrow)
extern int SellArrowCode = 234; // Sell arrow code (234=Wing down,
242=Down arrow)
extern int SignalSize = 3; // Signal arrow size
// Color settings
extern color WilliamsColor = Yellow; // Williams %R color
extern color MAColor = Red; // Moving Average color
extern color BuySignalColor = Lime; // Buy signal color
extern color SellSignalColor = Red; // Sell signal color
// Buffers
double WilliamsBuffer[];
double MABuffer[];
double BuyBuffer[];
double SellBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Indicator buffers mapping
SetIndexBuffer(0, WilliamsBuffer);
SetIndexBuffer(1, MABuffer);
SetIndexBuffer(2, BuyBuffer);
SetIndexBuffer(3, SellBuffer);
// Set indicator styles
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, WilliamsColor);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1, MAColor);
SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, SignalSize, BuySignalColor);
SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, SignalSize, SellSignalColor);
// Set arrow codes
SetIndexArrow(2, BuyArrowCode); // Buy arrow code
SetIndexArrow(3, SellArrowCode); // Sell arrow code
// Set indicator labels
SetIndexLabel(0, "Williams %R(" + WilliamsPeriod + ")");
SetIndexLabel(1, "MA(" + EMAPeriod + ")");
SetIndexLabel(2, "Buy Signal");
SetIndexLabel(3, "Sell Signal");
// Initialize buffers
ArraySetAsSeries(WilliamsBuffer, true);
ArraySetAsSeries(MABuffer, true);
ArraySetAsSeries(BuyBuffer, true);
ArraySetAsSeries(SellBuffer, true);
// Indicator name
string maMethodName = "";
switch(MAMethod)
{
case MODE_SMA: maMethodName = "SMA"; break;
case MODE_EMA: maMethodName = "EMA"; break;
case MODE_SMMA: maMethodName = "SMMA"; break;
case MODE_LWMA: maMethodName = "LWMA"; break;
default: maMethodName = "MA"; break;
}
string indicatorName = "Williams(" + WilliamsPeriod + ") & " + maMethodName +
"(" + EMAPeriod + ") Crossover";
IndicatorShortName(indicatorName);
if (ShowWilliamsInSeparateWindow)
{
// Set up indicator window
IndicatorDigits(2);
SetLevelValue(0, WilliamsUpperLevel);
SetLevelValue(1, WilliamsLowerLevel);
SetIndexStyle(0, DRAW_LINE, STYLE_DOT, 1, DarkGray);
SetIndexStyle(1, DRAW_LINE, STYLE_DOT, 1, DarkGray);
}
// Display information about arrow codes in the chart corner
Comment("\nBuy Arrow Code: " + BuyArrowCode + "\nSell Arrow Code: " +
SellArrowCode +
"\nSignal Offset: " + SignalOffset + " points");
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[])
{
int limit;
double pip_multiplier = 1;
// Adjust for different digit currencies (5-digit brokers)
if (Digits == 3 || Digits == 5) pip_multiplier = 10;
// Calculate starting bar
if(prev_calculated == 0)
limit = rates_total - WilliamsPeriod - EMAPeriod - 2;
else
limit = rates_total - prev_calculated;
// Loop through bars
for(int i = limit; i >= 0; i--)
{
// Calculate Williams %R
WilliamsBuffer[i] = iWPR(NULL, 0, WilliamsPeriod, i);
// Calculate MA on Williams %R
MABuffer[i] = iMAOnArray(WilliamsBuffer, 0, EMAPeriod, 0, MAMethod, i);
// Initialize signal buffers
BuyBuffer[i] = EMPTY_VALUE;
SellBuffer[i] = EMPTY_VALUE;
// Check for crossover on candle close
if(i < rates_total - 1) // Ensure we're not on the last incomplete candle
{
// Buy Signal: EMA crosses below Williams %R
if(ShowBuySignals && MABuffer[i+1] >= WilliamsBuffer[i+1] &&
MABuffer[i] < WilliamsBuffer[i])
BuyBuffer[i] = low[i] - (SignalOffset * Point * pip_multiplier);
// Sell Signal: EMA crosses above Williams %R
if(ShowSellSignals && MABuffer[i+1] <= WilliamsBuffer[i+1] &&
MABuffer[i] > WilliamsBuffer[i])
SellBuffer[i] = high[i] + (SignalOffset * Point * pip_multiplier);
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Clear the comment when indicator is removed
Comment("");
}