How to start with MQL5 - page 11

 
Bangun Prastyo :
Dear Vladimir, can you help me to make an EA with only fractal indicator signals. and wear SL and TP and trailing. for scalping trading. hopefully it can provide better colors in the mql5 world. You are a very good programmer.

 10:21

 Dear Vladimir, you can help me to make an Expert Advisor based on signals from fractal indicators only. and use SL, TP and trailing. for scalping. Hopefully it will increase the color in the MQL5 world. You are a very good programmer.

 10:22

 hope you can help me just to share. with fractal indicator codebase only for each transaction. if there is a fractal sign will open if not advance. and placing pending orders above the fractal line from the breakout area.

 10:26

 In fact, every time there is a transaction, the fracture will  function . For example the fractal above, the trade will sell dan SL and TP.

Good day.

What indicator do you want to create an Expert Advisor based on?

 
Vladimir Karputov:

Keep records of open charts (more precisely, keep records of not chart numbers, but records of actions: the symbol "such and such", the timeframe "such and such" is already open.

i've been trying but i can't find out how, i'm using a multi symbol and multi timeframe expert advisor so its hard for me to check symbol and timeframe for over 100 charts, is there a easier way ?
 
Ahmad861 :
i've been trying but i can't find out how, i'm using a multi symbol and multi timeframe expert advisor so its hard for me to check symbol and timeframe for over 100 charts, is there a easier way ?

The terminal can open a maximum of 100 charts (no more).

So, before applying the chart template, you need to bypass all charts and check: "Is the chart on the" SYMBOL "symbol and on the" TIMEFRAME "timeframe already open"?

An example of traversing all charts can be taken from here: ChartNext.

Final function:

//+------------------------------------------------------------------+
//| Is Chart Open                                                    |
//+------------------------------------------------------------------+
bool IsChartOpen(const string symbol, const ENUM_TIMEFRAMES timeframe)
  {
//--- variables for chart ID
   long currChart,prevChart=ChartFirst();
   int i=0,limit=100;
   while(i<limit)// We have certainly not more than 100 open charts
     {
      currChart=ChartNext(prevChart); // Get the new chart ID by using the previous chart ID
      if(currChart<0)
         break;          // Have reached the end of the chart list
      if(ChartSymbol(currChart)==symbol)
         if(ChartPeriod(currChart)==timeframe)
            return(true);
      i++;// Do not forget to increase the counter
     }
//---
   return(false);
  }


Call example:

   if(!IsChartOpen("USDJPY",PERIOD_D1))
     ChartApplyTemplate...
Documentation on MQL5: Chart Operations / ChartNext
Documentation on MQL5: Chart Operations / ChartNext
  • www.mql5.com
ChartNext - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov:

The terminal can open a maximum of 100 charts (no more).

So, before applying the chart template, you need to bypass all charts and check: "Is the chart on the" SYMBOL "symbol and on the" TIMEFRAME "timeframe already open"?

An example of traversing all charts can be taken from here: ChartNext.

Final function:


Call example:

I've tried this way, still doesn't work
 
Ahmad861 :
I've tried this way, still doesn't work

I don't believe anyone if there is no code and no description of actions :)

You did not show the code - you did not describe your actions - you only hear "it does not work". I do not believe you.
 

An example of working with iCustom - we get the indicator data in the EA [data folder]\MQL5\Indicators\Examples\MACD.mq5

Code: iCustom iMACD value on chart.mq5

The custom MACD indicator is located in the folder:

Remember the rule of working with indicators in MQL5: an indicator handle is created in OnInit. In the future, the indicator handle, using CopyBuffer, is used to receive data from the indicator.

//+------------------------------------------------------------------+
//|                                 iCustom iMACD value on chart.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
#property tester_indicator "Examples\\MACD"
//--- input parameters
input int                  Inp_MACD_fast_ema_period= 8;           // MACD: period for Fast average calculation
input int                  Inp_MACD_slow_ema_period= 17;          // MACD: period for Slow average calculation
input int                  Inp_MACD_signal_period  = 9;           // MACD: period for their difference averaging
input ENUM_APPLIED_PRICE   Inp_MACD_applied_price  = PRICE_CLOSE; // MACD: type of price
//---
int      handle_iCustom;                     // variable for storing the handle of the iCustom indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMACD ([data folder]\MQL5\Indicators\Examples\MACD.mq5)
   handle_iCustom=iCustom(Symbol(),Period(),"Examples\\MACD",Inp_MACD_fast_ema_period,Inp_MACD_slow_ema_period,
                          Inp_MACD_signal_period,Inp_MACD_applied_price);
//--- if the handle is not created
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(handle_iCustom!=INVALID_HANDLE)
      IndicatorRelease(handle_iCustom);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double main[],signal[];
   ArraySetAsSeries(main,true);
   ArraySetAsSeries(signal,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iCustom,MAIN_LINE,start_pos,count,main) ||
      !iGetArray(handle_iCustom,SIGNAL_LINE,start_pos,count,signal))
     {
      return;
     }
//---
   string text_main="Main |",text_signal="Signal |";
   for(int i=count-1; i>=0; i--)
     {
      text_main=text_main+" #"+IntegerToString(i)+" "+DoubleToString(main[i],Digits()+1)+" | ";
      text_signal=text_signal+" #"+IntegerToString(i)+" "+DoubleToString(signal[i],Digits()+1)+" | ";
     }
   Comment(text_main,"\n",text_signal);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov:

I don't believe anyone if there is no code and no description of actions :)

You did not show the code - you did not describe your actions - you only hear "it does not work". I do not believe you.
What would i get by lying ? if it doesn't work, it doesn't work, it's totally fine
 
Ahmad861 :
What would i get by lying ? if it doesn't work, it doesn't work, it's totally fine

Your problem is that you cannot formulate the problem. I'm not a wizard or telepathic - but I made an attempt to read your mind ( ). If you say "it doesn't work for me" - please put yourself in my place: look from the outside - you didn't say anything, but say that nothing works.

If you want help - describe your problem, show the code, tell us what exactly does not work.

How to start with MQL5
How to start with MQL5
  • 2020.09.06
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 

Remove all symbols from the Market Watch

Code: Remove all symbols.mq5

//+------------------------------------------------------------------+
//|                                           Remove all symbols.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- remove all symbols from the Market Watch
   int symbols_total=SymbolsTotal(true);
   for(int i=symbols_total-1; i>=0; i--)
     {
      string symbol_name=SymbolName(i,true);
      SymbolSelect(symbol_name,false);
     }
//---
  }
Files:
 

Example: how to get the value of the 'iAO' indicator in an advisor

Code: iAO Get Value.mq5

When working in MQL5, remember the main rule: the indicator handle (in this example, it is the 'handle_iAO' variable) must be created ONLY ONCE and IT IS NECESSARY TO DO IN OnInit ()

The code below shows an example of how to get the indicator handle in the EA and how to access the indicator data in the EA. For example, I additionally placed an indicator on the chart so that you can visually compare the indicator readings and the data obtained in the EA.

Code:

//+------------------------------------------------------------------+
//|                                                iAO Get Value.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.001"
#property description "Example: how to get the value of the 'iAO' indicator in an advisor"
//--- input parameters
input group                "AO"
input ENUM_TIMEFRAMES      Inp_AO_period        = PERIOD_CURRENT;    // AO: timeframe
input group                "Additional features"
input bool                 InpPrintLog          = false;             // Print log
//---
int      handle_iAO;                            // variable for storing the handle of the iAO indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iAO
   handle_iAO=iAO(Symbol(),Inp_AO_period);
//--- if the handle is not created
   if(handle_iAO==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iAO indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Inp_AO_period),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(handle_iAO!=INVALID_HANDLE)
      IndicatorRelease(handle_iAO);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double ao[];
   ArraySetAsSeries(ao,true);
   int start_pos=0,count=2;
   if(!iGetArray(handle_iAO,0,start_pos,count,ao))
      return;
//---
   string text="";
   int limit=(count>2)?2:count;
   for(int i=0; i<limit; i++)
     {
      text=text+
           " bar #"+IntegerToString(i)+": "+
           " AO "+DoubleToString(ao[i],Digits()+1)+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      if(InpPrintLog)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      if(InpPrintLog)
         PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                     __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+


Result:

iAO Get Value

Pic. 1. iAO Get Value


iAO Get Value

Pic. 2. iAO Get Value

Files:
Reason: