Help close positions

Hi guys, beginner here new to making EA's. Time to put my pride aside and ask for help, I cant figure this one out.

I want to close any opened buy positions when a sell condition is met, and close any opened sell positions when a buy condition is met. Here's the code! And much appreciated in advance!

```#include <Trade\Trade.mqh>

// Variables
int handleMiddleMa;
int barsTotal;

// Define risk-to-reward ratios
double riskToRewards[] = {2.5, 5, 7.5, 10};

// Initialization function
int OnInit()
{
barsTotal = iBars(_Symbol, PERIOD_CURRENT);
handleMiddleMa = iMA(NULL, PERIOD_CURRENT, 50, 0, MODE_SMA, PRICE_CLOSE);

return (INIT_SUCCEEDED);
}

// Deinitialization function
void OnDeinit(const int reason)
{
// Perform cleanup if needed
}

// Tick event handler
void OnTick()
{
int bars = iBars(_Symbol, PERIOD_CURRENT);

// Check for new bars
if (barsTotal < bars)
{
barsTotal = bars;

double middleMa[];
CopyBuffer(handleMiddleMa, MAIN_LINE, 1, 2, middleMa);

double close1 = iClose(_Symbol, PERIOD_CURRENT, 1);
double close2 = iClose(_Symbol, PERIOD_CURRENT, 2);

if (close1 > middleMa[1] && close2 < middleMa[0])
{

double low = iLow(_Symbol, PERIOD_CURRENT, 1);

// Open 4 buy positions with different take profit levels
for (int i = 0; i < 4; i++)
{
tp = NormalizeDouble(tp, _Digits);
}
}

// Sell signal
if (close1 < middleMa[1] && close2 > middleMa[0])
{
Print("Sell signal");

double high = iHigh(_Symbol, PERIOD_CURRENT, 1);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
bid = NormalizeDouble(bid, _Digits);

double slSell = high;
slSell = NormalizeDouble(slSell, _Digits);

// Open 4 sell positions with different take profit levels
for (int i = 0; i < 4; i++)
{
double tp = bid - (slSell - bid) * riskToRewards[i];
tp = NormalizeDouble(tp, _Digits);
}
}
}
}
```

Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

Hi @ Okeyo Giose

When you have a buy signal, I have the impression that you only buy, but you don't close your short positions if there are any, of course.

For example, you could create a simple function to close shorts, and call it up when you have a long condition.

```//+------------------------------------------------------------------+
//|                           My FonctionS                           |
//+------------------------------------------------------------------+
//---
// Clsoe All Short Position
void Close_All_Short_Positions(){
for (int i =  PositionsTotal()-1 ; i >= 0 ; i--){
ulong   ticket = PositionGetTicket(i) ;
long type;
if ( type == POSITION_TYPE_BUY) { continue; }