Here is my attempt of code however it is not rendering result fast and as its slow and sometime returns array out of range in 'HighLowSearch.mq5' (237,123)
, if someone can improve this code, it will be highly appreciated. Thanks in advance
//+------------------------------------------------------------------+ //| HighLowSearch.mq5 | //+------------------------------------------------------------------+ #property copyright "Copyright 2022" #property link "https://www.mql5.com/" #property version "1.00" #property indicator_chart_window #property indicator_plots 0 MqlRates History[]; int HighestCandle, LowestCandle; double MainHigh[],MainLow[]; datetime MainHighT[],MainLowT[]; string prefix = "Text_"; int StartBars; int FirstBar, LastBar; //+------------------------------------------------------------------+ //| Enumeration for Windows fonts indices storage | //+------------------------------------------------------------------+ enum type_font { // font type Font0, //Arial Font1, //Arial Black Font2, //Arial Bold Font3, //Arial Bold Italic Font4, //Arial Italic Font5, //Comic Sans MS Bold Font6, //Courier Font7, //Courier New Font8, //Courier New Bold Font9, //Courier New Bold Italic Font10, //Courier New Italic Font11, //Estrangelo Edessa Font12, //Franklin Gothic Medium Font13, //Gautami Font14, //Georgia Font15, //Georgia Bold Font16, //Georgia Bold Italic Font17, //Georgia Italic Font18, //Georgia Italic Impact Font19, //Latha Font20, //Lucida Console Font21, //Lucida Sans Unicode Font22, //Modern MS Sans Serif Font23, //MS Sans Serif Font24, //Mv Boli Font25, //Palatino Linotype Font26, //Palatino Linotype Bold Font27, //Palatino Linotype Italic Font28, //Roman Font29, //Script Font30, //Small Fonts Font31, //Symbol Font32, //Tahoma Font33, //Tahoma Bold Font34, //Times New Roman Font35, //Times New Roman Bold Font36, //Times New Roman Bold Italic Font37, //Times New Roman Italic Font38, //Trebuchet MS Font39, //Trebuchet MS Bold Font40, //Trebuchet MS Bold Italic Font41, //Trebuchet MS Italic Font42, //Tunga Font43, //Verdana Font44, //Verdana Bold Font45, //Verdana Bold Italic Font46, //Verdana Italic Font47, //Webdings Font48, //Westminster Font49, //Wingdings Font50, //WST_Czech Font51, //WST_Engl Font52, //WST_Fren Font53, //WST_Germ Font54, //WST_Ital Font55, //WST_Span Font56 //WST_Swed }; //+------------------------------------------------------------------+ //| Font name obtaining class | //+------------------------------------------------------------------+ class CFontName { public: string GetFontName(type_font FontNumber) { string FontTypes[]= { "Arial", "Arial Black", "Arial Bold", "Arial Bold Italic", "Arial Italic", "Comic Sans MS Bold", "Courier", "Courier New", "Courier New Bold", "Courier New Bold Italic", "Courier New Italic", "Estrangelo Edessa", "Franklin Gothic Medium", "Gautami", "Georgia", "Georgia Bold", "Georgia Bold Italic", "Georgia Italic", "Georgia Italic Impact", "Latha", "Lucida Console", "Lucida Sans Unicode", "Modern MS Sans Serif", "MS Sans Serif", "Mv Boli", "Palatino Linotype", "Palatino Linotype Bold", "Palatino Linotype Italic", "Roman", "Script", "Small Fonts", "Symbol", "Tahoma", "Tahoma Bold", "Times New Roman", "Times New Roman Bold", "Times New Roman Bold Italic", "Times New Roman Italic", "Trebuchet MS", "Trebuchet MS Bold", "Trebuchet MS Bold Italic", "Trebuchet MS Italic", "Tunga", "Verdana", "Verdana Bold", "Verdana Bold Italic", "Verdana Italic", "Webdings", "Westminster", "Wingdings", "WST_Czech", "WST_Engl", "WST_Fren", "WST_Germ", "WST_Ital", "WST_Span", "WST_Swed" }; return(FontTypes[int(FontNumber)]); }; }; input type_font FontType=Font24; string sFontType; input uint TextSize2 = 9; string _model; input color HColor = clrAliceBlue; input color LColor = clrCrimson; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping ArraySetAsSeries(History,true); ArraySetAsSeries(MainHigh,true); ArraySetAsSeries(MainHighT,true); ArraySetAsSeries(MainLow,true); ArraySetAsSeries(MainLowT,true); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0,prefix); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- FirstBar = ChartFirstVisibleBar(); LastBar = ChartVisibleBars(); StartBars = FirstBar-LastBar; if(StartBars<0) StartBars=0; ArrayResize(History,2000); ArrayResize(MainHigh,2000); ArrayResize(MainHighT,2000); ArrayResize(MainLow,2000); ArrayResize(MainLowT,2000); CopyRates(_Symbol,_Period,StartBars,FirstBar,History); CopyHigh(_Symbol,_Period,StartBars,FirstBar,MainHigh); CopyTime(_Symbol,_Period,StartBars,FirstBar,MainHighT); HighestCandle = ArrayMaximum(MainHigh,StartBars,FirstBar); CopyLow(_Symbol,_Period,StartBars,FirstBar,MainLow); CopyTime(_Symbol,_Period,StartBars,FirstBar,MainLowT); LowestCandle = ArrayMinimum(MainLow,StartBars,FirstBar); SetText(0,prefix+"MainHighT",0,History[HighestCandle].time,History[HighestCandle].high," High "+DoubleToString(History[HighestCandle].high,_Digits),HColor,_model,TextSize2,ANCHOR_LOWER); SetText(0,prefix+"MainLowT",0,History[LowestCandle].time,History[LowestCandle].low," Low "+DoubleToString(History[LowestCandle].low,_Digits),LColor,_model,TextSize2,ANCHOR_UPPER); //Print(History[HighestCandle].high); //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| creating a text label | //+------------------------------------------------------------------+ void CreateText(long chart_id,// chart ID string name, // object name int nwin, // window index datetime time, // price level time double price, // price level string text, // Labels text color Color, // Text color string Font, // Text font int Size, // Text size ENUM_ANCHOR_POINT point // The chart corner to Which the text is attached ) { //---- ObjectCreate(chart_id,name,OBJ_TEXT,nwin,time,price); ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); ObjectSetString(chart_id,name,OBJPROP_FONT,Font); ObjectSetInteger(chart_id,name,OBJPROP_FONTSIZE,Size); ObjectSetInteger(chart_id,name,OBJPROP_BACK,false); ObjectSetInteger(chart_id,name,OBJPROP_ANCHOR,point); //ObjectSetInteger(chart_id,name,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M1|OBJ_PERIOD_M2|OBJ_PERIOD_M3|OBJ_PERIOD_M4|OBJ_PERIOD_M5); //---- } //-end CreateText() //+------------------------------------------------------------------+ //| changing a text label | //+------------------------------------------------------------------+ void SetText(long chart_id,// chart ID string name, // object name int nwin, // window index datetime time, // price level time double price, // price level string text, // Labels text color Color, // Text color string Font, // Text font int Size, // Text size ENUM_ANCHOR_POINT point // The chart corner to Which the text is attached ) { //---- CreateText(chart_id,name,nwin,time,price,text,Color,Font,Size,point); ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectMove(chart_id,name,0,time,price); //---- } //+----------------------------------------------------------------------+ //| Gets the number of bars that are displayed (visible) in chart window | //+----------------------------------------------------------------------+ int ChartVisibleBars(const long chart_ID=0) { //--- prepare the variable to get the property value long result=-1; //--- reset the error value ResetLastError(); //--- receive the property value if(!ChartGetInteger(chart_ID,CHART_VISIBLE_BARS,0,result)) { //--- display the error message in Experts journal Print(__FUNCTION__+", Error Code = ",GetLastError()); } //--- return the value of the chart property return((int)result); } //+------------------------------------------------------------------------------+ //| Gets the index of the first visible bar on chart. | //| Indexing is performed like in timeseries: latest bars have smallest indices. | //+------------------------------------------------------------------------------+ int ChartFirstVisibleBar(const long chart_ID=0) { //--- prepare the variable to get the property value long result=-1; //--- reset the error value ResetLastError(); //--- receive the property value if(!ChartGetInteger(chart_ID,CHART_FIRST_VISIBLE_BAR,0,result)) { //--- display the error message in Experts journal Print(__FUNCTION__+", Error Code = ",GetLastError()); } //--- return the value of the chart property return((int)result); } //+------------------------------------------------------------------+
Try this :
#property version "1.00" #property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0 //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ string lines_tag="viewport_lines_"; int fvb=-1,clb=-1; int OnInit() { //--- indicator buffers mapping ObjectsDeleteAll(ChartID(),lines_tag); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- if(id==CHARTEVENT_CHART_CHANGE){ int _fvb=(int)ChartGetInteger(ChartID(),CHART_FIRST_VISIBLE_BAR,0); int _clb=(int)ChartGetInteger(ChartID(),CHART_WIDTH_IN_BARS,0); if(_fvb>0&&(_fvb!=fvb||_clb!=clb)){ ObjectsDeleteAll(ChartID(),lines_tag); /* The first visible bar returns a number that represents the distance of the leftmost visible bar from the live bar of the chart which is 0 So if the first visible bar is 9 bars to the left of 0 the fvb will return 9 We use the width in bars to project where the last visible bar is */ int lvb=_fvb-(_clb-1); //but if the last visible bar is below zero that is impossible so we check it if(lvb<0){lvb=0;} /* Then we utilize the iHighest function to get the highest in the range . In mt5 the series arrays resemble a salad where they are as series in some cases and not as series in others iHighest and iSeries are as series which means they operate with zero to the rightmost bar In the documentation for Highest it says the starting point is a deviation from the current bar So what we should send it there is the last visible bar But then we need to know how many bars that distance is so we subtract the last visible bar from the first visible bar */ int hix=iHighest(_Symbol,_Period,MODE_HIGH,(_fvb-lvb),lvb); int lix=iLowest(_Symbol,_Period,MODE_LOW,(_fvb-lvb),lvb); ObjectCreate(ChartID(),lines_tag+"_H",OBJ_HLINE,0,iTime(_Symbol,_Period,hix),iHigh(_Symbol,_Period,hix)); ObjectCreate(ChartID(),lines_tag+"_L",OBJ_HLINE,0,iTime(_Symbol,_Period,lix),iLow(_Symbol,_Period,lix)); ChartRedraw(); //then we store the following to track changes fvb=_fvb; clb=_clb; }} } //+------------------------------------------------------------------+
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Please recommend me any code if you know exists in codebase for mql5 which
draws horizontal lines of highest and lowest price of visible bars in chart. When you zoom in and zoom out chart, it updates high low as data of available bar changes.