Scripts: Export Indicator's Values v1

 

Export Indicator's Values v1:

After searching for such script, I decided to create mine, and decided to share it with the MQL5 community.

This script exports indicator's values to CSV File along with date and time (you can change the iCustom function parameters to change what indicator to export).

Author: NFTrader

Export Indicator's Values

 
This is a good and simple sample. Think you.
 

Hi,

 Thank you for your work. Is it possible to modify this script to only allow the indicator values of the last bar complete to be exported while backtesting? For example, i am going through the history of EurUsd, i notice a specific bar that i want to record indicator values for. I run your script and it records down the date/time of bar and the indicator value of the last complete bar? Is that possible?

 

Can we also use more than 1 indicator? Or will this require multiple scripts? 

 

Hi,

Thank you for the job.  Unfortunately, when I run the script, no file is created. I updated the script to write in C:/temp/. When i run in debug I can see that the filename is correct and I don't get any error but there is no file.

Any clue ?

 

 

 
Doesn't work.
 

Yes it works. You need to look at ...MQL5\Files path, its there the CSV file.

I´ve modified to save values of OBV (real volume) just change the code below 


Indicator_Directory_And_Name="Examples\\OBV";
IndicatorPeriod=VOLUME_REAL;
 
Ecirba:

Hi,

Thank you for the job.  Unfortunately, when I run the script, no file is created. I updated the script to write in C:/temp/. When i run in debug I can see that the filename is correct and I don't get any error but there is no file.

Any clue ?

 

 


If you check the handle, it will probably is returning -1. (invalid handle)


Print ( "fileHandle Value=", fileHandle);


For some reason MT5 doesnt´like that you save files in different places from default. Just let as original code and it will write in ...MQL5\Files path 

 

Congratulations on the script, I am exporting the values of the volatilitypivot flag to csv, but the script only correctly exports the high values, in the lines that should appear the low values an error appears.

Can someone help me please ?

VolatilityPivot script

#property indicator_chart_window
//--- 4 buffers are used for calculation and drawing the indicator
#property indicator_buffers 4
//--- 4 plots are used
#property indicator_plots   4
//+----------------------------------------------+
//|  Bullish indicator drawing parameters        |
//+----------------------------------------------+
//--- drawing indicator 1 as a line
#property indicator_type1   DRAW_LINE
//--- blue color is used for the indicator line
#property indicator_color1  clrBlue
//--- the indicator 1 line is a dot-dash one
#property indicator_style1  STYLE_DASHDOTDOT
//--- indicator 1 line width is equal to 2
#property indicator_width1  2
//---- displaying of the the indicator label
#property indicator_label1  "Upper VolatilityPivot"
//+----------------------------------------------+
//| Parameters of drawing a bearish indicator    |
//+----------------------------------------------+
//--- drawing indicator 2 as a line
#property indicator_type2   DRAW_LINE
//--- HotPink color is used for the indicator line
#property indicator_color2  clrHotPink
//--- the indicator 2 line is a dot-dash one
#property indicator_style2  STYLE_DASHDOTDOT
//--- indicator 2 line width is equal to 2
#property indicator_width2  2
//---- displaying of the the indicator label
#property indicator_label2  "Lower VolatilityPivot"
//+----------------------------------------------+
//| Bullish indicator drawing parameters         |
//+----------------------------------------------+
//--- drawing the indicator 3 as a label
#property indicator_type3   DRAW_ARROW
//--- DeepSkyBlue color is used for the indicator
#property indicator_color3  clrDeepSkyBlue
//--- indicator 3 width is equal to 4
#property indicator_width3  4
//--- displaying the indicator label
#property indicator_label3  "Buy VolatilityPivot"
//+----------------------------------------------+
//| Parameters of drawing a bearish indicator    |
//+----------------------------------------------+
//--- drawing the indicator 4 as a label
#property indicator_type4   DRAW_ARROW
//--- red color is used for the indicator
#property indicator_color4  clrRed
//--- indicator 4 width is equal to 4
#property indicator_width4  4
//--- displaying the indicator label
#property indicator_label4  "Sell VolatilityPivot"
//+----------------------------------------------+
//| declaration of constants                     |
//+----------------------------------------------+
#define RESET 0  // A constant for returning the indicator recalculation command to the terminal
//+----------------------------------------------+
//| Declaration of enumeration                   |
//+----------------------------------------------+
enum Mode_
  {
   Mode_ATR=0,   //ATR
   Mode_Price    //Price deviation
  };
//+----------------------------------------------+
//| Indicator input parameters                   |
//+----------------------------------------------+
input uint   atr_range=100;
input uint   ima_range=10;
input double atr_factor=3;
input Mode_  Mode=Mode_ATR;
input  uint  DeltaPrice=200;
input int    Shift=0;          // Horizontal shift of the indicator in bars
//+----------------------------------------------+
//--- declaration of dynamic arrays that
//--- will be used as indicator buffers
double ExtMapBufferUp[];
double ExtMapBufferDown[];
double ExtMapBufferUp1[];
double ExtMapBufferDown1[];
//---
double dDeltaPrice;
//--- declaration of integer variables for the indicators handles
int ATR_Handle;
//--- declaration of integer variables for the start of data calculation
int min_rates_total;
//+------------------------------------------------------------------+
//| CMoving_Average class description                                  |
//+------------------------------------------------------------------+
#include <SmoothAlgorithms.mqh>
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
int OnInit()
  {
//--- Getting the handle of the ATR indicator
   if(Mode==Mode_ATR)
     {
      min_rates_total=int(atr_range+ima_range)+1;
      ATR_Handle=iATR(NULL,0,atr_range);
      if(ATR_Handle==INVALID_HANDLE)
        {
         Print(" Failed to get handle of the ATR indicator");
         return(INIT_FAILED);
        }
     }
   else
     {
      min_rates_total=3;
      dDeltaPrice=DeltaPrice*_Point;
     }
//--- set ExtMapBufferUp[] dynamic array as an indicator buffer
   SetIndexBuffer(0,ExtMapBufferUp,INDICATOR_DATA);
//---- shifting the indicator 1 horizontally by Shift
   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//--- shifting the start of drawing the indicator 1
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//--- indexing buffer elements as timeseries   
   ArraySetAsSeries(ExtMapBufferUp,true);
//--- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- set ExtMapBufferDown[] dynamic array as an indicator buffer
   SetIndexBuffer(1,ExtMapBufferDown,INDICATOR_DATA);
//---- shifting the indicator 2 horizontally by Shift
   PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//--- shifting the starting point of calculation of drawing of the indicator 2
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
//--- indexing buffer elements as timeseries   
   ArraySetAsSeries(ExtMapBufferDown,true);
//--- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- Set the ExtMapBufferUp1[] dynamic array as an indicator buffer
   SetIndexBuffer(2,ExtMapBufferUp1,INDICATOR_DATA);
//---- shifting the indicator 1 horizontally by Shift
   PlotIndexSetInteger(2,PLOT_SHIFT,Shift);
//--- shifting the start of drawing of the indicator 3
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,min_rates_total);
//--- indexing buffer elements as timeseries   
   ArraySetAsSeries(ExtMapBufferUp1,true);
//--- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- indicator symbol
   PlotIndexSetInteger(2,PLOT_ARROW,118);
//--- Set the ExtMapBufferDown1[] dynamic array as an indicator buffer
   SetIndexBuffer(3,ExtMapBufferDown1,INDICATOR_DATA);
//---- shifting the indicator 2 horizontally by Shift
   PlotIndexSetInteger(3,PLOT_SHIFT,Shift);
//--- shifting the start of drawing of the indicator 4
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,min_rates_total);
//--- indexing buffer elements as timeseries   
   ArraySetAsSeries(ExtMapBufferDown1,true);
//--- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- indicator symbol
   PlotIndexSetInteger(3,PLOT_ARROW,118);
//--- creation of the name to be displayed in a separate sub-window and in a pop up help
   IndicatorSetString(INDICATOR_SHORTNAME,"VolatilityPivot");
//--- determining the accuracy of the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- initialization end
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // number of bars in history at the current tick
                const int prev_calculated,// amount of history in bars at the previous tick
                const datetime &time[],
                const double &open[],
                const double& high[],     // price array of price maximums for the indicator calculation
                const double& low[],      // price array of minimums of price for the indicator calculation
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- checking if the number of bars is enough for the calculation
   if((BarsCalculated(ATR_Handle)<rates_total && Mode==Mode_ATR) || rates_total<min_rates_total) return(RESET);
//--- declarations of local variables 
   double DeltaStop,Stop;
   static double PrevStop;
   int limit,bar;
//--- indexing elements in arrays as in timeseries  
   ArraySetAsSeries(close,true);
//--- calculation of the 'limit' starting index for the bars recalculation loop
   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of calculation of an indicator
     {
      limit=rates_total-min_rates_total-1;               // starting index for calculation of all bars
      PrevStop=close[limit+1];
     }
   else limit=rates_total-prev_calculated;               // starting index for calculation of new bars
//---
   if(Mode==Mode_Price)
     {
      //--- main indicator calculation loop
      for(bar=limit; bar>=0; bar--)
        {
         ExtMapBufferUp1[bar]=EMPTY_VALUE;
         ExtMapBufferDown1[bar]=EMPTY_VALUE;
         DeltaStop=dDeltaPrice;
         //---
         if(close[bar]==PrevStop) Stop=PrevStop;
         else
           {
            if(close[bar+1]<PrevStop && close[bar]<PrevStop) Stop=MathMin(PrevStop,close[bar]+DeltaStop);
            else
              {
               if(close[bar+1]>PrevStop && close[bar]>PrevStop) Stop=MathMax(PrevStop,close[bar]-DeltaStop);
               else
                 {
                  if(close[bar]>PrevStop) Stop=close[bar]-DeltaStop;
                  else Stop=close[bar]+DeltaStop;
                 }
              }
           }
         //---
         if(close[bar]>Stop) ExtMapBufferUp[bar]=Stop; else ExtMapBufferUp[bar]=EMPTY_VALUE;
         if(close[bar]<Stop) ExtMapBufferDown[bar]=Stop; else ExtMapBufferDown[bar]=EMPTY_VALUE;
         if(ExtMapBufferUp[bar+1]==EMPTY_VALUE && ExtMapBufferUp[bar]!=EMPTY_VALUE) ExtMapBufferUp1[bar]=ExtMapBufferUp[bar];
         if(ExtMapBufferDown[bar+1]==EMPTY_VALUE && ExtMapBufferDown[bar]!=EMPTY_VALUE) ExtMapBufferDown1[bar]=ExtMapBufferDown[bar];
         if(bar) PrevStop=Stop;
        }
     }
//---
   if(Mode==Mode_ATR)
     {
      //--- declarations of local variables 
      double ATR[];
      int to_copy,maxbar;
      //--- declaration of the CMoving_Average class variables from the SmoothAlgorithms.mqh file 
      static CMoving_Average EMA;
      //--- indexing elements in arrays as in timeseries  
      ArraySetAsSeries(ATR,true);
      to_copy=limit+1;
      if(CopyBuffer(ATR_Handle,0,0,to_copy,ATR)<=0) return(RESET);
      maxbar=rates_total-min_rates_total-1;
      //--- main indicator calculation loop
      for(bar=limit; bar>=0; bar--)
        {
         ExtMapBufferUp1[bar]=EMPTY_VALUE;
         ExtMapBufferDown1[bar]=EMPTY_VALUE;
         DeltaStop=atr_factor*EMA.EMASeries(maxbar,prev_calculated,rates_total,ima_range,ATR[bar],bar,true);
         //---
         if(close[bar]==PrevStop) Stop=PrevStop;
         else
           {
            if(close[bar+1]<PrevStop && close[bar]<PrevStop) Stop=MathMin(PrevStop,close[bar]+DeltaStop);
            else
              {
               if(close[bar+1]>PrevStop && close[bar]>PrevStop) Stop=MathMax(PrevStop,close[bar]-DeltaStop);
               else
                 {
                  if(close[bar]>PrevStop) Stop=close[bar]-DeltaStop;
                  else Stop=close[bar]+DeltaStop;
                 }
              }
           }
         //---
         if(close[bar]>Stop) ExtMapBufferUp[bar]=Stop; else ExtMapBufferUp[bar]=EMPTY_VALUE;
         if(close[bar]<Stop) ExtMapBufferDown[bar]=Stop; else ExtMapBufferDown[bar]=EMPTY_VALUE;
         //---
         if(ExtMapBufferUp[bar+1]==EMPTY_VALUE && ExtMapBufferUp[bar]!=EMPTY_VALUE) ExtMapBufferUp1[bar]=ExtMapBufferUp[bar];
         if(ExtMapBufferDown[bar+1]==EMPTY_VALUE && ExtMapBufferDown[bar]!=EMPTY_VALUE) ExtMapBufferDown1[bar]=ExtMapBufferDown[bar];
         //---
         if(bar) PrevStop=Stop;
        }
     }
//---     
   return(rates_total);
  }

csv error

 
Hi , please, im using it with a zigzag indicator. He alwais get a daily chart values. How to get other timeframes?
 

Hi, does this export just one indicator values, or all that are applied to the chart? I am looking to export multiple indicator values at once if possible.

Thanks! =)

 

Hey, good job! That's a really useful script.

I am having an small issue here...

When exporting ATR values with the script but since data is always delivered in 0.00 format i can't get values with more than two decimals.

E.g. 0.00045 is released as 0.00 as I see when I import the csv in Excel.

It doesn't seem to be an excel-number-display-issue but the data in the csv is already "cut" I guess...

Any idea on how to solve that?

Thank you in advance

Reason: