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 エキスパート

仕事が完了した

実行時間6 分
依頼者からのフィードバック
very good!
開発者からのフィードバック
Great customer! Thank you for the order!

指定

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.




応答済み

1
開発者 1
評価
(100)
プロジェクト
125
23%
仲裁
12
0% / 75%
期限切れ
22
18%
2
開発者 2
評価
(72)
プロジェクト
80
10%
仲裁
38
8% / 58%
期限切れ
6
8%
3
開発者 3
評価
(53)
プロジェクト
91
52%
仲裁
1
0% / 0%
期限切れ
1
1%
仕事中
4
開発者 4
評価
(23)
プロジェクト
45
20%
仲裁
25
28% / 48%
期限切れ
12
27%
5
開発者 5
評価
(67)
プロジェクト
97
35%
仲裁
11
27% / 45%
期限切れ
14
14%
6
開発者 6
評価
(539)
プロジェクト
620
33%
仲裁
36
39% / 53%
期限切れ
11
2%
取り込み中
7
開発者 7
評価
(6)
プロジェクト
10
10%
仲裁
9
0% / 89%
期限切れ
1
10%
8
開発者 8
評価
(69)
プロジェクト
146
34%
仲裁
13
8% / 62%
期限切れ
26
18%
パブリッシュした人: 6 codes
9
開発者 9
評価
(568)
プロジェクト
641
41%
仲裁
25
48% / 36%
期限切れ
46
7%
仕事中
類似した注文
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
As in tittle, I need EA to pass FTMO challange fast, you know rules, max 5% daily drawdown, max 10% total drawdown. Passing in maximum +/- 1 month, it's around 20/22 working days 10/20=0.5% average per day profit. of course can be faster
Hello, I’m reaching out because I’m interested in hiring you to develop a custom trading bot for me. The bot should trade only XAUUSD (Gold) and be designed for long-term account growth using my own trading account size. Here are the core requirements: - Account size: $300 - Asset: XAUUSD only - Risk management: Strict and properly controlled - Risk-to-reward ratio: Clearly defined and consistently applied -
I am looking for an experienced MQL5 developer to help build and support an automated trade execution system that connects TradingView custom indicator alerts to an Exness MT5 account. The role involves receiving TradingView webhook signals containing Buy/Sell direction and dynamically calculated lot size, processing them via a Python-based webhook listener, and executing market orders on MT5 running on a 24/7

プロジェクト情報

予算
30 - 45 USD