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;

CTrade trade;

// 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);

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

            double low = iLow(_Symbol, PERIOD_CURRENT, 1);
            double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            ask = NormalizeDouble(ask, _Digits);

            double slBuy = low;
            slBuy = NormalizeDouble(slBuy, _Digits);

            // Open 4 buy positions with different take profit levels
            for (int i = 0; i < 4; i++)
            {
                double tp = ask + (ask - slBuy) * riskToRewards[i];
                tp = NormalizeDouble(tp, _Digits);
                trade.Buy(0.05, _Symbol, ask, slBuy, tp);
            }
        }

        // 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);
                trade.Sell(0.05, _Symbol, bid, slSell, tp);
            }
        }
    }
}
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Hi @ Okeyo Giose


I'm not a professional, but I'll try to help you.

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; } 
    trade.PositionClose(ticket); 
  }
}
Okeyo Giose
Okeyo Giose
  • 2023.12.04
  • www.mql5.com
Trader's profile
 
so peroblem solved right ?
 
ZeroCafeine #:
so peroblem solved right ?

Yes thank you! I solved the problem using your suggestion, much appreciated mate!
Reason: