rickyandreas290292
New Member
- Credits
- 0
// Input parameters
input int period = 14; // RSI period
input double deviation = 0.0001; // Deviation threshold for divergence detection
// Function to check for bullish divergence
bool isBullishDivergence(int bar)
{
double rsiValue = iRSI(NULL, 0, period, PRICE_CLOSE, bar);
double priceValue = Close[bar];
double rsiPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 1);
double pricePrev = Close[bar + 1];
double rsiPrevPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 2);
double pricePrevPrev = Close[bar + 2];
if (rsiValue > rsiPrev && rsiPrev < rsiPrevPrev && priceValue > pricePrev && pricePrev < pricePrevPrev)
{
double rsiDeviation = MathAbs(rsiValue - rsiPrev);
double priceDeviation = MathAbs(priceValue - pricePrev);
if (rsiDeviation > deviation && priceDeviation < deviation)
{
return true;
}
}
return false;
}
// Function to check for bearish divergence
bool isBearishDivergence(int bar)
{
double rsiValue = iRSI(NULL, 0, period, PRICE_CLOSE, bar);
double priceValue = Close[bar];
double rsiPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 1);
double pricePrev = Close[bar + 1];
double rsiPrevPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 2);
double pricePrevPrev = Close[bar + 2];
if (rsiValue < rsiPrev && rsiPrev > rsiPrevPrev && priceValue < pricePrev && pricePrev > pricePrevPrev)
{
double rsiDeviation = MathAbs(rsiValue - rsiPrev);
double priceDeviation = MathAbs(priceValue - pricePrev);
if (rsiDeviation > deviation && priceDeviation < deviation)
{
return true;
}
}
return false;
}
// OnTick function
void OnTick()
{
int startBar = 2; // Start bar index for divergence checking
int endBar = Bars - 1; // End bar index
for (int bar = startBar; bar <= endBar; bar++)
{
if (isBullishDivergence(bar))
{
// Bullish divergence detected
// Perform necessary actions
Print("Bullish Divergence at Bar ", bar);
}
if (isBearishDivergence(bar))
{
// Bearish divergence detected
// Perform necessary actions
Print("Bearish Divergence at Bar ", bar);
}
}
}
input int period = 14; // RSI period
input double deviation = 0.0001; // Deviation threshold for divergence detection
// Function to check for bullish divergence
bool isBullishDivergence(int bar)
{
double rsiValue = iRSI(NULL, 0, period, PRICE_CLOSE, bar);
double priceValue = Close[bar];
double rsiPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 1);
double pricePrev = Close[bar + 1];
double rsiPrevPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 2);
double pricePrevPrev = Close[bar + 2];
if (rsiValue > rsiPrev && rsiPrev < rsiPrevPrev && priceValue > pricePrev && pricePrev < pricePrevPrev)
{
double rsiDeviation = MathAbs(rsiValue - rsiPrev);
double priceDeviation = MathAbs(priceValue - pricePrev);
if (rsiDeviation > deviation && priceDeviation < deviation)
{
return true;
}
}
return false;
}
// Function to check for bearish divergence
bool isBearishDivergence(int bar)
{
double rsiValue = iRSI(NULL, 0, period, PRICE_CLOSE, bar);
double priceValue = Close[bar];
double rsiPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 1);
double pricePrev = Close[bar + 1];
double rsiPrevPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 2);
double pricePrevPrev = Close[bar + 2];
if (rsiValue < rsiPrev && rsiPrev > rsiPrevPrev && priceValue < pricePrev && pricePrev > pricePrevPrev)
{
double rsiDeviation = MathAbs(rsiValue - rsiPrev);
double priceDeviation = MathAbs(priceValue - pricePrev);
if (rsiDeviation > deviation && priceDeviation < deviation)
{
return true;
}
}
return false;
}
// OnTick function
void OnTick()
{
int startBar = 2; // Start bar index for divergence checking
int endBar = Bars - 1; // End bar index
for (int bar = startBar; bar <= endBar; bar++)
{
if (isBullishDivergence(bar))
{
// Bullish divergence detected
// Perform necessary actions
Print("Bullish Divergence at Bar ", bar);
}
if (isBearishDivergence(bar))
{
// Bearish divergence detected
// Perform necessary actions
Print("Bearish Divergence at Bar ", bar);
}
}
}