Indicator stops updating

 

hello i have a indicator that does display the current risk with some math, it technically works just fine 

however after some time (mosly when i close positions) it just stops updating, and i have to reload it via template or change the symbol to make it work again, is there somethings i can do to force the update after positions are closed?


also for some reason the function below does return the wrong value when trading a index 

double CalculateSumMaxLosses()
{
    double sumMaxLosses = 0;
    double accountBalance = AccountBalance();

    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            double orderType = OrderType();
            double orderLots = OrderLots();
            double orderOpenPrice = OrderOpenPrice();
            double orderStopLoss = OrderStopLoss();
            double pointValue = MarketInfo(OrderSymbol(), MODE_POINT);

            double orderMaxLoss = 0;
            if (orderStopLoss != 0)
            {
                if (orderType == OP_BUY)
                    orderMaxLoss = orderLots * (orderOpenPrice - orderStopLoss) / pointValue;
                else if (orderType == OP_SELL)
                    orderMaxLoss = orderLots * (orderStopLoss - orderOpenPrice) / pointValue;
            }
            else
            {
                sumMaxLosses = accountBalance;
                break;
            }

            sumMaxLosses += orderMaxLoss;
        }
    }

    return sumMaxLosses;
}


 
You should post this in MT4 section.
 
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.     for (int i = 0; i < OrdersTotal(); i++)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy.
         Trade current timeframe, one strategy, and filter by symbol requires one MN.
         If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Please don't create topics randomly in any section. It has been moved to the section: MQL4 e MetaTrader 4
 
William Roeder #:
  1. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)

thanks for the reply, (yes a mod moved the topic already)

about the magic number, this isn't supposed to track anything in particular, but the account as a whole.

 
Mario Hennenberger #: about the magic number, this isn't supposed to track anything in particular, but the account as a whole.

All orders are visible in the connected account. What is the purpose of the Magic Number if isn't to filter to something particular? Think.

Reason: