Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

MACD Zero EA mt4 full code

  1. User

    MACD Zero EA mt4 full code

  2. Assistant

    As an AI language model, I can provide you a simple sample code for the MACD Zero Cross EA in MQL4. This is not a 'full code' EA but a starting point for your further improvements and adjustments. Here is the basic MACD Zero Cross EA for MT4:

    //+------------------------------------------------------------------+
    //|                                              MACDZeroCrossEA.mq4 |
    //|                                      Sample code by OpenAI GPT-3 |
    //+------------------------------------------------------------------+
    
    #property copyright "Sample code by OpenAI GPT-3"
    #property link      "https://github.com/Legrandstudios"
    
    #property version   "1.00"
    #property strict
    
    //--- EA settings
    input double Lots=0.1;
    input int StopLoss=50;
    input int TakeProfit=100;
    
    //+------------------------------------------------------------------+
    //| expert start function                                            |
    //+------------------------------------------------------------------+
    int OnInit()
    {
       return(INIT_SUCCEEDED);
    }
    
    void OnDeinit(const int reason)
    {
    }
    
    void OnTick()
    {
       // Get the current Bid and Ask prices
       double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
       double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
    
       // Calculate the MACD line and Signal line values
       double macd, signal, hist;
       int handle = iMACD(_Symbol, 0, 12, 26, 9, PRICE_CLOSE);
       CopyBuffer(handle, 0, 1, 2, macd); // Get MACD line value
       CopyBuffer(handle, 1, 1, 2, signal); // Get Signal line value
    
       // Check for MACD cross
       bool crossAbove = macd[0] > signal[0] && macd[1] < signal[1];
       bool crossBelow = macd[0] < signal[0] && macd[1] > signal[1];
    
       // Check for open orders
       bool hasOrders = false;
       for(int i=OrdersTotal()-1; i>=0; i--)
       {
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
          {
             if(OrderSymbol() == _Symbol && OrderMagicNumber() == 101)
             {
                hasOrders = true;
                break;
             }
          }
       }
    
       // Open trade if no open trades and MACD cross occurred
       if(!hasOrders)
       {
          if(crossAbove)
          {
             // Open BUY trade
             double sl = Bid - StopLoss * _Point;
             double tp = Bid + TakeProfit * _Point;
             OrderSend(_Symbol, OP_BUY, Lots, Ask, 3, sl, tp, "MACD Cross Buy", 101, 0, clrBlue);
          }
          else if(crossBelow)
          {
             // Open SELL trade
             double sl = Ask + StopLoss * _Point;
             double tp = Ask - TakeProfit * _Point;
             OrderSend(_Symbol, OP_SELL, Lots, Bid, 3, sl, tp, "MACD Cross Sell", 101, 0, clrRed);
          }
       }
    }
    //+------------------------------------------------------------------+
    

    Please consider that this sample code might need further customization and optimization depending on your trading strategy, account settings, risk management, etc. This code is just a basic starting point for a MACD Zero Cross EA.