/////////////////////////////////////////////////////// // // SaherElm IT Center MQL4 Global Library // --------------------------------------------- // saherelm useful tools and definitions ... // // // Maintainer: // ------------ // Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) // ////////////////////////////////////////////////////// #property library #property copyright "Copyright 2023, SaherElm IT Center" #property link "https://www.saherelm.ir" #property version "1.00" #property strict // // Includes Logger library ... #include "../Libraries/x-saherelm.log.lib.mq4" // // Includes Drawing library ... #include "../Libraries/x-saherelm.draw.lib.mq4" // static datetime lastBarTime; // struct XOHCL { double high; double open; double close; double low; }; // struct XRange { // datetime time; // double highestHigh; double lowestLow; // int levels; int marketLength; // double rate; }; // struct XPrice { // double askPrice; double bidPrice; double priceGap; // double longEntry; double longExit; // double shortEntry; double shortExit; }; // // START Functions ... // // // Determines current working is New Bar or not ... bool IsNewBar() { // // Reading Last Bar Date ... datetime curbar = (datetime)SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE ); // if(lastBarTime != curbar) { // lastBarTime = curbar; return true; } // return false; } // // Determines we are in new Day or not ... bool IsNewDay() { // bool result = false; // // Reading Last Bar Date ... datetime curbar = (datetime)SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE ); // if (lastBarTime == 0) { lastBarTime = curbar; } // if (curbar == lastBarTime) { return result; } // // Retrieve Day and Month and Year for Last Bar Time ... int lastBarDay = TimeDay(lastBarTime); int lastBarMonth = TimeMonth(lastBarTime); int lastBarYear = TimeYear(lastBarTime); // // Retrieve Day and Month and Year for Current Bar Time ... int curBarDay = TimeDay(curbar); int curBarMonth = TimeMonth(curbar); int curBarYear = TimeYear(curbar); // result = curBarDay > lastBarDay || curBarMonth > lastBarMonth || curBarYear > lastBarYear; // return result; } // // Determines we are in new Hour or not ... bool IsNewHour() { // // Save Last Hour value ... static int lastHour = 0; // bool isInNewDay = IsNewDay(); if (isInNewDay) { // // Reset Last Hour if it's in New Day ... lastHour = 0; } // // Retrieve Current Hour value ... int currentHour = Hour(); // // Check values ... bool result = currentHour > lastHour; if (result) { // // Update Last Hour value ... lastHour = currentHour; } // // returns result ... return result; } // // Retrieve Number of Candles in a Day ... int GetDailyCandleCount() { // int seconds = PeriodSeconds(_Period); // int daySeconds = 24 * 60 * 60; // int result = daySeconds / seconds; // return result; } // // XRange Detector ... XRange GetMarketRange( const int bar_index, const int marketLen, const int levels ) { // XRange result = {}; // result.levels = levels; result.marketLength = marketLen; // datetime time = iTime( _Symbol, _Period, bar_index ); result.time = time; // double highestHigh = GetMarketHighestHigh( bar_index, marketLen ); result.highestHigh = highestHigh; // double lowestLow = GetMarketLowestLow( bar_index, marketLen ); result.lowestLow = lowestLow; // double diff = highestHigh - lowestLow; double rate = diff / levels; // result.rate = rate; // return result; } // // Retrieve RMA on Specific Candle ... double GetRMA( int bar_index, int length ) { // double shortSMA = iMA(_Symbol, _Period, length, 0, MODE_SMA, PRICE_CLOSE, bar_index); double mediumSMA = iMA(_Symbol, _Period, length * 2, 0, MODE_SMA, PRICE_CLOSE, bar_index); double longSMA = iMA(_Symbol, _Period, length * 3, 0, MODE_SMA, PRICE_CLOSE, bar_index); // double result = longSMA - mediumSMA + shortSMA; // return result; } // // Retrieve Moving Average ... double GetMA( const int bar_index, const int maLength, const int maShift, const ENUM_MA_METHOD maMethod, const ENUM_APPLIED_PRICE appliedPrice ) { // double result = iMA( _Symbol, _Period, maLength, maShift, maMethod, appliedPrice, bar_index ); // return result; } // // Calculate Fib Level ... double GetFibonacciLevel( double upPrice, double downPrice, double level, int direction ) { // double ling = upPrice - downPrice; double pLevel = (ling / 100) * (level * 100); // double result = direction > 0 ? upPrice - pLevel : downPrice + pLevel; result = NormalizeDouble(result, _Digits); // return result; } // // Retrieve True Range ... double GetTR( const int bar_index ) { // double result = 0; // double high = iHigh( _Symbol, _Period, bar_index ); // double low = iLow( _Symbol, _Period, bar_index ); // double prevClose = iClose( _Symbol, _Period, bar_index + 1 ); // double highLowDif = high - low; double hpCDif = MathAbs(high - prevClose); double lpCDiff = MathAbs(low - prevClose); // result = MathMax(highLowDif, hpCDif); result = MathMax(result, lpCDiff); // return result; } // // Calculate True Range Simple Moving Average ... double GetMarketTRSMA( const int bar_index, const int marketLen ) { // double result = 0; // // Create Barket Length Trs ... double trs[]; ArrayResize( trs, marketLen ); int index = 0; double trSum = 0; for (int i = bar_index; i < bar_index + marketLen; i++) { // double tr = GetTR(i); trs[index] = tr; // trSum += tr; // index++; } // result = trSum / marketLen; // return result; } // // Retrieve Commodity Channel ... double GetMarketCCI( const int bar_index, const int marketLen, const ENUM_APPLIED_PRICE appliedPrice = PRICE_CLOSE ) { // double result = 0; // result = iCCI( _Symbol, _Period, marketLen, appliedPrice, bar_index ); // return result; } // // this function check crossing up two Buffers ... bool IsCrossedOver( double &arr1[], double &arr2[], int index1, int index2 = -1 ) { // // Normalize Index 2 Value ... if (index2 < 0) { index2 = index1 + 1; } // bool result = (arr1[index1] > arr2[index1]) && !(arr1[index2] > arr2[index2]); // return result; } // // this function check crossing down two Buffers ... bool IsCrossedUnder( double &arr1[], double &arr2[], int index1, int index2 = -1 ) { // // Normalize Index 2 Value ... if (index2 < 0) { index2 = index1 + 1; } // bool result = (arr1[index1] < arr2[index1]) && !(arr1[index2] < arr2[index2]); // return result; } // // Convert points to Actual Point ... double PointsToDouble(int points) { // double result = points * _Point; return result; } // // Converts Pips to Points ... int PipsToPoints(int pips) { // int result = pips * ((_Digits == 3 || _Digits == 5) ? 10 : 1); return result; } // // Converts Pips to Double ... double PipsToDouble(int pips) { // double result = PointsToDouble(PipsToPoints(pips)); return result; } // // Converts Double Value to Pips ... int PriceToPips(double value) { // if (value <= 0) { return 0; } // double pipValue = _Point * MathPow(10, _Digits); int result = (int)(value / pipValue); // return result; } // // Converts Pips to Double ... double PipsToPrice(int pips) { // if (pips <= 0) { return 0; } // double pipValue = _Point * MathPow(10, _Digits); double result = pipValue * pips; // return result; } // // Generate ATR StopLoss Value ... double CalculateATRSL( int period, double multiplier ) { // if (period <= 0 || multiplier <= 0) { return 0; } // double atrValue = iATR( _Symbol, _Period, period, 0 ); // double result = atrValue * multiplier; // return result; } // // Detect Candle Type ... // Bullish => means close > open ... bool IsBullishCandle( const int bar_index ) { // // Temp Result ... bool result = false; // if (bar_index < 0) { return result; } // double open = iOpen( _Symbol, _Period, bar_index ); // double close = iClose( _Symbol, _Period, bar_index ); // result = IsBullishCandle( open, close ); // return result; } bool IsBullishCandle( const double open, const double close ) { // // Temp Result ... bool result = false; // result = open < close; // return result; } // // Detect Candle Type ... // Bearish => means close < open ... bool IsBearishCandle( const int bar_index ) { // // Temp Result ... bool result = false; // if (bar_index < 0) { return result; } // double open = iOpen( _Symbol, _Period, bar_index ); // double close = iClose( _Symbol, _Period, bar_index ); // result = IsBearishCandle( open, close ); // return result; } bool IsBearishCandle( const double open, const double close ) { // // Temp Result ... bool result = false; // result = open > close; // return result; } // // Retrieve a Candle Info ... XOHCL GetCandleModel( const int bar_index ) { // XOHCL result = {}; // double high = iHigh( _Symbol, _Period, bar_index ); result.high = high; // double open = iOpen( _Symbol, _Period, bar_index ); result.open = open; // double close = iClose( _Symbol, _Period, bar_index ); result.close = close; // double low = iLow( _Symbol, _Period, bar_index ); result.low = low; // return result; } // // Retrieve Market Current Prices for Signals ... XPrice GetPrice() { // XPrice result = {}; // RefreshRates(); // double askPrice = SymbolInfoDouble( _Symbol, SYMBOL_ASK ); // double bidPrice = SymbolInfoDouble( _Symbol, SYMBOL_BID ); // double priceGap = MathAbs(bidPrice - askPrice); // result.askPrice = askPrice; result.bidPrice = bidPrice; result.priceGap = priceGap; // result.longEntry = askPrice; result.longExit = bidPrice; // result.shortEntry = bidPrice; result.shortExit = askPrice; // return result; } // // Detect Market Has Sharp Bullish or not ... bool IsSharpBullishDetected( const int bar_index, // Bar Index ... const int marketLen, // Market Length ... // const double shpDetectMultipliers // Sharp Detect Multiplier ) { // bool result = false; // double open = iOpen( _Symbol, _Period, bar_index ); // double close = iClose( _Symbol, _Period, bar_index ); // double high = iHigh( _Symbol, _Period, bar_index ); // double low = iLow( _Symbol, _Period, bar_index ); // double minValue = 0; double maxValue = 0; int positionedLength = bar_index + marketLen; // for (int i = bar_index; i < positionedLength; i++) { // double cHigh = iHigh( _Symbol, _Period, i ); // double cLow = iLow( _Symbol, _Period, i ); // minValue += MathMin(cLow, cHigh); maxValue += MathMax(cLow, cHigh); } // double minAvg = minValue / positionedLength; double maxAvg = maxValue / positionedLength; // double shpValue = shpDetectMultipliers * _Point; // result = (low - minAvg) > shpValue; // int index = bar_index; while ( index < positionedLength || !result ) { // low = iLow( _Symbol, _Period, index ); // result = result || (low - minAvg) > shpValue; // index++; }; // return result; } // // Detect Market Has Sharp Bullish or not ... bool IsSharpBearishDetected( const int bar_index, // Bar Index ... const int marketLen, // Market Length ... // const double shpDetectMultipliers // Sharp Detect Multiplier ) { // bool result = false; // double open = iOpen( _Symbol, _Period, bar_index ); // double close = iClose( _Symbol, _Period, bar_index ); // double high = iHigh( _Symbol, _Period, bar_index ); // double low = iLow( _Symbol, _Period, bar_index ); // double minValue = 0; double maxValue = 0; int positionedLength = bar_index + marketLen; // for (int i = bar_index; i < positionedLength; i++) { // double cHigh = iHigh( _Symbol, _Period, i ); // double cLow = iLow( _Symbol, _Period, i ); // minValue += MathMin(cLow, cHigh); maxValue += MathMax(cLow, cHigh); } // double minAvg = minValue / positionedLength; double maxAvg = maxValue / positionedLength; // double shpValue = shpDetectMultipliers * _Point; // result = (high - maxAvg) > shpValue; // int index = bar_index; while ( index < positionedLength || !result ) { // high = iHigh( _Symbol, _Period, index ); // result = result || (high - maxAvg) > shpValue; // index++; }; // return result; } // // Retrieve Highest High Value based on Given Market ... double GetMarketHighestHigh( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iHighest( _Symbol, _Period, MODE_HIGH, marketLen, bar_index ); // // Retrieve Price ... result = iHigh( _Symbol, _Period, foundedBarIndex ); // return result; } // // Retrieve Highest Low Value based on Given Market ... double GetMarketHighestLow( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iHighest( _Symbol, _Period, MODE_LOW, marketLen, bar_index ); // // Retrieve Price ... result = iLow( _Symbol, _Period, foundedBarIndex ); // return result; } // // Retrieve Highest Open Value based on Given Market ... double GetMarketHighestOpen( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iHighest( _Symbol, _Period, MODE_OPEN, marketLen, bar_index ); // // Retrieve Price ... result = iOpen( _Symbol, _Period, foundedBarIndex ); // return result; } // // Retrieve Highest Close Value based on Given Market ... double GetMarketHighestClose( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iHighest( _Symbol, _Period, MODE_CLOSE, marketLen, bar_index ); // // Retrieve Price ... result = iClose( _Symbol, _Period, foundedBarIndex ); // return result; } // // Retrieve Lowest Low Value based on Given Market ... double GetMarketLowestLow( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iLowest( _Symbol, _Period, MODE_LOW, marketLen, bar_index ); // // Retrieve Price ... result = iLow( _Symbol, _Period, foundedBarIndex ); // return result; } // // Retrieve Lowest High Value based on Given Market ... double GetMarketLowestHigh( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iLowest( _Symbol, _Period, MODE_HIGH, marketLen, bar_index ); // // Retrieve Price ... result = iHigh( _Symbol, _Period, foundedBarIndex ); // return result; } // // Retrieve Lowest Open Value based on Given Market ... double GetMarketLowestOpen( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iLowest( _Symbol, _Period, MODE_OPEN, marketLen, bar_index ); // // Retrieve Price ... result = iOpen( _Symbol, _Period, foundedBarIndex ); // return result; } // // Retrieve Lowest Close Value based on Given Market ... double GetMarketLowestClose( // const int bar_index, // Bar Index ... const int marketLen // Market Length ... ) { // // Empty Result ... double result = 0.0; // // Retrieve Desired Bar Index ... int foundedBarIndex = iLowest( _Symbol, _Period, MODE_CLOSE, marketLen, bar_index ); // // Retrieve Price ... result = iClose( _Symbol, _Period, foundedBarIndex ); // return result; } // // Find last Candle which it's Low Price is // less than given price ... double GetLowLessCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iLow( _Symbol, _Period, index ); // while (result >= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetLowLessCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iLow( _Symbol, _Period, result ); // while (cP >= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Find last Candle which it's Low Price is // great than given price ... double GetLowGreatCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iLow( _Symbol, _Period, index ); // while (result >= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetLowGreatCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iLow( _Symbol, _Period, result ); // while (cP >= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Find last Candle which it's High Price is // less than given price ... double GetHighLessCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iHigh( _Symbol, _Period, index ); // while (result >= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetHighLessCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iHigh( _Symbol, _Period, result ); // while (cP >= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Find last Candle which it's High Price is // great than given price ... double GetHighGreatCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iHigh( _Symbol, _Period, index ); // while (result <= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetHighGreatCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iHigh( _Symbol, _Period, result ); // while (cP <= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Find last Candle which it's Open Price is // less than given price ... double GetOpenLessCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iOpen( _Symbol, _Period, index ); // while (result >= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetOpenLessCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iOpen( _Symbol, _Period, result ); // while (cP >= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Find last Candle which it's Open Price is // less than given price ... double GetOpenGreatCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iOpen( _Symbol, _Period, index ); // while (result <= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetOpenGreatCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iOpen( _Symbol, _Period, result ); // while (cP <= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Find last Candle which it's Close Price is // less than given price ... double GetCloseLessCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iClose( _Symbol, _Period, index ); // while (result >= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetCloseLessCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iClose( _Symbol, _Period, result ); // while (cP >= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Find last Candle which it's Close Price is // great than given price ... double GetCloseGreatCandlePrice( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int index = bar_index + 1; double result = iClose( _Symbol, _Period, index ); // while (result <= price) { // index++; // result = iLow( _Symbol, _Period, index ); } // return result; } int GetCloseGreatCandleIndex( // const int bar_index, // Bar Index ... const double price // Position Open Price ... ) { // int result = bar_index + 1; double cP = iClose( _Symbol, _Period, result ); // while (cP <= price) { // result++; // cP = iLow( _Symbol, _Period, result ); } // return result; } // // Get Slope of a Line by it's two Point ... double GetSlope( double x1, double y1, double x2, double y2 ) { // double result = (y2 - y1)/MathAbs(x2 - x1); return result; } // // END Functions ... //