Trading history Arrow to horizontal lines

linux_usr

Trader
Oct 20, 2023
78
3
19
39
Trying to develop an indicator that will produce a horizontal line on trade entry and trade exits. where the trade history arrows show basically want horizontal lines instead or and horizontal lines. my codes compiles but does not draw the lines. any idea where to go from here. i can show the code
MQL5:
// Input parameters
input int HistoryDepth = 50; // Number of past trades to display lines for
 
//+------------------------------------------------------------------+
//| Custom Indicator Initialization function                        |
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialization code if necessary
    return(INIT_SUCCEEDED);
}
 
//+------------------------------------------------------------------+
//| Custom Indicator Deinitialization function                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Clean up any drawn objects when the indicator is removed
    for(int i = 0; i < HistoryDepth; i++)
    {
        string obj_name = "TradeLine_" + IntegerToString(i);
        if(ObjectFind(0, obj_name) >= 0)
        {
            ObjectDelete(0, obj_name); // Correct ObjectDelete usage for MT5
        }
    }
}
 
//+------------------------------------------------------------------+
//| Custom Indicator Calculation function                           |
//+------------------------------------------------------------------+
void OnTick()
{
    // Check if there are any historical orders
    int totalTrades = HistoryOrdersTotal();
    Print("Total trades in history: ", totalTrades); // Debugging line
 
    if (totalTrades == 0)
    {
        Print("No trade history found.");
        return;
    }
 
    // Loop through recent closed trades
    int startTrade = totalTrades - HistoryDepth;
    if (startTrade < 0) startTrade = 0;
 
    for(int i = startTrade; i < totalTrades; i++)
    {
        ulong ticket = HistoryOrderGetTicket(i);
 
        // Select the order by ticket
        if(HistoryOrderSelect(ticket))
        {
            // Get trade entry price (ORDER_PRICE_OPEN for open price)
            double entryPrice = HistoryOrderGetDouble(ticket, ORDER_PRICE_OPEN);
 
            // Get the time of the most recent bar (you can adjust the bar index if needed)
            datetime barTime = iTime(Symbol(), PERIOD_D1, 0);  // Get the time of the latest daily bar
 
            // Draw a horizontal line at the entry price
            string lineName = "TradeLine_" + IntegerToString(i);
            if(ObjectFind(0, lineName) < 0)
            {
                // Draw the horizontal line at the entry price
                ObjectCreate(0, lineName, OBJ_HLINE, 0, barTime, entryPrice);
                ObjectSetInteger(0, lineName, OBJPROP_COLOR, clrBlue); // Line color (can be customized)
                ObjectSetInteger(0, lineName, OBJPROP_RAY_RIGHT, 0); // Don't extend the line to the right
            }
        }
        else
        {
            Print("Error selecting history order ", i, " - ", GetLastError()); // Debugging line
        }
    }
}
Post automatically merged:

It compiles ok but i get no history data found. even if i open trades and close them . still no lines drawn
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,495
1,596
144
Odesa
www.earnforex.com
First, that's not an indicator, that's an EA. And it looks like it's better be converted to a script, which you run once to add those lines. Why would you need it to run each tick?

Second, please read the MQL5 help file for HistoryOrdersTotal() function. It explicitly says that before calling that function, you have to call either HistorySelect() or HistorySelectByPosition() to select the history period.
 

linux_usr

Trader
Oct 20, 2023
78
3
19
39
Finally got it. you can see how the open it now prints horizontal lines on the history arrows. next step it to now make this indicator an EA