MQL4 opposite order HELP!!
Where do you open your first order? I'm asking because in the code u posted, a first order is expected, before any further orders can be opened.
Now, assuming that you have the first order, then can you do a Print(GetLastError()) right after your OrderSend() function and let us know the error code?
Also, please use the code icon when you include codes:
![]()
-
Why did you post your MT4 question in the
Root /
MT5 General section
instead of the MQL4 section,
(bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. - When you post code please use the CODE button (Alt-S)!
(For large amounts of code, attach it.)
Please edit
your (original) post.
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor - Seng Joo Thio: Where do you open your first order?
int ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,OrderStopLoss(),OrderTakeProfit(),NULL,0,0,NULL);
You would know why (lack of error code means № 3 or you are not even calling the function,) if you check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.- You have an open buy order (assuming not № 3,) therefor any SL must be below market and any TP is above. How can you open a sell order with the same stops? You would know this if you had checked (№ 4)
- You didn't set your profile, but
Since 2009, hedging is not permitted for US traders.
NFA Enforces FIFO Rule, Bans Forex Hedging in US Forex Accounts - Trading Heroes
FAQ: FIFO in the Forex Market - BabyPips.com vbid==OrderStopLoss()
Doubles are rarely equal. Understand the links in:
The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forumdouble vbid= MarketInfo (Symbol(),MODE_BID); double vask= MarketInfo (Symbol(),MODE_ASK); double StopLoss= OrderStopLoss(); double openprice= OrderOpenPrice();
- These are globally declared variables, they are initialized once on load. They don't update unless you assign to them.
- Don't try to use any price or server related functions in
OnInit (or on load,) as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
- You can not use any Trade Functions until you first select an order.
Hello, can anyone tell me why my ordersend function wont execute?
#property strict
double vbid= MarketInfo (Symbol(),MODE_BID);
double vask= MarketInfo (Symbol(),MODE_ASK);
double StopLoss= OrderStopLoss();
double openprice= OrderOpenPrice();
extern double LotSize = 0.1;
//---------trailing stop variables----------
//------------------------------------------
void OrderSearch ()
{
for (int i=0;i<OrdersTotal();i++)
{ bool orderselect;
orderselect=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (orderselect==false)
{Alert("ERROR SELECTING ORDER");}
else { Alert("ORDER SELECTETD");
if (OrderSymbol()==Symbol()&& OrderType()==OP_BUY && vbid==OrderStopLoss())
{
//opposite trade (Sell)
int ticket;
ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,OrderStopLoss(),OrderTakeProfit(),NULL,0,0,NULL);
}
else if (OrderSymbol()==Symbol() && OrderType()==OP_SELL && vask==OrderStopLoss())
{
//opposite Trade (Buy)
int ticket;
ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,OrderStopLoss(),OrderTakeProfit(),NULL,0,0,NULL);
}
}
}
}
//------------MoveToBreakEven----------
void TrailingStop ()
{
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---Call Function----
OrderSearch();
}
//+------------------------------------------------------------------+