Need help ive got 2 errors don't know how to finish the code

 
// Define inputs
input double lotSize = 0.01;
input double stopLossPercent = 2.0;
input double profitPercent = 1.0;
input int MagicNumber = 12345;

void OnTick()
{
    MqlRates rates[];
    int copied = CopyRates(_Symbol, PERIOD_D1, 0, 7, rates);
    if(copied < 7)
    {
        Print("Failed to copy rates, error code = ", GetLastError());
        return;
    }

    double highPrice = 0;
    double lowPrice = 0;

    for(int i = 0; i < 7; i++)
    {
        if(i == 0)
        {
            highPrice = rates[i].high;
            lowPrice = rates[i].low;
        }
        else
        {
            if(rates[i].high > highPrice)
                highPrice = rates[i].high;
            if(rates[i].low < lowPrice)
                lowPrice = rates[i].low;
        }
    }

    double stopLoss = AccountBalance() * stopLossPercent / 100.0;

    double entryPriceLong = lowPrice;
    double entryPriceShort = highPrice;

    MqlTradeRequest request = {0};
    MqlTradeResult result = {0};
    request.magic = MagicNumber;
    request.action = TRADE_ACTION_DEAL;
    request.type_filling = ORDER_FILLING_FOK;
    request.symbol = _Symbol;
    request.volume = lotSize;
    request.price = entryPriceLong;
    request.sl = entryPriceLong - stopLoss;
    request.tp = 0;
    request.comment = "Open Long Trade";

    if(OrderSend(request, result))
    {
        Print("Long trade opened successfully");
    }
    else
    {
        Print("Failed to open long trade, error code = ", GetLastError());
        return;
    }

    request.action = TRADE_ACTION_DEAL;
    request.type_filling = ORDER_FILLING_FOK;
    request.type = ORDER_TYPE_SELL;
    request.price = entryPriceShort;
    request.sl = entryPriceShort + stopLoss;
    request.comment = "Open Short Trade";

    if(OrderSend(request, result))
    {
        Print("Short trade opened successfully");
    }
    else
    {
        Print("Failed to open short trade, error code = ", GetLastError());
        return;
    }

    while(true)
    {
        double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

        double longProfit = (currentPrice - entryPriceLong) / entryPriceLong * 100.0;
        double shortProfit = (entryPriceShort - currentPrice) / entryPriceShort * 100.0;

        if(longProfit >= profitPercent || shortProfit >= profitPercent)
        {
            double closeLotSize = lotSize * 0.8;

            if(longProfit >= profitPercent)
            {
                request.type = ORDER_TYPE_SELL;
                request.price = currentPrice;
                request.volume = closeLotSize;
                request.sl = OrderOpenPrice() + 0.0001;
                request.comment = "Close Long Trade";

                if(OrderSend(request, result))
                {
                    Print("Long trade closed partially");
                }
                else
                {
                    Print("Failed to close long trade, error code = ", GetLastError());
                    return;
                }
            }

            if(shortProfit >= profitPercent)
{
    request.type = ORDER_TYPE_BUY;
    request.price = currentPrice;
    request.volume = closeLotSize;
    request.sl = OrderOpenPrice() - 0.0001;
    request.comment = "Close Short Trade";

    if(OrderSend(request, result))
    {
        Print("Short trade closed partially");
    }
    else
    {
        Print("Failed to close short trade, error code = ", GetLastError());
        return;
    }
} // add this closing curly brace
// Define inputs
input double lotSize = 0.01;
input double stopLossPercent = 2.0;
input double profitPercent = 1.0;
input int MagicNumber = 12345;

void OnTick()
{
    MqlRates rates[];
    int copied = CopyRates(_Symbol, PERIOD_D1, 0, 7, rates);
    if(copied < 7)
    {
        Print("Failed to copy rates, error code = ", GetLastError());
        return;
    }

    double highPrice = 0;
    double lowPrice = 0;

    for(int i = 0; i < 7; i++)
    {
        if(i == 0)
        {
            highPrice = rates[i].high;
            lowPrice = rates[i].low;
        }
        else
        {
            if(rates[i].high > highPrice)
                highPrice = rates[i].high;
            if(rates[i].low < lowPrice)
                lowPrice = rates[i].low;
        }
    }

    double stopLoss = AccountBalance() * stopLossPercent / 100.0;

    double entryPriceLong = lowPrice;
    double entryPriceShort = highPrice;

    MqlTradeRequest request = {0};
    MqlTradeResult result = {0};
    request.magic = MagicNumber;
    request.action = TRADE_ACTION_DEAL;
    request.type_filling = ORDER_FILLING_FOK;
    request.symbol = _Symbol;
    request.volume = lotSize;
    request.price = entryPriceLong;
    request.sl = entryPriceLong - stopLoss;
    request.tp = 0;
    request.comment = "Open Long Trade";

    if(OrderSend(request, result))
    {
        Print("Long trade opened successfully");
    }
    else
    {
        Print("Failed to open long trade, error code = ", GetLastError());
        return;
    }

    request.action = TRADE_ACTION_DEAL;
    request.type_filling = ORDER_FILLING_FOK;
    request.type = ORDER_TYPE_SELL;
    request.price = entryPriceShort;
    request.sl = entryPriceShort + stopLoss;
    request.comment = "Open Short Trade";

    if(OrderSend(request, result))
    {
        Print("Short trade opened successfully");
    }
    else
    {
        Print("Failed to open short trade, error code = ", GetLastError());
        return;
    }

    while(true)
    {
        double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

        double longProfit = (currentPrice - entryPriceLong) / entryPriceLong * 100.0;
        double shortProfit = (entryPriceShort - currentPrice) / entryPriceShort * 100.0;

        if(longProfit >= profitPercent || shortProfit >= profitPercent)
        {
            double closeLotSize = lotSize * 0.8;

            if(longProfit >= profitPercent)
            {
                request.type = ORDER_TYPE_SELL;
                request.price = currentPrice;
                request.volume = closeLotSize;
                request.sl = OrderOpenPrice() + 0.0001;
                request.comment = "Close Long Trade";

                if(OrderSend(request, result))
                {
                    Print("Long trade closed partially");
                }
                else
                {
                    Print("Failed to close long trade, error code = ", GetLastError());
                    return;
                }
            }

            if(shortProfit >= profitPercent)
            {
                request.type = ORDER_TYPE_BUY;
                request.price = currentPrice;
                request.volume = closeLotSize;
                request.sl = OrderOpenPrice() - 0.0001;
    request.comment = "Close Short Trade";

    if(OrderSend(request, result))
    {
        Print("Short trade closed partially");
    }
    else
    {
        Print("Failed to close short trade, error code = ", GetLastError());
        return;
    }
}

Sleep(1000); // Sleep for 1 second before checking again
}
}

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trade Operation Types - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

As per Forum rules and recommendations, please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S).

NB! Very important! DO NOT create a new post. EDIT your original post.

 

Do you know how to code?

This seems like ChatGPT generated code, mixing MQL4 and MQL5.

Please stop using ChatGPT. It is horrendous.

Please dedicate some time and effort into learning MQL properly, or hire a human coder.

 
Your code
    double highPrice = 0;
    double lowPrice = 0;

    for(int i = 0; i < 7; i++)
    {
        if(i == 0)
        {
            highPrice = rates[i].high;
            lowPrice = rates[i].low;
        }
        else
        {
            if(rates[i].high > highPrice)
                highPrice = rates[i].high;
            if(rates[i].low < lowPrice)
                lowPrice = rates[i].low;
        }
    }
Simplified
    double highPrice = rates[0].high;
    double lowPrice  = rates[0].low;
    for(int i = 1; i < 7; i++)
    {
        if(rates[i].high > highPrice)
                highPrice = rates[i].high;
        if(rates[i].low < lowPrice)
                lowPrice = rates[i].low;
    }
 

William Roeder #:

My problem is 

'}' - unexpected end of program
'{' - unbalanced parentheses

 
Pavle Nikic #:either help me figure out this or dont thank you 

How do you want us to help, if you don't understand coding?

First, you a missing a closing brace "}" at the very end.

Second, after you fix that, you will have 7 new errors due to mixing MQL4 and MQL5 code, where "AccountBalance()" and "OrderOpenPrice()" are MQL4 code and not MQL5.

You also have some outdated MQL5 code practices as well.

Was that understandable for you?

 
seeFernando Carreiro #:

How do you want us to help, if you don't understand coding.

First, you a missing a closing brace at the very end.

Second, after you fix that, you will have 7 new errors due to mixing MQL4 and MQL5 code, where "AccountBalance()" and "OrderOpenPrice()" are MQL4 code and not MQL5.

You also have some outdated MQL5 code practices as well.

see that wasnt that hard, thank you for your input i will look into that rn
 
Pavle Nikic #: see that wasnt that hard, thank you for your input i will look into that rn

Actually, it was very hard. It took a very long time (years) and plenty of effort to learn the two MQL languages and to master them both—a lot of dedication to gain that knowledge and skill to be able to answer you.

 
iFernando Carreiro #:

Actually, it was very hard. It took a very long time (years) and plenty of effort to learn the two MQL languages and to master them both—a lot of dedication to gain that knowledge and skill to be able to answer you.

  • Is it easy (or difficult) to …

Fernando Carreiro #:

Actually, it was very hard. It took a very long time (years) and plenty of effort to learn the two MQL languages and to master them both—a lot of dedication to gain that knowledge and skill to be able to answer you.

I have no doubt in my mind that learning those 2 languages was extremely hard(that is not a sarcasm i rly think that), but that wasn't my point when i wrote my comment. But i will end the disquction here since i kinda got it to work, its a bootstrap and in theory it should work, its just not fully automated as i intended :D 

Reason: