Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1962

 

I posted a question earlier, but it seems to have been deleted, and I don't know why.

Is there a way to code an EA to determine whether or not the Market is Closed (or Open) without having to submit an OrderSend() or OrderModify() instruction to the broker's server?  I am trying to avoid constant error codes (132 = "market is closed").

Thanks

Trader Mike

 
@Trader Mike #: I posted a question earlier, but it seems to have been deleted, and I don't know why. Is there a way to code an EA to determine whether or not the Market is Closed (or Open) without having to submit an OrderSend() or OrderModify() instruction to the broker's server?  I am trying to avoid constant error codes (132 = "market is closed"). 

It was removed because you did not search before you posted. However, you should have received an automated private message (see below) with the reason as well as a link to answer your question.

Did you not read your messages?

Checking If Market Opened or Closed - Mql4
Checking If Market Opened or Closed - Mql4
  • 2023.09.30
  • www.mql5.com
Hello everyone, For the past few days I've been looking for a reliant solution for this issue as many of the solutions I found online have many dra...
 

Is there any way to have elements automatically added to a dynamic array in mql4?

 
dankashem #: Is there any way to have elements automatically added to a dynamic array in mql4?
No. Obviously your code has to add them.
 
Fernando Carreiro #:
No. Obviously your code has to add them.

I am looking for the code that will add elements automatically in the array.

My aim is to place multiple buystop and sellstop orders in mt4. With each successfully placed order, I want to have the order price stored in an array automatically (one array for buystop prices and another array for sellstop prices) After this I want the code to access the array and get the price that is closest to ASK (probably this will be something like: MyArray [0] - to refer to the zero index within the array) sso that every time the ASK price moves below the price at index zero by a certain distance, then the code will automatically place additional orders to fill that gap. I want to do this for both sell orders and buy orders

My main challenge with all of this is to get the code that will automatically add elements (in this case order prices) into my array automatically; so that it will be possible to refer to the price that is closest to ASK (in order to place more buy orders) or access the price that is closest to BID price (in order to place more sell orders) 

Somebody please advise!

 
@dankashem #:

I am looking for the code that will add elements automatically in the array.

My aim is to place multiple buystop and sellstop orders in mt4. With each successfully placed order, I want to have the order price stored in an array automatically (one array for buystop prices and another array for sellstop prices) After this I want the code to access the array and get the price that is closest to ASK (probably this will be something like: MyArray [0] - to refer to the zero index within the array) sso that every time the ASK price moves below the price at index zero by a certain distance, then the code will automatically place additional orders to fill that gap. I want to do this for both sell orders and buy orders

My main challenge with all of this is to get the code that will automatically add elements (in this case order prices) into my array automatically; so that it will be possible to refer to the price that is closest to ASK (in order to place more buy orders) or access the price that is closest to BID price (in order to place more sell orders) 

Somebody please advise!

As I already stated, you have to program it to that. That is the purpose of the MQL programming language, for you to make the code that will automate a task.

You have to code a function or a method to read the trade data and store it in an array. There is no "automatic" way. You have to program it yourself in MQL code.

So I have to ask ... do you know how to code?

