Yes, I'm sure there are, lol as I said I am very green and just wanting the bare essentials for testing.
Can you offer a piece of code that will work to buy 5 units of whichever graph i drag the expert onto to?
I don't want take profit or stop loss just to open the order when I tell it to and then to close the order when I tell it to?
Just like I clicked the Buy/Sell or Close button in MT4, surely there is a line or two of code that will work in any situation for whichever graph the expert is running on, even if its only going to work on one graph at any time?
I appreciate that most of you would have endless options and complexities and error odes in order open and close orders but I really just want the order to open and close for testing now if you can help?
Cheers
GG
int BuyTicket=OrderSend(Symbol(),OP_BUY,5,Bid,3,0,0,NULL,BSMagic); BuyOpen=TRUE; string buyerr = GetLastError(); if(buyerr==ERR_REQUOTE) { RefreshRates(); int BuyTicketicket=OrderSend(Symbol(),OP_BUY,5,Bid,3,0,0,NULL,BSMagic); BuyOpen=TRUE; }
First of all, a buy order is opened at Ask, not Bid
Your
BuyOpen=TRUE;
is not dependant on whether the order was successful or not.
GetLastError() returns an int, not a string. You do not check if there actually was an error or not.
int BuyTicket=OrderSend(Symbol(),OP_BUY,5,Ask,3,0,0,NULL,BSMagic); if(BuyTicket>0) BuyOpen=TRUE; else { int buyerr=GetLastError(); if(buyerr==ERR_REQUOTE) { RefreshRates(); int BuyTicketicket=OrderSend(Symbol(),OP_BUY,5,Ask,3,0,0,NULL,BSMagic); BuyOpen=TRUE; } }
/
Oooooh, that's beautiful. Thanks so much :) All taken on board, including formatting.
Any idea why the order won't close tho ?
Magic number seems ok when I print it
I have no other orders open, anywhere
I can't seem to get any extra error code details apart from " unknown ticket 1 for OrderClose function "
I really appreciate you spending this time with a complete noob, it means a lot to me.
Cheers
GG
Awesome, thank you, you said just the right thing to push me in the right direction to find the information myself.
I would have spend weeks reading before I realized I couldn't use the magic number to close the order, strange it works in tester....
This is what I've got now, look ok?
for (int BPos = OrdersTotal()-1; BPos >= 0; BPos--) { if( ! OrderSelect(BPos, SELECT_BY_POS, MODE_TRADES) ) continue; if( OrderMagicNumber() == BSMagic && OrderSymbol() == Symbol() && ( OrderType() == OP_BUY || OrderType() == OP_SELL ) ) if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),3,0 ) ) Print("Order # ",OrderTicket(), "failed with error - ",GetLastError() ); BuyOpen=FALSE; BuyTrig=0; BSMagic++; }
Cheers
GG
You don't include an else
if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,0)) Print("Order # ",OrderTicket(),"failed with error - ",GetLastError()); else { BuyOpen=FALSE; BuyTrig=0; BSMagic++; }
without it
BuyOpen=FALSE;
BuyTrig=0;
BSMagic++;
would all be set even if the close fails
I cannot see any reason to increase the magic number every time
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I am new, thanks for having me. This is my first post. I have a great strategy that works in real life, just trying to get it into code which I have learnt pretty much from scratch over the past few months.
My coding skills are quite poor I would imagine compared to your standards, but I managed to get it running, how I want, in the tester but when I run on live data my order close is not working.
So buy order when certain conditions are met works with this code:
int BuyTicket=OrderSend(Symbol(),OP_BUY,5,Bid,3,0,0,NULL,BSMagic);
BuyOpen=TRUE;
string buyerr = GetLastError();
if(buyerr==ERR_REQUOTE)
{
RefreshRates();
int BuyTicketicket=OrderSend(Symbol(),OP_BUY,5,Bid,3,0,0,NULL,BSMagic);
BuyOpen=TRUE;
}
And closing was working in tester but in live mode it returns : unknown ticket 1 for OrderClose function
result=OrderSelect(BSMagic,MODE_TRADES);
result=OrderClose(BSMagic,5,Bid,3,0);
BuyOpen=FALSE;
BuyTrig=0;
BSMagic++;
Please excuse my ignorange, but can anyone offer a simple fix/workaround to get order to close?
Thanks
GG