Beginner needs help with questions

 

Hello, new to MQL4 EA's and new to MT4.

Questions:

  1. Do MT4 EAs need OnInit() or OnStart()?
  2. Do I put indicators inside the EA or reference indicators within the EA?
  3. How can I reference indicators within my EA?

Scenario:

I am trying to have an EA that trades based on the conditions of 2 indicators. Right now I have the 2 indicators within the EA, but they were not popping up on the chart when I added the EA. I can add the indicators individually to the chart. It has been a. few days and my EA has not traded. I am pretty sure my account is connected to MT4 and my lot size is not too much for my account.

Thank you.

 

Your topic has been moved to the section: MQL4 e MetaTrader 4 — In the future, please consider which section is most appropriate for your query.

  1. OnStart is for Scripts, while OnInit is for EAs and IndicatorsProgram Running - MQL4 programs - MQL4 Reference
  2. You reference the Indicators from within the EAs.
  3. Using iCustom for custom Indicators, and the respective "i" function for built-in indicators — Technical Indicators - MQL4 Reference

If you need help with your EA code, you should show your code and describe your issue in detail.

Program Running - MQL4 programs - MQL4 Reference
Program Running - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Program Running - MQL4 programs - MQL4 Reference
 
Fernando Carreiro #:

Your topic has been moved to the section: MQL4 e MetaTrader 4 — In the future, please consider which section is most appropriate for your query.

  1. OnStart is for Scripts, while OnInit is for EAs and IndicatorsProgram Running - MQL4 programs - MQL4 Reference
  2. You reference the Indicators from within the EAs.
  3. Using iCustom for custom Indicators, and the respective "i" function for built-in indicators — Technical Indicators - MQL4 Reference

If you need help with your EA code, you should show your code and describe your issue in detail.

Hello, I will paste my code here. The issue is that I have not entered a trade since I have started. I have a $500 account and I don't think that is the issue. As you can see I have created indicators within this EA, but I also have them both as individual indicator files as well added to the chart.

My EA code:

//+------------------------------------------------------------------+
//|                                                    DidiX_ADX.mq4 |
//|                                                   Samuel Johnson |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Samuel Johnson"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#property indicator_chart_window

extern int curtaLength = 3;
extern int mediaLength = 8;
extern int longaLength = 20;
extern int len = 8;
extern int lensig = 14;
extern bool applyFilling = true;
extern bool highlightCrossovers = true;

double curtaBuffer[];
double mediaBuffer[];
double longaBuffer[];
double diPlusBuffer[];
double diMinusBuffer[];
double adxBuffer[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
      SetIndexBuffer(0, curtaBuffer, INDICATOR_DATA);
      SetIndexBuffer(1, mediaBuffer, INDICATOR_DATA);
      SetIndexBuffer(2, longaBuffer, INDICATOR_DATA);
      SetIndexBuffer(3, diPlusBuffer, INDICATOR_DATA);
      SetIndexBuffer(4, diMinusBuffer, INDICATOR_DATA);
      SetIndexBuffer(5, adxBuffer, INDICATOR_DATA);
      
      IndicatorSetString(INDICATOR_SHORTNAME, "My Didi Index");
      
      SetIndexStyle(0, DRAW_LINE);
      SetIndexStyle(1, DRAW_NONE);
      SetIndexStyle(2, DRAW_LINE);
      SetIndexStyle(3, DRAW_LINE);
      SetIndexStyle(4, DRAW_LINE);
      SetIndexStyle(5, DRAW_LINE);
      
      SetIndexLabel(0, "Curta");
      SetIndexLabel(2, "Longa");
      SetIndexLabel(3, "+DI");
      SetIndexLabel(4, "-DI");
      SetIndexLabel(5, "ADX");
      
      SetIndexDrawBegin(0, longaLength);
      SetIndexDrawBegin(2, longaLength);
      SetIndexDrawBegin(3, longaLength);
      SetIndexDrawBegin(4, longaLength);
      SetIndexDrawBegin(5, longaLength);
      
      int limit = Bars - longaLength;
      
      ArrayResize(curtaBuffer, limit);
      ArrayResize(mediaBuffer, limit);
      ArrayResize(longaBuffer, limit);
      ArrayResize(diPlusBuffer, limit);
      ArrayResize(diMinusBuffer, limit);
      ArrayResize(adxBuffer, limit);
   
//---
      return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }

bool IsLongOrderOpen()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == _Symbol && OrderType() == OP_BUY)
                return true;
        }
    }

    return false;
}

bool IsShortOrderOpen()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == _Symbol && OrderType() == OP_SELL)
                return true;
        }
    }

    return false;
}

void OpenLongOrder()
{
    double lotSize = 0.001;
   
    int ticket = OrderSend(_Symbol, OP_BUY, lotSize, Ask, 5, 0, 0, "Long Order", 0, 0, Blue);
    if (ticket > 0)
        Print("Long order opened: ", OrderTicket());
    else
        Print("Failed to open long order. Error code: ", GetLastError());
}

void OpenShortOrder()
{
    double lotSize = 0.001;

    int ticket = OrderSend(_Symbol, OP_SELL, lotSize, Bid, 5, 0, 0, "Short Order", 0, 0, Red);
    if (ticket > 0)
        Print("Short order opened: ", OrderTicket());
    else
        Print("Failed to open short order. Error code: ", GetLastError());
}

