[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 47

 
Tiken123:

If you know the function for calculating the total balance of loss of CLOSED positions by SL and the total balance of OPEN profitable trades, please write.

Loss balance calculation should start from the last maximum deposit value

Your question is somewhat similar to the previous one, so I tweaked the function from the previous answer a bit. I confess I have not tested it, but it should work. The variables profit and loss declared on global level should show the total profit and loss respectively for the closed positions.

If you need to do this for open orders as well, add a second for loop for selecting them not in history, but according to the OrdersTotal() criterion: for(int n=1; n<=OrdersTotal(); n++)

And add the values obtained to the same variables profit and loss .

//+----------------------------------------------------------------------------+
// Прибыльно или убыточно закрылся последний ордер, и возврат типа такого ордера
int fHistory(){
  profit = 0; loss = 0;
  for(int i=OrdersHistoryTotal(); i >= 0; i--){               // Выборка в истории
     if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true){    // Если есть следующий в истории
        if(OrderMagicNumber()!=magN) continue;                // Ордера не нашего эксперта

        if(OrderProfit() > 0 ) profit = profit+OrderProfit(); // Если прибыль по посл.ордеру - суммируем

        if(OrderProfit() < 0 ) loss = loss + OrderProfit();   // Если убыток по посл.ордеру - суммируем

     }
  }
  return(-1);
}
 
Twilight:

For example, I need to know if a pending buy limit has been triggered and if it has, at what price, and then set a new one.

If there is an order of OP_BUYLIMIT type on every tick using the OrderType() function, when this type disappears, it means that the order has opened and become OP_BUY. All 6 types are listedhere. Using the OrderOpenPrice() function, you can find out at what price the order was opened.
 
Twilight:

How do I know if an order has triggered a trailing stop and place new orders when the trailing stop has been triggered?

Did you mean to close the order or to modify it?
If closed, do you need to know if it just closed? Or did it close with a StopLoss? (It may close at TakeProfit and OrderClose() or OrderCloseBy())
 

Here is my new bar control function:

bool IsNewBar()
{
  if(bartime != iTime(Symbol(),i_trading_TF,0))
  {
    bartime = iTime(Symbol(),i_trading_TF,0);
    return(true);
  }
  
  return(false);
}

where, i_trading_TF is a variable declared globally external. Thus, when I set my Expert Advisor on a chart, I can set the value of this variable in the pop-up window and thus set the timeframe of the bar opening at which I want to control.

I have seen a similar function in one EA but with a formal parameter in the function header. Here it is:

bool IsNewBar(int i_trading_TF)
{
  if(bartime != iTime(Symbol(),i_trading_TF,0))
  {
    bartime = iTime(Symbol(),i_trading_TF,0);
    return(true);
  }
  
  return(false);
}

The time frame is passed through the function parameter. However, ifi_trading_TF variableis declared on the global level, I don't think it makes sense to pass it via a formal parameter since its visibility is not limited.

Please comment on this point.

The first option works for me too, but it's strange that some programmers have a habit of stuffing with formal parameters even those functions where I would not do it myself.

 
pu6ka:
On every tick, monitor the presence of an order with OP_BUYLIMIT type using OrderType() function, when this type disappears, it means that the order has opened and become OP_BUY type. All 6 types are listedhere. Using the OrderOpenPrice() function, you can find out at what price the order was opened.
It does not have to be. It can be manually killed. Rather, it is necessary to memorize the ticket of the order; as soon as we find the absence of the order with this ticket and the appearance of a market position with the same ticket - then yes, it is triggered...
 
hoz:

Here is my new bar control function:

where, i_trading_TF is a variable declared globally external. Thus, when I set my Expert Advisor on a chart, I can set the value of this variable in the pop-up window and thus set the timeframe of the bar opening at which I want to control.

I have seen a similar function in one Expert Advisor but with a formal parameter in the function header. It goes like this:

The time frame is passed through the function parameter. However, ifi_trading_TF variableis declared on the global level, I don't think it makes sense to pass it via a formal parameter since its visibility is not limited.

Please comment on this point.

The first option works for me too, but it's strange that some programmers have a habit of stuffing with formal parameters even those functions where I would not do it myself.

It makes sense if the EA analyzes several frames and a new bar must be formed at every frame.

That's not all, let's add the name of the instrument to the transferred parameters

bool IsNewBar(string Symb,int i_trading_TF)
{
  if(bartime != iTime(Symb,i_trading_TF,0))
  {
    bartime = iTime(Symb,i_trading_TF,0);
    return(true);
  }
  
  return(false);
}
and analyse the fact of a new bar forming, any instrument, period.
 
artmedia70:
Not necessarily. It can also be manually killed. Rather, you need to remember the ticket of the pending order; as soon as a pending order with that ticket is found missing and a market position with the same ticket appears, then yes, it's triggered...

Yes, that's more correct. But we can check the idea in the tester like that. To trace four types of pending orders, we need 4 additional variables to trace the ticks. Although if there are more than one pending for one type, then you should definitely enter a variable for tickets.
 
r772ra:

It makes sense if the EA analyses several frames and each frame needs the fact of forming a new bar.

That's not all, let's add the name of the instrument to the parameters

and analyse the fact of a new bar forming, any symbol, period.

Do you think it is reasonable to "involve" a symbol in the "mechanism of search of a new bar? :)))
 
TarasBY:
Do you think it is appropriate to "involve" a symbol in the "mechanism for finding a new bar"??? :)))

It would be interesting to make a universal control of the new bar. By the way - everything is there for that
 
Vinin:

It would be interesting to do universal control of the new bar. By the way - everything is there for that
Are you talking about "consider" and DC?! ;)