Returns zero for some unknown reason

 

The following code returns "0" for the BuyProfit variable although there are several Buy trades open and running.

can you perhaps tell me why?

Count = 0;

         for(Count = OrdersTotal()-1; Count >= 0; Count--)
           {
             if(OrderSelect(Count, SELECT_BY_POS,MODE_TRADES)
             && OrderMagicNumber() == MagicNumber
             && OrderSymbol() == Symbol())
            {
            if(OrderType() == OP_BUY)
            BuyProfit += OrderProfit();
         
            if(BuyProfit >= ProfitDollarValue || BuyProfit <= LossDollarValue)
             
            OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), UseSlippage, Blue);
            
            }
           }
 
Ernest Klokow:

The following code returns "0" for the BuyProfit variable although there are several Buy trades open and running.

can you perhaps tell me why?

There are buy orders, but are they in profit or loss from your values ?

  if(BuyProfit >= ProfitDollarValue || BuyProfit <= LossDollarValue)
 
Some are in profit and some are in loss. Why does it matter?
 
Ernest Klokow #:
Some are in profit and some are in loss. Why does it matter?

because of your codes

 
Ernest Klokow:

The following code returns "0" for the BuyProfit variable although there are several Buy trades open and running.

can you perhaps tell me why?

When you say " there are several Buy trades open and running" are you referring to positions?

Your loop will only execute if orders are in the system 

 
R4tna C #: When you say " there are several Buy trades open and running" are you referring to positions? Your loop will only execute if orders are in the system 

This is the MQL4 section. There are no positions and deals, only orders (Market or Pending). The OP is checking for OP_BUY which is a Market Order. Pending Orders will not have any profit or loss associated with it.

 
Ernest Klokow: The following code returns "0" for the BuyProfit variable although there are several Buy trades open and running. can you perhaps tell me why?

Your code does not show us what the initial value for "BuyProfit", "ProfitDollarValue" nor "LossDollarValue". It is also unclear if "LossDollarValue" is a positive or negative value.

You are also not checking the return code for the "OrderClose()" function.

It is also unclear what you mean by "returns zero" as your code does not show any value being returned or printed to the log.

 

You are summing profits and trying to close orders at the same time.

  1. Sum your profit.

    Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

  2. Then if(condition) close all your orders.

    MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

  3.          for(Count = OrdersTotal()-1; Count >= 0; Count--)
               {
                 if(OrderSelect(Count, SELECT_BY_POS,MODE_TRADES)
                 && OrderMagicNumber() == MagicNumber
                 && OrderSymbol() == Symbol())
                {
                if(OrderType() == OP_BUY)
    {
                BuyProfit += OrderProfit();
                if(BuyProfit >= ProfitDollarValue || BuyProfit <= LossDollarValue)
      {
                 
                OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), UseSlippage, Blue);
      }
    }
    Group your code properly with brackets. You sum buyProfit for buys, But your if(BuyProfit…) is for any order type.
 
Fernando Carreiro #:

This is the MQL4 section. There are no positions and deals, only orders (Market or Pending). The OP is checking for OP_BUY which is a Market Order. Pending Orders will not have any profit or loss associated with it.

Apologies - I missed that

 

Thanks everybody that has offeered some guidance. It is greatly appreciated.

I have made some changes to the code (see below) but still get a "0" result. I checked the open trades and it should give me a negative result since the trades are in a total loss.

Forgive me if I missed or misunderstood some of the tips you gave me.

 Count = 0;

         for(Count = OrdersTotal()-1; Count >= 0; Count--)
           {
             if(OrderSelect(Count, SELECT_BY_POS,MODE_TRADES)
             && OrderMagicNumber() == MagicNumber
             && OrderSymbol() == Symbol())
               {
               if(OrderType() == OP_BUY)
               BuyProfit += OrderProfit() +  OrderSwap() + OrderCommission();
               }
            if(BuyProfit >= ProfitDollarValue || BuyProfit <= -LossDollarValue)
             
            Result = OrderClose(OrderTicket(), OrderLots(), Bid, UseSlippage, Blue);
            if(Result = -1)
                {
                  ErrorCode = GetLastError();
                  ErrDesc = ErrorDescription(ErrorCode);

                  ErrAlert = StringConcatenate("Close Buy Orders - Error ",ErrorCode,": ",ErrDesc);
                  Alert(ErrAlert);
                               
               }
 
Anyone?