PhucMinh:
Hi guys,
Can you help me with my code, i need to close all current position when there is a new signal.
Example: i'm currently on a Sell position, when there's a buy signal, i want the sell signal to close at market price and open a new buy position. Thank you!
My code:
... //sell 0.8 lot if (signal=="sell") { CloseAll(); trade.Sell(0.80,NULL,Bid,0,0,NULL); } //buy 0.8 lot if (signal=="buy") { CloseAll(); trade.Buy(0.80,NULL,Ask,0,0,NULL); } ... void CloseAll() { for(int i=0;i<PositionsTotal();i++) { if(PositionSelectByTicket(PositionGetTicket(i))) { trade.PositionClose(PositionGetTicket(i), -1); i--; } } }
Certainly, I'd be happy to help you with your code. Here's an example code snippet in MQL4 that should close all current positions and open a new position based on a new signal:
int start() { // Check for new signal if (/*new signal is detected*/) { // Close all current positions int count = OrdersTotal(); for (int i = count - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderType() == OP_BUY || OrderType() == OP_SELL) { OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), slippage, Blue); } } } // Open new position based on new signal if (/*buy signal*/) { OrderSend(OrderSymbol(), OP_BUY, lotsize, Ask, slippage, Ask - stoploss * Point, Ask + takeprofit * Point, "buy order", MagicNumber, 0, Green); } else if (/*sell signal*/) { OrderSend(OrderSymbol(), OP_SELL, lotsize, Bid, slippage, Bid + stoploss * Point, Bid - takeprofit * Point, "sell order", MagicNumber, 0, Red); } } return(0); }
please let me know if it helps or not?
JordanBaker85 #:
please let me know if it helps or not?
What's the point to provide mql4 code to someone asking for mql5 ?
Certainly, I'd be happy to help you with your code. Here's an example code snippet in MQL4 that should close all current positions and open a new position based on a new signal:
please let me know if it helps or not?

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
Hi guys,
Can you help me with my code, i need to close all current position when there is a new signal.
Example: i'm currently on a Sell position, when there's a buy signal, i want the sell signal to close at market price and open a new buy position. Thank you!
My code: