Is your account a netting account or a hedging account? In case of a hedging account this code does not seem to close the open position, but it adds a second position, of opposite type. The original position remains open and continues to be beyond your target levels.
For example: if you opened a long position by buying trade.Buy(). This position reaches your profit target and you want to close it. In that case you should not use trade.Sell(), but trade.PositionClose().
Is your account a netting account or a hedging account? In case of a hedging account this code does not seem to close the open position, but it adds a second position, of opposite type. The original position remains open and continues to be beyond your target levels.
For example: if you opened a long position by buying trade.Buy(). This position reaches your profit target and you want to close it. In that case you should not use trade.Sell(), but trade.PositionClose().
Thanks for your advice, additional knowledge has been imparted through your post.
if (m_position.PriceOpen() < m_position.PriceCurrent() - (k+1)*(100 * _Point)) trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point), (Ask+TP * _Point),NULL) ;
Thanks, you are absolutely correct. The code currently continues to open orders without stopping (over 100 opened orders and still counting) so I made an improvement thus:
for(int k=0; k<4; k++) ... if (m_position.PriceOpen() < m_position.PriceCurrent() - (k+1)*(100 * _Point) && PositionsTotal()==k) trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point), (Ask+TP * _Point),NULL) ;
However, this improvement only limits total number of opened orders to 3 instead of just 1 when each condition is reached. Furthermore, one out of the 4 conditions does not open new position when its condition is reached. Any idea/solution, from anyone, to achieve this will be highly appreciated.
if(PositionsTotal()==0) { if (m_position.PriceOpen() < m_position.PriceCurrent() - (k+1)*(100 * _Point) && PositionsTotal()==k) trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point), (Ask+TP * _Point),NULL) ; }
You can expand on this by filtering trades for each instrument and or magic number.
//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool SymbolIsTradeCheck(string symbol) { int total=PositionsTotal(); // number of open positions //--- iterate over all open positions for(int i=total-1; i>=0; i--) { if(symbol==PositionGetString(POSITION_SYMBOL)) { return(true); } //can add if(magic == magicnumber) etc... } return(false); } //+------------------------------------------------------------------+
- 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,
I coded an EA which runs perfectly but places multiple trades instead of one.
Using the most currently opened order as reference point, the EA is meant to open 1 Buy order when price increases by 100 pips and open a Sell order when it decreases by 100 pips. However, if the most currently opened order is due to close by take profit (set at 100 pips) then EA allows it to close before opening new order in price direction and a Stop loss of zero is currently used. But instead of opening just One order, it opens multiple unending orders.
This code is pasted below: