Change of color - page 2

 

Helou,another day another question.

I have tried to create a Fibo Retracements in my chart, by using the ObjectCreate function. I have found two anchor points for Fibo, both with price and time and use them as a parameters but nothing happend in a chart....

Than I looked in Reference to OBJ_FIBO and there is a really long code, should I really put all the code inside mine or is there easy way? I thought, that MetaTrader will create FIBO, when i use ObjectCretate() with preset parameters and  I will change properties of it.....

void OnTick()
  {
   datetime FiboTime[];
   ArraySetAsSeries(FiboTime,true);
   CopyTime(_Symbol,_Period,0,25,FiboTime);
   
   double low[];
   ArraySetAsSeries(low,true);

   CopyLow(_Symbol,_Period,0,25,low);
   int minIdx = ArrayMinimum(low,0,WHOLE_ARRAY);// mám Low pozici
   double lowest = low[minIdx];//mám low price low
   NormalizeDouble(lowest,5);
   datetime LowestTime=FiboTime[minIdx];// mám Low Time
   
   double high[];
   ArraySetAsSeries(high,true);

   CopyHigh(_Symbol,_Period,0,25,high);
   int maxIdx = ArrayMaximum(high,0,WHOLE_ARRAY);//mám High pozici
   double highest = high[maxIdx];//mám high price high
   NormalizeDouble(highest,5);
   datetime HighestTime=FiboTime[maxIdx];//mám High Time
   
   //-- Fibonaciho retracement
   string FIBO;
   ObjectCreate(0,FIBO,OBJ_FIBO,0,LowestTime,lowest,HighestTime,highest);
   ChartRedraw(0);


   
 
vanuatu:

Helou,another day another question.

I have tried to create a Fibo Retracements in my chart, by using the ObjectCreate function. I have found two anchor points for Fibo, both with price and time and use them as a parameters but nothing happend in a chart....

Than I looked in Reference to OBJ_FIBO and there is a really long code, should I really put all the code inside mine or is there easy way? I thought, that MetaTrader will create FIBO, when i use ObjectCretate() with preset parameters and  I will change properties of it.....

No you don't have to put all the code of the documentation in your code, it's better to understand what you are doing

ObjectCreate(0,FIBO,OBJ_FIBO,0,LowestTime,lowest,HighestTime,highest);

ObjectCreate() is a function you have to check the return value to see if an error occurs, if so then print the error code with GetLastError().

I suggest you to also print the value LowestTime, lowest, HighestTime and highest, as you got them you don't check the return value either (CopyXXXX).

 

Helou,

i am back from my job, so I continue with MQL and have some follow up questions :)


Thanks for your advice, a went through my code, replenished return operator and try to print everything just to be sure that it is ok step by step.

From CreateObject() I received Error 5040 - Damaged parameter of string type, i have no idea how to solve it..........


and my second question is (sorry it is hard to exlain it in english for me) : I found that OnTick() event is only for Expert advisor, it doesnt work for custom indicators.....

I have started coding with custom indicator, my two MA, and set

property indicator_..... than i put some code in OnTick() but the code doesnt do enything....... I tried to open New empty EA and copy hole my code, but somehow the Meta Trader realize that it is custom indicator, is it because of the proeprty settings filled with indicators??


the OnTick part behave like it is not there....


//+------------------------------------------------------------------+
//|                                                       pokus1.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot MAFast
#property indicator_label1  "MAFast"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlack
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot MASlow
#property indicator_label2  "MASlow"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

//--- input parameters
input int                   MAFastPeriod= 14;
input int                   MAFastShift = 0;
input ENUM_MA_METHOD        MAFastMethod = MODE_EMA;
input ENUM_APPLIED_PRICE    MAFastPrice = PRICE_CLOSE;

input int                   MASlowPeriod = 30;
input int                   MASlowShift = 0;
input ENUM_MA_METHOD        MASlowMethod = MODE_EMA;
input ENUM_APPLIED_PRICE    MASlowPrice = PRICE_CLOSE;

//--- indicator buffers
double         MAFastBuffer[];
double         MASlowBuffer[];
int            MAFast_handle;
int            MASlow_handle;
//----handle pro ask,bid,time pro Fibo

datetime FiboTime[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
  {
 
//--- indicator buffers mapping
   SetIndexBuffer(0,MAFastBuffer,INDICATOR_DATA);
   ArraySetAsSeries(MAFastBuffer,true);
   
   Print(" buffer after SetIndexBuffer() is timeseries = ",
         ArrayGetAsSeries(MAFastBuffer));

   SetIndexBuffer(1,MASlowBuffer,INDICATOR_DATA);
   ArraySetAsSeries(MASlowBuffer,true);

   Print("Indicator after SetIndexBuffer() is timeseries = ",
         ArrayGetAsSeries(MASlowBuffer));

  //---nastavení handle pro Fibo

  ArraySetAsSeries(FiboTime,true);
  CopyTime(_Symbol,_Period,0,50,FiboTime);
  Print(FiboTime[0]);
  
   //--- get MA handle
   
   MAFast_handle=iMA(_Symbol,_Period,MAFastPeriod,MAFastShift,MAFastMethod,MAFastPrice);
   Print(MAFast_handle);
   
   MASlow_handle=iMA(_Symbol,_Period,MASlowPeriod,MASlowShift,MASlowMethod,MASlowPrice);
   Print(MASlow_handle);
   
   return(INIT_SUCCEEDED);
  }
  

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
 
   double low[];
   ArraySetAsSeries(low,true); 
   
   CopyLow(_Symbol,_Period,0,25,low);
   Print(low[0]);
   
     
   int minIdx = ArrayMinimum(low,0,WHOLE_ARRAY);// mám Low pozici
   double lowest = low[minIdx];//mám low price low
   NormalizeDouble(lowest,5);
   
   Print(lowest);
   datetime LowestTime=FiboTime[minIdx];// mám Low Time
   Print(LowestTime);
   
   double high[];
   ArraySetAsSeries(high,true);

   CopyHigh(_Symbol,_Period,0,25,high);
   int maxIdx = ArrayMaximum(high,0,WHOLE_ARRAY);//mám High pozici
   double highest = high[maxIdx];//mám high price high
   NormalizeDouble(highest,5);
   Print("highest:",highest);
   datetime HighestTime=FiboTime[maxIdx];//mám High Time
   Print("HighestTime:",HighestTime);
   
   ResetLastError();
   
   //-- Fibonaciho retracement
   string FIBO;
   if(!ObjectCreate(0,FIBO,OBJ_FIBO,0,LowestTime,lowest,HighestTime,highest))
    {
    Print(__FUNCTION__,": failed to create \"Fibonacci Retracement\"! Error code = ",GetLastError());
    return;
     }
   ResetLastError();
   ChartRedraw(0);
   

   
//---
   
  }
  //+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- check if all data calculated
   if(BarsCalculated(MAFast_handle)<rates_total) return(0);
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<=0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      //--- last value is always copied
      to_copy++;
     }
//--- try to copy
   if(CopyBuffer(MAFast_handle,0,0,to_copy,MAFastBuffer)<=0) return(0);
   if(CopyBuffer(MASlow_handle,0,0,to_copy,MASlowBuffer)<=0) return(0);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Helou,

I feel like fool, I declared string FIBO, but did not assign a value, now the problem is solved:)

BUt still the second part of my previous question is valid.

 
Helou,
I need help pleas:)
my question is (sorry it is hard to exlain it in english for me) : I found that OnTick() event is only for Expert advisor, it doesnt work for custom indicators.....

I have started coding with custom indicator, my two MA, and set

#property indicator_..... than i put some code in OnTick() but the code doesnt do enything....... I tried to open New empty EA and copy hole my code,
 but somehow the Meta Trader realize that it is custom indicator, is it because of the proeprty settings filled with indicators??


the OnTick part behave like it is not there....


//+------------------------------------------------------------------+
//|                                                       pokus1.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot MAFast
#property indicator_label1  "MAFast"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlack
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot MASlow
#property indicator_label2  "MASlow"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

//--- input parameters
input int                   MAFastPeriod= 14;
input int                   MAFastShift = 0;
input ENUM_MA_METHOD        MAFastMethod = MODE_EMA;
input ENUM_APPLIED_PRICE    MAFastPrice = PRICE_CLOSE;

input int                   MASlowPeriod = 30;
input int                   MASlowShift = 0;
input ENUM_MA_METHOD        MASlowMethod = MODE_EMA;
input ENUM_APPLIED_PRICE    MASlowPrice = PRICE_CLOSE;

//--- indicator buffers
double         MAFastBuffer[];
double         MASlowBuffer[];
int            MAFast_handle;
int            MASlow_handle;
//----handle pro ask,bid,time pro Fibo

datetime FiboTime[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
  {
 
//--- indicator buffers mapping
   SetIndexBuffer(0,MAFastBuffer,INDICATOR_DATA);
   ArraySetAsSeries(MAFastBuffer,true);
   
   Print(" buffer after SetIndexBuffer() is timeseries = ",
         ArrayGetAsSeries(MAFastBuffer));

   SetIndexBuffer(1,MASlowBuffer,INDICATOR_DATA);
   ArraySetAsSeries(MASlowBuffer,true);

   Print("Indicator after SetIndexBuffer() is timeseries = ",
         ArrayGetAsSeries(MASlowBuffer));

  //---nastavení handle pro Fibo

  ArraySetAsSeries(FiboTime,true);
  CopyTime(_Symbol,_Period,0,50,FiboTime);
  Print(FiboTime[0]);
  
   //--- get MA handle
   
   MAFast_handle=iMA(_Symbol,_Period,MAFastPeriod,MAFastShift,MAFastMethod,MAFastPrice);
   Print(MAFast_handle);
   
   MASlow_handle=iMA(_Symbol,_Period,MASlowPeriod,MASlowShift,MASlowMethod,MASlowPrice);
   Print(MASlow_handle);
   
   return(INIT_SUCCEEDED);
  }
  

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
 
   double low[];
   ArraySetAsSeries(low,true); 
   
   CopyLow(_Symbol,_Period,0,25,low);
   Print(low[0]);
   
     
   int minIdx = ArrayMinimum(low,0,WHOLE_ARRAY);// mám Low pozici
   double lowest = low[minIdx];//mám low price low
   NormalizeDouble(lowest,5);
   
   Print(lowest);
   datetime LowestTime=FiboTime[minIdx];// mám Low Time
   Print(LowestTime);
   
   double high[];
   ArraySetAsSeries(high,true);

   CopyHigh(_Symbol,_Period,0,25,high);
   int maxIdx = ArrayMaximum(high,0,WHOLE_ARRAY);//mám High pozici
   double highest = high[maxIdx];//mám high price high
   NormalizeDouble(highest,5);
   Print("highest:",highest);
   datetime HighestTime=FiboTime[maxIdx];//mám High Time
   Print("HighestTime:",HighestTime);
   
   ResetLastError();
   
   //-- Fibonaciho retracement
   string FIBO;
   if(!ObjectCreate(0,FIBO,OBJ_FIBO,0,LowestTime,lowest,HighestTime,highest))
    {
    Print(__FUNCTION__,": failed to create \"Fibonacci Retracement\"! Error code = ",GetLastError());
    return;
     }
   ResetLastError();
   ChartRedraw(0);
   

   
//---
   
  }
  //+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- check if all data calculated
   if(BarsCalculated(MAFast_handle)<rates_total) return(0);
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<=0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      //--- last value is always copied
      to_copy++;
     }
//--- try to copy
   if(CopyBuffer(MAFast_handle,0,0,to_copy,MAFastBuffer)<=0) return(0);
   if(CopyBuffer(MASlow_handle,0,0,to_copy,MASlowBuffer)<=0) return(0);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+---------------------------
 
vanuatu:

Of course. #property indicatorXXX directive are only for an indicator. You can't use it within an EA.

And OnTick() is not used for indicator. Please check the documentation...

https://www.mql5.com/en/docs/runtime/running

Documentation on MQL5: MQL5 programs / Program Running
Documentation on MQL5: MQL5 programs / Program Running
  • www.mql5.com
MQL5 programs / Program Running - Documentation on MQL5
 
angevoyageur:

Of course. #property indicatorXXX directive are only for an indicator. You can't use it within an EA.

And OnTick() is not used for indicator. Please check the documentation...

https://www.mql5.com/en/docs/runtime/running

OK now it is a lot clear. Follow up question: is possible to try to put the part of code,rellated to indicators ( 2 EMA) in to include file....

and call the #include file from EA?

 
vanuatu:

OK now it is a lot clear. Follow up question: is possible to try to put the part of code,rellated to indicators ( 2 EMA) in to include file....

and call the #include file from EA?

Probably but it's not the best thing to do in my opinion.

You have to use iCustom() in your EA to get the value of your indicator(s).

Reason: