Variables keep resetting to initial declared values

Quazon
107

wondering whether someone may be able to point out what I'm doing wrong here.


Just for background I am very new to MQL4 (or coding at all)

I wrote the following code to act as a virtual trailing stop loss.

I am trying to lock in HH (Highest High) and LL (Lowest Low) based on the below loop. However when I print, it looks like both HH and LL are resetting to both Bid and Ask on every tick, and not "locking".

I had three questions:

  1. What am I doing wrong?!
  2. Does HH and LL just reset to zero continuously because of where the variables have been declared?
  3. Will HH and LL be unique for every order? If not, is there a way of setting that?

(The ~ are just placeholders for now - can be ignored, I will correct that)

Thanks a lot!


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();
    orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}

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

            if (Ask > HH)
                {
                    HH = Ask;
                }

            if (Bid < LL)
                {
                    LL = Bid;
                }


            if ((OrderType() == OP_BUY && HH - OrderOpenPrice() > ~TrailingStartGap~*PipValue*Point) ||
                (OrderType() == OP_SELL && OrderOpenPrice() - LL > ~TrailingStartGap~*PipValue*Point))

            {

                if ((OrderType() == OP_BUY && HH - Ask > ~TrailingStop~*PipValue*Point) ||
                    (OrderType() == OP_SELL && Bid - LL > ~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));
                   }
                }
            }

        }



    }
}

Print("HH=",HH);
Print("LL)=",LL);





The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
Keith Watford
Moderator
32195
Quazon:

I am trying to lock in HH (Highest High) and LL (Lowest Low) based on the below loop. However when I print, it looks like both HH and LL are resetting to both Bid and Ask on every tick, and not "locking".

Of course it resets every tick. You do it in your code.

            double      HH = 0;
            double      LL = 1000;

            if (Ask > HH)
                {
                    HH = Ask;
                }

            if (Bid < LL)
                {
                    LL = Bid;
                }

Maybe you should use a static variable and not reset it every tick.

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

Quazon
107
Quazon  

Hi Keith,


Thanks a lot for the reply.


The issue with a static variable from my reading of the docs is that the HH and LL will lock permanently, and this will cause a problem for subsequent / simulataneous orders - is that right? I wanted a way that HH and LL will reset after the trade has been closed (anywhere).


Also was looking at the forum to see if it was possible to hire a private teacher for MQL4 to get answers for questions I am spending too long to figure out myself when I get stuck? Any recommendations?

Keith Watford
Moderator
32195

You simply reset the static variable when you want it reset.

Hiring a teacher could be expensive and any recommendations are not allowed in the forum.

Probably, every question that you may have has already been answered in the forum. You can learn a lot just by reading.

Quazon
107
Quazon  

Is it possible to store the HH and LL of every ticket in an Array, and have the array reset for only open orders?

Keith Watford
Moderator
32195
Quazon:

Is it possible to store the HH and LL of every ticket in an Array, and have the array reset for only open orders?

Is there any reason that you don't want to use a normal trailing stop and reset the actual SL for the order?

Quazon
107
Quazon  
Keith Watford:

Is there any reason that you don't want to use a normal trailing stop and reset the actual SL for the order?


I dont want to send SL or TP to the broker, I would rather kep them invisible if possible

Keith Watford
Moderator
32195
Quazon:


I dont want to send SL or TP to the broker, I would rather kep them invisible if possible

Well you already know how to do a 2 dimensional array.

Declare an array globally to store the ticket # and calculated SL.
Add to the array whenever you open a new trade.

Of course you will need to remove the trade from the array when the trade is closed.

Quazon
107
Quazon  

I took this snippet of code from another EA -


I just wanted to clarify something that didnt make sense to me here


This is a declaration of a 2 dimensional array with 90 rows and 3 columns

int ordticket[90][2]


But here


    ordticket[orders][0] = OrderOpenTime();
    ordticket[orders][1] = OrderTicket();


The first column seems to be orders (since it appears first in the square brackets), AND OrderOpenTime() since OrederOpenTime() has an index of 0?

Paul Anscombe
6431
Quazon:

I took this snippet of code from another EA -


I just wanted to clarify something that didnt make sense to me here


This is a declaration of a 2 dimensional array with 90 rows and 3 columns


But here



The first column seems to be orders (since it appears first in the square brackets), AND OrderOpenTime() since OrederOpenTime() has an index of 0?

It is an array with the first dimension of 90 elements 0-89   and a second dimension of 2 elements 0-1  

Keith Watford
Moderator
32195
Quazon:

I took this snippet of code from another EA -


I just wanted to clarify something that didnt make sense to me here


This is a declaration of a 2 dimensional array with 90 rows and 3 columns

int ordticket[90][2]

But here

    ordticket[orders][0] = OrderOpenTime();
    ordticket[orders][1] = OrderTicket();

The first column seems to be orders (since it appears first in the square brackets), AND OrderOpenTime() since OrederOpenTime() has an index of 0?

[orders] is the index. orders is an int variable, it could just as easily be called index. But as this relates to orders in a list, it is called orders.

The array ordticket is declared with 90 indexes (0 to 89) and each index has 2 elements (0 and 1).

AS the array is an integer array it can also store datetime as it can be represented as an integer.

I don't know why you store the OrderOpenTime as you don't seem to use it in your code.

You probably want to declare a double array as you can store both the ticket number (as a double) and the stop loss. As you calculate any new value for the stop loss you can update the array.