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:

```//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

//---
Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);

// we calculate the bid price
double bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

//   create string for signal
string signal = "";

// create an array for the price data
double myRSIArray[];

//sort the array from the current candle downwards
ArraySetAsSeries(myRSIArray,true);

//difine the properties for RSI
int myRSIDefinition = iRSI (_Symbol,_Period,18,PRICE_CLOSE);

//defined EA, from current candle, for 3 candles, save in array
CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);

//calculate the current RSI value
double myRSIValue=NormalizeDouble(myRSIArray[0],2);

if (myRSIValue>80)
signal="sell";

if (myRSIValue<20)

//sell 0.8 lot
if (signal=="sell" && PositionsTotal()<1)

//chart output
Comment("The signal is now: ",signal);

}

```

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();
}

{
CloseAll();
}
...
void CloseAll()
{
for(int i=0;i<PositionsTotal();i++)
{
if(PositionSelectByTicket(PositionGetTicket(i)))
{
i--;
}
}
}

```

Yashar Seyyedin #:

Another thing is you should count buy and sell positions to decide to open a new position or close one.

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 (OrderType() == OP_BUY || OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), slippage, Blue);
}
}
}

// Open new position based on new signal
{
}
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 #:

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?
What's the point to provide mql4 code to someone asking for mql5 ?

Yashar Seyyedin #:

In the CloseAll() function there are i++ and i-- in a loop.

Then "i" will always have the same value.

Reason: