These two fields raise questions:
request.type_filling = fillType; *** request.expiration = TimeCurrent()+86400;
Are you sure you are filling them in correctly? To check - use the code based on the Standard Library.
Vladimir Karputov #:
These two fields raise questions:
Are you sure you are filling them in correctly? To check - use the code based on the Standard Library.
Hi Vladimir,
Thanks for the reply. You are right. After I set the filltype properties to ORDER_FILLTYPE_IOC it send the market order
successfully. Thanks.
I have Error is given . It says "Order Modify Failed. Error code: 10013" .
void OnTick() { MqlTick tick; if(SymbolInfoTick(Symbol(), tick)) { double entry = tick.ask; double tick_size = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE); MqlTradeRequest request = {}; MqlTradeResult result = {}; // Open a new position with zero stop loss and take profit request.action = TRADE_ACTION_DEAL; request.symbol = Symbol(); request.volume = 0.1; // Modify this to your desired volume. request.type = ORDER_TYPE_BUY; // Modify this to ORDER_TYPE_SELL for a sell order. request.price = entry; request.sl = entry - 100 * tick_size; // Zero stop loss. request.tp = entry + 100 * tick_size; // Zero take profit. if(!OrderSend(request, result)) { Print("Order Failed = ", result.retcode); return; } else { Print("Order placed. Ticket: ", result.order); } Sleep(5000); // 5 seconds delay (modify this as needed). ulong Pos_ticket = PositionGetInteger(POSITION_TICKET); ENUM_POSITION_TYPE Pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); double Pos_entry=PositionGetDouble(POSITION_PRICE_OPEN); string Pos_Symbol=PositionGetString(POSITION_SYMBOL); double Pos_Volume=PositionGetDouble(POSITION_VOLUME); MqlTradeRequest modifyRequest = {}; MqlTradeResult modifyResult = {}; modifyRequest.symbol = Pos_Symbol; modifyRequest.action = TRADE_ACTION_SLTP; modifyRequest.position = Pos_ticket; modifyRequest.volume = Pos_Volume; modifyRequest.type_filling = ORDER_FILLING_FOK; if(Pos_type == POSITION_TYPE_BUY) { modifyRequest.sl = Pos_entry - 500 * tick_size; modifyRequest.tp = Pos_entry + 500 * tick_size; } else if(Pos_type == POSITION_TYPE_SELL) { modifyRequest.sl = Pos_entry + 500 * tick_size; modifyRequest.tp = Pos_entry - 500 * tick_size; } if(OrderSend(modifyRequest, modifyResult)) { Print("Order Modified. New SL: ", modifyRequest.sl, ", New TP: ", modifyRequest.tp); } else { Print("Order Modify Failed. Error code: ", modifyResult.retcode); } } }
Please Solve this 🙏..
Check bid and ask (you buy the ask and sell the bid!!) and your prices and use MqlTradeCheckResult the order before sending.
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Request Check Result Structure
- www.mql5.com
Request Check Result Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Carl Schreiber #:
Check bid and ask (you buy the ask and sell the bid!!) and your prices and use MqlTradeCheckResult the order before sending.
void OnTick() { ulong Pos_ticket = PositionGetInteger(POSITION_TICKET); ENUM_POSITION_TYPE Pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); double Pos_entry = PositionGetDouble(POSITION_PRICE_OPEN); double Pos_Volume = PositionGetDouble(POSITION_VOLUME); string Pos_Symbol = PositionGetString(POSITION_SYMBOL); MqlTick tick; if(SymbolInfoTick(Symbol(),tick)) { MqlTradeRequest request = {}; MqlTradeResult result = {}; MqlTradeCheckResult check = {}; double entry = tick.ask; double tick_size = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE); request.symbol = Symbol(); request.volume = 0.1; request.action = TRADE_ACTION_DEAL; request.price = entry; request.type = ORDER_TYPE_BUY; request.magic = 123456; request.type_filling = ORDER_FILLING_FOK; request.sl = entry - 500 * tick_size; request.tp = entry + 500 * tick_size; if(OrderCheck(request,check)) { if(OrderSend(request,result)) { Print("Order Send Placed = ",result.order); } else { Print("Order Send Failed = ",result.retcode); } } else { Print("Order Checked to Send Failed = ",check.retcode); } MqlTradeRequest Modifyrequest={}; MqlTradeResult Modifyresult={}; MqlTradeCheckResult Modifycheck ={}; double sl =PositionGetDouble(POSITION_SL); double tp =PositionGetDouble(POSITION_TP); Modifyrequest.symbol = Pos_Symbol; Modifyrequest.action = TRADE_ACTION_SLTP; Modifyrequest.position = Pos_ticket; Modifyrequest.sl = sl; Modifyrequest.tp = tp; if(Pos_type == POSITION_TYPE_BUY) { sl = Pos_entry - 500 * tick_size; tp = Pos_entry + 500 * tick_size; } else if(Pos_type == POSITION_TYPE_SELL) { sl = Pos_entry + 500 * tick_size; tp = Pos_entry - 500 * tick_size; } if(OrderCheck(Modifyrequest,Modifycheck)) { if(OrderSend(Modifyrequest,Modifyresult)) { Print("Order Modified = ",Modifyresult.order); } else { Print("Order Modify Failed = ",Modifyresult.retcode); } } else { Print("Order Checked to Modify Failed = ",Modifycheck.retcode); } } }It says Error "Order Checked to Modify Failed = 10013" . Sir, Please solve it🙏🙏
i have same error. please help me
Invalid request reply code 10013
im trying to set takeprofit and stoploss for every trade.
this is the code :
im trying to set takeprofit and stoploss for every trade.
this is the code :
//+------------------------------------------------------------------+ //| AutoSLTP2.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #include <Trade\Trade.mqh> //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { Print("0N init"); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //EventKillTimer(); // destroy timer } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction& trans,const MqlTradeRequest& request,const MqlTradeResult& result) { Print("0N TRANSACTON"); if (trans.type == TRADE_TRANSACTION_ORDER_ADD) { Print("insidee type checl"); MqlTradeResult result2 = result; SetStopLossAndTakeProfit(request, result2); } } void OnTick() { //Print("0N Tick"); } //+------------------------------------------------------------------+ //| Set Stop Loss and Take Profit | //+------------------------------------------------------------------+ void SetStopLossAndTakeProfit(const MqlTradeRequest& request, MqlTradeResult& result) { Print("Symbol: ", request.symbol); double stopLoss = CalculateStopLoss(request.symbol); double takeProfit = CalculateTakeProfit(request.symbol); Print("Stop Loss: ", stopLoss); // Add this line to print out stop loss Print("Take Profit: ", takeProfit); // Add this line to print out take profit MqlTradeRequest modRequest = request; modRequest.sl = stopLoss; modRequest.tp = takeProfit; Print("Attention Sending Order "); Print(modRequest.sl); Print(modRequest.tp); ulong ticket = OrderSend(modRequest, result); Print(__FUNCTION__,": ",result.comment," reply code ",result.retcode); if(ticket < 0) { Print("OrderSend failed, Error code: ", GetLastError()); Print("OrderSend failed, Reason: ", result.comment); } else { Print("Order sent successfully with ticket: ", ticket); } } //+------------------------------------------------------------------+ //| Calculate Stop Loss | //+------------------------------------------------------------------+ double CalculateStopLoss(const string symbol) { double point = SymbolInfoDouble(symbol, SYMBOL_POINT); double stopLoss = NormalizeDouble(point * 300, Digits()); Print("SLLLL IS "); Print(stopLoss); return stopLoss; } //+------------------------------------------------------------------+ //| Calculate Take Profit | //+------------------------------------------------------------------+ double CalculateTakeProfit(const string symbol) { double point = SymbolInfoDouble(symbol, SYMBOL_POINT); double takeProfit = NormalizeDouble(point * 900, Digits()); Print("TPPP IS "); Print(takeProfit); return takeProfit; }
Discover new MetaTrader 5 opportunities with MQL5 community and services
- 2024.05.14
- www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
https://www.mql5.com/en/docs/constants/errorswarnings/enum_trade_return_codes
You are sending invalid request. Use Crade class with standalone metaquotes library. I never seen a similar issue.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Here's the code : I've got it from the Book : Expert Advisor for Metatrader 5 by Andrew Young.
Calling code in OnInit: