Variables keep resetting to initial declared values - page 3

 
William Roeder:
  1. Different data types possible. No need to cast to real datatype on retrieval, everywhere.

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

    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.


Thanks, I probably needed those links, am new to coding and its etiquette!

I declared the initialization OnInit and the code worked as expected utilising the Array.

 

I'm running into an issue I anticipated but couldn't replicate until last night in Strategy Tester.


I declared a global array to capture the opentime, ticket, and highest high and lowest low for all open trades. (A massive thanks to everyone for helping me get this far).


double TicketHighLow[90][4];


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


However when multiple orders are opened, the way that I am stamping the tickets and resizing the array (I believe) causes a mismatch between the ticket and the HH and LL for that particular trade. In other words, the ticket takes the stamped HH and LL of another or previous trade -  the result is a trade opened and closed instantly.


I think I have pinned it to this - I understand that this is not right, because I am esentially changing the index for ticket to its corresponding HH and LL. But I am inserting this code because I need to get all open trades on every tick, and ignore all the closed trades from the array.

    for (int i = 0; i < orderstotal; i++)
    {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
        if (OrderSymbol() != Symbol() || OrderMagicNumber() != 1)
        {
            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);
    }


I was wondering if anyone had any ideas to get around this - kind of getting stuck!


This is the full code, highligting where I am stamping the highest high and lowest low.


 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() != 1)
        {
            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() == 1)
            {
                
                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() > TrailingStartGap13*PipValue*Point) ||
                    (OrderType() == OP_SELL && OrderOpenPrice() - TicketHighLow[i][2] > TrailingStartGap13*PipValue*Point))
                    
                    {
                        
                        if ((OrderType() == OP_BUY && TicketHighLow[i][3] - Ask > TrailingStop13*PipValue*Point) ||
                        (OrderType() == OP_SELL && Bid - TicketHighLow[i][2] > TrailingStop13*PipValue*Point))
                        {
                            bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Blue);
                            
                            if (ret == true)
                            {
                                int error = GetLastError();
                                if (ret == false && error > 0)
                                Print("OrderClose() error - ", ErrorDescription(error));
                            }
                        }
                    }
                }
            }
            
            
            
        }
    }
Reason: