Beginer in mql5

 

Hello, i'm begin to create my first EA.

BUT i try now with Ichimoku indocator.

my code

//+------------------------------------------------------------------+
//|                                               Ichimoku_Siuko.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

// Librairie
#include <Trade\Trade.mqh>
CTrade         trade;

// Input
input double   SLPercentage = 0.25;    // Taille du SL en pourcentage

// Déclaration des variables pour les indicateurs
int ichiHandle;
double IchiBuffer_Tenkan_sen_Buffer[],IchiBuffer_Kijun_sen_Buffer[],IchiBuffer_Senkou_Span_A_Buffer[],IchiBuffer_Senkou_Span_B_Buffer[],IchiBuffer_Chinkou_Span_Buffer[];

// Déclaration des autres variable

double Ask, Bid, SL, TP;
string typeLastTrade = "Long";
bool canLong, canShort;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   // Charger les indicateurs
   
   ichiHandle = iIchimoku(_Symbol,PERIOD_CURRENT,9,26,52);

   ArraySetAsSeries(IchiBuffer_Tenkan_sen_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Kijun_sen_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Senkou_Span_A_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Senkou_Span_B_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Chinkou_Span_Buffer, true);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CopyBuffer(ichiHandle, 0, 0, 1, IchiBuffer_Tenkan_sen_Buffer);
   CopyBuffer(ichiHandle, 1, 0, 1, IchiBuffer_Kijun_sen_Buffer);
   CopyBuffer(ichiHandle, 2, 0, 1, IchiBuffer_Senkou_Span_A_Buffer);
   CopyBuffer(ichiHandle, 3, 0, 1, IchiBuffer_Senkou_Span_B_Buffer);
   CopyBuffer(ichiHandle, 4, 0, 1, IchiBuffer_Chinkou_Span_Buffer);
   
   
   // Price
   Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   
   // Filtre
   if(Ask > IchiBuffer_Tenkan_sen_Buffer[0] && 
      Ask > IchiBuffer_Kijun_sen_Buffer[0] && 
      Ask > IchiBuffer_Senkou_Span_A_Buffer[25] && 
      Ask > IchiBuffer_Senkou_Span_B_Buffer[25] && 
      PositionsTotal() == 0)
   {
      canLong = true;
   }
   else
   {
      canLong = false;
   }
   
   if(Ask < IchiBuffer_Tenkan_sen_Buffer[0] && 
      Ask < IchiBuffer_Kijun_sen_Buffer[0] && 
      Ask < IchiBuffer_Senkou_Span_A_Buffer[25] && 
      Ask < IchiBuffer_Senkou_Span_B_Buffer[25] && 
      PositionsTotal() == 0)
   {
      canShort = true;
   }
   else
   {
      canShort = false;
   }

   // Signaux
   
   if(canShort)
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = NormalizeDouble(Bid - SLPercentage / 100 * Bid, _Digits);
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
   }
   
   if(canLong)
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = NormalizeDouble(Ask + SLPercentage / 100 * Ask, _Digits);
      trade.Buy(1.0, _Symbol, Bid, SL, TP);
   }
  }
//+------------------------------------------------------------------+


When i try IchiBuffer_Senkou_Span_B_Buffer[25] i have an error on test because i don't have a " IchiBuffer_Senkou_Span_B_Buffer[25] "


how i can load the 26 past tick ?

 

Read the CopyBuffer help

int  CopyBuffer(
   int       indicator_handle,     // indicator handle
   int       buffer_num,           // indicator buffer number
   int       start_pos,            // start position
   int       count,                // amount to copy
   double    buffer[]              // target array to copy
   );
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov:

Read the CopyBuffer help

Thank so now my open trade is always instant he don't wait the cloture of tick. (ex: Timeframe H1)

How i can do that ?

 
Siuko :

Thank so now my open trade is always instant he don't wait the cloture of tick. (ex: Timeframe H1)

How i can do that ?

Why do you think so?

Look carefully at the help:

    int        count,                 // amount to copy
 
Vladimir Karputov :

Pourquoi penses-tu ça?

Regardez attentivement l'aide:

Because in the backtest it opens the positions in the middle of the candles, I want that to open the position at the close of the candle.

 
Siuko :

Because in the backtest it opens the positions in the middle of the candles, I want that to open the position at the close of the candle.

I have already told you twice where you should look. You should now read the help and change your code. And only after you change your code - you can ask a question.

So I repeat - read the help:

   int        count,                 // amount to copy


:: Remind you - you initially complained about the error:

Forum on trading, automated trading systems and testing trading strategies

Beginer in mql5

Siuko, 2021.01.19 21:53

***

When i try IchiBuffer_Senkou_Span_B_Buffer[25] i have an error on test because i don't have a " IchiBuffer_Senkou_Span_B_Buffer[25] "

how i can load the 26 past tick ?

- so why don't you correct this mistake? I have already prompted you three times.
 
