Files
xMQL5/Libraries/x-saherelm.common.lib.mq5
T
2024-01-25 04:09:42 +03:30

2388 lines
43 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Common Library
// --------------------------------------
// Name: XCommon
// Description: provide all commonly used functions ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://www.saherelm.ir"
#property version "1.00"
#property strict
//
// START Inputs ...
//
//
// END Inputs ...
//
//
// Include Models Library ...
#include "x-saherelm.models.lib.mq5";
//
// START Global Definitions: Variables, Properties and etc ...
//
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// START Provided Functions ...
//
//
// Detect is new Candle or not ...
bool IsNewCandle()
{
//
bool result = false;
//
static int barsOnChart = 0;
int currentBarsOnChart = Bars(_Symbol, _Period);
result = currentBarsOnChart > barsOnChart;
if (!result)
{
return result;
}
//
barsOnChart = currentBarsOnChart;
result = true;
//
return result;
}
//
// Retrive Ask Price ...
double GetAsk()
{
return SymbolInfoDouble(_Symbol, SYMBOL_ASK);
}
double GetAsk(
string symbol // selected symbol for calculations
)
{
//
double result = SymbolInfoDouble(symbol, SYMBOL_ASK);
//
return result;
}
//
// Retrieve Bid Price ...
double GetBid()
{
return SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
double GetBid(
string symbol // selected symbol for calculations
)
{
//
double result = SymbolInfoDouble(symbol, SYMBOL_BID);
//
return result;
}
//
// Calculate and Retrieve Spread Price ...
double GetSpread()
{
//
double ask = GetAsk();
double bid = GetBid();
double result = MathAbs(ask - bid);
//
return result;
}
double GetSpread(
string symbol // selected symbol for calculations
)
{
//
double ask = GetAsk(symbol);
double bid = GetBid(symbol);
double result = MathAbs(ask - bid);
//
return result;
}
//
// Get Points ...
double GetPoints()
{
return _Point;
}
double GetPoints(
string symbol // selected symbol for calculations
)
{
//
double result = SymbolInfoDouble(symbol, SYMBOL_POINT);
//
return result;
}
//
// Get Digits ...
int GetDigits()
{
return _Digits;
}
int GetDigits(
string symbol // selected symbol for calculations
)
{
//
int result = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//
return result;
}
//
// Get Signal Entry ...
double GetEntry(
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_UNKNOWN // Get entry price for Which direction trade ...
)
{
//
double result = 0;
//
// Validate Arg ...
if (type == X_SIGNAL_UNKNOWN)
{
return result;
}
//
bool isLong = type == X_SIGNAL_LONG;
result = isLong ? GetAsk() : GetBid();
//
return result;
}
//
// Get 1 Pip in Price Value ...
double GetPipPrice()
{
//
double result = _Point;
if (_Digits == 3 || _Digits == 5)
{
result *= 10;
}
//
return result;
}
double GetPipPrice(
string symbol // selected symbol for calculations
)
{
//
double symbolPoint = GetPoints(symbol);
int symbolDigits = GetDigits(symbol);
//
double result = symbolPoint;
if (symbolDigits == 3 || symbolDigits == 5)
{
result *= 10;
}
//
return result;
}
//
// Converts Pips To Price ...
double PipsToPrice(
int pips // pips amount
)
{
//
int digits = GetDigits();
double pipValue = GetPipPrice();
double result = pips * pipValue;
//
result = NormalizeDouble(result, digits);
//
return result;
}
double PipsToPrice(
double pips // pips amount
)
{
//
int digits = GetDigits();
double pipValue = GetPipPrice();
double result = pips * pipValue;
//
result = NormalizeDouble(result, digits);
//
return result;
}
double PipsToPrice(
string symbol, // selected symbol for calculation
int pips // pips amount
)
{
//
int digits = GetDigits(symbol);
double pipValue = GetPipPrice(symbol);
double result = pips * pipValue;
//
result = NormalizeDouble(result, digits);
//
return result;
}
double PipsToPrice(
string symbol, // selected symbol for calculation
double pips // pips amount
)
{
//
int digits = GetDigits(symbol);
double pipValue = GetPipPrice(symbol);
double result = pips * pipValue;
//
result = NormalizeDouble(result, digits);
//
return result;
}
//
// Converts Price to Pips ...
double PriceToPips(
double price // the price amount which required to calculate
)
{
//
int digits = GetDigits();
double pipPrice = GetPipPrice();
//
double result = price / pipPrice;
//
result = NormalizeDouble(result, digits);
//
return result;
}
double PriceToPips(
string symbol, // Selected Symbol
double price // the price amount which required to calculate
)
{
//
int digits = GetDigits(symbol);
double pipPrice = GetPipPrice(symbol);
//
double result = price / pipPrice;
//
result = NormalizeDouble(result, digits);
//
return result;
}
//
// Get a Candle Body ...
double GetCandleBody(
XOHCL &candle // specified candle
)
{
//
double result = MathAbs(candle.open - candle.close);
//
return result;
}
//
// Get a Candle Range ...
double GetCandleRange(
XOHCL &candle // specified candle
)
{
//
double result = MathAbs(candle.high - candle.low);
//
return result;
}
//
// Get a Candle Shadow ...
double GetCandleShadow(
XOHCL &candle // specified candle
)
{
//
double range = GetCandleRange(candle);
double body = GetCandleBody(candle);
//
double result = MathAbs(range - body);
//
return result;
}
//
// Retrieve Applied Price at Specific Candle ...
double GetAppliedPrice(
XOHCL &candle, // specified candle
ENUM_APPLIED_PRICE source = PRICE_CLOSE // specified Applied To
)
{
//
double result = EMPTY_VALUE;
//
switch (source)
{
//
// Open ...
case PRICE_OPEN:
result = candle.open;
break;
//
// High ...
case PRICE_HIGH:
result = candle.high;
break;
//
// Low ...
case PRICE_LOW:
result = candle.low;
break;
//
// Median ((High + Low) / 2) ...
case PRICE_MEDIAN:
result = (candle.high + candle.low) / 2;
break;
//
// Typical ((High + Low + Close)/3) ...
case PRICE_TYPICAL:
result = (candle.high + candle.low + candle.close) / 3;
break;
//
// Weighted ((High + Low + Close + Close)/4) ...
case PRICE_WEIGHTED:
result = (candle.high + candle.low + (candle.close * 2)) / 4;
break;
//
// Close ...
default:
case PRICE_CLOSE:
result = candle.close;
break;
}
//
return result;
}
double GetAppliedPrice(
int bar_index, // specify candle index
ENUM_APPLIED_PRICE source = PRICE_CLOSE // specified Applied To
)
{
//
double result = EMPTY_VALUE;
//
XOHCL candle = GetCandle(bar_index);
result = GetAppliedPrice(candle, source);
//
return result;
}
double GetAppliedPrice(
int bar_index, // specify candle index
ENUM_SERIESMODE mode // converts Series Mode to Applied Price ...
)
{
//
double result = 0;
//
ENUM_APPLIED_PRICE appliedTo = To(mode);
result = GetAppliedPrice(
bar_index,
appliedTo);
//
return result;
}
//
// Converts ...
ENUM_APPLIED_PRICE To(
ENUM_SERIESMODE mode // converts Series Mode to Applied Price ...
)
{
//
ENUM_APPLIED_PRICE result = PRICE_CLOSE;
//
switch (mode)
{
//
// High ...
case MODE_HIGH:
result = PRICE_HIGH;
break;
//
// Open ...
case MODE_OPEN:
result = PRICE_OPEN;
break;
//
// Low ...
case MODE_LOW:
result = PRICE_LOW;
break;
//
// Close ...
// Default ...
case MODE_CLOSE:
default:
result = PRICE_CLOSE;
break;
}
//
return result;
}
//
// Retrieve a Candel Data ...
XOHCL GetCandle(int bar_index)
{
//
XOHCL result = {};
//
double open = iOpen(_Symbol, _Period, bar_index);
double high = iHigh(_Symbol, _Period, bar_index);
double close = iClose(_Symbol, _Period, bar_index);
double low = iLow(_Symbol, _Period, bar_index);
//
result.low = low;
result.open = open;
result.high = high;
result.close = close;
//
return result;
}
XOHCL GetCandle(
ENUM_TIMEFRAMES period, // specify timeframe
int bar_index // specify candle index
)
{
//
XOHCL result = {};
//
double open = iOpen(_Symbol, period, bar_index);
double high = iHigh(_Symbol, period, bar_index);
double close = iClose(_Symbol, period, bar_index);
double low = iLow(_Symbol, period, bar_index);
//
result.low = low;
result.open = open;
result.high = high;
result.close = close;
//
return result;
}
XOHCL GetCandle(
string symbol, // specify symbol
ENUM_TIMEFRAMES period, // specify timeframe
int bar_index // specify candle index
)
{
//
XOHCL result = {};
//
double open = iOpen(symbol, period, bar_index);
double high = iHigh(symbol, period, bar_index);
double close = iClose(symbol, period, bar_index);
double low = iLow(symbol, period, bar_index);
//
result.low = low;
result.open = open;
result.high = high;
result.close = close;
//
return result;
}
//
// Retrieve a candle index in current timeframe inside another timeframe ...
int GetCandleBarIndex(
int bar_index, // Specify Candle index in current timeframe
ENUM_TIMEFRAMES destPeriod // Specify destination timeframe
)
{
//
int result = -1;
//
datetime barTime = iTime(_Symbol, _Period, bar_index);
result = iBarShift(_Symbol, destPeriod, barTime);
//
return result;
}
//
// Retrieve a symbol candle index in source time frame inside dest timeframe ...
int GetCandleBarIndex(
int bar_index, // Specify Candle index in current timeframe
string symbol, // Specify desired Symbol
ENUM_TIMEFRAMES sourcePeriod, // Specify bar_index timeframe
ENUM_TIMEFRAMES destPeriod // Specify destination timeframe
)
{
//
int result = -1;
//
datetime barTime = iTime(symbol, sourcePeriod, bar_index);
result = iBarShift(symbol, destPeriod, barTime);
//
return result;
}
//
// Check a Candle is Bullish or not ...
bool IsBullishCandle(
XOHCL &candle // specified candle
)
{
//
bool result = false;
//
result = candle.open < candle.close;
//
return result;
}
bool IsBullishCandle(
int bar_index // candle index
)
{
//
bool result = false;
//
XOHCL candle = GetCandle(bar_index);
//
result = IsBullishCandle(candle);
//
return result;
}
bool IsBullishCandle(
string symbol, // specify symbol
ENUM_TIMEFRAMES period, // specify timeframe
int bar_index // candle index
)
{
//
bool result = false;
//
XOHCL candle = GetCandle(
symbol,
period,
bar_index);
//
result = IsBullishCandle(candle);
//
return result;
}
//
// Check a Candle is Bullish or not ...
bool IsBearishCandle(
XOHCL &candle // specified candle
)
{
//
bool result = false;
//
result = candle.open > candle.close;
//
return result;
}
bool IsBearishCandle(
int bar_index // candle index
)
{
//
bool result = false;
//
XOHCL candle = GetCandle(bar_index);
//
result = IsBearishCandle(candle);
//
return result;
}
bool IsBearishCandle(
string symbol, // specify symbol
ENUM_TIMEFRAMES period, // specify timeframe
int bar_index // candle index
)
{
//
bool result = false;
//
XOHCL candle = GetCandle(
symbol,
period,
bar_index);
//
result = IsBearishCandle(candle);
//
return result;
}
//
// Check Candle Body Bigger Than it's Shadow ...
bool HasStrongBody(XOHCL &candle)
{
//
double bodySize = MathAbs(candle.open - candle.close);
double shadowSize = MathAbs(candle.high - candle.low) - bodySize;
//
bool result = bodySize > (shadowSize * 1.5);
//
return result;
}
//
// Determines a cnadle is Swing High
// or Swing Low order block ...
ENUM_X_SWING_TYPE GetCandleSwing(int bar_index)
{
//
ENUM_X_SWING_TYPE result = X_NO_SWING;
//
// Reading Candle Models ...
XOHCL candles[3];
candles[0] = GetCandle(bar_index + 1);
candles[1] = GetCandle(bar_index + 2);
candles[2] = GetCandle(bar_index + 3);
//
// Check Swing Start Candle has Strong Body ...
bool isStrongBodyCandle = HasStrongBody(candles[2]);
if (!isStrongBodyCandle)
{
return result;
}
//
// Check Swing Low Conditions ...
bool isSwingLowOrderBlock =
candles[2].low < candles[1].low &&
candles[1].low < candles[0].low &&
candles[2].low < candles[0].low;
if (isSwingLowOrderBlock)
{
result = X_SWING_LOW;
}
//
// Check Swing High Conditions ...
bool isSwingHighOrderBlock =
candles[2].high > candles[1].high &&
candles[1].high > candles[0].high &&
candles[2].high > candles[0].high;
if (isSwingHighOrderBlock)
{
result = X_SWING_HIGH;
}
//
return result;
}
ENUM_X_SWING_TYPE GetCandleSwing(
ENUM_TIMEFRAMES period, // specify timeframe
int bar_index // specify candle index
)
{
//
ENUM_X_SWING_TYPE result = X_NO_SWING;
//
// Reading Candle Models ...
XOHCL candles[3];
candles[0] = GetCandle(period, bar_index + 1);
candles[1] = GetCandle(period, bar_index + 2);
candles[2] = GetCandle(period, bar_index + 3);
//
// Check Swing Start Candle has Strong Body ...
bool isStrongBodyCandle = HasStrongBody(candles[2]);
if (!isStrongBodyCandle)
{
return result;
}
//
// Check Swing Low Conditions ...
bool isSwingLowOrderBlock =
candles[2].low < candles[1].low &&
candles[1].low < candles[0].low &&
candles[2].low < candles[0].low;
if (isSwingLowOrderBlock)
{
result = X_SWING_LOW;
}
//
// Check Swing High Conditions ...
bool isSwingHighOrderBlock =
candles[2].high > candles[1].high &&
candles[1].high > candles[0].high &&
candles[2].high > candles[0].high;
if (isSwingHighOrderBlock)
{
result = X_SWING_HIGH;
}
//
return result;
}
ENUM_X_SWING_TYPE GetCandleSwing(
string symbol, // specify symbol
ENUM_TIMEFRAMES period, // specify timeframe
int bar_index // specify candle index
)
{
//
ENUM_X_SWING_TYPE result = X_NO_SWING;
//
// Reading Candle Models ...
XOHCL candles[3];
candles[0] = GetCandle(symbol, period, bar_index + 1);
candles[1] = GetCandle(symbol, period, bar_index + 2);
candles[2] = GetCandle(symbol, period, bar_index + 3);
//
// Check Swing Start Candle has Strong Body ...
bool isStrongBodyCandle = HasStrongBody(candles[2]);
if (!isStrongBodyCandle)
{
return result;
}
//
// Check Swing Low Conditions ...
bool isSwingLowOrderBlock =
candles[2].low < candles[1].low &&
candles[1].low < candles[0].low &&
candles[2].low < candles[0].low;
if (isSwingLowOrderBlock)
{
result = X_SWING_LOW;
}
//
// Check Swing High Conditions ...
bool isSwingHighOrderBlock =
candles[2].high > candles[1].high &&
candles[1].high > candles[0].high &&
candles[2].high > candles[0].high;
if (isSwingHighOrderBlock)
{
result = X_SWING_HIGH;
}
//
return result;
}
//
// Retrieve Market Highest High ...
double GetHighestHigh(
int mLength, // Which market Length use
int bar_index // bar index
)
{
//
double result = -1;
//
int rIdx = iHighest(
_Symbol,
_Period,
MODE_HIGH,
mLength,
bar_index);
//
result = iHigh(
_Symbol,
_Period,
rIdx);
//
return result;
}
double GetHighestHigh(
int mLength, // Which market Length use
int bar_index, // bar index
ENUM_SERIESMODE mode // Calculation mode
)
{
//
double result = -1;
//
int rIdx = iHighest(
_Symbol,
_Period,
mode,
mLength,
bar_index);
//
result = iHigh(
_Symbol,
_Period,
rIdx);
//
return result;
}
double GetHighestHigh(
string symbol, // Specified Symbol
ENUM_TIMEFRAMES period, // Specified Period
int mLength, // Which market Length use
int bar_index // bar index
)
{
//
double result = -1;
//
int rIdx = iHighest(
symbol,
period,
MODE_HIGH,
mLength,
bar_index);
//
result = iHigh(
symbol,
period,
rIdx);
//
return result;
}
double GetHighestHigh(
string symbol, // Specified Symbol
ENUM_TIMEFRAMES period, // Specified Period
ENUM_SERIESMODE mode, // Calculation mode
int mLength, // Which market Length use
int bar_index // bar index
)
{
//
double result = -1;
//
int rIdx = iHighest(
symbol,
period,
mode,
mLength,
bar_index);
//
result = iHigh(
symbol,
period,
rIdx);
//
return result;
}
//
// Retrieve Market Lowest Low ...
double GetLowestLow(
int mLength, // Which market Length use
int bar_index // bar index
)
{
//
double result = -1;
//
int rIdx = iLowest(
_Symbol,
_Period,
MODE_LOW,
mLength,
bar_index);
//
result = iLow(
_Symbol,
_Period,
rIdx);
//
return result;
}
double GetLowestLow(
int mLength, // Which market Length use
int bar_index, // bar index
ENUM_SERIESMODE mode // Calculation mode
)
{
//
double result = -1;
//
int rIdx = iLowest(
_Symbol,
_Period,
mode,
mLength,
bar_index);
//
result = iLow(
_Symbol,
_Period,
rIdx);
//
return result;
}
double GetLowestLow(
string symbol, // Specified Symbol
ENUM_TIMEFRAMES period, // Specified Period
int mLength, // Which market Length use
int bar_index // bar index
)
{
//
double result = -1;
//
int rIdx = iLowest(
symbol,
period,
MODE_LOW,
mLength,
bar_index);
//
result = iLow(
symbol,
period,
rIdx);
//
return result;
}
double GetLowestLow(
string symbol, // Specified Symbol
ENUM_TIMEFRAMES period, // Specified Period
ENUM_SERIESMODE mode, // Calculation mode
int mLength, // Which market Length use
int bar_index // bar index
)
{
//
double result = -1;
//
int rIdx = iLowest(
symbol,
period,
mode,
mLength,
bar_index);
//
result = iLow(
symbol,
period,
rIdx);
//
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;
}
//
// END Provided Functions ...
//
//
// Find Farest Time Frame ...
ENUM_TIMEFRAMES GetFarestTimeFrame()
{
//
ENUM_TIMEFRAMES result = PERIOD_MN1;
//
switch (_Period)
{
//
// M1-M30 ...
case PERIOD_M1:
case PERIOD_M2:
case PERIOD_M3:
case PERIOD_M4:
case PERIOD_M5:
case PERIOD_M6:
case PERIOD_M10:
case PERIOD_M12:
case PERIOD_M15:
case PERIOD_M20:
case PERIOD_M30:
result = PERIOD_D1;
break;
//
// H1-H12 ...
case PERIOD_H1:
case PERIOD_H2:
case PERIOD_H3:
case PERIOD_H4:
case PERIOD_H6:
case PERIOD_H8:
case PERIOD_H12:
result = PERIOD_W1;
break;
//
// D!, W1 ...
case PERIOD_D1:
case PERIOD_W1:
result = PERIOD_MN1;
break;
//
// Otherwise ...
default:
result = PERIOD_MN1;
}
//
return result;
}
//
// Find Medium Time Frame ...
ENUM_TIMEFRAMES GetMediumTimeFrame()
{
//
ENUM_TIMEFRAMES result = PERIOD_D1;
//
switch (_Period)
{
//
// M1-M30 ...
case PERIOD_M1:
case PERIOD_M2:
case PERIOD_M3:
case PERIOD_M4:
case PERIOD_M5:
case PERIOD_M6:
case PERIOD_M10:
case PERIOD_M12:
case PERIOD_M15:
case PERIOD_M20:
case PERIOD_M30:
result = PERIOD_H4;
break;
//
// H1-H12 ...
case PERIOD_H1:
case PERIOD_H2:
case PERIOD_H3:
case PERIOD_H4:
case PERIOD_H6:
case PERIOD_H8:
case PERIOD_H12:
result = PERIOD_D1;
break;
//
// D!, W1 ...
case PERIOD_D1:
result = PERIOD_W1;
break;
//
// Otherwise ...
default:
result = PERIOD_MN1;
}
//
return result;
}
//
// Find Smallest Time Frame ...
ENUM_TIMEFRAMES GetSmallestTimeFrame()
{
//
ENUM_TIMEFRAMES result = PERIOD_M5;
//
switch (_Period)
{
//
// M1-M5 ...
case PERIOD_M1:
case PERIOD_M2:
case PERIOD_M3:
case PERIOD_M4:
case PERIOD_M5:
result = _Period;
break;
//
// M6-M30 ...
case PERIOD_M6:
case PERIOD_M10:
case PERIOD_M12:
case PERIOD_M15:
case PERIOD_M20:
case PERIOD_M30:
result = PERIOD_M5;
break;
//
// H1-H12 ...
case PERIOD_H1:
case PERIOD_H2:
case PERIOD_H3:
case PERIOD_H4:
case PERIOD_H6:
case PERIOD_H8:
case PERIOD_H12:
result = PERIOD_H1;
break;
//
// D! ...
case PERIOD_D1:
result = PERIOD_H2;
break;
//
// Otherwise ...
default:
result = PERIOD_H4;
}
//
return result;
}
//
// Check first Cross Over second at bar_index ...
bool IsCrossOver(
const double &first[], // the buffer which check crossing over second buffer
const double &second[], // first buffer checks based on this buffer
const int bar_index // check crosses in specific index
)
{
//
bool result = false;
//
ArraySetAsSeries(first, true);
ArraySetAsSeries(second, true);
//
// Validate Args ...
if (ArraySize(first) < bar_index + 1 || ArraySize(second) < bar_index + 1)
{
return result;
}
//
result = first[bar_index] > second[bar_index] &&
!(first[bar_index + 1] > second[bar_index + 1]);
//
return result;
}
//
// Check first is Over second at bar_index ...
bool IsOver(
const double &first[], // the buffer which check over second buffer
const double &second[], // first buffer checks based on this buffer
const int bar_index // check crosses in specific index
)
{
//
bool result = false;
//
ArraySetAsSeries(first, true);
ArraySetAsSeries(second, true);
//
// Validate Args ...
if (ArraySize(first) < bar_index + 1 || ArraySize(second) < bar_index + 1)
{
return result;
}
//
result = first[bar_index] > second[bar_index] &&
first[bar_index + 1] > second[bar_index + 1];
//
return result;
}
//
// Check first Cross Under second at bar_index ...
bool IsCrossUnder(
const double &first[], // the buffer which check crossing under second buffer
const double &second[], // first buffer checks based on this buffer
const int bar_index // check crosses in specific index
)
{
//
bool result = false;
//
ArraySetAsSeries(first, true);
ArraySetAsSeries(second, true);
//
// Validate Args ...
if (ArraySize(first) < bar_index + 1 || ArraySize(second) < bar_index + 1)
{
return result;
}
//
result = first[bar_index] < second[bar_index] &&
!(first[bar_index + 1] < second[bar_index + 1]);
//
return result;
}
//
// Check first is Under second at bar_index ...
bool IsUnder(
const double &first[], // the buffer which check under second buffer
const double &second[], // first buffer checks based on this buffer
const int bar_index // check crosses in specific index
)
{
//
bool result = false;
//
ArraySetAsSeries(first, true);
ArraySetAsSeries(second, true);
//
// Validate Args ...
if (ArraySize(first) < bar_index + 1 || ArraySize(second) < bar_index + 1)
{
return result;
}
//
result = first[bar_index] < second[bar_index] &&
first[bar_index + 1] < second[bar_index + 1];
//
return result;
}
//
// Calculate Slope of Specified Buffer at Specified Index ...
double GetSlope(
int from, // In Past Index ...
int to, // In Past Index ...
double &buffer[])
{
//
double result = 0;
//
double toValue = buffer[to];
double fromValue = buffer[from];
double deltaValue = toValue - fromValue;
double deltaTime = from - to;
//
result = deltaValue / deltaTime;
//
return result;
}
//
// Check if a Value Increasing in Loopback ...
bool IsIncreasing(
int from, // Last Time Index
int to, // Fisr Tima Index
double &buffer[] // the Buffer which required to search
)
{
//
bool result = false;
//
// Validate Args ...
if (
from <= to ||
ArraySize(buffer) < from + 1)
{
return result;
}
//
// Loop through Items ...
double toValue = buffer[to];
double fromValue = buffer[from];
result = toValue > fromValue;
for (int i = to + 1; i <= from - 1; i++)
{
//
double iValue = buffer[i];
bool isPassed = toValue >= iValue;
//
bool isIIncreasing = IsIncreasing(
from,
i,
buffer);
//
// Check Result ...
result = result && isPassed && isIIncreasing;
if (!result)
{
break;
}
}
//
return result;
}
//
// Check if a Value Decreasing in Loopback ...
bool IsDecreasing(
int from, // Last Time Index
int to, // Fisr Tima Index
double &buffer[] // the Buffer which required to search
)
{
//
bool result = false;
//
// Validate Args ...
if (
from <= to ||
ArraySize(buffer) < from + 1)
{
return result;
}
//
// Loop through Items ...
double toValue = buffer[to];
double fromValue = buffer[from];
result = toValue < fromValue;
for (int i = to + 1; i <= from - 1; i++)
{
//
double iValue = buffer[i];
bool isPassed = toValue <= iValue;
//
bool isIDecreasing = IsDecreasing(
from,
i,
buffer);
//
// Check Result ...
result = result && isPassed && isIDecreasing;
if (!result)
{
break;
}
}
//
return result;
}
//
// Get Max Value of Specific Loopback of a Buffer ...
double GetMaxValue(
int mLoopback, // loopback length
double &buffer[] // the Buffer which required to search
)
{
//
double result = 0;
//
// Validate Args ...
if (
mLoopback < 2 ||
ArraySize(buffer) < mLoopback)
{
return result;
}
//
// Loop Through LoopBack ...
for (int i = 0; i < mLoopback; i++)
{
//
double iValue = buffer[i];
//
result =
//
result == 0 || result < iValue
//
? iValue
//
: result
//
;
}
//
return result;
}
//
// Get Min Value of Specific Loopback of a Buffer ...
double GetMinValue(
int mLoopback, // loopback length
double &buffer[] // the Buffer which required to search
)
{
//
double result = 0;
//
// Validate Args ...
if (
mLoopback < 2 ||
ArraySize(buffer) < mLoopback)
{
return result;
}
//
// Loop Through LoopBack ...
for (int i = 0; i < mLoopback; i++)
{
//
double iValue = buffer[i];
//
result =
//
result == 0 || result > iValue
//
? iValue
//
: result
//
;
}
//
return result;
}
//
bool IsInPeak(
int mLoopback, // loopback length
double &buffer[] // the Buffer which required to search
)
{
//
bool result = false;
//
// Validate Args ...
if (
mLoopback < 2 ||
ArraySize(buffer) < mLoopback + 1)
{
return result;
}
//
int q1Length = mLoopback / 4;
int halfLength = mLoopback / 2;
int q2Length = halfLength + q1Length;
//
double firstValue = buffer[1];
double q1Value = buffer[q1Length];
double q2Value = buffer[q2Length];
double halfValue = buffer[halfLength];
double lastValue = buffer[ArraySize(buffer) - 1];
//
// Peak ...
result =
//
firstValue > lastValue
//
&&
//
(
firstValue > q1Value &&
firstValue > halfValue)
//
;
//
return result;
}
//
bool IsInVale(
int mLoopback, // loopback length
double &buffer[] // the Buffer which required to search
)
{
//
bool result = false;
//
// Validate Args ...
if (
mLoopback < 2 ||
ArraySize(buffer) < mLoopback + 1)
{
return result;
}
//
int q1Length = mLoopback / 4;
int halfLength = mLoopback / 2;
int q2Length = halfLength + q1Length;
//
double firstValue = buffer[1];
double q1Value = buffer[q1Length];
double q2Value = buffer[q2Length];
double halfValue = buffer[halfLength];
double lastValue = buffer[ArraySize(buffer) - 1];
//
// Peak ...
result =
//
firstValue < lastValue
//
&&
//
(
firstValue < q1Value &&
firstValue < halfValue)
//
;
//
return result;
}
//
// Check Sharp Happens or not ...
bool IsSharpChanged(
double fromValue, // Past Value
double toValue, // Present Value
double maxValue, // Max in Range Value
double minValue // Min in Range Value
)
{
//
bool result = false;
//
// This Used In Increased or Decreased Sharp Detection ...
bool isIncreased = toValue > fromValue;
//
double toFromDelta = MathAbs(toValue - fromValue);
double minMaxDelta = maxValue - minValue;
//
result = toFromDelta >= (minMaxDelta / 3);
//
return result;
}
bool IsSharpChanged(
int from, // Past Index in Series
int to, // Present Index in Series
double &buffer[] // the Buffer which required to search
)
{
//
bool result = false;
//
// Validate Args ...
if (
from <= to ||
ArraySize(buffer) < from + 1)
{
return result;
}
//
// Loop through Items ...
double toValue = buffer[to];
double fromValue = buffer[from];
//
// This Used In Increased or Decreased Sharp Detection ...
bool isIncreased = toValue > fromValue;
//
double minValue = GetMinValue(
ArraySize(buffer) - 1,
buffer);
double maxValue = GetMaxValue(
ArraySize(buffer) - 1,
buffer);
double minMaxDelta = maxValue - minValue;
//
// Sharp Detection ...
result = IsSharpChanged(
fromValue,
toValue,
maxValue,
minValue);
//
return result;
}
bool IsSharpChanged(
int from, // Past Bar Index
int to, // Present Bar Index
ENUM_APPLIED_PRICE appliedPrice // Which Price Choose in From or To
)
{
//
bool result = false;
//
double toValue = GetAppliedPrice(
to,
appliedPrice);
double fromValue = GetAppliedPrice(
from,
appliedPrice);
//
int hIdx = iHighest(
_Symbol,
_Period,
MODE_HIGH,
from,
to);
double maxValue = iHigh(
_Symbol,
_Period,
hIdx);
//
int lIdx = iLowest(
_Symbol,
_Period,
MODE_LOW,
from,
to);
double minValue = iLow(
_Symbol,
_Period,
lIdx);
//
result = IsSharpChanged(
fromValue,
toValue,
maxValue,
minValue);
//
return result;
}
//
// Check InCreased Sharp Happens or not ...
bool IsSharpIncreasedChanged(
double fromValue, // Past Value
double toValue, // Present Value
double maxValue, // Max in Range Value
double minValue // Min in Range Value
)
{
//
bool result = false;
//
// This Used In Increased or Decreased Sharp Detection ...
bool isIncreased = toValue > fromValue;
//
double toFromDelta = MathAbs(toValue - fromValue);
double minMaxDelta = maxValue - minValue;
//
result =
isIncreased &&
toFromDelta >= (minMaxDelta / 3);
//
return result;
}
bool IsSharpIncreasedChanged(
int from, // Past Index in Series
int to, // Present Index in Series
double &buffer[] // the Buffer which required to search
)
{
//
bool result = false;
//
// Validate Args ...
if (
from <= to ||
ArraySize(buffer) < from + 1)
{
return result;
}
//
// Loop through Items ...
double toValue = buffer[to];
double fromValue = buffer[from];
//
double minValue = GetMinValue(
ArraySize(buffer) - 1,
buffer);
double maxValue = GetMaxValue(
ArraySize(buffer) - 1,
buffer);
double minMaxDelta = maxValue - minValue;
//
// Sharp Detection ...
result = IsSharpIncreasedChanged(
fromValue,
toValue,
maxValue,
minValue);
//
return result;
}
bool IsSharpIncreasedChanged(
int from, // Past Bar Index
int to, // Present Bar Index
ENUM_APPLIED_PRICE appliedPrice // Which Price Choose in From or To
)
{
//
bool result = false;
//
double toValue = GetAppliedPrice(
to,
appliedPrice);
double fromValue = GetAppliedPrice(
from,
appliedPrice);
//
int hIdx = iHighest(
_Symbol,
_Period,
MODE_HIGH,
from,
to);
double maxValue = iHigh(
_Symbol,
_Period,
hIdx);
//
int lIdx = iLowest(
_Symbol,
_Period,
MODE_LOW,
from,
to);
double minValue = iLow(
_Symbol,
_Period,
lIdx);
//
result = IsSharpIncreasedChanged(
fromValue,
toValue,
maxValue,
minValue);
//
return result;
}
//
// Check Decreased Sharp Happens or not ...
bool IsSharpDecreasedChanged(
double fromValue, // Past Value
double toValue, // Present Value
double maxValue, // Max in Range Value
double minValue // Min in Range Value
)
{
//
bool result = false;
//
// This Used In Increased or Decreased Sharp Detection ...
bool isIncreased = toValue > fromValue;
//
double toFromDelta = MathAbs(toValue - fromValue);
double minMaxDelta = maxValue - minValue;
//
result =
!isIncreased &&
toFromDelta >= (minMaxDelta / 3);
//
return result;
}
bool IsSharpDecreasedChanged(
int from, // Past Index in Series
int to, // Present Index in Series
double &buffer[] // the Buffer which required to search
)
{
//
bool result = false;
//
// Validate Args ...
if (
from <= to ||
ArraySize(buffer) < from + 1)
{
return result;
}
//
// Loop through Items ...
double toValue = buffer[to];
double fromValue = buffer[from];
//
double minValue = GetMinValue(
ArraySize(buffer) - 1,
buffer);
double maxValue = GetMaxValue(
ArraySize(buffer) - 1,
buffer);
double minMaxDelta = maxValue - minValue;
//
// Sharp Detection ...
result = IsSharpDecreasedChanged(
fromValue,
toValue,
maxValue,
minValue);
//
return result;
}
bool IsSharpDecreasedChanged(
int from, // Past Bar Index
int to, // Present Bar Index
ENUM_APPLIED_PRICE appliedPrice // Which Price Choose in From or To
)
{
//
bool result = false;
//
double toValue = GetAppliedPrice(
to,
appliedPrice);
double fromValue = GetAppliedPrice(
from,
appliedPrice);
//
int hIdx = iHighest(
_Symbol,
_Period,
MODE_HIGH,
from,
to);
double maxValue = iHigh(
_Symbol,
_Period,
hIdx);
//
int lIdx = iLowest(
_Symbol,
_Period,
MODE_LOW,
from,
to);
double minValue = iLow(
_Symbol,
_Period,
lIdx);
//
result = IsSharpDecreasedChanged(
fromValue,
toValue,
maxValue,
minValue);
//
return result;
}
//
// Validate Struct ...
bool IsValid(XSignal &model, ulong magicNumber)
{
//
bool result = false;
//
// Validate Signal Type ...
result = model.type != X_SIGNAL_UNKNOWN;
if (!result)
{
return result;
}
//
// Check Symbol ...
result = StringLen(model.symbol) == 0 ? true : model.symbol == _Symbol;
if (!result)
{
return result;
}
//
// Check magic number ...
result = model.magicNumber <= 0 ? true : model.magicNumber == magicNumber;
if (!result)
{
return result;
}
//
// Check Entry ...
result = model.entry > 0;
if (!result)
{
return result;
}
//
// Check SL ...
result = model.sl == 0 ? true : model.type == X_SIGNAL_LONG ? model.sl < model.entry
: model.sl > model.entry;
if (!result)
{
return result;
}
//
// Check TP ...
result = model.tp == 0 ? true : model.type == X_SIGNAL_LONG ? model.tp > model.entry
: model.tp < model.entry;
if (!result)
{
return result;
}
//
// Check Volume ...
result = model.volume > 0;
if (!result)
{
return result;
}
//
// Check Time ...
result = model.time > 0;
if (!result)
{
return result;
}
//
return result;
}
bool IsValid(XTPSL &model)
{
//
bool result = false;
//
result = model.entry > 0 && model.type != X_SIGNAL_UNKNOWN;
//
return result;
}
//
void CleanBuffer(int &buffer[])
{
//
ArrayFree(buffer);
ArrayResize(buffer, 0);
}
void CleanBuffer(double &buffer[])
{
//
ArrayFree(buffer);
ArrayResize(buffer, 0);
}
void CleanBuffer(XSignal &buffer[])
{
//
ArrayFree(buffer);
ArrayResize(buffer, 0);
}
//
void Add(
int item, // item want to add
int &buffer[] // Destination buffer
)
{
//
ArrayResize(
buffer,
ArraySize(buffer) + 1);
//
buffer[ArraySize(buffer) - 1] = item;
}
void Add(
double item, // item want to add
double &buffer[] // Destination buffer
)
{
//
ArrayResize(
buffer,
ArraySize(buffer) + 1);
//
buffer[ArraySize(buffer) - 1] = item;
}
void Add(
XOHCL &item, // item want to add
XOHCL &buffer[] // Destination buffer
)
{
//
ArrayResize(
buffer,
ArraySize(buffer) + 1);
//
buffer[ArraySize(buffer) - 1] = item;
}
void Add(
XSignal &item, // item want to add
XSignal &buffer[] // Destination buffer
)
{
//
ArrayResize(
buffer,
ArraySize(buffer) + 1);
//
buffer[ArraySize(buffer) - 1] = item;
}
//
// Check a Signal is Long or not ...
bool IsLong(ENUM_X_SIGNAL_TYPE type)
{
return type == X_SIGNAL_LONG;
}
bool IsLong(XSignal &signal)
{
//
bool result = IsLong(signal.type);
//
return result;
}
//
// Check a Signal is Short or not ...
bool IsShort(ENUM_X_SIGNAL_TYPE type)
{
return type == X_SIGNAL_SHORT;
}
bool IsShort(XSignal &signal)
{
//
bool result = IsShort(signal.type);
//
return result;
}
//
// Retrieve a Signal Type String Value ...
string GetSignalType(ENUM_X_SIGNAL_TYPE type)
{
//
string result = "";
//
if (type == X_SIGNAL_UNKNOWN)
{
return result;
}
//
result = type == X_SIGNAL_LONG ? "Long" : "Short";
//
return result;
}
string GetSignalType(XSignal &signal)
{
//
string result = "";
//
result = GetSignalType(signal.type);
//
return result;
}
//
// Check if a Signal is Primary or not ...
bool IsPrimary(XSignal &signal)
{
//
bool result = false;
//
int titlePosition = StringFind(signal.comment, "Primary");
result = titlePosition >= 0;
//
return result;
}
//
// Check if a Signal is Grid or not ...
bool IsGrid(XSignal &signal)
{
//
bool result = false;
//
int titlePosition = StringFind(signal.comment, "Grid");
result = titlePosition >= 0;
//
return result;
}
//
// Normalize a give Volume ...
double NormalizeVolume(
double volume // desired volume to normalize
) {
//
double result = volume;
//
// Normalize Volume ...
result = NormalizeDouble(result, 2);
//
double maxAvailableVolume = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double minAvailableVolume = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
//
// Validate Result ...
if (result > maxAvailableVolume)
{
result = maxAvailableVolume;
}
else if (result < minAvailableVolume)
{
result = minAvailableVolume;
}
//
return result;
}
double NormalizeVolume(
double volume, // desired volume to normalize
string symbol // Specified Symbol for Normalization
) {
//
double result = volume;
//
// Normalize Volume ...
result = NormalizeDouble(result, 2);
//
double maxAvailableVolume = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
double minAvailableVolume = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
//
// Validate Result ...
if (result > maxAvailableVolume)
{
result = maxAvailableVolume;
}
else if (result < minAvailableVolume)
{
result = minAvailableVolume;
}
//
return result;
}