EA coding problems by a noob eager to learn.

 

I am using metatrader 5 on a linux manjaro distribution through wine and I am having some coding problems. I am pretty sure the code is right but it keeps giving me compile errors on the OP_BUY and OP_SELL as undeclared identifiers and when I search in the Trade.mqh file I cant find them so including the file won't work. Also on the OrderSend it says wrong parameters count... This one may be me.


It also gives me an open parenthesis error but I can't find the error. I have even passed it through chatgpt and the code seems sound. it also gives me a possible loss of data due to type conversion from double to int.

I am a noob to mql  programming please help.

 
Andrew Kerr:

I am using metatrader 5 on a linux manjaro distribution through wine and I am having some coding problems. I am pretty sure the code is right but it keeps giving me compile errors on the OP_BUY and OP_SELL as undeclared identifiers and when I search in the Trade.mqh file I cant find them so including the file won't work. Also on the OrderSend it says wrong parameters count... This one may be me.


It also gives me an open parenthesis error but I can't find the error. I have even passed it through chatgpt and the code seems sound. it also gives me a possible loss of data due to type conversion from double to int.

I am a noob to mql  programming please help.

the code is not right, hence all the errors

but how do you expect anyone to answer you if you don't post the code?  

use the code button </> to post code

 
Paul Anscombe #:

the code is not right, hence all the errors

but how do you expect anyone to answer you if you don't post the code?  

use the code button </> to post code

I couldn't find a button on the toolbar that showed </> so that I could post the code but here it is  anyway. As I said I am a noob to mql 5 but I have been on many forums and normally some sanctimonius expert will make a noob feel more inadequate than he really is so I am a bit weary of these forums and I first enquire as to if someone can help me so as to guage the willingness of people to help usually I am berated and so please forgive me for being cautious of forums.


<double LotSize = 0.05; // Initial lot size
double LotStep = 0.01; // Step for increasing lot size
double MaxMarginLevel = 20000.0; // Maximum margin level percentage
double CurrentMarginLevel = 0.0; // Current margin level percentage
int MaxTrades = 1; // Maximum number of trades when margin level is below 1000%
int CurrentTrades = 0; // Current number of trades placed
bool TradeAllowed = true; // Flag to allow placing trades

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialization code here
    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Deinitialization code here
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Check conditions for placing a trade
    if (CheckConditions() && TradeAllowed)
    {
        // Place a trade
        PlaceTrade();
    }
}

//+------------------------------------------------------------------+
//| Function to check trading conditions                             |
//+------------------------------------------------------------------+
bool CheckConditions()
{
    // Check conditions for placing a trade
    double boom500 = iCustom(_Symbol, PERIOD_CURRENT, "Boom500", 0, 0);
    return boom500 == 1; // Place trade when boom 500 occurs
}

//+------------------------------------------------------------------+
//| Function to place a trade                                        |
//+------------------------------------------------------------------+
void PlaceTrade()
{
    // Define trade parameters
    double takeProfit = 100 * Point;
    double stopLoss = iATR(_Symbol, PERIOD_CURRENT, 14) * 2;

    // Place a buy trade
    int ticket = OrderSend(_Symbol, OP_BUY, LotSize, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 3, SymbolInfoDouble(_Symbol, SYMBOL_ASK) - stopLoss, SymbolInfoDouble(_Symbol, SYMBOL_ASK) + takeProfit, "Buy trade", 0, 0, clrNONE);

    if (ticket > 0)
    {
        Print("Buy trade placed successfully with ticket: ", ticket);
        CurrentTrades++; // Increase the count of trades placed

        // Check if we need to adjust lot size based on margin level
        CurrentMarginLevel = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); // Update current margin level
        double newLotSize = calculateLotSize(CurrentMarginLevel);
        if (newLotSize > 0)
        {
            LotSize = (newLotSize <= 1.0) ? newLotSize : 1.0; // Set new lot size, ensuring it doesn't exceed 1.0 lot
        }

        // Check if maximum number of trades reached
        if (CurrentMarginLevel >= 20000.0 && CurrentTrades >= 2)
        {
            TradeAllowed = false; // Stop placing trades
        }
    }
    else
    {
        Print("Failed to place buy trade! Error code: ", GetLastError());
    }
}

//+------------------------------------------------------------------+
//| Function to monitor margin level                                  |
//+------------------------------------------------------------------+
void OnMarginLevel()
{
    CurrentMarginLevel = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); // Update current margin level
}

//+------------------------------------------------------------------+
//| Function to calculate lot size based on margin level             |
//+------------------------------------------------------------------+
double calculateLotSize(int marginLevel)
{
    double lotSize;

    if (marginLevel < 100)
    {
        lotSize = 0;
    }
    else if (marginLevel <= 20000)
    {
        int trades = (marginLevel < 10000) ? 1 : 2;
        lotSize = (marginLevel < 10000) ? 0.05 : 0.05 + (0.01 * ((marginLevel / 20000) - 1));
    }
    else
    {
        int trades = 2;
        lotSize = 0.05 + (0.01 * ((marginLevel / 20000) - 1));
    }

    return lotSize;
}>


 
Andrew Kerr #:

I couldn't find a button on the toolbar that showed </> so that I could post the code but here it is  anyway. As I said I am a noob to mql 5 but I have been on many forums and normally some sanctimonius expert will make a noob feel more inadequate than he really is so I am a bit weary of these forums and I first enquire as to if someone can help me so as to guage the willingness of people to help usually I am berated and so please forgive me for being cautious of forums.


//+------------------------------------------------------------------+
//| Function to place a trade                                        |
//+------------------------------------------------------------------+
void PlaceTrade()
{
    // Define trade parameters
    double takeProfit = 100 * Point;       NO SUCH THING USE     _Point  or Point()
    double stopLoss = iATR(_Symbol, PERIOD_CURRENT, 14) * 2;

and OrderSend requires an MqlTradeRequest structure as per the documentation

https://www.mql5.com/en/docs/trading/ordersend

Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
The OrderSend() function is used for executing trade operations by sending requests to a trade server. Parameters request [in]  Pointer to a...
 
Paul Anscombe #:

and OrderSend requires an MqlTradeRequest structure as per the documentation

https://www.mql5.com/en/docs/trading/ordersend

Thank you for your time and help I really appreciate it.
 
Andrew Kerr #:
Thank you for your time and help I really appreciate it.

you are welcome.   but please don't use ChatGPT it is terrible at MT4/5 coding and you won't get help with such code here.