Variables keep resetting to initial declared values - page 2

 

Thank you both for the replies, that makes sense.


Is it possible to call an element in an array based on matching the value within it - so kind of like a VLOOKUP in Excel (its the best analogy I could think of).


In the above example, how would it be possible to return the OrderOpenTime for a particular ticket? Is it this? Or is there another better/easier way?


int ticket = ~the ticket I want~;
int opentime = 0;


for (int i = 0; i < orderstotal; i++)
{
     if (ordticket[orders][1] = ticket)

         {
           opentime = ordticket[orders][0];
         }
}
 

You have to think about what you are trying to do.

int ticket = ~the ticket I want~;
int opentime = 0;


for (int i = 0; i < orderstotal; i++)
{
     if (ordticket[orders][1] = ticket)

         {
           opentime = ordticket[orders][0];
         }
}

You are not using i in the loop at all.

You are accessing the same array element every pass of the loop.

Doesn't make any sense!

Documentation on MQL5: Language Basics / Variables
Documentation on MQL5: Language Basics / Variables
  • www.mql5.com
Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Ofcourse thanks for correcting!

But this is the best way of looking up the ticket - using a loop, as opposed to another (easier) way?

int ticket = ~the ticket I want~;
int opentime = 0;


for (int orders = 0; orders < orderstotal; orders++)
{
     if (ordticket[orders][1] = ticket)

         {
           opentime = ordticket[orders][0];
         }
}
 
That should be Ok.
 

Thanks a lot for all your help!

 

Hi there,


Does anyone know how to declare a global Array

double TicketHighLow[90][4];


Such that the 3rd column TicketHighLow[2] all have an initial value 1000

and the 4th column TicketHighLow[3] all have an initial value 0?


 0  1 2
3


1000
0


1000
0
 
  1. Quazon: Does anyone know how to declare a global Array Such that …

    Can't be done. Declare it and then initialize it.

    double TicketHighLow[90][4];
    for(int i=ArrayRange(TicketHighLow,0)-1; i >= 0; --i){ TicketHighLow[i][2]=1000; TicketHighLow[i][3]=0; }
  2. You probably don't want a two-dimensional array. You probably want a one dimension array of a struct.
    struct Data{ int ticket; whatever X; double high,low; 
       void Data() : high(EMPTY_VALUE), low(-EMPTY_VALUE){}
    }
    Data TicketHighLow[90];
    ⋮
    TicketHighLow[0].ticket=…;

 
William Roeder:
  1. Can't be done. Declare it and then initialize it.

  2. You probably don't want a two-dimensional array. You probably want a one dimension array of a struct.


Thanks a lot for this. Is there an distict advantage of using struct over a two dimensional array?

Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference
Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference
 

I ran into this when I incorporate:

'for' - expressions are not allowed on a global scope    trailing stop.mq4    73    1

double TicketHighLow[90][4];
for(int i=ArrayRange(TicketHighLow,0)-1; i >= 0; --i){ TicketHighLow[i][2]=1000; TicketHighLow[i][3]=0; }



I wanted to initialize the array with 

TicketHighLow[i][2]=1000

so that this part of the code would work:

                    if (Bid < TicketHighLow[i][2])
                        {
                            TicketHighLow[i][2] = Bid;
                        }


Which is why I used an arbitary high value of 1000


If I don't declare the array as a global variable, it  resets the highs and lows on every tick for every open order.


This is the code how it stands - I


int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
     if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
     if (OrderSymbol() != Symbol() || OrderMagicNumber() != ~OrderId~)
     {
         continue;
     }
    ordticket[orders][0] = OrderOpenTime();
    ordticket[orders][1] = OrderTicket();
    TicketHighLow[orders][0] = OrderOpenTime();
    TicketHighLow[orders][1] = OrderTicket();


    orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
ArrayResize(TicketHighLow,orders);
ArraySort(TicketHighLow);
}

for (i = 0; i < orders; i++)
{
    if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == ~OrderId~)
        {

            if (TicketHighLow[i][1] == OrderTicket())

                {

                    if (Ask > TicketHighLow[i][3])
                        {
                            TicketHighLow[i][3] = Ask;
                        }

                    if (Bid < TicketHighLow[i][2])
                        {
                            TicketHighLow[i][2] = Bid;
                        }


                    if ((OrderType() == OP_BUY && TicketHighLow[i][3] - OrderOpenPrice() > ~TrailingStartGap~*PipValue*Point) ||
                        (OrderType() == OP_SELL && OrderOpenPrice() - TicketHighLow[i][2] > ~TrailingStartGap~*PipValue*Point))

                    {

                        if ((OrderType() == OP_BUY && TicketHighLow[i][3] - Ask > ~TrailingStop~*PipValue*Point) ||
                            (OrderType() == OP_SELL && Bid - TicketHighLow[i][2] > ~TrailingStop~*PipValue*Point))
                        {
                           bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), ~Slippage~, ~Color~);

                           if (ret == true)
                           {
                                int error = GetLastError();
                                if (ret == false && error > 0)
                                Print("OrderClose() error - ", ErrorDescription(error));
                           }
                        }
                    }
                }
        }



    }
}


Would using struct solve this problem?

 
  1. Quazon: Is there an distict advantage of using struct over a two dimensional array?

    Different data types possible. No need to cast to real datatype on retrieval, everywhere.

  2. Quazon: 'for' - expressions are not allowed on a global scope    trailing stop.mq4    73    1

    If you don't understand that initialization code goes in functions (like OnInit), step away from the code and hire some.

  3. Quazon: If I don't declare the array as a global variable, it  resets the highs and lows on every tick for every open order.


    Would using struct solve this problem?

    1. Of course, it does. Only globally declared and static variables retain values between calls.

      Perhaps you should read the manual.
         How To Ask Questions The Smart Way. 2004
            How To Interpret Answers.
               RTFM and STFW: How To Tell You've Seriously Screwed Up.

    2. No. The datatype of a variable has nothing to do with the retention of that variable.
Reason: