preciso ter opção de configurar quantos ordens placed serao abertas
ou seja atualmente ele coloca muita ordens em placed e ficam dezenas e centenas de ordens placed
preciso ter um limite em caso de eu ter pouca margem e limitar essas ordens placed
To add the functionality of limiting the number of pending orders (placed orders) that your trading robot generates, you need to introduce a new input parameter for the limit and modify the logic for sending orders accordingly. Let's go through the adjustments you need to make in your code:
Introduce a new input variable to set the maximum number of pending orders allowed.
Modify the logic that places new orders to ensure it respects this limit.
Code Modifications
Introduce a new input variable at the top of your script:
cpp
input int MaxPendingOrders = 5; // Maximum allowed pending orders
Place this line in the early part of your input declarations, alongside your other inputs.
Count current pending orders and limit placement of new orders in the OnTick function:
You already have logic in place to count pending and active orders. What you need now is to check this count against the maximum you've set.
Update your OnTick function like this:
cpp
void OnTick()
{
TrailStop();
if (!IsNewBar()) return;
MqlDateTime time;
TimeToStruct(TimeCurrent(), time);
int Hournow = time.hour;
if (Hournow < SHChoice) {
CloseAllOrders();
return;
}
if (Hournow >= EHChoice && EHChoice != 0) {
CloseAllOrders();
return;
}
if (SysChoice == 1) {
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
Tppoints = ask * TPasPct;
Slpoints = ask * SLasPct;
OrderDistPoints = Tppoints / 2;
TslPoints = Tppoints * TSLasPctofTP / 100;
TslTriggerPoints = Tppoints * TSLTgrasPctofTP / 100;
}
int BuyTotal = 0;
int SellTotal = 0;
int PendingOrdersTotal = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--) {
pos.SelectByIndex(i);
if (pos.Symbol() == _Symbol && pos.Magic() == InpMagic) {
if (pos.PositionType() == POSITION_TYPE_BUY) BuyTotal++;
if (pos.PositionType() == POSITION_TYPE_SELL) SellTotal++;
}
}
for (int i = OrdersTotal() - 1; i >= 0; i--) {
ord.SelectByIndex(i);
if (ord.Symbol() == _Symbol && ord.Magic() == InpMagic) {
if (ord.OrderType() == ORDER_TYPE_BUY_STOP) BuyTotal++;
if (ord.OrderType() == ORDER_TYPE_SELL_STOP) SellTotal++;
PendingOrdersTotal++; // Increment total pending orders
}
}
if (PendingOrdersTotal >= MaxPendingOrders) return; // Stop if the limit is reached
if (BuyTotal <= 0) {
double high = findHigh();
if (high > 0) {
SendBuyOrder(high);
}
}
if (SellTotal <= 0) {
double low = findLow();
if (low > 0) {
SendSellOrder(low);
}
}
}
Explanation
Input Variable:MaxPendingOrders allows you to set the maximum number of pending orders you want to allow. Change 5 to any number that suits your needs.
Order Counting: Introduced an additional PendingOrdersTotal counter which keeps track of the current number of pending orders. This number is then checked before issuing new buy/sell stop orders.
Conditional Check: Before placing any new type of order, verify if PendingOrdersTotal has reached the MaxPendingOrders threshold. If yes, skip the order placement logic using return.
This ensures that the number of pending orders (orders placed but not executed yet) never exceeds the limit you define, helping you manage trading account margin and exposure effectively.
altere e me entregue o codigo completo:
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
Below is the modified code with the addition of a configuration option for limiting the number of placed pending orders. The change is reflected in both the input parameters and the logic within the OnTick function to respect this new limit.
New Input Parameter: Added MaxPendingOrders to control the maximum number of pending orders allowed.
Enhanced Order Logic: Incorporated logic to check the current count of pending orders and compare it with MaxPendingOrders, preventing the submission of additional orders once the limit is reached.
These modifications will help you manage and limit the number of placed pending orders according to your risk management criteria.