How to get a value or array on indicator to ea?

To add comments, please log in or register
solice
18
solice  
//This my Indicator
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
double    A[];
double    B[];

double smaVal=0.0;
string ValSend="";

void OnInit()
  {
        SetIndexBuffer(0,A,INDICATOR_DATA);
        SetIndexBuffer(1,B,INDICATOR_DATA);
}
double SMA9(const double&array[],int range,int fromIndex)
  {
   double res=0;
//---
//res=array[fromIndex];

   for(int i=fromIndex; i>fromIndex-range && i>=0; i--)
     {
      res=res+array[i];
     }
   res=res/range;
//---
   return(res);
  }
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[])
  {
        for(int i=limit; i<rates_total && !IsStopped(); i++)
     {
        double smaVal=SMA9(low,9,i);
        A[i]=smaVal;
        if(smaVal>open[i]){
                ValSend="GOOD";
        }
        else{
         ValSend="BAD";
        }
        }
}

Hello 

i have a question

I want to get a value string || indicator_buffers to my EA. 

But I don't know what to do now?

This is my code EA

//THIS MY EA

#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

void OnTick()
  {
        // how to get ValSend and array A[]?
	if(ValSend=="Good")
	-->> Buy and using A[]
	else
	-->> Not run
}



Vladimir Karputov
Moderator
207274
Vladimir Karputov  

Here is your indicator:

//+------------------------------------------------------------------+
//|                                                      GoodBad.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot GOOD
#property indicator_label1  "GOOD"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrYellowGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot BAD
#property indicator_label2  "BAD"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrDodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int      Input1=9;
//--- indicator buffers
double GOODBuffer[];
double BADBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,GOODBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,BADBuffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
//--- set the vertical shift of arrows in pixels
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-5);
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,5);
//--- set as an empty value 0
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   if(rates_total<10)
      return(0);
//---
   int limit=prev_calculated-1;
   if(prev_calculated==0)
      limit=9;
   for(int i=limit; i<rates_total && !IsStopped(); i++)
     {
      GOODBuffer[i]=0.0;
      BADBuffer[i]=0.0;
      double smaVal=SMA9(low,9,i);
      if(smaVal>open[i])
        {
         GOODBuffer[i]=high[i];
        }
      else
        {
         BADBuffer[i]=low[i];
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| SMA9                                                             |
//+------------------------------------------------------------------+
double SMA9(const double&array[],int range,int fromIndex)
  {
   double res=0;
//---
   for(int i=fromIndex; i>fromIndex-range && i>=0; i--)
      res=res+array[i];
   res=res/range;
//---
   return(res);
  }
//+------------------------------------------------------------------+


Files:
GoodBad.mq5 7 kb
solice
18
solice  

Thank you. But

I want to get value string and array of indicator to EA?

Ahmet Metin Yilmaz
6211
Ahmet Metin Yilmaz  
Vladimir Karputov
Moderator
207274
Vladimir Karputov  
solice:

Thank you. But

I want to get value string and array of indicator to EA?

And EA:

//+------------------------------------------------------------------+
//|                                                   GoodBad EA.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.00"
//--- input parameters
input int      Input1=9;
//---
int    handle_iCustom;                       // variable for storing the handle of the iCustom indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iCustom
   handle_iCustom=iCustom(Symbol(),Period(),"Test\\GoodBad");
//--- 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 iCustom 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)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double good[],bad[];
   ArraySetAsSeries(good,true);
   ArraySetAsSeries(bad,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iCustom,0,start_pos,count,good) ||
      !iGetArray(handle_iCustom,1,start_pos,count,bad))
     {
      return;
     }
   string text_good="",text_bad="";
   for(int i=count-1; i>=0; i--)
     {
      string temp_good  = (good[i]!=0.0)?"GOOD":"-//-";
      string temp_bad   = (bad[i]!=0.0)?"BAD":"-//-";
      text_good   = text_good + temp_good +" | ";
      text_bad    = text_bad  + temp_bad  +" | ";
     }
   Comment(text_good+"\n"+text_bad);
  }
//+------------------------------------------------------------------+
//| 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);
  }
//+------------------------------------------------------------------+



Files:
solice
18
solice  

Thank you very much!

I have relied on that to implement my ideas.

one more time thank you very much :)

William Roeder
22703
William Roeder  
SMA9 only sums range-1 values.
To add comments, please log in or register