My MQL4 Journal

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

  1. expiated

    expiated

    René Balke's MT4 Programming Lessons (continued)

    So, to summarize the first nine minutes from René Balke’s fourth video (see Post #81), if I am going to use an EA that relies on the RSI, I am going to need the OnTick function to look at the RSI's corresponding values with each new tick, and to do that, I am going to make the request (or query) between the OnTick function's opening and closing braces { }. Or more specifically, I am going to type code in the form of…

    double rsi = iRSI(const string symbol,int timeframe,int period,ENUM_APPLIED_PRICE applied_price,int shift)

    All that "stuff" between the parentheses is information I have to provide so that the expert advisor knows what manner of RSI I want to calculate, because when attaching an indicator to a chart, it might have several different options for its inputs (but as it turns out, the RSI only has one input—the period).

    However, in addition to the inputs, indicators also have a number of other parameters that are required to call the indicator function (to get the indicator's corresponding data).

    ScreenHunter_10799 Sep. 11 20.24.jpg

    So, in the case of the RSI, the first of all the parameters is the symbol (i.e., const string symbol).

    So, if I attach the expert advisor to the Euro-US dollar chart, for example, I will want that same chart’s corresponding data. Of course, this is kind of obvious, except that if I wanted to, I could opt to calculate the RSI of the GBPUSD instead of the EURUSD, even though the expert advisor is running on the EURUSD chart. So, this is what the first parameter is for. I get the symbol of the corresponding chart by typing an underscore (underline) followed by the word "symbol" (i.e., _Symbol). This holds the name/symbol of the underlying chart to which the expert advisor is attached.

    The next parameter is the time frame, which is identified by the corresponding minutes/integer such as 1, 5, 15, 30, etc. However, because it would be kind of difficult to calculate the number for…say…weekly charts, there are also enumerations that can be used instead (i.e., ENUM_TIMEFRAMES) which are found here:

    https://docs.mql4.com/constants/chartconstants/enum_timeframes

    To specify the timeframe of the underlying chart, the correct ENUM to use is PERIOD_CURRENT. (Note that to enumerate means "to mention a number of things one by one.")

    The next parameter required is the period, with the standard value used by the RSI indicator being 14. The parameter required after this is the applied price, with the RSI standard being the closing price, which is to say, PRICE_CLOSE. There are also numerical values that can be used here instead, but using the format just cited is a lot clearer.

    And finally, there is the shift value. Since I ceased shifting values several years ago, I am likely to always set this value to zero (0).

    By the way, the shift value is the number that identifies the bar (candlestick). The very last candle always has the number zero. The next value to the left will have the number one, and so on, with every single bar (number) having its own RSI value.

    The reason a coder might need the shift value is to identify exactly which bar it is from which they want to retrieve the desired RSI value(s) (if it isn't the current bar).

    (Note that it is because the readings on the RSI indicator are expressed as decimal numbers between 0 and 100 that the iRSI function is categorized as a double.)
     
    Last edited: Sep 12, 2021
    #91     Sep 11, 2021
  2. Be aware though that bar 0 is the current bar. Most likely has this bar not yet been completed. For example: if you use 10 minute bars on your chart, bar 0 represents only 3 minutes of trading, if the current time is 43 minutes after the hour. To ensure that you only use completed bars you can shift by one, which removes the current, incomplete, bar.
    Alternatively you can use PRICE_OPEN instead of PRICE_CLOSE. Then it is not necessary to shift and you know that all price samples are equidistant.
     
    #92     Sep 12, 2021
    expiated likes this.
  3. expiated

    expiated

    In Post #88, I should have said that the dark arrows were generated BASED on proprietary indicators I coded in the recent past. I just now realized how I can upload the actual custom indicators themselves into the EABuilder platform, which allows me to use the precise, exact settings for which they were designed, yielding the following outcome...

    upload_2021-9-12_1-14-27.png

    I think I'm liking EABuilder more and more as I am discovering all it can do, but time will tell. The real test will come if and when I upgrade and begin attempting to implement the use of the theoretical expert advisor I have in mind in real time.
     
    #93     Sep 12, 2021
  4. expiated

    expiated

    I can't see typing this stuff out all the time. Unless it already exists, I'm going to have to create a "library" of all these regularly used lines of code on my laptop that I can just copy and paste whenever I need them.
     
    #94     Sep 12, 2021
  5. expiated

    expiated

    Orchard Forex's Trend Ribbon Indicator, in Plain English

    Property strict is an instruction to the compiler to follow the
    MQL4 strict compilation mode that came about as a result of updates to
    the MQL4 language. It was introduced a while back, probably around build 600.
    Before that time, there were a number of defaults which the language would imply,
    and they were not strictly good coding practice. So, MetaTrader introduced
    better practices in the compiler. But, to maintain compatibility with code
    that had already been written, they added this property. It is therefore
    highly recommended that #property strict always be used.

    upload_2021-9-12_19-25-18.png

    upload_2021-9-12_19-32-16.png

    upload_2021-9-12_19-22-52.png

    upload_2021-9-12_19-26-56.png

    upload_2021-9-12_19-37-59.png

    By the way, all of the above information is found in a part of
    the code known as the "global scope." This space above the
    OnTick() function is reserved for information that needs to occur
    outside any other functions so that it can be seen inside all the
    functions.

    This might seem like a convenient way to define variables, but this
    is NOT recommended, except that one has no choice when it comes to
    inputs, which MUST be defined in the global scope.

    UPDATE: Change your wording in the DEFINING INPUTS section to the following...

    (MetaTrader repeatedly substitutes newly identified "labels" in place of corresponding orginal "labels" to which they have been declared equivalent, so that it can use THEM rather than using the original "tags.")
     
    Last edited: Sep 12, 2021
    #95     Sep 12, 2021
  6. expiated

    expiated

    This operation seems to go by two or three different names:

    upload_2021-9-13_18-54-38.png

    I don't know if they are Forex Software, forexsb, or Expert Advisor Studio.
     
    #96     Sep 13, 2021
  7. expiated

    expiated

    There is a good chance I will opt to use EABuilder within the next two weeks to create an expert advisor that relies on the indicators I just finalized today. So in preparation, I want to take a closer look at the videos the company provides, starting with this one focused on a moving average crossover strategy.


    1. The first thing I will need to do is create a new file where I will select "Strategy" instead of selecting "Indicator," and giving it a name. Right off the bat I notice that whereas the Indicator tabs consisted of: Indicator, Common, Alerts, Inputs, and Source; the Strategy tabs are: Open Trade, Close Trade, Common, Money Management, Alerts, Inputs, Source. Also, where the Indicator builder had "New Indicator Buffer," the Strategy builder has "New Buy Order" and "New Sell Order."
    2. I add the conditions in the same way that I did when I created indicators. However, I need to keep in mind that since "is above" is a continuous signal, if I were to use it, it would mean that an order would be opened on every tick so long as the faster moving average with the shorter period is above the slower moving average with the longer period, which would of course lead to many open trades. Consequently, I will want to use "crosses above," which is an instant signal, meaning that the order will be opened only once—when triggered by the crossover.
    3. Once I've set the initial condition for the "Open Buy Order," I can simply click on the "Open Sell Order" magic wand icon to create the opposite condition for sell orders.
    4. After I have all my "Open Trade" conditions set, I'll want to click on the "Close Trade" tab to set those conditions as well. If I want to use a moving average crossover to close the trade, I have the option of using the "cross" OR the "is" functions, because even if I choose the continuous signal, once the trade is closed, there will be no additional trades left that can also be closed.
    5. Again, when I have all my conditions set for closing long positions, I can use the "Close Short Positions" magic wand icon to automatically generate the corresponding opposite closing conditions.
    6. In the common tab, there are some additional options. But, I will come back to them later.
    7. Under "Money Management," I'll want to select a Fixed Size, and then move on to the Alerts tab to select an Audible Alert for "Each Order" and for "Errors" (i.e., error sending order).
    8. In inputs, I will need to check the "Add" box corresponding to "Trade Size" so that I can modify this if desired when using the EA.
    This is all I will need to do before copying the Source code.


    PROBLEM SOLVING:

    When back testing, supposing that I discover orders are opening and closing multiple times in the same place (due to the moving averages moving upwards and downwards multiple times in a single bar) this is where I would navigate back to the "Common" tab.

    (By the way, with this type of expert advisor, I will want to select the "Every tick" option when selecting the test "Model.")
    • After clicking on the tab, I would drop down to the list at the bottom, I would select "Next Open Trade After," and specify how many bars I want the EA to wait before opening another trade. I would then drop down to also select "Minimum Trade Duration," and specify the same number of bars so the EA will not close positions until then.
    Another way to deal with this is to go back to the "Open Trade" tab, and instruct the EA to to send an order only when a new bar opens. This would require adding an additional condition using the "Bar Opens" option from the menu, which consists of:
    • Indicator
    • Price
    • Candlestick
    • At Time
    • Bar Opens
    • Trades
    • Custom Code
    1. Because this signal is instant, I would need to change the "crosses above" to "is above," given that an order can only include one instant signal, under which "crosses above/below" are included. I would then create the opposite order by clicking on the magic wand icon again.
    2. Next, I'd have to click on the "Close Trade" tab so I could make the same or identical (type of) changes.
    3. This is followed by going back to the "Common" tab, and unclicking "Next Open Trade After" (if necessary) and unclicking "Minimum Trade Duration" (if necessary).
    4. However, since I would not want to open a trade for every bar as long as the condition is met, what I WOULD need to select here is "Maximum Open Trades" so I could set it to one (1). This will prevent the EA from opening a new trade before the current position is closed.
    And for this type of EA, where I am sending an order only at the bar opening, the back testing "Model" I will want to use is the fastest one: Open prices only.
     
    #97     Sep 13, 2021
  8. expiated

    expiated

    I will not be using a breakout strategy, but I am still interested in this video because it mentions the use of trailing stops.


    1. Of course, the first step is to create a new MetaTrader 4 Strategy and give it a fitting name. Then, under the "Open Trade" tab, click on the "Add condition" link to begin the process of generating the desired code.
    2. Once the "work page" opens up, select "Price" from the menu on the left-had side of the window. (You'll want to use the Bid price).
    3. Next, click on "crosses above," and then select support and resistance (S/R) from the menu on the right-hand side of the window. (Since you are using crosses above, you will want to set this parameter to Resistance.)
    4. In this instance, the "Time Interval" is set to 12 Hours, with the corresponding "Fixed time of the day" box selected/checked. Also, the end of the time interval needs to be specified using the "Time Interval ends at" tool/field/function. (Under all these "controls" is a diagram illustrating the meaning of Support, Resistance, and Time Interval.
    5. In this case, there was a desire to see Resistance drawn on the chart, so the "Draw lines on chart" box was checked.
    6. Finally, click on the OK button.
    There will be no need to set a stop loss (to click on SL) because the EA is going to use a trailing stop instead. But they did set the take profit, which first required clicking on the abbreviation TP.

    upload_2021-9-14_10-50-28.png

    In the popup window, they used the "Order Open Price +" field to set Take Profit to 100 pips (The Pips menu item was already opened.) and then they clicked on the OK button.

    The next move is to click on the magic wand icon down below (corresponding to Open Sell Order) to create the opposite instructions to sell. Of course, you need to double check to make sure that all the generated instructions are correct.

    (Note the following word of advice: If the website becomes a bit slow due to the number of conditions, just hit Crtl R to refresh. It supposedly helps.)
    1. Instead of closing the trade, the EA will use the take profit and trailing stop. So skip the "Close Trade" tab and go straight to the "Common" tab, and click on the "Add Trailing Stop" link.
    2. In the popup window, set the trailing stop at support for long positions (click S/R in the right-hand menu item list to do this.) along with the number of bars under "Time Interval" (in this case, the last 12).
    3. Unclick the Draw lines on chart box if necessary. Otherwise, the program will draw too many lines as the stop loss is moved. Then click the OK button.
    4. Again, click the magic wand icon down below to create the opposite trailing stop for short positions, and double check the automatically generated instruction to make sure they are correct.
    Because the number of trades has not yet been limited, the order will be sent multiple times when the price moves around the support or resistance.

    So, click on the "Common" tab, and then click the check box that corresponds to "Next Open Trade After TOD" in order to send the next order on the next day. Also, specify the time of day (using ten o'clock in this case, seeing as how this was the ending time of the support or resistance interval that was set earlier).

    Under the "Money Management" tab, set the desired "Fixed Size" in (decimal parts of) Lots. Then go to the "Inputs" tab and put a check in the "Add" box corresponding to "Trade Size" so this can be modified when using the EA, if desired.

    At this point, the source code should be ready.
     
    #98     Sep 14, 2021
  9. expiated

    expiated

    Well, I just got full access to EABuilder, but this is going to be more of a challenge than I thought.

    I might not know what I'm looking at, but it appears to me that it will not be enough to simply have a winning system. I'm going to have to first figure out what the MT4 platform will clearly understand and which instructions it will actually carry out as requested.

    I suspect it might have something to do with my using one-minute charts given that I've developed winning strategies (in the past) that worked with four-hour charts relatively easily. It took me an hour or two just to come up with what appears below, so the the graph generated by the back test did not simply consist of a steadily descending line...

    upload_2021-9-15_3-26-6.png

    So, tomorrow I will continue to attempt to figure out exactly which type of directions the program will perform as requested (at the one-minute level) and which it won't, and maybe then I'll be able to go back to attempting to devise a set of instructions that will result in outcomes that approximate the performance characterizing my manual trading.
     
    #99     Sep 15, 2021
  10. expiated

    expiated

    This is crazy!

    I have given the EA instructions to open a short position (have only one trade open at a time) when my indicator drops below -0.0535...

    upload_2021-9-15_13-44-37.png

    But, it is BUYING the pair instead—multiple times!!!

    ScreenHunter_10833 Sep. 15 13.32.jpg

    It is also executing trades when it supposed to be doing NOTHING!

    Last night and this morning and afternoon have been a terrific exercise in pinpointing exactly which aspects of my system are the most powerful in terms of rightly interpreting the direction of profitable trades.

    But, as far as figuring out how to take such insights and translate them into instructions that MetaTrade 4 will carry out as written, I have made ZERO progress!
     
    #100     Sep 15, 2021