Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1952

 
законопослушный гражданин #:

Good afternoon everyone.

I need your help.

The EA has a built-in counting function for the current profit:

void OnTick is made as above.

How to make profit counter reset to 0.0 when conditions are met:

if(CountOrders()==0) && (GetProfitFromStart()>0

I.e., all orders are closed and the total profit when the last order is closed was >0?

This task is not quite clear. Each time the calculation function starts to go through the orders/positions and summarizes the profits from zero.

 double lp=0,cp=0;

What do you want to zero out? If you have no open positions/market orders, the counting function will return zero. It will zero itself)))

 
законопослушный гражданин #:

Good afternoon everyone.

I need your help.

How to make the profit counter reset to 0.0 when conditions are met:

i.e. all orders closed and the total profit when the last order closed was >0???

What exactly of this does not work?

 

Good afternoon. Please help.

Looking for closed orders with negative returns.
By totals it misses, I do not get the result of all negative orders.

I am learning to write.

double Minus_profit(){
 for (int i=OrdersHistoryTotal()-1; i>=0; i--){
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)){
         if (OrderSymbol() == Symbol() && OrderMagicNumber()==Magic && (OrderType() == OP_BUY || OP_SELL)){
          if (OrderCloseTime()>=time && OrderTicket()>ticket){
            if (OrderProfit()+OrderSwap()+OrderCommission()<0){
             time=OrderCloseTime();
             ticket=OrderTicket();
             profit+=OrderProfit();swap+=OrderSwap(); ;comis+=OrderCommission();
             result=profit+swap+comis;   
   }}}}}return(result);
}
 
Alexander Avksentyev #:

Good afternoon. Please help.

Looking for closed orders with negative returns.
By totals it misses, I do not get the result of all negative orders.

I am trying to learn how to write.

At a quick glance, I see that you are looking through the positions from the end and at the same time, you write the closing time in the variable. It may turn out that you have captured the time of the most recently closed position and the next condition will no longer be true:

OrderCloseTime()>=time

as other positions have lower closing times. This will all depend on the sorting, but in standard sorting this is the problem.


Also fix this condition

(OrderType() == OP_BUY || OP_SELL)

to

(OrderType() == OP_BUY || OrderType() == OP_SELL)

If we take a position from the history, OrderProfit() will already store the final result with swaps and commission. In open positions - no, everything must be summed up. But this is not certain, check it.

 
Nikita Chernyshov #:

At a quick glance, I see that you are looking through the positions from the end and you are writing the closing time into the variable. It may turn out that you have captured the time of the most recently closed position and the next condition will no longer be true:

as other positions have lower closing times. This will all depend on the sorting, but in standard sorting this is the problem.


Also fix this condition

to

If we take a position from the history, OrderProfit() will already store the final result with swaps and commission. In open positions - no, everything must be summed up. But this is not accurate, please check.

Thank you. This works fine.

double Minus_profit(){
time=TimeCurrent();
 for (int i=0; i<OrdersHistoryTotal(); i++){
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)){
       if (OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){
        if (OrderType() == OP_BUY || OrderType() ==OP_SELL){
         if (OrderCloseTime()>=time){
          if (OrderProfit()+OrderSwap()+OrderCommission()<=0){
              time=OrderCloseTime();
              profit+=OrderProfit();swap+=OrderSwap(); ;comis+=OrderCommission();
              result=profit+swap+comis;   
   }}}}}}return(result);
}

Further; when we come across an order with a positive profit, we should reset the result and look for negative ones.

 
Alexander Avksentyev #:

Good afternoon. Please help.

Looking for closed orders with negative returns.
By totals it misses, I do not get the result of all negative orders.

I am learning to write.

I recommend using a debugger for this and similar situations.

 

Good afternoon.

Faced with a lack of understanding of how ".Search()" in the standard library.

I am using standard class - CiTime() - The class is a class to access bar opening time series.

int OnInit()
  {
//..
 TimeFrac=new CiTime();
   if(CheckPointer(TimeFrac)==POINTER_INVALID || !TimeFrac.Create(symbol_Name,TimeFr_Frac_D1))
      return INIT_FAILED;
   TimeFrac.Refresh();  
   TimeFrac.Sort();
//..

When trying to find the Date I need (class is the same but Period is different)

 int k = TimeFrac.Search(  Time.GetData(i)    );

Makes an error:

'GetData' - parameter conversion not allowed.

Tried to replace

Time.GetData(i)

to

datetime Time_GetData  =  Time.GetData(i);

Doesn't work...

I've referred to the Help. It says there

int  Search( 
   CObject*  element      // образец 
   ) const

And a wild application example.

It turns out that searching for a "Date" in a standard class by a known "Date" from the same class is not possible!?!?

Only a Class Element.

CObject

I could not find a working example of how to search for a Date in this class.

Who do I contact for help???

Thank you.

 
Hi all, please tell me what's wrong, it's not working for some reason
double Minus_profit(){
time=TimeCurrent();
 for (int i=0; i<OrdersHistoryTotal(); i++){
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)){
       if (OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){
        if (OrderType() == OP_BUY || OrderType() ==OP_SELL){
         if (OrderCloseTime()>=time){
          if (OrderProfit()+OrderSwap()+OrderCommission()<=0){
              time=OrderCloseTime();
              profit+=OrderProfit();swap+=OrderSwap(); ;comis+=OrderCommission();
              result=profit+swap+comis;   
   }}}}}}return(result);
}
 
Alexander Avksentyev #:
Hi all, please tell me what's wrong, it's not working for some reason
double Minus_profit(){
time=TimeCurrent();   // это текущее время
 for (int i=0; i<OrdersHistoryTotal(); i++){
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)){
       if (OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){
        if (OrderType() == OP_BUY || OrderType() ==OP_SELL){
         if (OrderCloseTime()>=time){                  // условие не выполнимо, что бы время закрытия ордера в истории
          if (OrderProfit()+OrderSwap()+OrderCommission()<=0){     // было больше/позже чем текущее
              time=OrderCloseTime();
              profit+=OrderProfit();swap+=OrderSwap(); ;comis+=OrderCommission();
              result=profit+swap+comis;   
   }}}}}}return(result);
}
 

Question. For 4k.

doubleMarketInfo(

)

MODE_LOTSIZE

15

Contract size in the base currency of the instrument

do I get it right, this is the value of 1 (one) lot? and divided by the leverage, the value of the lot including the leverage in the first currency of the instrument

MODE_MARGINREQUIRED

32

Amount of free funds needed to open 1 lot to buy

is there an analogue to the last one in 5, if there is, I haven't found it.

MarginFree funds are clear. If we divide them by the value of one lot, we obtain how many lots we can open without leverage, and multiplied by the leverage, how many with leverage. Is it correct or not?

Zy. Is this correct for a 5

double   Free   =AccountInfoDouble(ACCOUNT_MARGIN_FREE);                 // Свободн средства
double   One_Lot=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_CONTRACT_SIZE);// Стоимость 1 лота без плеча
double   Step   =SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);       // Шаг изменен размера
long     Laverage=AccountInfoInteger(ACCOUNT_LEVERAGE); // Плечо
double   One_Lot_Lav=ND((One_Lot/Laverage)*Ask);      // Стоимость лота с плечом для бай
Lts=MathFloor(Free*Prots/One_Lot_Lav/Step)*Step;// Для открытия