Seite wählen

Welcome back to our Tradomite Algo-Trading series! Having acquainted ourselves with MQL4 and its potential in the previous part, it’s now time to delve into practical application. This segment will guide you through creating your first simple algo-trading bot using MQL4.

Step 1: Define Your Trading Strategy Before you start writing any code, it’s vital to have a clear trading strategy in mind. For our example, we’ll create a basic moving average crossover strategy:

  • The bot will execute a buy order when the fast moving average crosses above the slow moving average.
  • Conversely, it will execute a sell order when the fast moving average crosses below the slow moving average.
  • When the bot opens a trade, it will set a stop loss and take profit level.

Step 2: Set Up Your Environment To begin coding, launch MetaEditor from MT4 by clicking on Tools -> MetaQuotes Language Editor. Once in MetaEditor, go to File -> New -> Expert Advisor (template). Name it “MA_Crossover” and hit next.

Step 3: Write the Initialization Function In the generated code, you’ll see a function called OnInit(). This function runs when the EA starts up. You don’t need to change anything here for our simple bot.

Step 4: Write the Deinitialization Function The OnDeinit() function runs when the EA stops. You don’t need to change anything here either.

Step 5: Write the Main Execution Function The main part of our bot will be in the OnTick() function. This function runs every time a new price quote is received. Delete the existing code inside OnTick(), and then add the following:


void OnTick() {

double fastMA = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);

double slowMA = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 0);


if(fastMA > slowMA) {

OrderSend(Symbol(), OP_BUY, 1, Ask, 3, Bid – 20 * Point, Bid + 50 * Point);

} else if(fastMA < slowMA) {

OrderSend(Symbol(), OP_SELL, 1, Bid, 3, Ask + 20 * Point, Ask – 50 * Point);




In this code, iMA() is a built-in function that calculates the moving average. OrderSend() is another built-in function that sends a trade order. This simple bot will buy or sell 1 lot based on the moving average crossover, with a 20 point stop loss and 50 point take profit.

Step 6: Compile and Test Your Bot Once you’ve written the code, compile the bot by clicking on the Compile button in MetaEditor. If the compilation is successful, your bot will appear in the Navigator window of MT4, under Expert Advisors. You can now test and optimize your bot using MT4’s Strategy Tester.

Congratulations, you’ve built your first algo-trading bot! While it’s a simple bot, it forms the basis upon which you can expand and create more complex strategies. Remember, patience and continuous learning are key to mastering algo-trading. Stay tuned for the next part of this series where we’ll discuss backtesting and optimization in depth. With Tradomite, your journey into the world of algo-trading is never alone!