22028
by samuelkanu
Please can someone help me make this code to work on mt4 platform. Here is the code
//+------------------------------------------------------------------+
//| SignalHistogram_MTF.mq4 |
//| Histogram: ALL 4 must agree — RSI>50, Momentum>100, |
//| DeMarker>0.5, OsMA>0 (bullish) and inverse bearish |
//| Alert on NEW candle open (non-repainting) |
//| MTF: user selects higher timeframe in inputs |
//+------------------------------------------------------------------+
#property copyright "Custom"
#property link ""
#property version "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2
// Bull histogram (positive)
#property indicator_label1 "Bull Signal"
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrDodgerBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 3
// Bear histogram (negative)
#property indicator_label2 "Bear Signal"
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_color2 clrOrangeRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 3
//--- Input Parameters
input int RSI_Period = 14; // RSI Period
input int Mom_Period = 14; // Momentum Period
input int DeM_Period = 14; // DeMarker Period
input int OSMA_Fast = 12; // OsMA Fast EMA
input int OSMA_Slow = 26; // OsMA Slow EMA
input int OSMA_Signal = 9; // OsMA Signal Period
input ENUM_TIMEFRAMES HTF = PERIOD_H4; // Higher Timeframe (MTF)
input bool UseHTF = true; // Enable MTF Filter
input bool EnableAlerts = true; // Enable Alerts
input bool AlertOnScreen = true; // Alert: popup message
input bool AlertOnEmail = false; // Alert: send email
input bool AlertOnPush = false; // Alert: push notification
//--- Buffers
double BullBuffer[];
double BearBuffer[];
//--- Alert state tracker (to fire only once per new bar)
datetime LastAlertBarTime = 0;
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, BullBuffer);
SetIndexBuffer(1, BearBuffer);
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
IndicatorShortName("SignalHistogram MTF ["+(string)HTF+"]");
IndicatorDigits(2);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Get signal for a given timeframe and bar shift |
//| Returns: 1 = Bull, -1 = Bear, 0 = No signal |
//+------------------------------------------------------------------+
int GetSignal(ENUM_TIMEFRAMES tf, int shift)
{
// --- RSI
double rsi = iRSI(NULL, tf, RSI_Period, PRICE_CLOSE, shift);
// --- Momentum
double mom = iMomentum(NULL, tf, Mom_Period, PRICE_CLOSE, shift);
// --- DeMarker
double dem = iDeMarker(NULL, tf, DeM_Period, shift);
// --- OsMA
double osma = iOsMA(NULL, tf, OSMA_Fast, OSMA_Slow, OSMA_Signal, PRICE_CLOSE, shift);
// --- Check BULL: all above their levels
bool bull = (rsi > 50.0) &&
(mom > 100.0) &&
(dem > 0.5) &&
(osma > 0.0);
// --- Check BEAR: all below their levels
bool bear = (rsi < 50.0) &&
(mom < 100.0) &&
(dem < 0.5) &&
(osma < 0.0);
if(bull) return 1;
if(bear) return -1;
return 0;
}
//+------------------------------------------------------------------+
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 = rates_total - prev_calculated;
if(prev_calculated == 0) limit = rates_total - 1;
for(int i = limit; i >= 0; i--)
{
BullBuffer = 0.0;
BearBuffer = 0.0;
// Signal on current TF (use bar i)
int sigCurrent = GetSignal(Period(), i);
// MTF Filter (use iBarShift to map current bar time to HTF bar)
int sigHTF = sigCurrent; // default: pass-through if MTF disabled
if(UseHTF && HTF != Period())
{
int htfShift = iBarShift(NULL, HTF, time, false);
sigHTF = GetSignal(HTF, htfShift);
}
// Final signal: both TFs must agree
int finalSig = 0;
if(sigCurrent == sigHTF) finalSig = sigCurrent;
// Assign histogram value (height = 1 or -1 for clean uniform bars)
if(finalSig == 1) BullBuffer = 1.0;
if(finalSig == -1) BearBuffer = -1.0;
}
//--- Alert logic: fire ONCE on the open of bar[0] (new candle)
if(EnableAlerts && time[0] != LastAlertBarTime)
{
// Check signal on bar[1] (the just-CLOSED bar), which is confirmed
// and now visible at bar-open of the new candle — non-repainting
int alertSig = 0;
{
int sigC = GetSignal(Period(), 1);
int sigH = sigC;
if(UseHTF && HTF != Period())
{
int htfShift = iBarShift(NULL, HTF, time[1], false);
sigH = GetSignal(HTF, htfShift);
}
if(sigC == sigH) alertSig = sigC;
}
if(alertSig != 0)
{
string direction = (alertSig == 1) ? "BULLISH" : "BEARISH";
string msg = StringFormat(
"SignalHistogram MTF | %s | %s signal on %s [HTF:%s]",
Symbol(),
direction,
EnumToString((ENUM_TIMEFRAMES)Period()),
EnumToString(HTF)
);
if(AlertOnScreen) Alert(msg);
if(AlertOnEmail) SendMail("MT4 Signal Alert", msg);
if(AlertOnPush) SendNotification(msg);
}
LastAlertBarTime = time[0];
}
return(rates_total);
}
//+------------------------------------------------------------------+