void OnTick()
{
    double curta[4];
    double diPlus, diMinus;

    // Calculate Didi Index values
    for (int i = 0; i < 4; i++)
    {
        curta[i] = iMA(_Symbol, 0, curtaLength, 0, MODE_SMA, PRICE_CLOSE, i) / iMA(_Symbol, 0, mediaLength, 0, MODE_SMA, PRICE_CLOSE, i);
    }

    diPlus = iADX(NULL, 0, len, PRICE_CLOSE, MODE_PLUSDI, 0);
    diMinus = iADX(NULL, 0, len, PRICE_CLOSE, MODE_MINUSDI, 0);

    bool buySignal = curta[3] > curta[2] && curta[2] > curta[1] && curta[1] <= curta[0] && diPlus > diMinus;
    bool sellSignal = curta[3] < curta[2] && curta[2] < curta[1] && curta[1] >= curta[0] && diMinus > diPlus;

    if (IsLongOrderOpen() && !IsShortOrderOpen() && sellSignal)
    {
        Print("Closing long order: ", OrderTicket());
        OrderClose(OrderTicket(), OrderLots(), Bid, 5, Red);
    }
    else if (IsShortOrderOpen() && !IsLongOrderOpen() && buySignal)
    {
        Print("Closing short order: ", OrderTicket());
        OrderClose(OrderTicket(), OrderLots(), Ask, 5, Blue);
    }
    else if (!IsLongOrderOpen() && !IsShortOrderOpen())
    {
        if (buySignal)
            OpenLongOrder();
        else if (sellSignal)
            OpenShortOrder();
    }
}

通过MQL5社区和服务探索MetaTrader 5的新机遇
通过MQL5社区和服务探索MetaTrader 5的新机遇
  • 2023.06.20
  • www.mql5.com
MQL5:MetaTrader 5客户端内置的交易策略语言。语言允许编写您自己的自动交易系统,技术指标,脚本和函数程序库
 
Samuel Johnson #: , I will paste my code here. The issue is that I have not entered a trade since I have started. I have a $500 account and I don't think that is the issue. As you can see I have created indicators within this EA,
      SetIndexBuffer(0, curtaBuffer, INDICATOR_DATA);
      SetIndexBuffer(1, mediaBuffer, INDICATOR_DATA);
      SetIndexBuffer(2, longaBuffer, INDICATOR_DATA);
      SetIndexBuffer(3, diPlusBuffer, INDICATOR_DATA);
      SetIndexBuffer(4, diMinusBuffer, INDICATOR_DATA);
      SetIndexBuffer(5, adxBuffer, INDICATOR_DATA);

It is an issue; your code is babble. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
          (MT4) Detailed explanation of iCustom - MQL4 programming forum (2017)

 
William Roeder #:

It is an issue; your code is babble. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
          (MT4) Detailed explanation of iCustom - MQL4 programming forum (2017)

Hello, I made some changes after learning about iCustom(). Do you think this will work? Just waiting for the market to open to test.

I am connecting 2 external indicators to an EA using iCustom(). Thank you.

#property strict

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
      return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }

bool IsLongOrderOpen()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == _Symbol && OrderType() == OP_BUY)
                return true;
        }
    }

    return false;
}

bool IsShortOrderOpen()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == _Symbol && OrderType() == OP_SELL)
                return true;
        }
    }

    return false;
}

void OpenLongOrder()
{
    double lotSize = 0.001;
   
    int ticket = OrderSend(_Symbol, OP_BUY, lotSize, Ask, 5, 0, 0, "Long Order", 0, 0, Blue);
    if (ticket > 0)
        Print("Long order opened: ", OrderTicket());
    else
        Print("Failed to open long order. Error code: ", GetLastError());
}

void OpenShortOrder()
{
    double lotSize = 0.001;

    int ticket = OrderSend(_Symbol, OP_SELL, lotSize, Bid, 5, 0, 0, "Short Order", 0, 0, Red);
    if (ticket > 0)
        Print("Short order opened: ", OrderTicket());
    else
        Print("Failed to open short order. Error code: ", GetLastError());
}

void OnTick()
{
    double curta[4] = {0};
    double diPlus = iCustom(NULL, 0, "ADX_Smoothed", 0, 0);
    double diMinus = iCustom(NULL, 0, "ADX_Smoothed", 1, 0);
    
    curta[0] = iCustom(NULL, 0, "Didi_Index", 0, 0);
    curta[1] = iCustom(NULL, 0, "Didi_Index", 0, 1);
    curta[2] = iCustom(NULL, 0, "Didi_Index", 0, 2);
    curta[3] = iCustom(NULL, 0, "Didi_Index", 0, 3);

    bool buySignal = curta[3] > curta[2] && curta[2] > curta[1] && curta[1] <= curta[0];
    bool sellSignal = curta[3] < curta[2] && curta[2] < curta[1] && curta[1] >= curta[0];

    if (IsLongOrderOpen() && !IsShortOrderOpen() && diMinus > diPlus && sellSignal)
    {
        Print("Closing long order: ", OrderTicket());
        OrderClose(OrderTicket(), OrderLots(), Bid, 5, Red);
    }
    else if (IsShortOrderOpen() && !IsLongOrderOpen() && diPlus > diMinus && buySignal)
    {
        Print("Closing short order: ", OrderTicket());
        OrderClose(OrderTicket(), OrderLots(), Ask, 5, Blue);
    }
    else if (!IsLongOrderOpen() && !IsShortOrderOpen())
    {
        if (buySignal)
            OpenLongOrder();
        else if (sellSignal)
            OpenShortOrder();
    }
}