If not then here is the usual message ...

  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Documentation.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
  •  
    Fernando Carreiro #:

    As I already stated, you have to program it to that. That is the purpose of the MQL programming language, for you to make the code that will automate a task.

    You have to code a function or a method to read the trade data and store it in an array. There is no "automatic" way. You have to program it yourself in MQL code.

    So I have to ask ... do you know how to code?

    If not then here is the usual message ...

  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Documentation.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
  • I have  been learning from scratch how to code. Now I want to attempt my 1st EA. Am not very good yet buy I know I will get there. I was only hoping to get a small help on the forum.

     

    How to implement a table for MT4?

    Hello, I'm currently developing a utility for MT4. It uses a table where it presents data about various symbols (instruments). I need the utility to be able to update the table values and sort them as the prices for the various symbols change. It should be possible for the user to select which column the table shall be sorted on.

    So far I have used graphical objects such as OBJ_EDIT and OBJ_BUTTON to implement the table on a chart, but as the table has grown too large for my window, I will need a way to scroll the table. As it seems overly complex to implement a scroll functionality as well, I'm looking for alternative, easier, ways to implement this table.

    I've found some interesting articles on how to implement tables for MT5, such as the following:
    https://www.mql5.com/en/articles/2500
    https://www.mql5.com/en/articles/3104

    However, I don't know if it's possible to use this library for MT4/MQL4. Does anyone here have experience with using these classes for MT4/MQL4, and is it even possible?

    Are there more appropriate ways to implement tables in MT4/MQL4? I'd appreciate some advice from more experienced MQL4 programmers. Thanks!

    PS. Below I've attached a screenshot of the table in my utility. DS.

    Graphical Interfaces VII: the Tables Controls (Chapter 1)
    Graphical Interfaces VII: the Tables Controls (Chapter 1)
    • www.mql5.com
    The seventh part of the series on MetaTrader graphical interfaces deals with three table types: text label, edit box and rendered one. Another important and frequently used controls are tabs allowing you to show/hide groups of other controls and develop space effective interfaces in your MQL applications.
    Files:
     
    What am I doing wrong? The logic is opening a single long or single short at a specified time when the macd conditions are met. It keeps opening duplicates. One of the duplicate orders respects the desired TP and SL. The other order is not needed and never closes.  It happens in the tester and on a live account. I'm not using a VPS or attaching it to multiple charts to justify the duplicate orders. Here is the script. I also attached the report. Help will be much appreciated.
    // Input parameters
    input double LotSize = 0.05; // Lot size for orders
    input int StopLossPips = 1000; // Stop loss in pips
    input int TakeProfitPips = 200; // Take profit in pips
    input int TradeHour = 15; // Desired trading hour
    input int TradeMinute = 30; // Desired trading minute
    
    // MACD parameters
    input int FastLength = 7; // Fast length for MACD
    input int SlowLength = 14; // Slow length for MACD
    input int SignalSmoothing = 9; // Signal smoothing for MACD
    
    // Order type constants
    #define OP_BUY 0
    #define OP_SELL 1
    
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
    {
        return(INIT_SUCCEEDED);
    }
    
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
    }
    
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
    {
        if (IsNewTradingDay() && IsTradingTime())
        {
            double macdMainCurrent = iMACD(Symbol(), Period(), FastLength, SlowLength, SignalSmoothing, PRICE_CLOSE, MODE_MAIN, 0);
            double macdSignalCurrent = iMACD(Symbol(), Period(), FastLength, SlowLength, SignalSmoothing, PRICE_CLOSE, MODE_SIGNAL, 0);
    
            if (macdMainCurrent > macdSignalCurrent)
            {
                OpenTrade(OP_BUY);
            }
            else if (macdMainCurrent < macdSignalCurrent)
            {
                OpenTrade(OP_SELL);
            }
        }
    }
    
    //+------------------------------------------------------------------+
    //| Custom function to open a trade                                  |
    //+------------------------------------------------------------------+
    void OpenTrade(int orderType)
    {
        double openPrice = (orderType == OP_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);
        double stopLossPrice = (orderType == OP_BUY) ? openPrice - StopLossPips * Point : openPrice + StopLossPips * Point;
        double takeProfitPrice = (orderType == OP_BUY) ? openPrice + TakeProfitPips * Point : openPrice - TakeProfitPips * Point;
    
        int ticket = OrderSend(Symbol(), orderType, LotSize, openPrice, 3, 0, 0, 0, clrNONE);
    
        if (ticket > 0)
        {
            bool sltpSet = OrderSend(Symbol(), orderType, LotSize, openPrice, 0, stopLossPrice, takeProfitPrice, 0, clrNONE, 0, clrNONE);
            if (!sltpSet)
            {
                Print("Error setting stop loss or take profit for order. Error code: ", GetLastError());
            }
        }
        else
        {
            Print("Error opening order. Error code: ", GetLastError());
        }
    }
    
    //+------------------------------------------------------------------+
    //| Custom function to check if it's trading time                     |
    //+------------------------------------------------------------------+
    bool IsTradingTime()
    {
        int serverHour = Hour();
        int serverMinute = Minute();
    
        if (serverHour == TradeHour && serverMinute == TradeMinute)
        {
            return true;
        }
    
        return false;
    }
    
    //+------------------------------------------------------------------+
    //| Custom function to check if it's a new trading day                |
    //+------------------------------------------------------------------+
    bool IsNewTradingDay()
    {
        static datetime lastTradeDay = 0;
        datetime currentDay = iTime(Symbol(), Period(), 0);
    
        if (currentDay != lastTradeDay)
        {
            lastTradeDay = currentDay;
            return true;
        }
    
        return false;
    }
    
    Files:
    EA_report.txt  15 kb
     
    sibifid #:
    What am I doing wrong? The logic is opening a single long or single short at a specified time when the macd conditions are met. It keeps opening duplicates. One of the duplicate orders respects the desired TP and SL. The other order is not needed and never closes.  It happens in the tester and on a live account. I'm not using a VPS or attaching it to multiple charts to justify the duplicate orders. Here is the script. I also attached the report. Help will be much appreciated.
    Figured out myself. Missed magic number and this.
    // Order type constants
    enum OrderTypes
    {
        OP_BUY = 0,
        OP_SELL = 1
    };
    Reason: