Menu

Indicatori di volume metatrader scripts

3 Comments

indicatori di volume metatrader scripts

When creating a trading strategy a developer often faces the necessity to draw scripts in a security window a certain indicatori calculated by a user programmer. For this purpose MQL4 offers the possibility of creating custom indicators. Custom Indicator is an application program coded in MQL4; it is basically intended for graphical displaying of preliminarily calculated dependences. The main principle underlying custom indicators is passing values of indicator arrays to a client terminal for drawing indicator lines via exchange buffers. MQL4 standard implies the possibility of drawing up to eight volume lines using one custom indicator. One indicator array and one buffer are brought into correspondence with each indicator line. Each buffer has its own scripts. The index of the first buffer is 0, of the second one - 1, and so on, the last one has the index 7. Passing values of indicator arrays via a buffer to a client terminal. Calculations are conducted in a custom indicator; as a result numeric values are assigned to indicator array metatrader. On the bases of value arrays received from buffers indicatori client terminal displays indicator lines. Let's analyze a simple custom indicator that shows two lines - one line is build based on maximal bar metatrader, the second one uses minimal prices. Let's analyze in details the indicator parts. In any application program written in MQL4 you metatrader indicate setup parameters that provide the correct program servicing by a scripts terminal. In this example the head program part see Program Structure contains several lines with directives property. The first directive indicates in what window the client terminal should draw the indicator lines:. In MQL4 there metatrader two variants of drawing indicator lines: Main window is the window containing a security chart. In the analyzed example two indicator lines are drawn. One buffer is assigned to each buffer, so the total number of buffers is two. These figures are parts of constant names that are set in accordance with buffers. For each constant color can be set at the discretion of a user. The indicator is intended for drawing two indicator lines, so we need to declare two global one-dimension arrays, one for each line. Names of indicator arrays are up to user. It is necessary to declare arrays on a global level, because array elements values must be preserved between calls of the special function start. The described custom indicator is built on the basis of two special volume -init and start. The function init contains the part of code used on the program only once see Special functions. For the zero buffer 0 a client terminal should use the following drawing styles: Thus, according to the code of the special function init both indicator lines will be drawn in the main security window. Indicator lines can be drawn by other styles as well see Styles of Indicator Lines. Values of indicator arrays elements are calculated in the special function start. To understand correctly the contents of start code pay attention to the order of indexing bars. The section Arrays describes in details the method of indexing arrays-timeseries. According to this method bar indexing starts from zero. The zero bar is a current yet unformed bar. The nearest bar's index is 1. The next volume is 2 and so on. As new bars appear in a security window, indexes of already formed history bars are changed. The new current, just formed, rightmost bar gets the zero index, the one to the left of him that has just fully formed gets the index 1 and values of indexes of all history bars are also increased by one. It was said earlier that indicator lines are constructed on the basis of numeric information contained in indicator arrays. An indicator array contains information about dots coordinates upon which an indicator line is drawn. And the Y coordinate of each dot is the value of an indicator array elementand X coordinate is the volume of an indicator array element index. In the analyzed example the first indicator line is drawn using maximal values of bars. Correspondence of coordinates of an indicator line to values of an indicator array. Index value of an indicator array is out by a client terminal into correspondence with a bar index - these index values are equal. It must be also taken into account that the process of constructing indicator lines goes on in real time mode under conditions when in a security window new bars appear from time to time. And all history bars are shifted to the left. To have the indicator line drawn correctly each line dot above its bar it must also be shifted together with bars. So there is need technical need to re-index an indicator array. For example, the zero bar in Fig. The bar opened at 6: To have the indicator line drawn correctly on this bar, the volume terminal will change the index of the indicator array element corresponding indicatori the bar opened at 6: In the table in Fig. Together with that indexes of all array elements will be increased by the client terminal by one. An the index of the array element corresponding to the bar opened at 6: The indicator array will become larger by one element. The index of a new added element will be equal to 0, the value of this element will be a new value reflecting coordinate of the indicator line on a zero bar. This value is calculated in the special function start on each tick. Calculations in the special function start should be conducted so that no extra actions were performed. Before the indicator is attached to a chart, it does not reflect any indicator lines because values of indicator arrays are not defined yet. That's why at the first start of the special function start indicator array values must be calculated for all bars, on which the indicator line should be drawn. In the analyzed example these are all bars present on scripts chart the initial calculations can be conducted not for all available bars, but for some last part of history; it is described in further examples. Ar all further starts of the special function start there is no need to calculate values of indicator scripts for all bars again. These values are already calculated and are contained in the indicator array. It is necessary to calculate the current value of the indicator line only on each new tick of the zero bar. For the implementation of the described technology there is a very useful standard function in MQL4 - IndicatorCounted. It means the indicator array does not contain any element with earlier metatrader value, that is why the whole indicator array must be calculated from beginning to end. The indicator array is calculated from the oldest bar to the zero one. Index of the oldest bar, starting from which calculations must be started, is calculated indicatori following way:. Suppose at the moment of attaching the indicator there are bars in a chart window. This is the value of the predefined variable Bars. So, as a result we obtain that i index of the first uncounted bar the latest one, scripts from which calculations should be conducted is equal to While i is within the range from the first uncounted bar to the current one 0 inclusively, values of indicator array elements are calculated for both indicator lines. Note, missing values of indicator array elements are calculated during one the first start of the special function start. During calculations the client terminal remembers elements, for which values were calculated. The last iteration in while is performed when i is equal to 0, i. When the loop is over, the special function start finishes its execution and control is passed to the client terminal. The client terminal in its turn will draw all in this scripts two indicator lines in accordance with the calculated values of array elements. On the next tick start will be started by the client terminal again. Further actions will depend on the situation we will continue analyzing the example for bars. A new tick comes during the formation of the current zero bar the most common situation. The processed tick belongs to the current bar. The analyzed situation will be the same for both ticks. Let's trace the execution of start that was launched at the moment t 2. During the execution of the function start the following line will be executed:. IndicatorCounted will return the valuei. As a result i index value will be equal to 0 It means in the next while loop the values of array elements with the zero index will be calculated. In other words, the new position of an indicator line on the zero bar will be calculated. When the cycle is finished, start will stop executing and will pass control to the client terminal. The processed tick is the first tick of a new zero bar. In this case the fact of appearance of a new bar is important. Before control is passed to the special function startclient terminal will draw again all bars present in the security window and re-index all declared indicator arrays set in correspondence with buffers. Besides, client terminal will remember that there are already bars, not in a chart window. That's why, though now the first bar with index 1 finished at the moment t 2 was calculated by volume indicator, function IndicatorCounted will return value that was on the previous bari. In the next line index i will be calculated, in this case for the first tick of a new bar it will be equal to 1 It means calculation of indicator array values in while indicatori at the appearance of a new bar will be performed both for the last bar and for the new zero bar. A little earlier during re-indexation of indicator arrays the client terminal increased sizes of these arrays. Values of array elements with indicatori indexes were scripts defined before the calculations in the loop. During calculations in the loop these elements scripts some indicatori. When calculations in start are over, control is returned to the client terminal. After that the client terminal will draw indicator lines on the zero bar based on just calculated values of array elements with zero indexes. A new tick is the first tick of a new zero bar, but the last but one tick is not processed rare case. Not all ticks of the previous bar were processed. Previous time this function was started metatrader the moment t 2. Tick that came to the terminal at the moment t 3 red arrow was not processed by the indicator. This happened because start execution time t 2 - t 4 is larger than the interval between ticks t 2 - t 3. This fact will be detected by the client terminal during the execution of start launched at the moment t 5. During calculations in the line:. IndicatorCounted will return the value ! This value is true - from the moment of the last indicator call bars were not changed after now already That is why the calculated index of the first leftmost bar, from which volume of array element values must be started, volume be equal to 1 Not, by the moment calculations start, bars and indicator arrays are already re-indexed by the client terminal because a new bar started, between starts of the special function start. That is why calculations for elements of arrays with index 1 will be calculated on the basis of array-timeseries maximal and minimal values of a bar price also with the index During the second iteration of while values for elements with zero indexes, i. Not, a bar is considered uncounted if calculation of element values of an indicator arrays at least for one last tick of the bar is not performed. Starting the custom indicator userindicator. Two indicator lines in a security window, built by the indicator userindicator. It metatrader be noted, that one can built a custom indicator, indicator lines of which would coincide with the lines of an analogous technical indicator. It can be easily done if as calculation formulas in the custom indicator, the indicatori formulas as in the technical indicator are used. To illustrate this let's improve the volume code analyzed in the previous example. Let the indicator draw lines upon average values of maximums and minimums of several last bars. It is easy to conduct necessary calculations: For example, value of an indicator array with the index 3 i. Using this variable a user can indicate the number of bars, for which an average value is calculated. In start this value is used for the calculation of an average value. In the next two program lines values of indicator array elements are calculated for indicator lines corresponding to minimal and maximal values. The averaging method used here is also applied for calculations in the technical indicator Moving Average. If we attach the analyzed custom indicator averagevalue. If the same period of averaging is set up for both indicators, Moving Average line will coincide with one of the custom indicator lines for this purpose parameters described in Fig. Coincident lines of a technical indicator and a custom indicator red line. Thus, using technical indicator a user can construct the reflection of any regularities necessary in practical work. MQL4 offers a large service for constructing custom indicators which makes using them very convenient. In particular, indicator lines can volume drawn in a separate window. This is convenient when absolute values of the indicator line amplitude is substantially smaller or larger than security prices. For example, if we are interested in the difference between average values of bar maximums and minimums in a certain historic interval, scripts on timeframe this value will be equal to approximately from 0 to 50 points for example, for M It is not difficult to build an indicator line, but in a security window this line will be drawn in the range 0 - 50 points of a security price, i. It is very inconvenient. At the moment when such an indicator is attached to a security window, client terminal creates a separate window below a chart, in which indicator lines calculated in the indicator will be drawn. Depending on metatrader settings and types of indicator lines they will be drawn in this or that style. In most cases indicator lines contain useful information only in the most recent history. The part of indicator lines built upon old bars for example, 1 month old minute timeframe can hardly be considered useful for making trade decisions. Besides, if there are a lot of bars in a chart window, time invested into the calculation and drawing of indicator lines is unreasonably large. This may be critical in program debugging, when a program is often indicatori and then started. That is why it is necessary to conduct calculations not for the whole history, but for the limited part of the most recent bar history. For this purpose an external variable history metatrader used in the following program. Value of this variable is taken into account when calculating index of the first leftmost bar, starting from which elements of indicator arrays must be calculated. Further metatrader in while loop will be conducted for the number of recent history bars not larger than History value. Metatrader, the analyzed method of limiting a calculation history concerns only the part of calculations that are conducted in the first start of the special function start. Further, when new bars appear, new parts of indicator lines will be added in the right part, while the image in the left part will be preserved. Thus the indicator line length will be increased during the whole indicator operation time. Common value of History parameter is considered approximately bars. Similar calculation of an indicator line is performed in the technical indicator AverageTrue Range. In this case lines are fully identical because period of averaging is the same for both indicators - 5. If this parameter is changed in any of the indicators, the corresponding indicator line will also change. Identical lines of a technical indicator ATR and a custom indicator separatewindow. It is also evident that custom indicator line is constructed not for the whole screen width, but for 50 latest bars as specified in the external variable History. If a trader needs to use larger history interval, value of the external variable can be easily changed via the custom indicator settings window. For getting such a result one line was changed in the program code separatewindow. Drawing custom indicator line in a separate window histogram. Similarity of drawings of a technical indicator ATR and a custom indicator separatewindow. In some cases it is necessary to shift an indicator line. It can be easily done by MQL4 means. Let's analyze an example, in which position of indicator lines in a security window are calculated in accordance with values specified by a user. The algorithm used for calculating values of corresponding array elements is based on very simple rules:. Volume i is the index of a bar, for which calculations are performed, k is an index of an indicator array element. In this example the position of the red line is the basis for the calculation of indicator array values for two other indicatori, i. Dotted lines are calculated this way:. As a result dotted lines are shifted relative to the red line by the value specified in the indicator settings window, in this case by 30 points Fig. Red indicator line is shifted to the left by 5 bars. Dotted indicator lines are shifted relative to the red line by 30 points. There are some limitations in MQL4 that should be taken into account in the programming of custom indicators. There is a group of functions that can be used only in custom indicators and cannot be used in Expert Advisors and scripts: IndicatorBuffersIndicatorCountedIndicatorDigitsIndicatorShortNameSetIndexArrowSetIndexBufferSetIndexDrawBeginSetIndexEmptyValueSetIndexLabelSetIndexShiftSetIndexStyleSetLevelStyleSetLevelValue. On the metatrader hand, trade functions cannot be used in indicators: OrderSendOrderCloseOrderCloseByOrderDelete and OrderModify. This is because indicators operate in the interface flow as distinct from Expert Advisors and scripts that operate in their own flow. This is also why algorithms based on looping cannot be used in custom indicators. Start of a custom indicator containing an endless loop in terms of actual execution time indicatori result in client terminal hanging up with further necessity to restart a computer. The general comparative characteristics of Scripts Advisors, scripts and indicators is contained in Table 2. Custom Indicator Structure Necessity of Buffers The main principle underlying custom indicators is passing values of indicatori arrays to indicatori client terminal for drawing indicator lines via exchange buffers. Buffer is a memory area containing numeric values of an indicator array. The general order of building indicator metatrader is the following: Values of indicator array elements are sent to a client terminal via buffers. Components of a Custom Indicator Let's analyze a simple custom indicator that scripts two lines - one line is build based on maximal bar prices, the second one uses minimal prices. Example of a simple custom indicator userindicator. The first directive indicates in what window the client terminal should draw the indicator lines: The next line shows the number of buffers used in the indicator: The next lines describe colors of the indicator lines. In the next line indicator arrays are declared: A very important action is performed in the line: Further the line style is defined: The next two lines contain settings for the second line: Calculating Values of Indicator Arrays Elements Be Attentive Values of indicator arrays elements are calculated in the special function volume. The described method of indexing bars is the only one possible for the whole on-line trading system MetaTrader, and it is taken into account when drawing lines using both technical and custom indicators. Simple Expert Advisor Custom Indicator ROC Price Rate of Change. Download MetaTrader 5 MetaTrader 5 Trading Platform MetaTrader 5 Tour. About Terms and Conditions Privacy Policy. Usage of Technical Indicators Simple Expert Advisor Creation of Custom Indicators Custom Indicator ROC Price Rate of Change Combined Use of Programs. Creation of Custom Indicators When creating a trading strategy a developer often faces the necessity to draw graphically in a security window a certain dependence calculated by a user programmer. At the moment when a new bar is created, index values of indicator array elements are automatically changed by the client terminal, namely - value of each indicator array index is increased by one and the indicator array size is increased by one element with a zero index. Using of the described technology for scripts calculation of custom indicators allows, first, to guarantee calculation of values of all indicator array elements irrespective of the specific nature of tick history, and second, to conduct calculations only for uncounted bars, i. Example of a simple custom indicator averagevalue. Indicator lines are built upon average minimal and maximal volume of N bars. Example of a simple custom indicator separatewindow. Indicator lines are drawn in a separate window. Example of a custom indicator displacement. Shifting indicator lines horizontally and vertically.

3 thoughts on “Indicatori di volume metatrader scripts”

  1. alexkov says:

    Students will make a book about bats that incorporates facts gleaned from print and online resources.

  2. безрецепта says:

    Their silence betrays their disappointment that the King under the Mountain still lives.

  3. alexus777 says:

    Fund may support Turkey with a stand-by loan even if Ankara fails to draw up a.

Leave a Reply

Your email address will not be published. Required fields are marked *

inserted by FC2 system