The Internet

Log In or Register

Image of a computer screen displaying an MQL4 coding interface with sample code for an expert advisor, showcasing trading logic and indicators.

Ask AI: please make a sample expert advisor for mq4

Bookmark

0 Points

▲ good image

bad image ▼

Ask AI on The Internet

Question: please make a sample expert advisor for mq4

Asked by md_abu_rayhan

0 Points

Log In or Register to Vote

Certainly, I can help you with a basic example of an Expert Advisor (EA) for MetaTrader 4 (MT4) using the MQL4 programming language. Below is a sample EA that implements a simple moving average crossover strategy. This strategy will create buy and sell orders based on the crossover of two moving averages. ```mql4 //+------------------------------------------------------------------+ //| SimpleEA.mq4 | //| Copyright 2023 | //| Your Name | //+------------------------------------------------------------------+ #property strict // Input parameters input int FastMAPeriod = 12; // Period of the fast moving average input int SlowMAPeriod = 26; // Period of the slow moving average input double LotSize = 0.1; // Size of each trade input int Slippage = 3; // Slippage for order execution input double TakeProfitPoints = 50; // Take profit in points input double StopLossPoints = 50; // Stop loss in points int ticket; double EMAFast; double EMASlow; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Print a message in the Expert tab Print("EA initialized"); // Return initialization result return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Print a message in the Expert tab Print("EA deinitialized"); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Calculate the moving averages EMAFast = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,0); EMASlow = iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,0); double EMAFastPrev = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,1); double EMASlowPrev = iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,1); // Check for a buy crossover if (EMAFast > EMASlow && EMAFastPrev <= EMASlowPrev) { // Close any open sell orders CloseOrders(OP_SELL); // Open a buy order ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, 0, 0, "Buy Order", MAGIC_NUMBER, 0, Blue); if(ticket < 0) Print("Error opening BUY order: ", GetLastError()); else Print("BUY order opened successfully"); } // Check for a sell crossover if (EMAFast < EMASlow && EMAFastPrev >= EMASlowPrev) { // Close any open buy orders CloseOrders(OP_BUY); // Open a sell order ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, 0, 0, "Sell Order", MAGIC_NUMBER, 0, Red); if(ticket < 0) Print("Error opening SELL order: ", GetLastError()); else Print("SELL order opened successfully"); } } //+------------------------------------------------------------------+ //| Function to close orders | //+------------------------------------------------------------------+ void CloseOrders(int orderType) { for (int i=OrdersTotal()-1; i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderType() == orderType && OrderSymbol() == Symbol()) { OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, clrNONE); } } } } //+------------------------------------------------------------------+ ``` ### Explanation of the EA: 1. **Input Parameters:** - `FastMAPeriod` and `SlowMAPeriod`: These determine the periods for the fast and slow moving averages. - `LotSize`: The size of each trade in lots. - `Slippage`: The acceptable slippage in points. - `TakeProfitPoints` and `StopLossPoints`: The take profit and stop loss levels in points. 2. **OnInit and OnDeinit Functions:** - These functions handle the initialization and deinitialization tasks respectively. 3. **OnTick Function:** - This function is executed on every tick. - It calculates the moving averages and checks for a crossover. - If a crossover is detected, it closes any existing opposite orders and opens a new order in the direction of the crossover. 4. **CloseOrders Function:** - This function closes orders of a specified type (buy or sell). Before using this EA on a live account, it's important to test it thoroughly on a demo account to ensure it behaves as expected. Additionally, you may need to add more error handling and risk management based on your trading strategy and risk tolerance.

Answered by gpt-4o

Dec. 6, 2024, 4:41 a.m.

0 Points

Log In or Register to Vote

Think your answer is better than the AI's? Post it below.

Please log in or sign up to post your answer.

Question Tags
This article was generated in part by one of OpenAI’s large-scale language-generation model, answering user md_abu_rayhan's question. Images were generated with OpenAI's AI model, DALL·E 3. The Internet takes ultimate responsibility for the content of this publication.
If you want your question answered by an AI, click here.

Published: Friday, December 6, 2024

Comment Section

Post your own comment: