A function to loop through open positions and make pairs of arrays of tickets then close each pair of array if greater than target profit

MQL4 Experts

Job finished

Execution time 6 minutes
Feedback from customer
very good!
Feedback from employee
Great customer! Thank you for the order!

Specification

This is for an MT4 EA.

I need a function to loop through all open positions and store them in arrays based on price levels.
Then from these arrays (i.e. @ price 1760 there are open orders and @ price 1770 there are open orders and so on).
I want to calculate the ongoing profit of pairs of 2 arrays (i.e. sell array i with buy array i+1     AND    buy array i with sell array i-1).
And if profit is greater than target, close orders of these pairs of arrays. Each array may contain more than 1 ticket and can go up to x tickets, and the function will close the arrays only if the pairs contain minimally an array in each level.
The function will loop through all the arrays crawling from initial price up and down.
An optional function can also close the pairs of arrays if the total tickets in these pairs are x (can be input).
Code is already written, but i currently have problem with :

void CalculateAndCloseOrderGroupsForBuyAndSell() {
    // Loop from the initial_index down to 1 (since i-1 sell for i=0 doesn't exist)
    for (int i = initial_index; i > 0; i--) {
        double totalProfit = 0.0;
        
        if (buyTicketCounts[i] > 0 && sellTicketCounts[i-1] > 0){
           // Calculate profit for all buy tickets at index i
           for (int j = 0; j < buyTicketCounts[i]; j++) {
               int ticket = buyTickets[i][j];
               totalProfit += ComputeProfitForOrder(ticket);
           }
   
           // Calculate profit for all sell tickets at index i-1
           for (int j = 0; j < sellTicketCounts[i - 1]; j++) {
               int ticket = sellTickets[i - 1][j];
               totalProfit += ComputeProfitForOrder(ticket);
           }
          }

        // If the combined profit meets or exceeds the target, close the tickets and remove them from arrays
        if (totalProfit >= profit_target) {
            // Close and remove buy tickets at index i
            for (int j = buyTicketCounts[i] - 1; j >= 0; j--) {
                CloseOrder(buyTickets[i][j]);
                // Shift tickets to remove the closed ticket from the array
                for (int k = j; k < buyTicketCounts[i] - 1; k++) {
                    buyTickets[i][k] = buyTickets[i][k + 1];
                }
                buyTicketCounts[i]--;
            }

            // Close and remove sell tickets at index i-1
            for (int j = sellTicketCounts[i - 1] - 1; j >= 0; j--) {
                CloseOrder(sellTickets[i - 1][j]);
                // Shift tickets to remove the closed ticket from the array
                for (int k = j; k < sellTicketCounts[i - 1] - 1; k++) {
                    sellTickets[i - 1][k] = sellTickets[i - 1][k + 1];
                }
                sellTicketCounts[i - 1]--;
            }

            Print("Closed and removed orders for buy zone ", i, " and sell zone ", i - 1, " with total profit: ", totalProfit);
        }
    }
}


it always gives me "out of array" error. I will give the whole set of code. the focus is to fix this part to overcome the "out of array" issue/ error.


Thank you.




Responded

1
Developer 1
Rating
(100)
Projects
125
23%
Arbitration
12
0% / 75%
Overdue
22
18%
Free
2
Developer 2
Rating
(68)
Projects
77
8%
Arbitration
33
9% / 55%
Overdue
6
8%
Working
3
Developer 3
Rating
(45)
Projects
74
49%
Arbitration
1
0% / 0%
Overdue
1
1%
Working
4
Developer 4
Rating
(23)
Projects
45
20%
Arbitration
24
29% / 46%
Overdue
12
27%
Free
5
Developer 5
Rating
(64)
Projects
93
37%
Arbitration
11
27% / 45%
Overdue
14
15%
Free
6
Developer 6
Rating
(451)
Projects
510
33%
Arbitration
25
40% / 48%
Overdue
7
1%
Loaded
7
Developer 7
Rating
(6)
Projects
10
10%
Arbitration
8
0% / 88%
Overdue
1
10%
Working
8
Developer 8
Rating
(66)
Projects
143
34%
Arbitration
10
10% / 60%
Overdue
26
18%
Free
9
Developer 9
Rating
(568)
Projects
641
41%
Arbitration
21
57% / 29%
Overdue
47
7%
Working
Similar orders
Hi, I have an indicator from my friend, I want to copy it to my own Traidingview or MT5 can you do that for me. Here is the link
Develop a simple trading robot from rainbow oscillator Additional features Include timeframe specification in rainbow oscillator trailing features moving average filter TP (true/false) SL (true/false) Fixed lot Risk percentage Percentage profit lock
Hi, I have an indicator from my friend, I want to copy it to my own Traidingview or MT5 can you do that for me. Here is the link
I need a dashboard that shows my various accounts that shows balance, equity, number of trades open, drawdown and some more information. I want to use it to monitor my accounts in one screen or chart. If there is anyone who can do it please show me the screenshot of your sample
Hi, I need a robot, which wil get instructions to trade in 3 symbols at the same time based on few parameters and calculations. Example: There is 1 symbol called Gold-Near and the rate for it is 1000-1002 If i specify that when the rate reaches 1050, it should sell 1 lot Upon execution it will have to sell 1 lot of cme gold, buy 3 lots of mcx gold and buy currently (lots will be based on calculation). All the
Trading bot 300+ USD
We need bot that trades when medium and low impact news hits It will release pending order both directions few min prior to news impact And will have certain risk management strategy attached Example If Monday and Tuesday news successful clears profits It will reduce risk for next news events until new week starts each week message on tg: Dstatewealthtrading NOTE: 4 YAERS OF EXPERIENCE UPWORD, YOU MUST BE A
Estou a procura de um programador que faça um robo que automatize as análises feitas por ele nas ondas de Elliot e que faça entradas precisas com proteções das mesmas e com proteções também ajustáveis para não entrar em noticias de 2 ou 3 estrelas, e que seja para o mercado B3 Mini Indice ou Mini Dolar
Sosso 30+ USD
I would like to order an automated robot to the job for me as I have no clue on whatsoever the trading is about. The bot should be able to do it's job, everything for me and it providing the best services for one to use
are you aware of the Monday Range Strategy? https://www.youtube.com/watch?v=7B_yBBFx6z8 5pm EST time sunday - monday 5pm est and it has to be on the H1 chart , minimum 1:2 Risk to reward and break even function after 1:1
Strategy: 1. Identify strong supply and demand levels. Draw them in the chart in green and red. In Tradingview: Market Structure Break & Order Block by EmreKb von EmreKb --> Strong demand and supply area refer to picture below (Buy and Sell Zones). There must be a strong demand / supply zone on a higher time frame in order for the set up to be valid. The time frame can be selected in the settings. E.g. "Higher Time

Project information

Budget
30 - 45 USD
VAT (19%): 5.7 - 8.55 USD
Total: 35.7 - 53.55 USD
For the developer
27 - 40.5 USD