How do I count the total money amount of consecutive losses?

 

For example, below are my trades

Trade 1: -100$ (lose) => loss_count = 1, loss_amount = -100 // Identify the first loss and assign it into loss_amount var

Trade 2: -200$ (lose) => loss_count = 2, loss_amount = -300 // 2nd loss, negative profit will be cumulate loss_amount++

Trade 3: +100$ (win) => loss_count = 3, loss_amount = -200 // a win won't be counted as reset as long as the loss_amount still negative

Trade 4: +500$ (win) => loss_count = 0, loss_amount = 0 //reset to 0

So how do I make this in MQL5?

I wrote this one based on MQL5 reference, so I must have messed up something. Though there is no error in the code, but it didn't return correct value when it was running

You can just apply it into your chart, and open a random market trade and close immediately to test it

int loss_count;
double loss_amount;
double win_amount;

void OnTick()
  {
  loss_count = 0;
  loss_amount = 0;
  win_amount = 0;

  // Get the total number of deals
  int total_deals = HistoryDealsTotal();

  // Iterate through the deals history
  for (int i = total_deals - 1; i >= 0; i--)
  {
    // Get the deal ticket
    ulong deal_ticket = HistoryDealGetTicket(i);

    // Get the deal information
    if (HistoryDealSelect(deal.Ticket()))
    {
      // Check if the deal is for the current symbol and is a position close deal
      if (deal.Symbol() == _Symbol && (deal.Entry() == DEAL_ENTRY_OUT || deal.Entry() == DEAL_ENTRY_INOUT))
      {
        // Check if the deal is a loss
        if (deal.Profit() < 0)
        {
          loss_count++;
          loss_amount += -deal.Profit();
        }
        // Check if the deal is a win
        else if (deal.Profit() > 0)
        {
          win_amount += deal.Profit();

          // Check if win_amount exceeds loss_amount
          if (win_amount >= loss_amount)
          {
            loss_count = 0;
            loss_amount = 0;
            win_amount = 0;
          }
          else
          {
            loss_count++;
          }
        }
      }
    }
  }

  Print("Loss count: ", loss_count, " Loss amount: ", loss_amount);
}
 
Viet Tung Nguyen:

For example, below are my trades

Trade 1: -100$ (lose) => loss_count = 1, loss_amount = -100 // Identify the first loss and assign it into loss_amount var

Trade 2: -200$ (lose) => loss_count = 2, loss_amount = -300 // 2nd loss, negative profit will be cumulate loss_amount++

Trade 3: +100$ (win) => loss_count = 3, loss_amount = -200 // a win won't be counted as reset as long as the loss_amount still negative

Trade 4: +500$ (win) => loss_count = 0, loss_amount = 0 //reset to 0

So how do I make this in MQL5?

I wrote this one based on MQL5 reference, so I must have messed up something. Though there is no error in the code, but it didn't return correct value when it was running

You can just apply it into your chart, and open a random market trade and close immediately to test it

Before asking people to run your code and invest their time, please consider providing all the relevant information.

What it returned ? What are you expecting it should have returned ? and how do you come to this conclusion ?