Vladimir Karputov:

Je vous ai déjà dit deux fois où vous devriez regarder. Vous devriez maintenant lire l'aide et changer votre code. Et ce n'est qu'après avoir changé votre code que vous pouvez poser une question.

Alors je répète - lisez l'aide:


:: Rappelez-vous - vous vous êtes initialement plaint de l'erreur:

- alors pourquoi ne corrigez-vous pas cette erreur? Je vous ai déjà invité trois fois.
//+------------------------------------------------------------------+
//|                                               Ichimoku_Siuko.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

// Librairie
#include <Trade\Trade.mqh>
CTrade         trade;

// Input
input double   SLPercentage = 0.25;    // Taille du SL en pourcentage

// Déclaration des variables pour les indicateurs
int ichiHandle;
double IchiBuffer_Tenkan_sen_Buffer[],IchiBuffer_Kijun_sen_Buffer[],IchiBuffer_Senkou_Span_A_Buffer[],IchiBuffer_Senkou_Span_B_Buffer[],IchiBuffer_Chinkou_Span_Buffer[];

// Déclaration des autres variable

double Ask, Bid, SL, TP;
string typeLastTrade = "Long";
bool canLong, canShort;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   // Charger les indicateurs
   
   ichiHandle = iIchimoku(_Symbol,PERIOD_CURRENT,9,26,52);

   ArraySetAsSeries(IchiBuffer_Tenkan_sen_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Kijun_sen_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Senkou_Span_A_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Senkou_Span_B_Buffer, true);
   ArraySetAsSeries(IchiBuffer_Chinkou_Span_Buffer, true);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CopyBuffer(ichiHandle, 0, 0, 2, IchiBuffer_Tenkan_sen_Buffer);
   CopyBuffer(ichiHandle, 1, 0, 2, IchiBuffer_Kijun_sen_Buffer);
   CopyBuffer(ichiHandle, 2, 0, 26, IchiBuffer_Senkou_Span_A_Buffer);
   CopyBuffer(ichiHandle, 3, 0, 26, IchiBuffer_Senkou_Span_B_Buffer);
   CopyBuffer(ichiHandle, 4, 0, 1, IchiBuffer_Chinkou_Span_Buffer);
   
   
   // Price
   Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   
   // Filtre
   if(Ask > IchiBuffer_Tenkan_sen_Buffer[1] && 
      Ask > IchiBuffer_Kijun_sen_Buffer[1] && 
      Ask > IchiBuffer_Senkou_Span_A_Buffer[1] && 
      Ask > IchiBuffer_Senkou_Span_B_Buffer[1] && 
      PositionsTotal() == 0)
   {
      canLong = true;
   }
   else
   {
      canLong = false;
   }
   
   if(Ask < IchiBuffer_Tenkan_sen_Buffer[1] && 
      Ask < IchiBuffer_Kijun_sen_Buffer[1] && 
      Ask < IchiBuffer_Senkou_Span_A_Buffer[1] && 
      Ask < IchiBuffer_Senkou_Span_B_Buffer[1] && 
      PositionsTotal() == 0)
   {
      canShort = true;
   }
   else
   {
      canShort = false;
   }

   // Signaux
   
   if(canShort)
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = NormalizeDouble(Bid - SLPercentage / 100 * Bid, _Digits);
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
   }
   
   if(canLong)
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = NormalizeDouble(Ask + SLPercentage / 100 * Ask, _Digits);
      trade.Buy(1.0, _Symbol, Bid, SL, TP);
   }
  }
//+------------------------------------------------------------------+

I change the code, it works very well by adjusting with the CopyBuffer. I have no more errors at launch, it's perfect. Now the problem I am having is that the position does not open at the candle close, but at the instant "T" where my condition is true. I would like this to happen only when the candle closes. I wonder if we should also create the "candles" indicator? and ask to close the last candle instead of my "Ask" in the condition.

Here he take position "Sell", but the candle is not close:

Where the candle close, the signal is not OK for my condition.


 

This is because you are working on every tick. And you only need to work when a new bar is born.

Example: Example: a simple Expert Advisor at the intersection of two iMA (Moving Average, MA) indicators.

So, the EA will look for a signal only at the moment of a new bar birth. The code responsible for finding a new bar:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
***
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(m_symbol.Name(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
***
  }
How to start with MQL5
How to start with MQL5
  • 2020.09.17
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Vladimir Karputov :

C'est parce que vous utilisez sur chaque tick. Et vous n’avez pas besoin de travailler que vous ne voulez plus bar est né.

Exemple:  Exemple: un simple Expert Advisor à l'intersection de deux indicateurs iMA (Moving Average, MA) .

Ainsi, l'EA ne cherchera un signal qu'au moment de la naissance d'un nouveau bar. Le code chargé de trouver une nouvelle barre:

Wow, your post too good. Thank you, there are all the answers, I didn't know!

Thank you and have a good day, it's working very well :)

Reason: