Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Obviously your code is "faulty".
Just look at the output ...
17799267': failed cancel order #0 buy 0 at market [Invalid request]'
Do you really think that is going to be valid?
Could be.I have no problem in sharing it , here it is :
PYTHON:
import MetaTrader5 as mt5 # Connect to MetaTrader 5 if not mt5.initialize(): print("initialize() failed") mt5.shutdown() # Search for pending orders orders = mt5.orders_get(symbol="XAUUSD", action=mt5.TRADE_ACTION_PENDING) if len(orders) > 0: current_order = orders[0] # Delete the pending order result = mt5.order_send( action=mt5.TRADE_ACTION_REMOVE, symbol=current_order.symbol, ticket=current_order.ticket ) if result.retcode == mt5.TRADE_RETCODE_DONE: print(f"Pending order {current_order.ticket} deleted") else: print(f"Failed to delete pending order {current_order.ticket}. Error code: {result.retcode}") else: print("No pending orders found")
Improperly formatted code edited by moderator.
It is a simple one.What do you think?
In the future, please use the CODE button (Alt-S) when inserting code.
I'm not a Python coder, but where exactly in the documentation does it mention an "action" parameter?
orders = mt5.orders_get(symbol="XAUUSD", action=mt5.TRADE_ACTION_PENDING)
From here: https://www.mql5.com/en/docs/python_metatrader5/mt5ordercheck_py#trade_request_actions
TRADE_ACTION_PENDING
"Place an order for performing a deal at specified conditions (pending order)."
The code itself doesn't shot error message on IDLE, it identifies correctly the PENDING magic number, and sends the deleting order to MT5, but it is rejected.I'm gonna pass you the MQL5 code in the next message. May it can be more clarifying than the Python one is, and thanks for answering.
You have referenced the documentation for order_check.
I will repeat, where does it say that there is an "action" parameter for orders_get?
There is none, so revise your code to filter for the action after obtaining all the orders.
You have referenced the documentation for order_check.
I will repeat, where does it say that there is an "action" parameter for orders_get?
There is none, so revise your code to filter for the action after obtaining all the orders.
Ok.so you're telling me to print the current PENDING list, then Ask for them to be deleted, and as it is not being done so far, the command it sends is "delete order 0" it means 'None'.Good point. I'll check it out on PYTHON.Now I will share the MQL5 version of 'Deleting PENDING'.I'd like you to check it out, if you please, and thanks in advance.
#include <Trade\Trade.mqh> CTrade trade; void OnTick() { // Get the account balance double Balance = AccountInfoDouble(ACCOUNT_BALANCE); // Get the account equity double Equity = AccountInfoDouble(ACCOUNT_EQUITY); // We calculate the Ask Price double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); // We calculate the Bid Price double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits); // if we have at least 1 position if (Balance != Equity) { CancelOrder(); } } void CancelOrder() { // Check all orders for (int i = OrdersTotal() - 1; i >= 0; i--) // count all currency pair positions { // Get the ticket number ulong OrderTicket = OrderGetTicket(i); // Delete the pending orders trade.OrderDelete(OrderTicket); } // End for loop } // End Cancel Order function ------------------------- To Backtest the code I add the following lines (and the whole code works on Backtesting only): { Print("### PostitionsTotal: ",PositionsTotal(), "OrdersTotal:",OrdersTotal()); // buy stop, 10 microlots, 100 points above Ask, no SL, // 300 points TP, no expiration, no date, no comment trade.BuyStop(0.10, Ask+100*_Point,_Symbol,0,Ask+300*_Point, ORDER_TIME_GTC,0,0); // sell stop, 10 microlots, 100 points below Bid, no SL, // 300 points TP, no expiration, no date, no comment trade.SellStop(0.10, Bid-100*_Point,_Symbol,0,Bid-200*_Point, ORDER_TIME_GTC,0,0); } ---------------------------------------
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone.I´ve been experiencing an issue, on my MT5, which seems to be a setting one, but can´t figure out why.
I can´t execute a delete PENDING, non from a MQL5 bot, and from PYTHON API either.Both bots seem to be correctly coded, MQL5´s one compiles well, and works in Backtesting, but when I want to run it on a demo account, it simply does nothing(and no messages in journal at all).And at the other hand, PYTHON API shots 'Failed to delete pending order 'whatevernumberbe'. Error code: 10013' on IDLE,which on the MT5 journal means ' 2023.10.05 10:07:38.123 Trades '17799267': failed cancel order #0 buy 0 at market [Invalid request]'
So, if some one has a clue of what could be wrong, I will be very thanked.