why does this doesn't do trades?

 

no errors, but no trades are done while testing ... why?


//#include <MetaTrader5\MetaTrader5.mqh>
#include <Trade\Trade.mqh>
#include <Trade\AccountInfo.mqh>
#include <Object.mqh>
// Resto del código de tu programa


// Definir los parámetros de entrada
input int MA_Period = 20; // _Period de la MMA
input int ADX_Period = 14; // _Period del ADX
input int ADX_threshold = 25; // Umbral del ADX
input double StopLoss = 50; // Stop Loss en pips
input double TakeProfit = 100; // Take Profit en pips

// Variables globales
double last_ma = 0; // Valor de la MMA anterior
double last_adx = 0; // Valor del ADX anterior
double current_ma = 0; // Valor de la MMA actual
double current_adx = 0; // Valor del ADX actual




double Close[];
int limit = 1;
int start = 0;

// To be used for getting recent/latest price quotes


CTrade         m_trade;  
CAccountInfo accountInfo;
    // Definir el instrumento y el marco de tiempo

// Función de inicialización
void OnInit()
{
   
 
    
      
      // Resto de tu código
   
}

// Función de trading
void OnTick()
{
    // Calcular la MMA
    //double 
    current_ma = iMA(_Symbol, _Period, MA_Period, 0, MODE_SMA, PRICE_CLOSE);
    //double last_ma = current_ma[1];
    // Calcular el ADX
    //double 
    current_adx = iADX(_Symbol, _Period, ADX_Period);
    //double last_adx = current_adx[1] ;
    double Close[]={ CopyClose(_Symbol,_Period, start, limit, Close)} ;

    // Comprar si la MMA cruza por encima del precio y el ADX está por encima del umbral
    if (last_ma < Close[1] && current_ma > Close[0] && last_adx < ADX_threshold && current_adx > ADX_threshold)
    {
        // Calcular el tamaño de la posición en función del riesgo
        double risk = accountInfo.FreeMargin() * 0.01;
        double lot_size = risk / StopLoss;
        
        
        // generate a buy signal
        MqlTradeRequest request={};
         request.action = TRADE_ACTION_DEAL;
         request.symbol = _Symbol;
         request.volume = lot_size;
         request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         request.type_filling = ORDER_FILLING_IOC;
         request.type = ORDER_TYPE_BUY;
         request.sl = SymbolInfoDouble(_Symbol, SYMBOL_BID) - StopLoss * Point();
         request.tp = SymbolInfoDouble(_Symbol, SYMBOL_BID) + TakeProfit * Point();
         request.comment = "Sell";
         request.magic = 0;
         request.expiration = 0;
         request.type_time = ORDER_TIME_GTC;
         
        
        MqlTradeResult result;
        if (OrderSend(request,result))
        {
            Print("Buy order sent");
        }
        else
        {
            Print("Error sending buy order: ", result.retcode);
        }

        // Comprar con el tamaño de la posición calculado
       
   }
    // Vender si la MMA cruza por debajo del precio y el ADX está por encima del umbral
    if (last_ma > Close[1] && current_ma < Close[0] && last_adx < ADX_threshold && current_adx > ADX_threshold)
    {
        // Calcular el tamaño de la posición en función del riesgo
        double risk = accountInfo.FreeMargin() * 0.01;
        double lot_size = risk / StopLoss;

        // Vender con el tamaño de la posición calculado
        
        // generate a buy signal
        MqlTradeRequest request={};
         request.action = TRADE_ACTION_DEAL;
         request.symbol = _Symbol;
         request.volume = lot_size;
         request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
         request.type_filling = ORDER_FILLING_IOC;
         request.type = ORDER_TYPE_SELL;
         request.sl = SymbolInfoDouble(_Symbol, SYMBOL_ASK) + StopLoss * Point();
         request.tp = SymbolInfoDouble(_Symbol, SYMBOL_ASK) - TakeProfit * Point();
         request.comment = "Sell";
         request.magic = 0;
         request.expiration = 0;
         request.type_time = ORDER_TIME_GTC;
         
        
        MqlTradeResult result;
        if (OrderSend(request,result))
        {
            Print("Buy order sent");
        }
        else
        {
            Print("Error sending buy order: ", result.retcode);
        }
    }
 

    // Actualizar los valores de la MMA y el ADX
    last_ma = current_ma;
    last_adx = current_adx;

}
 

Here are the tools to find the problems in your code:

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
Carl Schreiber #:

Here are the tools to find the problems in your code:

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Thanks, very helpfull


I've noticed its the variable close ... when it reaches the first if, its value changes to 1, and don't know why or how to fix this. Can you tell me whats happening?

 

if (even though commented) close = PRICE_CLOSE it demonstrates you have no idea about MQL5

As I said place the cursor on that colored word PRICE_CLOSE (where it's not commented) and press F1.

You should learn to code:

All: https://www.mql5.com/en/forum/11134
Quickstart for newbies: https://www.mql5.com/de/articles/496
and: https://www.mql5.com/de/articles/100

How to Start with MT5, a summary ! - How to start with Metatrader 5 on MT5 platform
How to Start with MT5, a summary ! - How to start with Metatrader 5 on MT5 platform
  • 2013.03.15
  • www.mql5.com
How to start with mt5 platform : summary. As our topic about " how to start with metatrader 5 " is going to be huge, here you find a summary, with main links. How to start with metatrader and forex , the beginning
 
Normally if you are using CTrade you don't need to do the request structure yourself, CTrade does it in the methods Buy/Sell or OrderSend respectively.
 
void OnTick()
{
    // Calcular la MMA
    //double 
    current_ma = iMA(_Symbol, _Period, MA_Period, 0, MODE_SMA, PRICE_CLOSE);
    //double last_ma = current_ma[1];
    // Calcular el ADX
    //double 
    current_adx = iADX(_Symbol, _Period, ADX_Period);

Those functions do not return a double.

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Reason: