Files
MQL5Data/Classes/x-saherelm.x-chart.helper.class.mq5

872 lines
20 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// --------------------------------------
// Name: XCChartHelper
// Description: Chart Helper Tools ...
//
// 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
//
// Imports ...
#include "../Classes/x-saherelm.base.class.mq5"
//
// Implementation ...
class XCChartHelper : public XCBase
{
//
// Public ...
public:
//
//
// Constructor ...
XCChartHelper(
ulong _chartId = 0,
string _symbol = NULL,
ENUM_TIMEFRAMES _period = NULL)
{
//
bool canSetSymbolPeriod =
IsSpecifiedValid(_symbol) &&
IsSpecifiedValid(_period);
//
// Normalize ...
if (!canSetSymbolPeriod)
{
//
mChartID = NormalizeChartId(_chartId);
mSymbol = NormalizeSymbol(_symbol);
mPeriod = NormalizePeriod(_period);
}
else
{
//
SetSymbolPeriod(mSymbol, mPeriod);
}
}
//
// Desconstructor ...
~XCChartHelper()
{
}
//
// Actioins ...
/**
* Redraw Specified Chart ...
*/
void Redraw()
{
ChartRedraw(mChartID);
}
/**
* Chart Navigation ...
*
* @param position: ENUM_CHART_POSITION
* @param shift: int
*
* @return ( bool )
*/
bool Navigate(
ENUM_CHART_POSITION position,
int shift //
)
{
return (ChartNavigate(mChartID, position, shift));
}
/**
* Apply a Template to Chart ...
*
* @param filename: string file path ...
*
* @return ( bool )
*/
bool ApplyTemplate(string filename)
{
return (ChartApplyTemplate(mChartID, filename));
}
/**
* Add Specified Indicator to Chart ...
*
* @param subWindow: int ...
* @param handle: int ...
*
* @return ( bool )
*/
bool IndicatorAdd(int subWindow, int handle)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (ChartIndicatorAdd(mChartID, subWindow, handle));
}
/**
* Delete Specified Indicator from Chart ...
*
* @param subWindow: int ...
* @param name: string ...
*
* @return ( bool )
*/
bool IndicatorDelete(int subWindow, string name)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (ChartIndicatorDelete(mChartID, subWindow, name));
}
/**
* Count all Charts Indicator ...
*
* @param subWindow: int ...
*
* @return ( int )
*/
int IndicatorsTotal(int subWindow)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (ChartIndicatorsTotal(mChartID, subWindow));
}
/**
* Get Specified Indexed Indicator's Name ...
*
* @param subWindow: int ...
* @param index: int ...
*
* @return ( string )
*/
string IndicatorName(int subWindow, int index)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (ChartIndicatorName(mChartID, subWindow, index));
}
/**
* Set Chart Symbol, Period ...
*
* @param symbol: string ...
* @param period: ENUM_TIMEFRAMES ...
*
* @return ( bool )
*/
bool SetSymbolPeriod(
string symbol,
ENUM_TIMEFRAMES period //
)
{
bool result = (ChartSetSymbolPeriod(mChartID, symbol, period));
if (result)
{
//
mSymbol = symbol;
mPeriod = period;
}
//
return result;
}
/**
* Take an Screenshot of Chart ...
*
* @param filename: string file path ...
* @param width: int ...
* @param height: int ...
* @param align_mode: ENUM_ALIGN_MODE ...
*
* @return ( bool )
*/
bool ScreenShot(
const string filename,
const int width,
const int height,
const ENUM_ALIGN_MODE align_mode //
) const
{
return (ChartScreenShot(mChartID, filename, width, height, align_mode));
}
/**
* Writing parameters of chart to file ...
**/
bool Save(const int file_handle)
{
//
string work_str;
int work_int;
//
bool has = false;
//
// Validate ...
if (file_handle == INVALID_HANDLE || mChartID == -1)
{
return has;
}
//
// Write start marker - 0xFFFFFFFFFFFFFFFF ...
has = FileWriteLong(file_handle, -1) == sizeof(long);
if (!has)
{
return has;
}
//
// Write chart type ...
has = FileWriteInteger(file_handle, Type(), INT_VALUE) == INT_VALUE;
if (!has)
{
return has;
}
//
// Write chart symbol ...
work_str = Symbol();
work_int = StringLen(work_str);
has = FileWriteInteger(file_handle, work_int, INT_VALUE) == INT_VALUE;
if (!has)
{
return has;
}
if (work_int != 0)
{
//
has = FileWriteString(file_handle, work_str, work_int) == work_int;
if (!has)
{
return has;
}
}
//
// Write Period of chart ...
has = FileWriteInteger(file_handle, Period(), INT_VALUE) == sizeof(int);
if (!has)
{
return has;
}
//
// Write value of the "Mode" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_MODE), INT_VALUE) == sizeof(int);
if (!has)
{
return has;
}
//
// Write value of the "Foreground" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_FOREGROUND), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "Shift" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHIFT), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ShiftSize" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHIFT), INT_VALUE) == sizeof(int);
if (!has)
{
return has;
}
//
// Write value of the "AutoScroll" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_AUTOSCROLL), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "Scale" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SCALE), INT_VALUE) == sizeof(int);
if (!has)
{
return has;
}
//
// Write value of the "ScaleFix" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SCALEFIX), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ScaleFix_11" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SCALEFIX_11), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "FixedMax" property ...
has = FileWriteDouble(file_handle, ChartGetDouble(mChartID, CHART_FIXED_MAX)) == sizeof(double);
if (!has)
{
return has;
}
//
// Write value of the "FixedMin" property ...
has = FileWriteDouble(file_handle, ChartGetDouble(mChartID, CHART_FIXED_MIN)) == sizeof(double);
if (!has)
{
return has;
}
//
// Write the "ScalePPB" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SCALE_PT_PER_BAR), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "PointsPerBar" property ...
has = FileWriteDouble(file_handle, ChartGetDouble(mChartID, CHART_POINTS_PER_BAR)) == sizeof(double);
if (!has)
{
return has;
}
//
// Write value of the "ShowOHLC" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_OHLC), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ShowLineBid" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_BID_LINE), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ShowLineAsk" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_ASK_LINE), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ShowLastLine" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_LAST_LINE), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ShowPeriodSep" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_PERIOD_SEP), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ShowGrid" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_GRID), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
// Write value of the "ShowVolumes" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_VOLUMES), INT_VALUE) == sizeof(int);
if (!has)
{
return has;
}
//
// Write value of the "ShowObjectDescr" property ...
has = FileWriteInteger(file_handle, (int)ChartGetInteger(mChartID, CHART_SHOW_OBJECT_DESCR), CHAR_VALUE) == sizeof(char);
if (!has)
{
return has;
}
//
return has;
}
/**
* Load Specified Chart File ...
*
* @param file_handle: int ...
*
* @return ( bool )
*/
bool Load(const int file_handle)
{
//
string work_str;
int work_int;
//
bool has = false;
//
// Validate ...
if (file_handle == INVALID_HANDLE || mChartID == -1)
{
return has;
}
//
// Read and checking start marker - 0xFFFFFFFFFFFFFFFF ...
if (FileReadLong(file_handle) != -1)
return has;
//
// Read and checking chart type ...
if (FileReadInteger(file_handle, INT_VALUE) != Type())
return has;
//
// Read chart symbol ...
work_int = FileReadInteger(file_handle);
if (work_int != 0)
{
work_str = FileReadString(file_handle, work_int);
}
else
{
work_str = "";
}
//
// Read chart period ...
work_int = FileReadInteger(file_handle);
SetSymbolPeriod(work_str, (ENUM_TIMEFRAMES)work_int);
//
// Read value of the "Mode" property ...
has = ChartSetInteger(mChartID, CHART_MODE, FileReadInteger(file_handle, INT_VALUE));
if (!has)
{
return has;
}
//
// Read value of the "Foreground" property ...
has = ChartSetInteger(mChartID, CHART_FOREGROUND, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Read value of the "Shift" property ...
has = ChartSetInteger(mChartID, CHART_SHIFT, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Read value of the "ShiftSize" property ...
has = ChartSetInteger(mChartID, CHART_SHIFT, FileReadInteger(file_handle, INT_VALUE));
if (!has)
{
return has;
}
//
// Read value of the "AutoScroll" property ...
has = ChartSetInteger(mChartID, CHART_AUTOSCROLL, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Read value of the "Scale" property ...
has = ChartSetInteger(mChartID, CHART_SCALE, FileReadInteger(file_handle, INT_VALUE));
if (!has)
{
return has;
}
//
// Read value of the "ScaleFix" property ...
has = ChartSetInteger(mChartID, CHART_SCALEFIX, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Read value of the "ScaleFix_11" property ...
has = ChartSetInteger(mChartID, CHART_SCALEFIX_11, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "FixedMax" property ...
has = ChartSetDouble(mChartID, CHART_FIXED_MAX, FileReadDatetime(file_handle));
if (!has)
{
return has;
}
//
// Rread value of the "FixedMin" property ...
has = ChartSetDouble(mChartID, CHART_FIXED_MIN, FileReadDatetime(file_handle));
if (!has)
{
return has;
}
//
// Rread value of the "ScalePPB" property ...
has = ChartSetInteger(mChartID, CHART_SCALE_PT_PER_BAR, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "PointsPerBar" property ...
has = ChartSetDouble(mChartID, CHART_POINTS_PER_BAR, FileReadDatetime(file_handle));
if (!has)
{
return has;
}
//
// Rread value of the "ShowOHLC" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_OHLC, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "ShowLineBid" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_BID_LINE, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "ShowLineAsk" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_ASK_LINE, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "ShowLastLine" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_LAST_LINE, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "ShowPeriodSep" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_PERIOD_SEP, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "ShowGrid" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_GRID, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "ShowVolumes" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_VOLUMES, FileReadInteger(file_handle, INT_VALUE));
if (!has)
{
return has;
}
//
// Rread value of the "ShowObjectDescr" property ...
has = ChartSetInteger(mChartID, CHART_SHOW_OBJECT_DESCR, FileReadInteger(file_handle, CHAR_VALUE));
if (!has)
{
return has;
}
//
return has;
}
ulong Open()
{
//
mChartID = ChartOpen(mSymbol, mPeriod);
return (mChartID);
}
void Close()
{
//
if (mChartID != -1 && mChartID != 0)
{
ChartClose(mChartID);
mChartID = 0;
}
}
//
// Properties ...
/**
* Get Chart ID ...
*
* @return ( ulong )
*/
ulong ChartId()
{
return mChartID;
}
/**
* Get Object Type ...
*
* @return ( int )
**/
int Type() const
{
return (0x1111);
}
/**
* Get Chart Mode ...
*
* @return ( ENUM_CHART_MODE )
**/
ENUM_CHART_MODE Mode()
{
return ((ENUM_CHART_MODE)ChartGetInteger(mChartID, CHART_MODE));
}
/**
* Get Chart Symbol ...
*
* @return ( string )
*/
string Symbol()
{
return (ChartSymbol(mChartID));
}
/**
* Get Chart Period ...
*
* @return ( ENUM_TIMEFRAMES )
*/
ENUM_TIMEFRAMES Period()
{
return (ChartPeriod(mChartID));
}
/**
* Count Chart Visible Bars ...
*
* @return ( int )
*/
int VisibleBars()
{
return ((int)ChartGetInteger(mChartID, CHART_WIDTH_IN_BARS));
}
/**
* Get Chart First Visible Bars Index ...
*
* @return ( int )
*/
int FirstVisibleBar()
{
return ((int)ChartGetInteger(mChartID, CHART_FIRST_VISIBLE_BAR));
}
/**
* Get Chart Last Visible Bars Index ...
*
* @return ( int )
*/
int LastVisibleBar()
{
//
int first = FirstVisibleBar();
int count = VisibleBars();
int result = first + count;
return result;
}
/**
* Get Chart's Specified SubWindows Minimum Price ...
*
* @param subWindow: int
* @return ( double )
*/
double PriceMin(int subWindow = 0)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (ChartGetDouble(mChartID, CHART_PRICE_MIN, subWindow));
}
/**
* Get Chart's Specified SubWindows Maximum Price ...
*
* @param subWindow: int
* @return ( double )
*/
double PriceMax(int subWindow = 0)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (ChartGetDouble(mChartID, CHART_PRICE_MAX, subWindow));
}
/**
* Get Chart Width in Pixel ...
*
* @param subWindow: int ...
*
* @return ( int )
*/
int Width(int subWindow = 0)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (int)ChartGetInteger(mChartID, CHART_WIDTH_IN_PIXELS, subWindow);
}
/**
* Get Chart Height in Pixel ...
*
* @param subWindow: int ...
*
* @return ( int )
*/
int Height(int subWindow = 0)
{
//
// Normalize Subwindow ...
if (subWindow <= 0)
{
subWindow = 0;
}
//
return (int)ChartGetInteger(mChartID, CHART_HEIGHT_IN_PIXELS, subWindow);
}
//
// Protected ...
protected:
//
// Private ...
private:
//
// Props ...
//
ulong mChartID; // Chart ID ...
string mSymbol; // Chart Symbol ...
ENUM_TIMEFRAMES mPeriod; // Chart Period ...
};
//