Trading Bot doing nothing (Demo)

 

My trading bot doesn't buy or sell anything when i try to use the strategy calculator, other bot's do. Heres the code!

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade/Trade.mqh> // biblioteca trade
//-----------
// INPUTS  
//-----------
input int lote = 100;
input int periodoCurta = 10;
input int periodoLonga = 50;   

//---------------
// VAR GLOBAIS
//---------------
int curtaHandle = INVALID_HANDLE;
int longaHandle = INVALID_HANDLE;

double mediaCurta[];
double mediaLonga[];

CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
      ArraySetAsSeries(mediaCurta,true);
      ArraySetAsSeries(mediaLonga,true);
      
      curtaHandle = iMA(_Symbol,_Period,periodoCurta,0,MODE_SMA,PRICE_CLOSE);
      longaHandle = iMA(_Symbol,_Period,periodoCurta,0,MODE_SMA,PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {   
      if(isNewBar())
         {
         //logica operacional
            // ---------BUSCAR DADOS
           int copied1 = CopyBuffer(curtaHandle,0,0,3,mediaCurta); 
           int copied2 = CopyBuffer(longaHandle,0,0,3,mediaLonga); 
           
           bool sinalCompra=false;
           bool sinalVenda=false;
           if (copied1 == 3 && copied2==3)
            {
            //sinal compra
               if (mediaCurta[1]>mediaLonga[1] && mediaCurta[2]<mediaLonga[2])
               {
                  sinalCompra=true;
               }
               //sinal venda
               if(mediaCurta[1]<mediaLonga[1] && mediaCurta[2]>mediaLonga[2])
               {
                  sinalVenda=true;
               }
               //-----VERIFICAR SE ESTOU POSICIONADO
               bool comprado = false;
               bool vendido = false;
               if(PositionSelect(_Symbol))
               {
               //se a posiçao for comprada
                  if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
                  {
                     comprado=true;
                  }
                  //se a posiçao for vendida
                  if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
                  {
                     vendido=true;
                  }
                  //--------LÓGICA DE ROTEAMENTO
                  if(!comprado && !vendido)
                  {
           //   ---sinal compra
                     if(sinalCompra)
                     {
                        trade.Buy(lote,_Symbol,0,0,0,"Compra a mercado");
                     }
              //  ---sinal venda
                     if(sinalVenda)
                     {
                     trade.Sell(lote,_Symbol,0,0,0,"Venda a Mercado");
                     }
                   }
            else
            {
               //----estou comprado
               if(comprado)
               {
                  if(sinalVenda)
                  {
                     trade.Sell(lote*2,_Symbol,0,0,0,"Virada de mão(compra->venda)");
                  } 
               }
               //----- estou vendido
                else if (vendido)
                {
                  if(sinalCompra)
                  {
                     trade.Buy(lote*2,_Symbol,0,0,0,"Virada de mão(venda->compra)");
                  }
                }
            }
            }
         }
  }
}
          
//+------------------------------------------------------------------+
   bool isNewBar()
   {
      static datetime last_time=0;
      datetime lastbar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
 
   if(last_time==0)
   {
      last_time = lastbar_time;
      return(false);
   } 
   if (last_time!=lastbar_time)
   {
   last_time=lastbar_time;
   return(true);
   }
   return(false);
   }

 

It appears that these two lines are producing the exact same data

      curtaHandle = iMA(_Symbol,_Period,periodoCurta,0,MODE_SMA,PRICE_CLOSE);
      longaHandle = iMA(_Symbol,_Period,periodoCurta,0,MODE_SMA,PRICE_CLOSE);


So, curtaHandle will always equal longaHandle.

To troubleshoot further you can comment out the most complicated lines in your code.  Then add a Print Statement to what is left and see if the code is running.   So comment out everything in your first IF statement , Add a print comment.  then look in the journal to see if the code enters the print statement ( ie print the value of curtaHandle and longaHandle). 

A further question is : Does curtaHandle and longaHandle even get to 3?


Hope this helps,

Chris

 
Chris Pinter #:

It appears that these two lines are producing the exact same data


So, curtaHandle will always equal longaHandle.

To troubleshoot further you can comment out the most complicated lines in your code.  Then add a Print Statement to what is left and see if the code is running.   So comment out everything in your first IF statement , Add a print comment.  then look in the journal to see if the code enters the print statement ( ie print the value of curtaHandle and longaHandle). 

A further question is : Does curtaHandle and longaHandle even get to 3?


Hope this helps,

Chris

Thank you for the reply, how would i print? New to MQL5.

Edit: printed the code by using: printf(curtaHandle,longaHandle); Commented out the entire IF statement, Where do i acess the print? Not seeing anything.  

 

If you are new you maybe should read:

Quickstart for newbies: https://www.mql5.com/en/articles/496
and: https://www.mql5.com/en/articles/100

Beside that if your EAQ compiles without any error bus does not do what it should debug it:

    https://www.metatrader5.com/en/metaeditor/help/development/debug
    https://www.mql5.com/en/articles/654
    Die Fehlerverarbeitung und Protokollierung in MQL5: https://www.mql5.com/en/articles/2041
    https://www.mql5.com/en/articles/272
    Fehler finden und Protokollierung  https://www.mql5.com/en/articles/150
    Can i get any Free Tutorial for Forex Trading?  https://www.mql5.com/en/forum/381853#comment_25845157

My personal recommendation would be, look (search) for something that could be close to your idea and start from there by changing this according to your idea.

Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready even for you.
You can be much faster when you copy, paste, and modify than when you check out all the rookie mistakes. :)

In your case an EA of crossing MA: https://www.mql5.com/en/search#!keyword=crossing%20MA&module=mql5_module_articles

=> https://www.mql5.com/en/articles/10479

Quick Start: Short Guide for Beginners
Quick Start: Short Guide for Beginners
  • www.mql5.com
Hello dear reader! In this article, I will try to explain and show you how you can easily and quickly get the hang of the principles of creating Expert Advisors, working with indicators, etc. It is beginner-oriented and will not feature any difficult or abstruse examples.