I am new in 2D Arrays in Mql5 . - page 2

 
Sunny DP #:

MyArray [0] [0] =  Here we will save the ticket number of the previous open trade.

Here is some code to get you started - play around with it.
But do consider the advice above - using a struct in an array is far more effective


//+------------------------------------------------------------------+
//|                                               457908-2dArray.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

#define def_cols           4
#define def_ticketNo       0
#define def_OpenPrice      1
#define def_LotSize        2
#define def_NextDayPrice   3
double MyArray [][def_cols];




int    LastRow = ArrayResize(MyArray, 1) - 1;   // Start with first row [0]

void OnStart()
  {

         MyArray[LastRow][def_ticketNo]      = 123456789;
         MyArray[LastRow][def_OpenPrice]     = 1.2345;
         MyArray[LastRow][def_LotSize]       = 1.0;
         MyArray[LastRow][def_NextDayPrice]  = 1.3456;

         LastRow = ArrayResize(MyArray, (ArraySize(MyArray)/def_cols) +1) - 1; //Increase rows by 1 [Note ArraySize() returns number of cells so divide by cols to get rows]

         MyArray[LastRow][def_ticketNo]      = 123456790;
         MyArray[LastRow][def_OpenPrice]     = 1.2345;
         MyArray[LastRow][def_LotSize]       = 1.1;
         MyArray[LastRow][def_NextDayPrice]  = 1.4567;
        
        ArrayPrint(MyArray);
        
        
  }
//+------------------------------------------------------------------+
 
R4tna C #:

Here is some code to get you started - play around with it.
But do consider the advice above - using a struct in an array is far more effective


thank you so much brother 
 
R4tna C #: Here is some code to get you started - play around with it. But do consider the advice above - using a struct in an array is far more effective

according to my knowledge i am doing it correctly but it is not working can u please have a look ?

void OnTick()
  {        
        
          int totalPositions = PositionsTotal(); // Get the total number of open positions
          double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
          double lotSize = PositionGetDouble(POSITION_VOLUME);
          double newPrice = NormalizeDouble(openPrice - 500 * _Digits); // for buy
         
         // Iterate through all open positions to get their information
          for (int i = 0; i < totalPositions; i++)
          {
              ulong ticket= PositionGetTicket(i);
          
              if (PositionSelectByTicket(ticket)) // Select the position by its ticket
              {
           
         MyArray[LastRow][def_ticketNo]      = (double)ticket;
         MyArray[LastRow][def_OpenPrice]     = openPrice;
         MyArray[LastRow][def_LotSize]       = lotSize;
         MyArray[LastRow][def_NextDayPrice]  = openPrice + Points;

         LastRow = ArrayResize(MyArray, (ArraySize(MyArray)/def_cols) +1) - 1; //Increase rows by 1 [Note ArraySize() returns number of cells so divide by cols to get rows]

         MyArray[LastRow][def_ticketNo]      = (double)ticket;
         MyArray[LastRow][def_OpenPrice]     = openPrice;
         MyArray[LastRow][def_LotSize]       = lotSize;
         MyArray[LastRow][def_NextDayPrice]  = openPrice + Points;

        ArrayPrint(MyArray);
        }
       }
  } 

Improperly formatted code edited by moderator.

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Sunny DP #: according to my knowledge i am doing it correctly but it is not working can u please have a look ?

Some suggestions:

  • Be more specific when you say "its not working" - what are the errors?
  • Paste compilable code (the full mq5 file) - that way I can debug it if necessary
  • You need to read the documentation on PositionGetDouble() - at first glance your usage does not appear correct.
    You have to select the context of the ticket you want before using such functions [i.e. within the for (int i = 0; i < totalPositions; i++) loop
    - AFTER the ulong ticket= PositionGetTicket(i) statement]
  • Write a function to encapsulate the addition to the array - just pass the values. No point writing repetitive code - re-use

Please read the MQL5 documentation - there are lots of good examples for everything you need - copy their style for optimal/better code

 
Sunny DP #: according to my knowledge i am doing it correctly but it is not working can u please have a look ?

Improperly formatted code edited by moderator.

Please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
R4tna C #:

Some suggestions:

  • Be more specific when you say "its not working" - what are the errors?
  • Paste compilable code (the full mq5 file) - that way I can debug it if necessary
  • You need to read the documentation on PositionGetDouble() - at first glance your usage does not appear correct.
    You have to select the context of the ticket you want before using such functions [i.e. within the for (int i = 0; i < totalPositions; i++) loop
    - AFTER the ulong ticket= PositionGetTicket(i) statement]
  • Write a function to encapsulate the addition to the array - just pass the values. No point writing repetitive code - re-use

Please read the MQL5 documentation - there are lots of good examples for everything you need - copy their style for optimal/better code

here is the file brother 
Files:
28nov.mq5  4 kb
 
Sunny DP #:
here is the file brother 

Please follow the bullet points 3 & 4 i mentioned - your efforts will result in you learning MQL5 better.

Coding it yourself is the only way.

 
Sunny DP #:
here is the file brother 

...wouldnt new price be "... * _Point" not Digits?

 
Revo Trades #:

...wouldnt new price be "... * _Point" not Digits?

yes brother my mistake

Reason: