工作已完成
指定
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.