#include <Trade\Trade.mqh> #include <Trade\PositionInfo.mqh> CTrade Trade; CPositionInfo m_position; //General Inputs input double LotSize = 0.10; //Global Variables double FirstBuy, SellBuy, OldNumOfBars, Ask, Bid; bool SellPrice, BuyPrice; int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } void OnTick() { { if(NewBarPresent()) { //BID and ASK Ask = NormalizeDouble (SymbolInfoDouble (_Symbol, SYMBOL_ASK), _Digits); Bid = NormalizeDouble (SymbolInfoDouble (_Symbol, SYMBOL_BID), _Digits); if(PositionsTotal()==0) FirstBuySell(); } } SellPrice = Bid + 10*_Point; BuyPrice = Ask - 10*_Point; } void FirstBuySell() { //Executing First Trades if(PositionsTotal() == 0) { Trade.Buy(LotSize, _Symbol, Ask); Trade.Sell(LotSize, _Symbol, Bid); return; }else { SellExit(); BuyExit(); } } void SellExit() { if (PositionsTotal() == 2){ for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current position if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==Symbol()) if (SellPrice == true){ Trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol } } } void BuyExit() { if (PositionsTotal() == 2){ for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current position if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==Symbol()) if (BuyPrice == true){ Trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol } } } bool NewBarPresent() { int bars = Bars(_Symbol,PERIOD_CURRENT); if(OldNumOfBars!=bars) { OldNumOfBars = bars; return true; } return false; }
You were even told this before in a previous thread ...
Forum on trading, automated trading systems and testing trading strategies
Fernando Carreiro, 2022.08.15 00:18
@Mr David Mcbride, please edit your previous post and use the
"</>" icon or Alt-S to post your code, or attach it as a file with
.
Don't just copy/paste it in plain text because it is difficult to read.
int bars = Bars(_Symbol,PERIOD_CURRENT); if(OldNumOfBars!=bars)
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)
I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011)
It seems you did not understand my English. I stated "EDIT" your existing post, not create a new one.
does it make that much of difference? I find coming on here people are so nit picky or they flex their coding knowledge and try and belittle people!! Let me tell you now my English excellent!!!!!
Yes, it does because it makes for a long thread. That is the whole point.
I'm nit picky, because it makes reading the code difficult. Even your second post with proper code insertion, has so many empty lines also making it difficult to read.
Not every user is using a large screen to view posts. Sometimes we are on the road and reading it on a mobile device, making it much worse.
If you expect the readers to put in the effort to analyse and debug your code for you, then at least show the same amount of effort when placing your query on the forum.
Yes, I know I am now ranting and taking my fury out on you alone, but this happens all the time with so many posters that it gets to a point that we boil over a flip a lid.
Yes it does. If you can't follow such simple instructions, we will start ignoring you and your unreadable code questions.
It seems you did not understand my English. I stated "EDIT" your existing post, not create a new one.
does it make that much of difference? I find coming on here people are so nit picky or they flex their coding knowledge and try and belittle people!! Let me tell you now my English excellent!!!!!
It is not nit picking!
By leaving your first post as it was, people will struggle to read and understand your original post as they obviously do not realise that you have posted your code properly in a later post.
It also means that I have had to waste my time deleting the code from your original post because you can't be bothered.

- 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 after some advice on how to close a position using PositionClose. However I cant seem to link the trade to the PositionClose. Please could someone point me in the right direction.
*******
Thank you for any help.