Hi everyone, it will be great if someone could help me to solve this problem,
I have this partition in my code :
The problem is in the two function TotalOpenOrders() and ClosePendingOrders() , they return a false values if I put the condtion OrersTotal() == 0, but when I remove this condition they return true values but the OrderClosePrice() return a false value !
and I thik that the problem come from the OrderSelect, it dosn't return a true value of OrderType() if it's preceded by the condtion OrdersTotal() == 0 , and it dosn't return a true value of OrderClosePrice() if it's not preceded by the condtion OrdersTotal() == 0.
by the way, in the second cas it return the value of the price of execution of the selected order.
int T1, T2, T3;
int Ticket = 0;
double OC = 0;
double E1,E2,E3 StopLossSell, StopLossBuy, TakeProfitBuy , TakeProfitSell; // All these variables are defined previously
// in the code I need these two functions :
if (OrderType() == OP_SELL || OrderType() == OP_BUY) continue;
int TotalOpenOrders() // For Conting Open Orders.
int OrderCount = 0;
for (int i = OrdersTotal()-1; i >= 0; i--)
bool res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_SELL || OrderType() == OP_BUY) OrderCount++;
if (OrdersTotal() == 0)
if (Ticket == 0 && OC == 0)
T1 = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,E1,0,StopLossBuy,TakeProfitBuy,NULL);
T2 = OrderSend(Symbol(),OP_BUYSTOP,LotSize,E2,0,StopLossBuy,TakeProfitBuy,NULL);
T3 = OrderSend(Symbol(),OP_SELLSTOP,LotSize,E3,0,StopLossSell,TakeProfitSell,NULL);
if (TotalOpenOrders() == 1) ClosePendingOrders();
bool Result = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
Ticket = OrderTicket();
OC = OrderClosePrice();
One of the problems (if not the only problem you have) is in the calculation of the Stop Loss and Take profit value.
Always remember that TP for BuyStop is always higher than the current price while the TP for the BuyLimit
order is lower than the current price. The same for the SL (but in reverse) ...
Use the same logic for the BuyLimit and SellLimit orders as well.
As I see, you are using same values of StopLossBuy,TakeProfitBuy,StopLossSell,TakeProfitSell
for all types of pending orders which will never work for you.
Do you have more code that you are not posting? What you have posted does not check the return values, or print error codes if there is a problem.
This whole section doesn't make any sense to me:
Breaking it down:
The code is only going to run if there are NO orders at all, pending or otherwise.
You try to set pending orders.
ClosePendingOrders() never gets called. How can you have an open order if you tested OrdersTotal()==0 at the start and then only set pending orders?
You are always selecting index 0 in the order pool. You can't presume anything about the indexing of the order pool.
Whether you set pending orders or not, you are going to set ticket and OC to whatever is in index 0 of the order pool.
If the OrderSelect() failed, you still store values in ticket and OC but they are probably wrong values.