A solution/suggestion for the current Virtual Trailing Stop Code

  algorithmic-trading, c++, forex, metatrader4, mql4

Was wondering if someone could figure out a solution for the below virtual trailing stop. Just to caveat I am also very new to coding!

The idea here is when a trade is opened, the Ask and Bid are used to stamp the highest high (TicketHighLow[i][3]) and lowest low made (TicketHighLow[i][2]) after OrderOpenTime. Once the TrailingStartGap is reached, and the price retraces to the TrailingStop, the order is closed.

The requirements are that the Virtual Trailing Stop should have the ability to trail each multiple orders open at the same time based on the OrderTicket, but also filter based on the OrderMagicNumber.

The problem with the below code is that when multiple orders are opened, the way that I am stamping the tickets and resizing the array (I believe) causes a mismatch between the OrderTicket and the HighestHigh and LowestLow in the Array. In other words;

TicketHighLow[i][1],TicketHighLow[i][2],TicketHighLow[i][3]

becomes something like

TicketHighLow[h][1],TicketHighLow[i][2],TicketHighLow[i][3]

The result is a trade opened and closed instantly.

Any ideas on how to get around this would be massively appreciated!

double TicketHighLow[90][4]; // declare a two dimensional global array


int init()
{
    for(int i=ArrayRange(TicketHighLow,0)-1; i >= 0; --i){ TicketHighLow[i][2]=1000; TicketHighLow[i][3]=0; } // initialize array by stamping a high value for LowestLow and low value for HighestHigh

}

// Expert start
int start()
{ 
    OnEveryTick();
    return (0);
}

void OnEveryTick()
{
    VirtualTrailingStop();    
}
        

void VirtualTrailingStop()
{
        
        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(); // I think this is where the problem lays!!!
            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; // stamping new HighestHigh
                        }
                        
                        if (Bid < TicketHighLow[i][2])
                        {
                            TicketHighLow[i][2] = Bid; // stamping new LowestLow
                        }
                        
                        
                        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(), 4, Blue);
                                
                                if (ret == true)
                                {
                                    int error = GetLastError();
                                    if (ret == false && error > 0)
                                    Print("OrderClose() error - ", ErrorDescription(error));
                                }
                            }
                        }
                    }
                }
                
                
                
            }
        }
        

Source: Windows Questions C++

LEAVE A COMMENT