Profit calculation of closed orders "HELP" - page 9

 
Natashe4ka:

I also thought it was counting correctly.
I should not have put this info in the code and my life would have been easier))

And now I'm wondering how the function didn't work correctly all this time!!!

I see, I have to remove the check for profit = positive, because of that the account stops later, I just had to take into account my previous loss as well.

double lastloss()
{
double lastlos=0.0,lastlot=0.0,op=0.0;
int cnt=0;
datetime time=0;
for(int i=OrdersHistoryTotal()-1; i>=0; i--)
{
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==Magic && OrderType()<2)
         {
            if(cnt==0 && OrderCloseTime()!=0) time=OrderCloseTime();
            if(OrderCloseTime()+PeriodSeconds()<time/* && op>0.0*/) break;
            op=OrderProfit()+OrderSwap()+OrderCommission();
            lastlos+=op;
            cnt++;
         }
      }
}
return(lastlos);
}
 
Natashe4ka:

It seemed to me too, that everything counts as it should.
I shouldn't have put this info in the code and life would have been easier))

And now it's baffling how the function didn't work correctly all this time!!!

You are using the function from the first pages of your beginner's book to close. In the tester it will close almost normally, but in real trade the bot will often close in the red, especially if it takes a small profit.

Have you ever tried trading on the real market or only in the tester?

Have you ever seen such a situation, when a series of positions are closed? Note the difference in pips from the signal to close, and to close the last position

So as not to spam, I will attach only 3 screenshots, although in reality there are more than a hundred of them available:


All functions work correctly for everyone, but only you do not work correctly. Have you ever wondered why?

And finally, the question you need to ask yourself, we don't need an answer: are you going to restart the terminal at any time, or close it for the weekend, ...? If so, how will you get the profit at the next launch of the terminal to continue the work of the Expert Advisor that was done at the last closing of the series, if you don't want to save it anywhere?

 

There's more:

Why did I ask about what removing an order between closing positions does?


Now we have a situation: An EA starts to close positions when it reaches profit and then it gets an order which is of no use at all, i.e. it makes neither profit nor loss but an order to remove this order is sent and while it is being removed, the price leaves and the EA continues to close further. The result is as follows: we have started to close the profit but as we have closed everything wrong, we have ended up closing the series with a loss. Is it normal? Do not write codes only for the tester, write a code at least for working on a demo account.

 
Vitalie Postolache:

Got it, I need to remove the check for profit = positive, it makes the account stop later, I just needed to take into account my previous loss as well.

double lastloss()
{
double lastlos=0.0,lastlot=0.0,op=0.0;
int cnt=0;
datetime time=0;
for(int i=OrdersHistoryTotal()-1; i>=0; i--)
{
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==Magic && OrderType()<2)
         {
            if(cnt==0 && OrderCloseTime()!=0) time=OrderCloseTime();
            if(OrderCloseTime()+PeriodSeconds()<time/* && op>0.0*/) break;
            op=OrderProfit()+OrderSwap()+OrderCommission();
            lastlos+=op;
            cnt++;
         }
      }
}
return(lastlos);
}

It works, counts as it should, genius, awesome!!!
Thank you - thank you - thank you !!!!!!!!!!!!)))))))))))))

You did well))

 
Vitaly Muzichenko:

There's more:

Why did I ask about what removing an order between closing positions does?


Now we have a situation: An EA starts to close positions when it reaches profit and then it gets an order which is of no use at all, i.e. it makes neither profit nor loss but an order to remove this order is sent and while it is being removed, the price leaves and the EA continues to close further. The result is as follows: we have started to close the profit but as we have closed everything wrong, we have ended up closing the series with a loss. Is it normal? Do not write codes only for the tester, write a code at least for working on a demo account.

Yes, they do not close there in order, it happens if the closing cycle starts from zero.
 
Vitalie Postolache:
They do not close in any order, it happens if the closing cycle starts from zero.

From what zero?

Well, why waste time on order deletion at the moment of grid closing? You know yourself that deleting an order takes time, so why waste it, and if your brokerage company is not fast, we are screwed. I showed a screenshot with 15 positions closed in 1 minute and 34 seconds and we have to realize that the price can go through more than a dozen points during this time and we will be lucky if it goes in our direction and not against us.

 
Vitaly Muzichenko:

From what zero?

Well, why waste time on order deletion at the moment of grid closing? You know yourself that deleting an order takes time, so why waste time? And if your brokerage company is not fast, we are screwed. I once posted a screenshot where 15 positions were closed in 1 minute and 34 seconds.

From this zero point for(int i=0;i<OrdersHistoryTotal();i++) - This way, the orders will be closed one by one at the best case if they are selected by ordinal number.

Yes, I know, a pending order must not be deleted in the middle of the process of closing a series, and it would be better to close them in order, but this comes with experience.

Reason: