In this video we are going to create buy and sell signals and code a standalone version that is able to calculate this one line from the Ichimoku Kinko Hyo, it’s called a Tenkan-sen, that’s the red line here.
Now we want to find out how to do that in MQL5?
To do that please click on the little icon here or press F4 on your keyboard now you should see the Metaeditor window and here you want to click on: ”File/ New/ Expert Advisor (template)” from template, “ Continue”, I will call this file: “SimpleTenkansenStandaloneEA”, click on “Continue”, “Continue” and “Finish”.
Now you can delete everything above the “OnTick” function and the two comment lines here.
We start with the “include” statement to include the file “Trade.mqh”, that comes with MQL5 and we need it to create an instance of “CTrade” that will be called: “trade” and we will use it to open and close positions later on.
We start by calculating the Ask price and the Bid price, this is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_ASK” to calculate the Ask price and “SYMBOL_BID” to calculate the Bid price.
“NormalizeDouble” and “_Digits” will automatically calculate the right number of digits behind the dot.
Next we need to create a signal variable for the signal that also will be called: “signal” and we don’t assign a value here.
We use “MqlRates” to create a price array called: “PriceInformation”, that one will contain the prices of the candles, we use “ArraySetAsSeries” to sort the array (PriceInformation) from the current candle downwards and with “CopyRates” we fill the price array (PriceInformation) with data for the current symbol on the chart and the currently selected period on the chart. We start from the candle 0 (zero) and copy the price information for 3 candles into our price array (PriceInformation).
For the Tenkan-sen we need to create another array and we use the MQL5 function: “iIchimoku”, this one can calculate the Tenkan-sen, the Kijun-sen, the Senkou Span depending on the index buffer that we use, buffer 0 (zero) is for the Tenkan-sen, buffer 1 is for the Kijun-sen and so on.
For the parameters we use the current symbol, the currently selected period on the chart, here we have three more values; 9, 26 and 52 and when we click on: “Insert/ Indicators/ Trend/ Ichimoku Kinko Hyo” we will see the same values here so that’s how we use them. 9 is for the Tenkan-sen, 26 is for the Kijun-sen and 52 is for the Senkou Span B.
In our example we only calculate one of them; that’s the Tenkan-sen, so let’s use “ArraySetAsSeries” to sort our Tenkan-sen array (TenkansenArray) from the current candle downwards and with “CopyBuffer” we fill our Tenkan-sen array (TenkansenArray) according to the Ichimoku definition (IchimokuDefinition) that we have created here for buffer 0 (zero), we start with the current candle 0 (zero) and we use the data for 3 candles.
Now we can calculate the Tenkan-sen value (TenkansenValue) by looking at candle 0 (zero) in our “TenkansenArray”.
For our simplified example we would say that whenever the value is above, so the “ThenkansenValue” would be greater than the close price for candle 1 in our “PriceInformation” array, we would consider that to be a sell signal and so we assign the word: “sell” to or signal.
In the other case if the “TenkansenValue” is below the “PriceInformation” close price for candle 1 we would consider that to be a buy signal and now we would assign the word: “buy” to our signal.
And when the signal equals sell and we have no open positions that’s when we would use “trade.Sell” to sell 10 micro lot, otherwise if the signal equals buy and we have no open positions we would use: “trade.Buy” and buy 10 micro lot.
We finish by creating a chart output using the “Comment” function to output the text: “The current signal is:” followed by the calculated “signal”.
Now this is only one of several values that you could calculate for this Indicator, you can spend hours to learn how to read it and there are also several interpretations but in our simplified case we just go with the red line here so when you are done you can click on the “Compile” button or press F7, that should work without any errors here and if this is the case please click here or press F4 to go back to Metatrader.
When inside of Metatrader we click on; “View/ Strategy Tester” or press CTRL and R, here we select the: “SimpleTenkansenStandaloneEA.ex5”, please enable the visualization option and start your test.
…and here we are! We have a buy signal right now and whenever the price crosses the red line that would switch to a sell signal.
Now you know how to calculate the Tenkan-sen value for the Ichimoku Kinko Hyo Indicator, it’s also possible to calculate all the other values here and create specific rules but now you know how to automate the Tenkan-sen and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 Tutorial - Simple Tenkansen Standalone Expert Advisor”SimpleTenkansenStandaloneEA.txt – Downloaded 223 times – 1.83 KB