Issue with indicator code

 

hello all

getting crazy with a function not working.

I have a report showing open PnL and closed positions.

I'd like to show a column with maxDD registered and save it into a file for checking when reload the indicator.

so when OpenPNL < last recorderd DDMAX, then "update DDMAX".

the code below unfortunately doesn't freeze the DDMAX that changes also when OPENPNL > DDMAX.

can someone suggest a fix?

//+------------------------------------------------------------------+
//| Add or update Magic Number data                                  |
//+------------------------------------------------------------------+
void AddToMagic(string magicNumber, double openPnL, double todayPnL, double weekPnL, double monthPnL, double allTimePnL,
                string description, datetime activity, string &magicNumbers[], string &descriptions[],
                double &openPnLMap[], double &todayPnLMap[], double &weekPnLMap[], double &monthPnLMap[], double &allTimePnLMap[],
                datetime &lastActivity[], double &maxDDMap[], double &maxLossMap[], int &size)
{
    for (int i = 0; i < size; i++)
    {
        if (magicNumbers[i] == magicNumber)
        {
            openPnLMap[i] += openPnL;
            todayPnLMap[i] += todayPnL;
            weekPnLMap[i] += weekPnL;
            monthPnLMap[i] += monthPnL;
            allTimePnLMap[i] += allTimePnL;
            lastActivity[i] = MathMax(lastActivity[i], activity);
            descriptions[i] = StringSubstr(description, 0, Comment_Digits);  // Update and limit description

// Update Max DD only if a new maximum drawdown is reached
if (openPnLMap[i] < maxDDMap[i])
{
    maxDDMap[i] = openPnLMap[i];
    // Update the date when maxDD was recorded
    lastActivity[i] = TimeCurrent();
    // Save updated values to the file
    SaveMaxDDValues(filename, magicNumbers, maxDDMap, lastActivity, size);
}
            maxLossMap[i] = MathMin(maxLossMap[i], todayPnL + weekPnL + monthPnL + allTimePnL);
            return;
        }
    }

    ArrayResize(magicNumbers, size + 1);
    ArrayResize(descriptions, size + 1);
    ArrayResize(openPnLMap, size + 1);
    ArrayResize(todayPnLMap, size + 1);
    ArrayResize(weekPnLMap, size + 1);
    ArrayResize(monthPnLMap, size + 1);
    ArrayResize(allTimePnLMap, size + 1);
    ArrayResize(lastActivity, size + 1);
    ArrayResize(maxDDMap, size + 1);
    ArrayResize(maxLossMap, size + 1);

    magicNumbers[size] = magicNumber;
    descriptions[size] = StringSubstr(description, 0, Comment_Digits);  // Limit description to specified characters
    openPnLMap[size] = openPnL;
    todayPnLMap[size] = todayPnL;
    weekPnLMap[size] = weekPnL;
    monthPnLMap[size] = monthPnL;
    allTimePnLMap[size] = allTimePnL;
    lastActivity[size] = activity;
    maxDDMap[size] = (openPnL < 0) ? openPnL : 0;  // Initialize Max DD only if openPnL is negative
    maxLossMap[size] = todayPnL + weekPnL + monthPnL + allTimePnL;  // Initialize maxLoss
    size++;

    // Save initial values to the file
    SaveMaxDDValues(filename, magicNumbers, maxDDMap, lastActivity, size);
}

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893