
Learn to code a carry-trade rollover strategy in MQL5 that earns from swap interest, trading a 2-minute overnight position once weekly on Wednesdays, with backtests from 2021–2024.
Configure a carry trade expert advisor by defining inputs like symbol euro/usd, period m5, and slippage, and customize the chart to display candles with a dark background and no grid.
Regulate the number of trades on the chart by building a const input function that counts only matching position types, symbols, and metrics. Use a for loop and index-based selection.
Configure a spread filter for the expert advisor by defining a risk inputs group, a maximum spread in points, and a boolean check that stops trades when spread is high.
Generate buy and sell signals by checking swap long and swap short greater than 15, ensure no buy or sell trades and favorable spread, then return true to enter trades.
Calculate the trade lot size from account balance and risk inputs, applying volume increase and balance increase, and clamp to symbol max and min volumes, with a five-pip stop loss.
Initialize a new trade object for buy and sell entries, set maximum slippage and deviation in points, and configure the magic number and lot size for precise, trackable trades.
Create a close positions function to automatically close trades at daily rollover by filtering positions by E and symbol, comparing times, and using position tickets.
Implement on tick events to close positions when conditions are met, manage date time data, and open trades at 23:58 on days when the swap is multiplied three times.
Backtesting a carry trade strategy in the strategy tester uses gold on a five-minute chart, with swaps and spreads driving profits while highlighting gap risks and drawdown.
Conclude by coding a carry trade/rollover strategy in MQL5, testing it during rollover, and noting MT5 does not store swap data, while applying swap insights to your trading strategies.
Rollover in forex refers to the process of extending the settlement date of an open position beyond the standard window. When you trade a currency pair, you're essentially agreeing to buy one currency and sell another at a specific exchange rate. Traditionally, this settlement occurs within a standard window period and you will be obliged to pay the equivalent of the currency you bought using the currency you sold. However, since we are only trading for speculative purposes and we don’t want the actual physical currency, we usually buy back the currency we sold using the currency we bought before the settlement date. However If you hold a position past the standard settlement date by not closing it, a rollover occurs. This means you're essentially borrowing one currency to hold the other overnight.
Each currency has an interest rate that is set by the currency country for borrowers of the currency. Since you're borrowing one currency and holding the other, you'll either pay or receive interest depending on the interest rate differential between the two currencies in the pair. If the interest rate of the currency you're selling (quote currency) is higher than the interest rate of the currency you're buying (base currency), you'll incur an interest expense for holding the position overnight. This is deducted from your account balance.
Conversely, if the interest rate of the base currency is higher than the quote currency, you'll earn interest for holding the position overnight. This is credited to your account balance.
A carry trade involves borrowing a currency with a low interest rate and using it to invest or buy in a currency with a higher interest rate. The goal is to pocket the positive difference between the two rates. In this course we are going to code a strategy together that takes advantage of this and it’s completely legal. The strategy we shall code together enters trades only for 2 minutes during midnight to earn money on differential interest rates or swaps. It is a strategy that does not need you to speculate on whether the market will be bullish or bearish. It just relies on you borrowing a currency with a low interest rate to buy a currency with a high interest rate and taking the profit for yourself.
To understand how such a strategy works and how to code it in MQL5, click hard on that enroll button now and follow me into this course as we dive deeper into uncommon knowledge and uncover more secrets about the forex market.