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

Tâche terminée

Temps d'exécution 6 minutes
Commentaires du client
very good!
Commentaires de l'employé
Great customer! Thank you for the order!

Spécifications

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.




Répondu

1
Développeur 1
Évaluation
(100)
Projets
125
23%
Arbitrage
12
0% / 75%
En retard
22
18%
Gratuit
2
Développeur 2
Évaluation
(72)
Projets
80
10%
Arbitrage
38
8% / 58%
En retard
6
8%
Gratuit
3
Développeur 3
Évaluation
(53)
Projets
91
52%
Arbitrage
1
0% / 0%
En retard
1
1%
Travail
4
Développeur 4
Évaluation
(23)
Projets
45
20%
Arbitrage
25
28% / 48%
En retard
12
27%
Gratuit
5
Développeur 5
Évaluation
(67)
Projets
97
35%
Arbitrage
11
27% / 45%
En retard
14
14%
Gratuit
6
Développeur 6
Évaluation
(539)
Projets
620
33%
Arbitrage
36
39% / 53%
En retard
11
2%
Chargé
7
Développeur 7
Évaluation
(6)
Projets
10
10%
Arbitrage
9
0% / 89%
En retard
1
10%
Gratuit
8
Développeur 8
Évaluation
(69)
Projets
146
34%
Arbitrage
13
8% / 62%
En retard
26
18%
Gratuit
Publié : 6 codes
9
Développeur 9
Évaluation
(568)
Projets
641
41%
Arbitrage
25
48% / 36%
En retard
46
7%
Travail
Commandes similaires
look at attachment! i dont know if it is a expert or indi what it needs to do is for whatever par you are on the timeframe must adjust to the slider realtime on chart as you slide the slider and the chart and loaded indicators chnag eto the trimeframe you slide to time frame range M1 to D1
I want an experienced developer to create an EA that works with a grid system. I have a video on how the EA works. I want an experienced developer who will use the video to create the same bot
Ninja Trader 8 - **** https://www.youtube.com/watch?v=iDXAA6hUt2M - Orderflows Trader vs NinjaTrader Volumetric Charts What Are The Differences Only watch this video as a reference point, this is the standard setting for Volumetric https://www.youtube.com/watch?v=YrI8byj9_mg - How to Setup Volumetric Footprint Charts in NinjaTrader 8 | Futures Day Trading Tutorial - by Bull Barbie -----------------------------------
I’m looking for a skilled MQL5 developer to help build and manage an automated trading system that bridges TradingView custom indicator alerts to an Exness MT5 account . This role involves capturing Buy/Sell signals and lot sizes from TradingView webhooks , processing them with a Python listener , and executing orders on MT5 hosted on a 24/7 VPS . The ideal candidate will ensure accurate trades, reliable execution
I need a trading signals indicator. So I can learn how to upgrade my trading skills and mentality. I will be very glad if I can upgrade my trading mentality
I want to add. to my ea pay back I want to have a Max on the grid step. Because sometimes the market is ranging and the same grid steps 5 times or more It's getting close and open, close and open. To many times. So I want to add. Maximum Payback for this grid step 1.2.3. So if I set it for 0. It will delete. The stop loss. As long as ranging in this grid step. After it went out of the grid step. Replace the stop
Hi, I am looking for a good Forex programmer that could build a EA robot , It will have to be built base on fibonachi retracement, 0 , 0.236, 0.382 , 0,5 , 0.618 , 0.786, 1, 1.618 , 2.618, and 3.618, the robot should be able to make me input the fibo numbers myself. stop Loss, T.P
PROJECT DESCRIPTION I am using a Telegram → MetaTrader trade copier (currently TWP Copier 1.08) which receives trade signals correctly but does NOT consistently execute trades on MT4, despite: Signals being received and logged AutoTrading enabled Live trading allowed Correct symbol names (no suffix/prefix issues) Manual trades working perfectly No trade conflicts or filters blocking execution CURRENT ISSUE Telegram
An EA that can pass multiple prop firms that has drawdown limits no counts blown or violated An EA that is unique With the E A bagtest for MT5 Screen share over discord assistance with back test EA that passes the prop firm in less than two months Be able to solve solve issues with back test
hello Hello, I have a project I want done for a NinjaTrader Script that would involve 2 phases. Phase 1. I simply want a ninja trader strategy that will take whatever custom user defined high/low range defines in the settings than executes a breakout trade either once a candle closes above / below the range or simply crosses the high/low of the range by placing stop orders. The way it takes the breakout will be

Informations sur le projet

Budget
30 - 45 USD