#ifndef LIB_MATH_SIMPLE_IMPLEMENTATION_MQH_INCLUDED #define LIB_MATH_SIMPLE_IMPLEMENTATION_MQH_INCLUDED #property version "1.9"; /********************************************************************************** * Copyright (C) 2010-2022 Dominik Egert * * This file is the simple math main functions include file. * * MQLplus, including this file may not be copied and/or distributed * without explecit permit by the author. * Author Dominik Egert / Freie Netze UG. ********************************************************************************** * * Version: 1.9 * State: production * * File information * ================ * * Use: A collection of math functions * * * * */ #ifdef DBG_MSG_TRACE_FILE_LOADER DBG_MSG_TRACE_FILE_LOADER; #endif ///////////////////////////////////////////////////////////////////////////////////////////////////// // // Setup function macros // #define mqp_MathMinNN LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathMinNN)) #define mqp_MathRoot LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRoot)) #define mqp_MathRange LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRange)) #define mqp_MathRangeX LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRangeX)) #define mqp_MathRangeMatch LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRangeMatch)) #define mqp_MathRangeOverlap LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRangeOverlap)) #define mqp_MathDigits LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathDigits)) #define mqp_MathNeg LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathNeg)) #define mqp_MathRandN LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRandN)) #define mqp_MathRandX LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRandX)) #define mqp_MathZero LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathZero)) #define mqp_MathGreaterThan LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathGreaterThan)) #define mqp_MathLessThan LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathLessThan)) #define mqp_MathCompare LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathCompare)) #define mqp_MathRoundPow2 LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathRoundPow2)) #define mqp_MathFiboLevel LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathFiboLevel)) #define mqp_MathUID LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_MathUID)) #define mqp_RTCdeltaMilliseconds LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_RTCdeltaMilliseconds)) #define mqp_RTCdeltaMicroseconds LIB_NAMESPACE_ADD(LIB_NAMESPACE(numbers, _mqlplus_RTCdeltaMicroseconds)) // ///////////////////////////////////////////////////////////////////////////////////////////////////// /*********************************************************************************************************************************************************/ /* */ /* MQLplus overloaded functions and add ons */ /* */ /*********************************************************************************************************************************************************/ #ifndef __MQL4_COMPATIBILITY_CODE__ namespace LIB_MQLPLUS_LIBRARY_PREFIX { namespace numbers { #endif //+------------------------------------------------------------------+ //| MathMax | //+------------------------------------------------------------------+ const float LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const float value1, const float value2, const float value3, const float value4 = M_CONST_FLT_MAX_NEG, const float value5 = M_CONST_FLT_MAX_NEG, const float value6 = M_CONST_FLT_MAX_NEG, const float value7 = M_CONST_FLT_MAX_NEG, const float value8 = M_CONST_FLT_MAX_NEG, const float value9 = M_CONST_FLT_MAX_NEG, const float value10 = M_CONST_FLT_MAX_NEG) LIB_MQLPLUS_LIB_EXPORT { float val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } // Multi input function const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const double value1, const double value2, const double value3, const double value4 = M_CONST_DBL_MAX_NEG, const double value5 = M_CONST_DBL_MAX_NEG, const double value6 = M_CONST_DBL_MAX_NEG, const double value7 = M_CONST_DBL_MAX_NEG, const double value8 = M_CONST_DBL_MAX_NEG, const double value9 = M_CONST_DBL_MAX_NEG, const double value10 = M_CONST_DBL_MAX_NEG) LIB_MQLPLUS_LIB_EXPORT { double val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } // Multi input function const char LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const char value1, const char value2, const char value3, const char value4 = CHAR_MIN, const char value5 = CHAR_MIN, const char value6 = CHAR_MIN, const char value7 = CHAR_MIN, const char value8 = CHAR_MIN, const char value9 = CHAR_MIN, const char value10 = CHAR_MIN) LIB_MQLPLUS_LIB_EXPORT { char val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } // Multi input function const short LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const short value1, const short value2, const short value3, const short value4 = SHORT_MIN, const short value5 = SHORT_MIN, const short value6 = SHORT_MIN, const short value7 = SHORT_MIN, const short value8 = SHORT_MIN, const short value9 = SHORT_MIN, const short value10 = SHORT_MIN) LIB_MQLPLUS_LIB_EXPORT { short val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } // Multi input function const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const int value1, const int value2, const int value3, const int value4 = INT_MIN, const int value5 = INT_MIN, const int value6 = INT_MIN, const int value7 = INT_MIN, const int value8 = INT_MIN, const int value9 = INT_MIN, const int value10 = INT_MIN) LIB_MQLPLUS_LIB_EXPORT { int val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } // Multi input function const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const color value1, const color value2, const color value3, const color value4 = INT_MIN, const color value5 = INT_MIN, const color value6 = INT_MIN, const color value7 = INT_MIN, const color value8 = INT_MIN, const color value9 = INT_MIN, const color value10 = INT_MIN) LIB_MQLPLUS_LIB_EXPORT { color val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } // Multi input function const long LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const long value1, const long value2, const long value3, const long value4 = LONG_MIN, const long value5 = LONG_MIN, const long value6 = LONG_MIN, const long value7 = LONG_MIN, const long value8 = LONG_MIN, const long value9 = LONG_MIN, const long value10 = LONG_MIN) LIB_MQLPLUS_LIB_EXPORT { long val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } // Multi input function const long LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMax)( const datetime value1, const datetime value2, const datetime value3, const datetime value4 = LONG_MIN, const datetime value5 = LONG_MIN, const datetime value6 = LONG_MIN, const datetime value7 = LONG_MIN, const datetime value8 = LONG_MIN, const datetime value9 = LONG_MIN, const datetime value10 = LONG_MIN) LIB_MQLPLUS_LIB_EXPORT { long val_a = inline_MathMax(value1, value2); val_a = inline_MathMax(val_a, value3); val_a = inline_MathMax(val_a, value4); val_a = inline_MathMax(val_a, value5); val_a = inline_MathMax(val_a, value6); val_a = inline_MathMax(val_a, value7); val_a = inline_MathMax(val_a, value8); val_a = inline_MathMax(val_a, value9); val_a = inline_MathMax(val_a, value10); return(val_a); } //+------------------------------------------------------------------+ //| MathMin | //+------------------------------------------------------------------+ // Multi input function const float LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const float value1, const float value2, const float value3, const float value4 = FLT_MAX, const float value5 = FLT_MAX, const float value6 = FLT_MAX, const float value7 = FLT_MAX, const float value8 = FLT_MAX, const float value9 = FLT_MAX, const float value10 = FLT_MAX) LIB_MQLPLUS_LIB_EXPORT { float val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } // Multi input function const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const double value1, const double value2, const double value3, const double value4 = DBL_MAX, const double value5 = DBL_MAX, const double value6 = DBL_MAX, const double value7 = DBL_MAX, const double value8 = DBL_MAX, const double value9 = DBL_MAX, const double value10 = DBL_MAX) LIB_MQLPLUS_LIB_EXPORT { double val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } // Multi input function const char LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const char value1, const char value2, const char value3, const char value4 = CHAR_MAX, const char value5 = CHAR_MAX, const char value6 = CHAR_MAX, const char value7 = CHAR_MAX, const char value8 = CHAR_MAX, const char value9 = CHAR_MAX, const char value10 = CHAR_MAX) LIB_MQLPLUS_LIB_EXPORT { char val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } // Multi input function const short LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const short value1, const short value2, const short value3, const short value4 = SHORT_MAX, const short value5 = SHORT_MAX, const short value6 = SHORT_MAX, const short value7 = SHORT_MAX, const short value8 = SHORT_MAX, const short value9 = SHORT_MAX, const short value10 = SHORT_MAX) LIB_MQLPLUS_LIB_EXPORT { short val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } // Multi input function const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const int value1, const int value2, const int value3, const int value4 = INT_MAX, const int value5 = INT_MAX, const int value6 = INT_MAX, const int value7 = INT_MAX, const int value8 = INT_MAX, const int value9 = INT_MAX, const int value10 = INT_MAX) LIB_MQLPLUS_LIB_EXPORT { int val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } // Multi input function const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const color value1, const color value2, const color value3, const color value4 = INT_MAX, const color value5 = INT_MAX, const color value6 = INT_MAX, const color value7 = INT_MAX, const color value8 = INT_MAX, const color value9 = INT_MAX, const color value10 = INT_MAX) LIB_MQLPLUS_LIB_EXPORT { color val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } // Multi input function const long LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const long value1, const long value2, const long value3, const long value4 = LONG_MAX, const long value5 = LONG_MAX, const long value6 = LONG_MAX, const long value7 = LONG_MAX, const long value8 = LONG_MAX, const long value9 = LONG_MAX, const long value10 = LONG_MAX) LIB_MQLPLUS_LIB_EXPORT { long val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } // Multi input function const long LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMin)( const datetime value1, const datetime value2, const datetime value3, const datetime value4 = LONG_MAX, const datetime value5 = LONG_MAX, const datetime value6 = LONG_MAX, const datetime value7 = LONG_MAX, const datetime value8 = LONG_MAX, const datetime value9 = LONG_MAX, const datetime value10 = LONG_MAX) LIB_MQLPLUS_LIB_EXPORT { long val_a = inline_MathMin(value1, value2); val_a = inline_MathMin(val_a, value3); val_a = inline_MathMin(val_a, value4); val_a = inline_MathMin(val_a, value5); val_a = inline_MathMin(val_a, value6); val_a = inline_MathMin(val_a, value7); val_a = inline_MathMin(val_a, value8); val_a = inline_MathMin(val_a, value9); val_a = inline_MathMin(val_a, value10); return(val_a); } //+------------------------------------------------------------------+ //| MathMinNN not null | //+------------------------------------------------------------------+ // Multi input function, ingore zeros const float LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)( const float value1, const float value2, const float value3 = FLT_MAX, const float value4 = FLT_MAX, const float value5 = FLT_MAX, const float value6 = FLT_MAX, const float value7 = FLT_MAX, const float value8 = FLT_MAX, const float value9 = FLT_MAX, const float value10 = FLT_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? FLT_MAX : value1), ((value2 == NULL) ? FLT_MAX : value2), ((value3 == NULL) ? FLT_MAX : value3), ((value4 == NULL) ? FLT_MAX : value4), ((value5 == NULL) ? FLT_MAX : value5), ((value6 == NULL) ? FLT_MAX : value6), ((value7 == NULL) ? FLT_MAX : value7), ((value8 == NULL) ? FLT_MAX : value8), ((value9 == NULL) ? FLT_MAX : value9), ((value10 == NULL) ? FLT_MAX : value10)) ); } // Multi input function, ingore zeros const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)(const double value1, const double value2, const double value3 = DBL_MAX, const double value4 = DBL_MAX, const double value5 = DBL_MAX, const double value6 = DBL_MAX, const double value7 = DBL_MAX, const double value8 = DBL_MAX, const double value9 = DBL_MAX, const double value10 = DBL_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? DBL_MAX : value1), ((value2 == NULL) ? DBL_MAX : value2), ((value3 == NULL) ? DBL_MAX : value3), ((value4 == NULL) ? DBL_MAX : value4), ((value5 == NULL) ? DBL_MAX : value5), ((value6 == NULL) ? DBL_MAX : value6), ((value7 == NULL) ? DBL_MAX : value7), ((value8 == NULL) ? DBL_MAX : value8), ((value9 == NULL) ? DBL_MAX : value9), ((value10 == NULL) ? DBL_MAX : value10)) ); } // Multi input function, ingore zeros const char LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)( const char value1, const char value2, const char value3 = CHAR_MAX, const char value4 = CHAR_MAX, const char value5 = CHAR_MAX, const char value6 = CHAR_MAX, const char value7 = CHAR_MAX, const char value8 = CHAR_MAX, const char value9 = CHAR_MAX, const char value10 = CHAR_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? CHAR_MAX : value1), ((value2 == NULL) ? CHAR_MAX : value2), ((value3 == NULL) ? CHAR_MAX : value3), ((value4 == NULL) ? CHAR_MAX : value4), ((value5 == NULL) ? CHAR_MAX : value5), ((value6 == NULL) ? CHAR_MAX : value6), ((value7 == NULL) ? CHAR_MAX : value7), ((value8 == NULL) ? CHAR_MAX : value8), ((value9 == NULL) ? CHAR_MAX : value9), ((value10 == NULL) ? CHAR_MAX : value10)) ); } // Multi input function, ingore zeros const short LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)( const short value1, const short value2, const short value3 = SHORT_MAX, const short value4 = SHORT_MAX, const short value5 = SHORT_MAX, const short value6 = SHORT_MAX, const short value7 = SHORT_MAX, const short value8 = SHORT_MAX, const short value9 = SHORT_MAX, const short value10 = SHORT_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? SHORT_MAX : value1), ((value2 == NULL) ? SHORT_MAX : value2), ((value3 == NULL) ? SHORT_MAX : value3), ((value4 == NULL) ? SHORT_MAX : value4), ((value5 == NULL) ? SHORT_MAX : value5), ((value6 == NULL) ? SHORT_MAX : value6), ((value7 == NULL) ? SHORT_MAX : value7), ((value8 == NULL) ? SHORT_MAX : value8), ((value9 == NULL) ? SHORT_MAX : value9), ((value10 == NULL) ? SHORT_MAX : value10)) ); } // Multi input function, ingore zeros const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)( const int value1, const int value2, const int value3 = INT_MAX, const int value4 = INT_MAX, const int value5 = INT_MAX, const int value6 = INT_MAX, const int value7 = INT_MAX, const int value8 = INT_MAX, const int value9 = INT_MAX, const int value10 = INT_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? INT_MAX : value1), ((value2 == NULL) ? INT_MAX : value2), ((value3 == NULL) ? INT_MAX : value3), ((value4 == NULL) ? INT_MAX : value4), ((value5 == NULL) ? INT_MAX : value5), ((value6 == NULL) ? INT_MAX : value6), ((value7 == NULL) ? INT_MAX : value7), ((value8 == NULL) ? INT_MAX : value8), ((value9 == NULL) ? INT_MAX : value9), ((value10 == NULL) ? INT_MAX : value10)) ); } // Multi input function, ingore zeros const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)( const color value1, const color value2, const color value3 = INT_MAX, const color value4 = INT_MAX, const color value5 = INT_MAX, const color value6 = INT_MAX, const color value7 = INT_MAX, const color value8 = INT_MAX, const color value9 = INT_MAX, const color value10 = INT_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? INT_MAX : value1), ((value2 == NULL) ? INT_MAX : value2), ((value3 == NULL) ? INT_MAX : value3), ((value4 == NULL) ? INT_MAX : value4), ((value5 == NULL) ? INT_MAX : value5), ((value6 == NULL) ? INT_MAX : value6), ((value7 == NULL) ? INT_MAX : value7), ((value8 == NULL) ? INT_MAX : value8), ((value9 == NULL) ? INT_MAX : value9), ((value10 == NULL) ? INT_MAX : value10)) ); } // Multi input function, ingore zeros const long LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)( const long value1, const long value2, const long value3 = LONG_MAX, const long value4 = LONG_MAX, const long value5 = LONG_MAX, const long value6 = LONG_MAX, const long value7 = LONG_MAX, const long value8 = LONG_MAX, const long value9 = LONG_MAX, const long value10 = LONG_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? LONG_MAX : value1), ((value2 == NULL) ? LONG_MAX : value2), ((value3 == NULL) ? LONG_MAX : value3), ((value4 == NULL) ? LONG_MAX : value4), ((value5 == NULL) ? LONG_MAX : value5), ((value6 == NULL) ? LONG_MAX : value6), ((value7 == NULL) ? LONG_MAX : value7), ((value8 == NULL) ? LONG_MAX : value8), ((value9 == NULL) ? LONG_MAX : value9), ((value10 == NULL) ? LONG_MAX : value10)) ); } // Multi input function, ingore zeros const long LIB_NAMESPACE_DEF(numbers, _mqlplus_MathMinNN)( const datetime value1, const datetime value2, const datetime value3 = LONG_MAX, const datetime value4 = LONG_MAX, const datetime value5 = LONG_MAX, const datetime value6 = LONG_MAX, const datetime value7 = LONG_MAX, const datetime value8 = LONG_MAX, const datetime value9 = LONG_MAX, const datetime value10 = LONG_MAX) LIB_MQLPLUS_LIB_EXPORT { return( LIB_NAMESPACE(numbers, _mqlplus_MathMin)(((value1 == NULL) ? LONG_MAX : value1), ((value2 == NULL) ? LONG_MAX : value2), ((value3 == NULL) ? LONG_MAX : value3), ((value4 == NULL) ? LONG_MAX : value4), ((value5 == NULL) ? LONG_MAX : value5), ((value6 == NULL) ? LONG_MAX : value6), ((value7 == NULL) ? LONG_MAX : value7), ((value8 == NULL) ? LONG_MAX : value8), ((value9 == NULL) ? LONG_MAX : value9), ((value10 == NULL) ? LONG_MAX : value10)) ); } //+------------------------------------------------------------------+ //| MathGreaterThan() | //+------------------------------------------------------------------+ const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathGreaterThan)( const double reference, const double value1, const double value2, const double value3 = M_CONST_DBL_MAX_NEG, const double value4 = M_CONST_DBL_MAX_NEG, const double value5 = M_CONST_DBL_MAX_NEG, const double value6 = M_CONST_DBL_MAX_NEG, const double value7 = M_CONST_DBL_MAX_NEG, const double value8 = M_CONST_DBL_MAX_NEG, const double value9 = M_CONST_DBL_MAX_NEG, const double value10 = M_CONST_DBL_MAX_NEG) LIB_MQLPLUS_LIB_EXPORT { const double v1_diff = value1 - reference; const double v2_diff = value2 - reference; const double v3_diff = value3 - reference; const double v4_diff = value4 - reference; const double v5_diff = value5 - reference; const double v6_diff = value6 - reference; const double v7_diff = value7 - reference; const double v8_diff = value8 - reference; const double v9_diff = value9 - reference; const double v10_diff = value10 - reference; return(reference + LIB_NAMESPACE(numbers, _mqlplus_MathMin)( (v1_diff < NULL) ? DBL_MAX : v1_diff, (v2_diff < NULL) ? DBL_MAX : v2_diff, (v3_diff < NULL) ? DBL_MAX : v3_diff, (v4_diff < NULL) ? DBL_MAX : v4_diff, (v5_diff < NULL) ? DBL_MAX : v5_diff, (v6_diff < NULL) ? DBL_MAX : v6_diff, (v7_diff < NULL) ? DBL_MAX : v7_diff, (v8_diff < NULL) ? DBL_MAX : v8_diff, (v9_diff < NULL) ? DBL_MAX : v9_diff, (v10_diff < NULL) ? DBL_MAX : v10_diff)); } const float LIB_NAMESPACE_DEF(numbers, _mqlplus_MathGreaterThan)( const float reference, const float value1, const float value2, const float value3 = M_CONST_FLT_MAX_NEG, const float value4 = M_CONST_FLT_MAX_NEG, const float value5 = M_CONST_FLT_MAX_NEG, const float value6 = M_CONST_FLT_MAX_NEG, const float value7 = M_CONST_FLT_MAX_NEG, const float value8 = M_CONST_FLT_MAX_NEG, const float value9 = M_CONST_FLT_MAX_NEG, const float value10 = M_CONST_FLT_MAX_NEG) LIB_MQLPLUS_LIB_EXPORT { const float v1_diff = value1 - reference; const float v2_diff = value2 - reference; const float v3_diff = value3 - reference; const float v4_diff = value4 - reference; const float v5_diff = value5 - reference; const float v6_diff = value6 - reference; const float v7_diff = value7 - reference; const float v8_diff = value8 - reference; const float v9_diff = value9 - reference; const float v10_diff = value10 - reference; return(reference + LIB_NAMESPACE(numbers, _mqlplus_MathMin)( (v1_diff < NULL) ? FLT_MAX : v1_diff, (v2_diff < NULL) ? FLT_MAX : v2_diff, (v3_diff < NULL) ? FLT_MAX : v3_diff, (v4_diff < NULL) ? FLT_MAX : v4_diff, (v5_diff < NULL) ? FLT_MAX : v5_diff, (v6_diff < NULL) ? FLT_MAX : v6_diff, (v7_diff < NULL) ? FLT_MAX : v7_diff, (v8_diff < NULL) ? FLT_MAX : v8_diff, (v9_diff < NULL) ? FLT_MAX : v9_diff, (v10_diff < NULL) ? FLT_MAX : v10_diff)); } //+------------------------------------------------------------------+ //| MathLessThan() | //+------------------------------------------------------------------+ const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathLessThan)( const double reference, const double value1, const double value2, const double value3 = DBL_MAX, const double value4 = DBL_MAX, const double value5 = DBL_MAX, const double value6 = DBL_MAX, const double value7 = DBL_MAX, const double value8 = DBL_MAX, const double value9 = DBL_MAX, const double value10 = DBL_MAX) LIB_MQLPLUS_LIB_EXPORT { const double v1_diff = reference - value1; const double v2_diff = reference - value2; const double v3_diff = reference - value3; const double v4_diff = reference - value4; const double v5_diff = reference - value5; const double v6_diff = reference - value6; const double v7_diff = reference - value7; const double v8_diff = reference - value8; const double v9_diff = reference - value9; const double v10_diff = reference - value10; return(reference - LIB_NAMESPACE(numbers, _mqlplus_MathMin)( (v1_diff < NULL) ? DBL_MAX : v1_diff, (v2_diff < NULL) ? DBL_MAX : v2_diff, (v3_diff < NULL) ? DBL_MAX : v3_diff, (v4_diff < NULL) ? DBL_MAX : v4_diff, (v5_diff < NULL) ? DBL_MAX : v5_diff, (v6_diff < NULL) ? DBL_MAX : v6_diff, (v7_diff < NULL) ? DBL_MAX : v7_diff, (v8_diff < NULL) ? DBL_MAX : v8_diff, (v9_diff < NULL) ? DBL_MAX : v9_diff, (v10_diff < NULL) ? DBL_MAX : v10_diff)); } const float LIB_NAMESPACE_DEF(numbers, _mqlplus_MathLessThan)( const float reference, const float value1, const float value2, const float value3 = FLT_MAX, const float value4 = FLT_MAX, const float value5 = FLT_MAX, const float value6 = FLT_MAX, const float value7 = FLT_MAX, const float value8 = FLT_MAX, const float value9 = FLT_MAX, const float value10 = FLT_MAX) LIB_MQLPLUS_LIB_EXPORT { const float v1_diff = reference - value1; const float v2_diff = reference - value2; const float v3_diff = reference - value3; const float v4_diff = reference - value4; const float v5_diff = reference - value5; const float v6_diff = reference - value6; const float v7_diff = reference - value7; const float v8_diff = reference - value8; const float v9_diff = reference - value9; const float v10_diff = reference - value10; return(reference - LIB_NAMESPACE(numbers, _mqlplus_MathMin)( (v1_diff < NULL) ? FLT_MAX : v1_diff, (v2_diff < NULL) ? FLT_MAX : v2_diff, (v3_diff < NULL) ? FLT_MAX : v3_diff, (v4_diff < NULL) ? FLT_MAX : v4_diff, (v5_diff < NULL) ? FLT_MAX : v5_diff, (v6_diff < NULL) ? FLT_MAX : v6_diff, (v7_diff < NULL) ? FLT_MAX : v7_diff, (v8_diff < NULL) ? FLT_MAX : v8_diff, (v9_diff < NULL) ? FLT_MAX : v9_diff, (v10_diff < NULL) ? FLT_MAX : v10_diff)); } const char LIB_NAMESPACE_DEF(numbers, _mqlplus_MathLessThan)( const char reference, const char value1, const char value2, const char value3 = CHAR_MAX, const char value4 = CHAR_MAX, const char value5 = CHAR_MAX, const char value6 = CHAR_MAX, const char value7 = CHAR_MAX, const char value8 = CHAR_MAX, const char value9 = CHAR_MAX, const char value10 = CHAR_MAX) LIB_MQLPLUS_LIB_EXPORT { const char v1_diff = reference - value1; const char v2_diff = reference - value2; const char v3_diff = reference - value3; const char v4_diff = reference - value4; const char v5_diff = reference - value5; const char v6_diff = reference - value6; const char v7_diff = reference - value7; const char v8_diff = reference - value8; const char v9_diff = reference - value9; const char v10_diff = reference - value10; return(reference - LIB_NAMESPACE(numbers, _mqlplus_MathMin)( (v1_diff < NULL) ? CHAR_MAX : v1_diff, (v2_diff < NULL) ? CHAR_MAX : v2_diff, (v3_diff < NULL) ? CHAR_MAX : v3_diff, (v4_diff < NULL) ? CHAR_MAX : v4_diff, (v5_diff < NULL) ? CHAR_MAX : v5_diff, (v6_diff < NULL) ? CHAR_MAX : v6_diff, (v7_diff < NULL) ? CHAR_MAX : v7_diff, (v8_diff < NULL) ? CHAR_MAX : v8_diff, (v9_diff < NULL) ? CHAR_MAX : v9_diff, (v10_diff < NULL) ? CHAR_MAX : v10_diff)); } //+------------------------------------------------------------------+ //| MathDigits | //+------------------------------------------------------------------+ const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathDigits)(const float value, const int max_digits = 9) LIB_MQLPLUS_LIB_EXPORT { if(value > (1.0 - DBL_MIN)) { return((int)(::MathFloor(::MathLog10(::MathAbs((double)value)))) + 1); } // Return return((int)(::MathRound(::MathAbs(::MathLog10((double)value))))); } const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathDigits)(const double value, const int max_digits = 16) LIB_MQLPLUS_LIB_EXPORT { if(value > (1.0 - DBL_MIN)) { return((int)(::MathFloor(::MathLog10(::MathAbs(value)))) + 1); } // Return return(inline_MathDigits(value)); } //+------------------------------------------------------------------+ //| MathCompare | //+------------------------------------------------------------------+ const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathCompare)(const double value1, const double value2, const int digits) LIB_MQLPLUS_LIB_EXPORT { // Local init static int c_digits = NULL; static double digit_f[]; // Update cache if(c_digits <= digits) { ArrayResize(digit_f, digits + 1); for(int cnt = c_digits; (cnt <= digits) && !_StopFlag; cnt++) { digit_f[cnt] = MathPow(10.0, cnt); } c_digits = digits + 1; } // Convert input const long v_1 = (long)(::MathRound(value1 * digit_f[digits])); const long v_2 = (long)(::MathRound(value2 * digit_f[digits])); // Return return((v_1 != v_2) * ((v_1 > v_2) ? 1 : -1)); } //+------------------------------------------------------------------+ //| MathInverseSqrt | //+------------------------------------------------------------------+ const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathInverseSqrt)(const double value) { union l_d_i32 { ulong ui; double f; } i; i.f = value; i.ui = 0x5FE6Eb50C7B537A9 - (i.ui >> 0x01); return((i.f * (1.5 - ((value * 0.5) * i.f * i.f)))); } //+------------------------------------------------------------------+ //| MathRoundPow2 | //+------------------------------------------------------------------+ const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathRoundPow2)(const int in) LIB_MQLPLUS_LIB_EXPORT { int result = in; result--; result |= result >> 1; result |= result >> 2; result |= result >> 4; result |= result >> 8; result |= result >> 16; // Return return(result + 1); } //+------------------------------------------------------------------+ //| MathFiboLevel() | //+------------------------------------------------------------------+ const int LIB_NAMESPACE_DEF(numbers, _mqlplus_MathFiboLevel)(const double value, const bool fx_lvl = true) LIB_MQLPLUS_LIB_EXPORT { // Local init const static double digit_f = 1.0 / (Point()); static double fib_lvls[]; static double fx_lvls[]; static int lvl_size = NULL; int cnt = NULL; int retval = NULL; // Check input if(value <= NULL) { return(NULL); } // Initialize array if(lvl_size == NULL) { // FX levels ::ArrayResize(fx_lvls, 10); for(cnt = NULL; (cnt < 10) && !_StopFlag; cnt++) { fx_lvls[cnt] = LIB_NAMESPACE(numbers, _mqlplus_MathFiboLevel)(cnt); } // Fibonacci levels lvl_size = ::ArrayResize(fib_lvls, 20); for(cnt = 3; (cnt < 20) && !_StopFlag; cnt++) { fib_lvls[cnt] = LIB_NAMESPACE(numbers, _mqlplus_MathFiboLevel)(cnt, NULL, 1.0) * digit_f; } } // Extend array as needed if(fib_lvls[lvl_size -1] < value) { ::ArrayResize(fib_lvls, lvl_size, (int)(::MathPow(value, M_CONST_M_DIV1_GR))); while( (fib_lvls[lvl_size - 1] < value) && (!_StopFlag) ) { lvl_size = ::ArrayResize(fib_lvls, lvl_size + 1); fib_lvls[lvl_size - 1] = LIB_NAMESPACE(numbers, _mqlplus_MathFiboLevel)(lvl_size, NULL, 1.0); } } // Match input value to fx levels if(fx_lvl) { // Precheck input if(value > fx_lvls[9]) { return(9); } while( (fx_lvls[retval] < value) && (!_StopFlag) ) { retval++; } // Check closest level return((retval - ((::MathAbs(fx_lvls[retval - 1] - value)) < (fx_lvls[retval] - value))) + 1); } // Match input value to fibo level while( (fib_lvls[retval] < value) && (!_StopFlag) ) { retval++; } // Return return((retval - ((::MathAbs(fib_lvls[retval - 1] - value)) < (fib_lvls[retval] - value))) + 1); } //+------------------------------------------------------------------+ //| MathFiboLevel() | //+------------------------------------------------------------------+ const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathFiboLevel)(const int level) LIB_MQLPLUS_LIB_EXPORT { return(LIB_NAMESPACE(numbers, _mqlplus_MathFiboLevel)(level, NULL, 1.0, false, true)); } const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathFiboLevel)(const int level, const double range_start, const double range_end, const bool ratio_levels = false, const bool fx_levels = false) LIB_MQLPLUS_LIB_EXPORT { // Cache init static double _point = Point(); static int _count = -1; static bool fx_init = false; static double fx_r[10]; static double _levels[]; static double _ratios[]; static int fib_sequence[]; bool _fx_levels = (fx_levels) && (level < 10); // Local init const double _range = ::MathAbs(range_start - range_end); const int _level = (fx_levels) ? 11 : level; int cnt = NULL; // Fibonacci sequence if(_count <= _level) { // Fib sequence for(cnt = LIB_NAMESPACE(numbers, _mqlplus_MathMax)(0, _count); (cnt <= _level) && !_StopFlag; cnt++) { _count = ::ArrayResize(fib_sequence, cnt + 1, level + 1); fib_sequence[cnt] = (_count < 3) ? (_count > 1) : fib_sequence[cnt - 2] + fib_sequence[cnt - 1]; } // Fib levels ::ArrayResize(_levels, _count); for(cnt = (::ArraySize(_levels)); (cnt < _count) && !_StopFlag; cnt++) { _levels[cnt] = fib_sequence[cnt] * _point; } } // Fib ratios if(ratio_levels) { ::ArrayResize(_ratios, _count); for(cnt = (::ArraySize(_ratios)); (cnt < _count) && !_StopFlag; cnt++) { _ratios[(_count - 1) - cnt] = (fib_sequence[_count - 1] == NULL) ? NULL : ((double)fib_sequence[(_count - 1) - cnt] / (double)fib_sequence[_count - 1]); } } // FX fib levels if(_fx_levels) { if(!fx_init) { fx_r[0] = NULL; fx_r[1] = LIB_NAMESPACE(numbers, _mqlplus_MathRound)(((double)fib_sequence[8]) / ((double)fib_sequence[11]) * 100.0, 1); fx_r[2] = LIB_NAMESPACE(numbers, _mqlplus_MathRound)(((double)fib_sequence[9]) / ((double)fib_sequence[11]) * 100.0, 1); fx_r[3] = 50.0; fx_r[4] = LIB_NAMESPACE(numbers, _mqlplus_MathRound)(((double)fib_sequence[10]) / ((double)fib_sequence[11]) * 100.0, 1); fx_r[5] = 100.0 - fx_r[1]; fx_r[6] = 100.0; fx_r[7] = 100.0 + fx_r[4]; fx_r[8] = 200.0 + fx_r[4]; fx_r[9] = 400.0 + fx_r[1]; fx_init = true; } // Return fx_fibo levels return((level < 10) ? fx_r[level] : NULL); } // Calculate levels if(ratio_levels) { return((range_start < range_end) ? range_start + (_range * _ratios[level]) : range_start - (_range * _ratios[level])); } // Return return((range_start <= range_end) ? range_start + _levels[level] : range_start - _levels[level]); } //+------------------------------------------------------------------+ //| MathUID() | //+------------------------------------------------------------------+ // Microsecond unix timestamp as uID. const ulong LIB_NAMESPACE_DEF(numbers, _mqlplus_MathUID)() LIB_MQLPLUS_LIB_EXPORT { // Local init union _64bit { ulong ul; uchar uc[8]; _64bit(const ulong p_in) : ul(p_in) {}; }; const datetime _tm = ::TimeGMT(); const ulong _ms = ::GetTickCount(); const ulong _mc = ::GetMicrosecondCount(); const _64bit uid = (ulong)_tm + _ms + _mc; uchar result[]; uchar key[4]; // Time based UUID if(CryptEncode(CRYPT_HASH_SHA1, uid.uc, key, result) > NULL) { _64bit cnv = NULL; ArrayCopy(cnv.uc, result, 0, 0, 8); return(cnv.ul); } // Return return(-1); } //+------------------------------------------------------------------+ //| Can given value be evaluated to be zero | //+------------------------------------------------------------------+ const bool LIB_NAMESPACE_DEF(numbers, _mqlplus_MathZero)(const float value) LIB_MQLPLUS_LIB_EXPORT { return( ((value <= M_CONST_FLT_NULL_P) && (value >= M_CONST_FLT_NULL_N)) || (value == (float)EMPTY_VALUE) || !(::MathIsValidNumber((double)value)) ); } //+------------------------------------------------------------------+ //| Can given value be evaluated to be zero | //+------------------------------------------------------------------+ const bool LIB_NAMESPACE_DEF(numbers, _mqlplus_MathZero)(const double value) LIB_MQLPLUS_LIB_EXPORT { return( ((value <= M_CONST_DBL_NULL_P) && (value >= M_CONST_DBL_NULL_N)) || (value == EMPTY_VALUE) || !(::MathIsValidNumber(value)) ); } //+------------------------------------------------------------------+ //| RTCdeltaMilliseconds() | //+------------------------------------------------------------------+ const ulong LIB_NAMESPACE_DEF(numbers, _mqlplus_RTCdeltaMilliseconds)(const bool recalibrate = false) LIB_MQLPLUS_LIB_EXPORT { // Static init static bool is_init = false; static ulong _delta = NULL; // Check state if( (is_init) && (!recalibrate) ) { return(_delta); } // Local init const bool live = !( (::MQLInfoInteger(MQL_DEBUG)) || (::MQLInfoInteger(MQL_PROFILER)) || (::MQLInfoInteger(MQL_TESTER)) || (::MQLInfoInteger(MQL_FORWARD)) || (::MQLInfoInteger(MQL_OPTIMIZATION)) || (::MQLInfoInteger(MQL_VISUAL_MODE)) || (::MQLInfoInteger(MQL_FRAME_MODE)) ); const long init_ts = ::TimeLocal(); // Calibrate while( (live) && (::TimeLocal() == init_ts) ) { _delta = GetMicrosecondCount(); } // Save results is_init = true; _delta /= 1000; _delta %= 1000; // Return return(_delta); } //+------------------------------------------------------------------+ //| RTCdeltaMicroseconds() | //+------------------------------------------------------------------+ const ulong LIB_NAMESPACE_DEF(numbers, _mqlplus_RTCdeltaMicroseconds)(const bool recalibrate = false) LIB_MQLPLUS_LIB_EXPORT { // Static init static bool is_init = false; static ulong _delta = NULL; // Check state if( (is_init) && (!recalibrate) ) { return(_delta); } // Local init const bool live = !( (::MQLInfoInteger(MQL_DEBUG)) || (::MQLInfoInteger(MQL_PROFILER)) || (::MQLInfoInteger(MQL_TESTER)) || (::MQLInfoInteger(MQL_FORWARD)) || (::MQLInfoInteger(MQL_OPTIMIZATION)) || (::MQLInfoInteger(MQL_VISUAL_MODE)) || (::MQLInfoInteger(MQL_FRAME_MODE)) ); const long init_ts = ::TimeLocal(); // Calibrate while( (live) && ((::TimeLocal()) == init_ts) ) { _delta = ::GetMicrosecondCount(); } // Save results is_init = true; _delta %= 1000000; // Return return(_delta); } /*********************************************************************************************************************************************************/ /* */ /* Simple math helper functions */ /* */ /*********************************************************************************************************************************************************/ /* ********************************************************************************************************* * Pseudo random generator * ********************************************************************************************************* */ //+------------------------------------------------------------------+ //| Generate a random byte array | //+------------------------------------------------------------------+ const double LIB_NAMESPACE_DEF(numbers, _mqlplus_MathRandN)() LIB_MQLPLUS_LIB_EXPORT { return(LIB_NAMESPACE(numbers, random)()); } const uint LIB_NAMESPACE_DEF(numbers, _mqlplus_MathRand)() LIB_MQLPLUS_LIB_EXPORT { // Local init _32_bit rnd = 0x00; LIB_NAMESPACE_DEF(numbers, _mqlplus_MathRand)(rnd.uc, 4); // Return return(rnd.ui); } void LIB_NAMESPACE_DEF(numbers, _mqlplus_MathRand)(uchar& out[], const uint bytes, const uint start = NULL) LIB_MQLPLUS_LIB_EXPORT { // Local init if(::ArraySize(out) < ((int)(bytes + start))) { ::ArrayResize(out, (bytes + start)); } //////////////////////////////////////////////////////////////////////// // // Fill array // // Insert here ->quantum random number generator<- // Polling mechanism for data from // https://qrng.anu.edu.au/API/jsonI.php?length=1024&type=uint8 // // Local init static bool qrnd = true; static int rnd_full_size = LIB_NAMESPACE(numbers, _mqlplus_MathRoundPow2)(MathMax(bytes, 1024)); static uchar rnd_cache[]; char body[]; char result[]; string result_hdr; // Precheck cache size if(rnd_full_size < LIB_NAMESPACE(numbers, _mqlplus_MathRoundPow2)(bytes)) { rnd_full_size = LIB_NAMESPACE(numbers, _mqlplus_MathRoundPow2)(bytes); } // Try access quantum random generator if( (qrnd) && (::ArraySize(rnd_cache) < (int)bytes) ) { // Init loop int r_sz = NULL; uchar char_bin[]; int cnt = NULL; int next = NULL; // Loop request to fill cache while( (qrnd) && (::ArraySize(rnd_cache) < (int)(rnd_full_size + bytes)) && (!_StopFlag) ) { // Query data from server ::ResetLastError(); if(::WebRequest("GET", "https://qrng.anu.edu.au/wp-content/plugins/colours-plugin/get_block_hex.php", NULL, NULL, 10000, body, NULL, result, result_hdr) != 200) { int err_no = ::GetLastError(); qrnd = false; switch(err_no) { #ifndef __MQL4_COMPATIBILITY_CODE__ case ERR_FUNCTION_NOT_ALLOWED: printf("%s", "WebRequests cannot be used from within an indicator or tester environment."); #endif case ERR_WEBREQUEST_INVALID_ADDRESS: case ERR_WEBREQUEST_CONNECT_FAILED: case ERR_WEBREQUEST_TIMEOUT: case ERR_WEBREQUEST_REQUEST_FAILED: printf("%s", "Add URL to allowed list: https://qrng.anu.edu.au"); printf("%s", "Warning: Quantum source random number generator disabled."); } break; } // Convert hex to binary cnt = NULL; next = NULL; r_sz = ::ArraySize(result); r_sz -= (r_sz & 0x01); ::ArrayResize(char_bin, r_sz / 2); while( (cnt < r_sz) && (!_StopFlag) ) { char_bin[next] = ((result[cnt] - ((result[cnt] < 58) ? 48 : 87)) << 4) + result[cnt + 1] - ((result[cnt + 1] < 58) ? 48 : 87); next++; cnt += 2; } // Add result to cache ::ArrayCopy(rnd_cache, char_bin, ::ArraySize(rnd_cache), NULL, next); } } // Check quantum results if(qrnd) { ::ArrayCopy(out, rnd_cache, 0, ::ArraySize(rnd_cache) - bytes, bytes); ::ArrayResize(rnd_cache, ::ArraySize(rnd_cache) - bytes); return; } // Local pseudo random generator uint tmp = NULL; ::ArrayResize(out, (start + bytes) + (4 - ((start + bytes) % 4))); for(uint cnt2 = start; cnt2 < (bytes + start); cnt2 += 4) { tmp = LIB_NAMESPACE(numbers, random_max)(); out[cnt2] = (uchar)tmp; out[cnt2 + 1] = (uchar)(tmp >> 8); out[cnt2 + 2] = (uchar)(tmp >> 16); out[cnt2 + 3] = (uchar)(tmp >> 24); } ::ArrayResize(out, (start + bytes)); // Return return; } //+------------------------------------------------------------------+ //| Generate a random byte | //+------------------------------------------------------------------+ const uchar LIB_NAMESPACE_DEF(numbers, random_byte)() { // Squish random result to range of 8 bit return((uchar)(LIB_NAMESPACE(numbers, random)() * 256.0)); } //+------------------------------------------------------------------+ //| Generate a random integer with a max | //+------------------------------------------------------------------+ const uint LIB_NAMESPACE_DEF(numbers, random_max)(uint max = UINT_MAX) { return((uint)(LIB_NAMESPACE(numbers, random)() * ((double)max))); } //+------------------------------------------------------------------+ //| Unified 32 bit random source function | //+------------------------------------------------------------------+ const double LIB_NAMESPACE_DEF(numbers, random)(const ulong seed = NULL, const ulong sequence = NULL) { ////////////////////////////////////////////////////////////////// // // PCG Random Number Generation, Minimal C Edition // // http://www.pcg-random.org // // Copyright 2014 Melissa O'Neill // // Local init static bool is_init = false; static ulong state = NULL; static ulong increment = 0xDA3E39CB94B95BDB; // Initi state is_init = (seed == sequence) && is_init; if(!is_init) { state = NULL; increment = (sequence << 1) | 1; is_init = true; LIB_NAMESPACE(numbers, random)(increment, increment); state += (seed == NULL) ? 0x853C49E6748FEA9B : seed; LIB_NAMESPACE(numbers, random)(NULL, NULL); } // Calculate const ulong oldstate = state; state = (oldstate * 0x5851F42D4C957F2D) + increment; const uint xorshifted = (uint)(((oldstate >> 18) ^ oldstate) >> 27); const uint rot = (uint)(oldstate >> 59); // Return const uint tmp = ((xorshifted >> rot) | (xorshifted << ((0 - rot) & 31))); const double tmp2 = ((double)(((xorshifted >> rot) | (xorshifted << ((0 - rot) & 31))) / (double)UINT_MAX)); return((double)(((xorshifted >> rot) | (xorshifted << ((0 - rot) & 31))) / (double)UINT_MAX)); } /* { // Static init static int random_seed = (int)(::GetTickCount()); // Local init int k = random_seed / 127773; // Calculate random seed random_seed = 16807 * ( random_seed - k * 127773 ) - k * 2836; // Push random value into positive range if( random_seed < NULL ) { random_seed = random_seed + 2147483647; } // Squish random result to range between 0.0 and 1.0 return((((double)random_seed) * 4.656612875E-10) / 0.999999999481554269); } */ //+------------------------------------------------------------------+ //| flt_compare | //+------------------------------------------------------------------+ const bool LIB_NAMESPACE_DEF(numbers, flt_cmp_invalid)(const float in, const uint ref) { const _32_bit cmp = in; return(cmp.ui == ref); } //+------------------------------------------------------------------+ //| dbl_compare | //+------------------------------------------------------------------+ const bool LIB_NAMESPACE_DEF(numbers, dbl_cmp_invalid)(const double in, const ulong ref) { const _64_bit cmp = in; return(cmp.ul == ref); } #ifndef __MQL4_COMPATIBILITY_CODE__ }; // END Namespace numbers }; // END Namespace LIB_MQLPLUS_LIBRARY_PREFIX #endif // // END Simple math functions */ //*********************************************************************************************************************************************************/ #endif // LIB_MATH_SIMPLE_IMPLEMENTATION_MQH_INCLUDED