How to reach more history data from High and Low arrays - mql4

i have this code .. does somebody know how to reach more historic bars?
void OnTick()
{
ChartNavigate(0, CHART_END, 0);
Print(High[2000]);
}
this gives me error but with usage of High[1000] it works.
I tried that ChartNavigate funciton but it does not help.
Thanks!!

It depends on the number of bars that you have loaded on the chart.
To reach the number of bars on current time frame use:
int bars = Bars;
Print(High[bars-1]);
or if you want to know how many bars are loaded in another time frame (e.g Daily) you can use :
int bars = iBars(_Symbol,PERIOD_D1);

Related

How do you write a function that returns the low and the top of the moving average for a specified number of candles in mql4?

I hope someone guides me,
I want to calculate the lowest moving average level during a certain number of candles, as well as the highest moving average level during a certain number of candles.
That is, I want the moving average top and the moving average bottom, not the price.
How do I write a function that returns these values ​​to me?
Thank you for your interest and help
You should try to attempt to write the code yourself, you will learn a lot more and get more help along the way when you hit any problems.
int startCandle=1;
int endCandle=100;
double highest=NULL, lowest=NULL;
for(int i=startCandle; i<=endCandle; i++)
{
double ma=iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, i);
if(highest==NULL){highest=ma; lowest=highest;}
if(ma>highest) highest=ma;
if(ma<lowest) lowest=ma;
}

How to look back a certain number of days for an indicator and how to only calculate logic upto the last complete bar (i.e. not the current bar)

I'm relatively new to Pine and was having trouble with a couple of things.
So I've got a indicator that I want to look back for the last 5 full days of activity, however not each stock trades in the premarket/afterhours so I want to be able to look back say 5 days and then calculate the number of bars on the time frame (i.e. for a 5 min or hourly resolution). That way I can calculate the number of bars present on the current ticker for that time frame and adjust my indicator length (instead of just having a constant figure of 80 bars or whatever).
The second thing I want to do is only have the indicator update on the completion of a full bar (for example the hourly resolution). So I want it to only calculate up to x-1 bars. Right now I'm finding that it's updating on each new tick. ( I was able to figure this one out, just changed it from i= 0 to length -1 to i=1 to length).
Any help is greatly appreciated.
Thanks,
A

How can I automatically rescale the price range shown on the current chart? (MT4 EA)

I'm creating an MT4 EA in mql4, and would like to automatically adjust the price scale (y-axis) shown, based on some indicator results.
I.e. I have (y-max, y-min) values from a pivot indicator, and would like to re-scale my chart to center on the pivot line, regardless of where the current price is. For example, in the figure shown, I'd like to see the scale between the orange and yellow lines. Perhaps adding a few pixels above and below for visibility.
Unfortunately the MT4 forums are very lacking in the info on this, and I cannot even find a starting point. Possibly related info here:
https://docs.mql4.com/constants/chartconstants/enum_chart_property
https://book.mql4.com/functions/charts
https://www.mql5.com/en/forum/154523
How can I have my EA to resize/re-scale the price range for the current chart window?
(Possibly as a percentage of the total available size.)
After collecting some bits and pieces from various forums and sources, I managed to patch together a beautiful solution.
What does it do?
Adds a bool setting to enable automatic scaling, otherwise only do it once upon OnInit().
Uses the OnChartEvent() event handler to detect CHARTEVENT_CHART_CHANGE, which is a Windows originated event of re-sizing the chart window, and scale the contents accordingly.
...
extern bool autoChartScaling = true; // Enable Automatic chart scaling
...
void AdjustChartPrice() {
...
ChartSetInteger(cid,CHART_SCALEFIX, 0, true); // Set the MODE for using a fixed chart scale ([x] Fixed MT4 option)
ChartSetDouble(cid,CHART_FIXED_MAX, ymax); // Maximum chart price (height) in [points]
ChartSetDouble(cid,CHART_FIXED_MIN, ymin); // Minimum chart price (height) in [points]
}
...
int OnInit() {
...
OnChartEvent();
}
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) {
// Adjust the chart price Max/Min if chart window changed
if (autoChartScaling && id == CHARTEVENT_CHART_CHANGE) AdjustChartPrice();
}

MQL4 max bars in chart

I'm trying to limit resources consumed by MT4 and go to options to set max bars in chart to something smaller like 500. My custom indicator calculates values from a 10 and 20 EMA.
Now the indicator does not even want to attach to the chart!
Even if I increase the number I cannot get my CI to attach to the chart again.
Please can you let me know what the minimum number should be for max bars in chart and why a simple CI as set out above will not attach to the chart?
Lastly, could you please help me also by stating what the default value for max bars in chart normally is? I thought it is 999999 but cannot remember.
Thanks so much for your effort.
In order to use your resources efficiently, ask your indicator to compute only last bars, prev_calculated should be used and go only since that value to the end of the chart(to the current bar). You can also make your indicator sleeping if number of bars is same, and let it run only when new bar appears (rates_total>prev_calculated) if that can be accepted for your task

MetaTrader: indicator buffers amount is less than needed

I get this error on compilation in MetaTrader:
indicator buffers amount is less than needed
any help?
MQL4 Indicator setup may look as a dual-headed nightmare.Maybe yes,maybe no:
There are two principal methods used for an Indicator setup.
one, using #property ... syntax interface,
and
another, using function calls ...
Why these two exist? How do they co-exist?
#property ...
indicator_chart_window Show the indicator in the chart window
indicator_separate_window Show the indicator in a separate window
indicator_height int Fixed height of the indicator subwindow in pixels (property INDICATOR_HEIGHT)
indicator_buffers int Number of buffers for indicator calculation
indicator_minimum double The bottom scaling limit for a separate indicator window
indicator_maximum double The top scaling limit for a separate indicator window
indicator_labelN string Sets a label for the N-th graphic series displayed in DataWindow
indicator_colorN color The color for displaying line N, where N is the number of graphic series; numbering starts from 1
indicator_widthN int Line thickness in graphic series, where N is the number of graphic series; numbering starts from 1
indicator_styleN int Line style in graphic series, specified by the values of ENUM_LINE_STYLE. N is the number of graphic series; numbering starts from 1
indicator_typeN int Type of indicator drawing style. N is the number of graphic series; numbering starts from 1
indicator_levelN double Horizontal level of N in a separate indicator window
indicator_levelcolor color Color of horizontal levels of the indicator
indicator_levelwidth int Thickness of horizontal levels of the indicator
indicator_levelstyle int Style of horizontal levels of the indicator
After some time in MQL4 you will adopt your own best way to go forwards.
Why failed to work?
Short version: a compile-time decision sets the amount of arrays preset for indicator time-series aligned data. Going past this set ceiling then must throw an exception as there is no further buffer to store data in.
However, setting a much bigger ceiling first is possible and may help, somewhere at a later time, to have still space enough, even when you do not use it right now.
That seems to be a problem of:
#property indicator_buffers XXX //How Many Plots
XXX should be correct number of Buffers/plots

Resources