My MQL4 Journal

Discussion in 'Journals' started by expiated, Jul 14, 2021.

  1. expiated

    expiated

    SETTING THE BUTTON LABEL/TEXT COLOR

    The last thing I want to do is change the color of the text displayed on the button to Blue.

    Because colors are designated by numbers that represent the amount or proportion of red, green, and blue which go into creating them, the function that is used to set color is NOT ObjectSetClolor(), but rather, it is...

    ObjectSetInteger()

    There are four parameters that must be included within its parentheses...
    1. First, the numeral zero (0) is used to designate the current chart.
    2. Listed next is the name of the corresponding object ("GRID").
    3. Third is the property of the object that is being changed (OBJPROP_COLOR).
    4. And the fourth and final component is the desired color of the text (clrBLUE).
    Before February 2014, you could just type the name of the desired color, with the first letter in the word capitalized. But now, you have to stick the abbreviation "clr" on the front.

    So, here is the line of code to set the text color…

    ObjectSetInteger(0,"GRID",OBPROP_COLOR,clrBLUE);

    This is what it actually looks like on my laptop:

    upload_2021-8-17_12-35-26.png

    And here's the resulting button:

    upload_2021-8-17_12-36-28.png
     
    #41     Aug 17, 2021
  2. expiated

    expiated

    Saturday / August 21, 2021
    Writing about stuff in the abstract is sure to be terribly boring, not to mention an incredibly inefficient use of my time. So instead, at least for the next day or two, I plan to take notes from this video and hopefully walk away with a real indicator I can actually use by Monday.

     
    #42     Aug 21, 2021
  3. expiated

    expiated

    This ribbon indicator I am trying to code following the instruction in Joe’s video is simply a combination of three moving averages.

    The moving averages will use the same values found in a MACD. It’s just that instead of displaying them in a separate window centered around a zero line, I am showing them based around the slow moving average line.

    I am going to to the meta editor > going to new custom indicator (project?) and naming it "Trend Driven Ribbon" or something similar.

    upload_2021-8-21_21-50-22.png

    As a matter of habit, Joe does not fill in parameters at this next point. He prefers to get to the code screen and create them. So, this is all I am going to need to get the skeleton of the indicator written.

    upload_2021-8-21_21-52-10.png

    (I unclicked/unselected the OnChartEvent event handler in the next window, since Joe did not seem to have it selected...)

    upload_2021-8-21_21-53-59.png

    Again, we will be working in the main chart—not the lower panel. So, I skipped the “Drawing properties of the Custom indicator program” window, and I also one again switched from the mqproj tab to the mq4 tab.

    Unlike the image below, the only thing showing in my formatted code (in the standard header block?) was #property strict (and the "strict" was in red) so I guess I will need to type in or add #property indicator_chart_window to what I understand to be the “standard header block” so I can have the indicator in the same window as the main chart rather than the secondary window for lower panel indicators.

    upload_2021-8-21_21-57-40.png

    The second thing I need to do is tell the program how many property buffers I need, which is two—one for the fast moving average and one for the slow moving average. (Property buffers are where Metatrader will store the information that’s going to be displayed on the screen.)

    (Why don’t I need a third property buffer for the signal moving average? Perhaps it will have something to do with drawing the histograms at the end.)

    And now I need some inputs…

    input int SignalMAPeriod = 5;
    input int FastMAPeriod = 13;
    input int SlowMAPeriod = 34;

    The above inputs will allow me to specify how I want the calculations done. In this instance, I am going to set my signal moving average to five periods, my fast moving average to 13 periods, and my slow moving average to 34 periods.

    I am using the MA methods

    Joe chose an exponential moving average for his calculations, but I use simple moving averages, which I am guessing is abbreviated by SMA:

    input ENUM_MA_METHOD MAMethod = MODE_SMA;

    These are the default values that I set up for when I’m running the indicator. Of course, I can change these by using the Edit tools once I have dropped the indicator on a chart, but the values I have put in the code are what will appear initially:

    upload_2021-8-21_22-1-57.png

    Now that I have set these values, I need a place to store them so that the moving averages can be displayed on screen. I am therefore going to create two arrays. (Why not three? Perhaps it will have something to do with drawing the histograms at the end.)

    double BufferFast[];
    double BufferSlow[];

    So, these are arrays of double values because the values on a chart are doubles—the fast being my fast moving average array and the slow being my slow moving average array.

    That's all I need to define them at this point, though a little later in the code I will need to show Metatrader how to use them.

    So now, what is this. Am I assigning buffer values/labels to my indicators here?

    #define FastIndicator 0
    #define SlowIndicator 1

    I guess that later on I am going to be associating these double buffers with those corresponding numbers in the init section. Or actually, I guess I am going to get to that right now.

    upload_2021-8-21_22-4-13.png

    In the init section, the first thing I am setting is the style.

    SetIndexStyle(FastIndicator,DRAW_LINE,STYLE_DOT, 1, clrFireBrick);

    What I am saying above is that the fast indicator (number zero) is a line, a dotted line. The numeral one is the width parameter. So, it’s just a narrow dotted line. And finally, I am selecting a color—in this case, firebrick. (I am going to use the same form of a statement here soon for the slow indicator.)

    The next thing is the buffer. This is where I am associating the buffer value I am storing for the moving average with the indicator number. So, the fast indicator has the fast buffer:

    SetIndexBuffer(FastIndicator,BufferFast);

    And the label is simply a name that gets applied if I were to hover over the line on screen. I would see this name attached, and it’s useful if I wish to identify the values that are actually showing on screen.

    SetIndexLabel(FastIndicator,"Fast");

    Note that I've done exactly the same thing for both the fast and the slow indicator…

    upload_2021-8-21_22-6-56.png

    As you can see, Joe likes to group everything for a particular indicator/buffer in one block. Some people would set off the index styles first, then set off the index buffers, etc. But, Joe just finds this arrangement a little easier to follow, and I agree.

    Now, next thing I need to do is to actually create the code.

    Now, on calculate will run when something changes on the chart, which means that I need to get a calculation. And typically, that’s going to mean a market event, or in other words, a new trade (i.e., a price change).

    There’s not really much point in doing the calculations any other time, though there are other options available in Metatrader based on timers. They would be for very specific purposes however, because typically, calculating this in between market events isn’t going to yield any different results.

    So, in this case, I can just stay with the on calculate.

    Now there are a number of things that I need to do. First, I’m going to set up the variable that I will use in here…

    int limit;
    double signalMa,
    fastMa,
    slowMa;

    Now, Joe already knew these variable because he'd written them earlier.

    upload_2021-8-21_22-8-47.png

    (What Joe will normally do when he is writing code is he will just begin writing the code, and then come back and fill things in as he goes if he realizes there was something he missed.)

    Now, I need to put in a little check here…

    if(rates_total<=SlowMAPeriod)
    return(0);

    Rates total, which is being passed in here, shows me how many bars are available for performing calculation.

    I know that I’ve set my slow moving average period above as part of the inputs.

    If I don’t have at least as many rates as the slow moving average period, I can’t perform the calculation. So, I am just going to leave it at this point and wait until later when this gets called again and I might have enough rates to calculate. (What?)

    Next, I want to determine how many bars I will actually perform my calculation on. So, I limit my variable to what I said earlier is the number of rates that I have available minus the number that I’ve already calculated. (What?)

    upload_2021-8-21_22-11-32.png

    So, the first line instructs Metatrader to keep track of what’s been done previously and won’t be forced to recalculate everything on the chart unless some event happens such that it becomes necessary to recalculate. So typically, this will cut down on the work that the computer is doing as each tick goes by.

    The second and third lines are simply where, if there is already something that has been calculated, I am just making sure that Metatrader recalculates the last bars that may have changed as a result of price movement. (i.e., increasing the limit if the program has already done some calculations).

    Now I’ll get into the calculation here…

    upload_2021-8-21_22-13-0.png

    I will have to run this in a loop. I believe that is what the first line up above does. Sometimes you will see this loop written in the opposite way, rather than as it is shown here (see below).

    for (int i=0;i<limit; i++)

    Joe will typically go from the limit minus one up to zero. The bars in Metatrader are counted such that the current bar is zero and the bar number increases as the bars get older. So, I am calculating here from left to right. Sometimes it makes sense to calculate from right to left, and some people simply prefer to do this form. But, Joe finds that by calculating from left to right, it avoids the possibility that the coder accidentally looks into the future when writing the code.
     
    #43     Aug 22, 2021
  4. expiated

    expiated

    So, I'm calculating these three moving average variables I created above...

    ScreenHunter_10619 Aug. 21 22.19.jpg

    And I’m calculating each of them using the standard iMA moving average calculation.

    This calculation allows Joe to specify null, and in that case it will pick up symbol from the chart. He likes to be more specific and actually type Symbol. This is the same value. It’s still coming from the chart. It’s just a little bit more explicit as to what he is using.

    Also, this moving average calculation allows you to leave 0 here, and the program will pick up the period from the chart. Joe likes to specify that period as well. So these are his three values entered in the inputs. (The green zeros are the moving average shift, which simply moves the moving average that number of bars to one side. Since I’m not using that, I am shifting 0.)

    The method again, this was part of the inputs. I am using the simple moving average. Next, I am using the closing price, and this tells me, or this tells the calculation which bar I want the moving average for. And finally, because I am working on every bar from my limit in the past up to now, "i" is the bar number I am calculating.

    So, I calculate the three moving averages first. And then, I perform some calculations to determine what I’m going to show on screen.

    upload_2021-8-21_22-22-38.png

    So, what I am saying is if the signal is greater than the fast moving average, and the fast moving average is greater than the slow moving average, so all three moving averages are aligned trending up, the I am setting the fast and the slow buffer to the fast and the slow moving averages.

    Equally, I am saying the if the signal is below the fast moving average, and the fast moving average is below the slow moving average, so all moving averages are trending down, then I am also setting values.

    Now, it would be possible to combine these into a single statement. You can see there is no difference between the lower pairs of lines. But, when I get to adding the histogram later, it will become apparent that there is a slight difference here.

    I am at the moment just plotting the two lines on screen.

    And the one case left here is where these three don't align, which is typically where the signal is between the fast and slow moving averages, or on the opposite side of the slow moving average to the fast moving average. In that case, nothing is to be done.

    And finally, return(rates_total); was already inserted by the Wizard when it created the template, and it returns rates total, telling Metatrader how many of the bars I have used. This will come back next time in the previously calculated, so that there is no need to recalculate past bars.

    This should be all I need to compile and see this on screen, except I don’t have the histogram yet, so I will compile the indicator and see if there are any errors.

    upload_2021-8-21_22-24-23.png

    Hmmm… I DO get an error, and so far I have not been able to figure out where my code does not match Joe’s.
     
    Last edited: Aug 22, 2021
    #44     Aug 22, 2021
  5. The error text gives the impression that on line #72 you have written slowMA instead of slowMa (capital letter A versus small a).
     
    #45     Aug 22, 2021
    expiated likes this.
  6. expiated

    expiated

    Thanks a million! I might have spent a couple of hours going over that same line of code before finally spotting this oversight/blind spot. (I also colored the slow moving average fire brick, like the fast moving average, instead of coloring it green.).:thumbsup:

    UPDATE: Thanks to HobbyTrading I was finally able to compile my indicator, but something looks awry. I think it might have something to do with the 5-period moving average and where it crosses the fast line.

    The indicator is supposed to do nothing when the three moving averages do not align—when the signal is between the fast and slow moving averages, or on the opposite side of the slow moving average to the fast moving average, so I figure that is what is probably going on.

    EURUSDDaily.png

    Maybe it will be okay once I add the histogram, but that may have to wait until Monday. I might take a break tomorrow given the amount of time I spent today typing up my previous two posts.
     
    Last edited: Aug 22, 2021
    #46     Aug 22, 2021
  7. I'm not sure what you mean by "do nothing". Looking at the indicators on the chart I do see portions where the indicators are not drawn (e.g. April 3rd ~ 16th). I guess those are the situations which you call "do nothing".

    For debugging purposes it could be helpful if you also draw the signalMa on your chart. Later on, when you have completed debugging, you could remove it.
     
    #47     Aug 22, 2021
  8. expiated

    expiated

    Oops! There were supposed to be gaps in the graphics. I wasn't paying close attention to the video, so I missed it when Joe said this (and his chart was so dark that I couldn't easily see that this was going on). As the video played, he actually plotted (dropped) all three moving averages on the chart manually so they could be seen (more clearly).

    So originally, the 5-period MA wasn't on HIS chart either. Joe had no desire to show it, so that's why there was no third property buffer for it.

    I'll type up Joe's directions for coding the histograms tomorrow.
     
    Last edited: Aug 22, 2021
    #48     Aug 22, 2021
  9. expiated

    expiated

    I dropped it on the chart manually, which is what helped my to confirm that the gaps between the fast and slow moving averages probably had something to do with the five-period measure.
     
    #49     Aug 22, 2021
  10. expiated

    expiated

    CODING THE HISTOGRAMS

    According to Joe, the histogram is a little bit unusual in its implementation. He said that in the new MetaTrader releases, there are better ways to do this, but at the time of his recording at least, he found them to be a little bit inconsistent in the way they operated.

    So, what he demonstrated was the older method. It has its own quirks, he says, but at least it is consistent in the way it operates. So, I had to make a couple of changes to what I had before.

    First of all, in order to show the histogram in two different colors depending on whether the trend is up or down, I needed two additional buffers. So, I increased the number of buffers from two to four.

    upload_2021-8-23_20-12-13.png

    Accordingly, I also needed to have places to store the histogram values...of course. So, I created two more buffers, one for my up histogram and the other for my down histogram. This required me to change the indicator numbers.

    I changed my fast indicator number from zero (0) to three (3), but my slow indicator still had the number one (1). This was to accommodate the numbers assigned to my new up-and-down indicators for the up-and-down histograms.

    The reason for changing the numbers was because when drawing histograms, MetaTrader draws them between the indicator for the histogram (so my up indicator, for example is the number zero) down to the next indicator number.

    So, when the asset is in an uptrend, I have to draw the histogram above the slow moving average up to the value specified in the up (i.e., histogram?) indicator; and when in a downtrend, I have to draw the histogram from the fast moving average up to some value specified in the down (i.e., histogram?) indicator.

    It's just the way they work, so the sequence is important. The value that one puts into the histogram indicator is the HIGH, and the value in the next greater (i.e., the moving average?) indicator number is the BOTTOM of that histogram bar.

    So, that means these numbers are going to have to change in the next section as well, and this is a good reason for defining numbers in the buffer section rather than hard coding the numbers of the indicators in the next (i.e., Custom indicator initialization function) section, because now these new numbers are going to be working automatically!

    upload_2021-8-23_20-45-44.png

    So then, I had to add the histogram definitions. So first, I specified the up indicator, then designated that the histogram was a dotted line, I then made it green, associated the up buffer with the up indicator, and finally, I indicated that empty values when nothing was specified were going to be zero (0) for this buffer, which simply means that no histogram lines would appear.

    upload_2021-8-23_20-43-28.png

    And next, I did the same thing for the down indicator. The style was dot, I chose a different color (i.e., fire brick), I associated the down buffer with the down indicator, etc...

    Now down here, Joe said there was a reason for separating those two pairs of identical statements. The reason is that when the asset is in an uptrend, I need to ONLY specify the up buffer; and when the asset is in a downtrend, I need to ONLY put a value into the downward buffer...

    upload_2021-8-23_20-30-22.png

    Again, the buffer on the histogram stores the high value for the histogram. Now, in an uptrend, the high value is the fast moving average. Consequently, because of the association of indicator numbers, the slow moving average is the number one (1), and the up indicator is the number zero (0).

    So, the histogram line will be drawn between the slow indicator and the up indicator.

    As already stated, when the asset is in a downtrend, I need to ONLY put a value into the downward buffer. And in this case, the top of the down buffer is at the slow moving average, and the bottom of the down buffer will be specified by the fast moving average.

    That should be it. Now I just need to compile it…

    AUDUSDH1.png

    (I plotted the three solid-line moving averages manually.)
     
    Last edited: Aug 23, 2021
    #50     Aug 23, 2021