Changing the TF is a problem - page 5

 

Here's an indicator:

//+------------------------------------------------------------------+
//|                                                    Shabl_ind.mq4 |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "eevviill"
#property link      "http://alievtm.blogspot.com"
#property version   "1.42"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property  indicator_color1 clrLightBlue
#property  indicator_color2 clrRed
#property  indicator_width1 2
#property  indicator_width2 2

input string   arr_set="Arrow settings";
input int      arrow_indent=22;
input int      up_arrow_code=233;
input int      down_arrow_code=234;

double         up_arr[];
double         down_arr[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,up_arr);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,up_arrow_code);
   SetIndexLabel(0,"UP arrow");

   SetIndexBuffer(1,down_arr);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,down_arrow_code);
   SetIndexLabel(1,"DOWN arrow");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Shabl_ind                                                        |
//+------------------------------------------------------------------+
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[])
  {
   int i,limit=0;
//---
   if(rates_total<=20)
      return(0);
//--- last counted bar will be recounted
   if(prev_calculated==0)
      limit=rates_total-1;
   if(prev_calculated>0)
      limit=rates_total-prev_calculated+1;
   Comment("rates_total=",rates_total,"; prev_calculated=",prev_calculated,"; limit=",limit);
//--- Shabl_ind counted in the 1-st buffer
   datetime temp;
   for(i=0; i<limit; i++)
     {
      temp=time[i];
      if(close[i]>close[i+1])
         up_arr[i]=low[i]-arrow_indent*Point; //up arrow
      if(close[i]<close[i+1])
         down_arr[i]=high[i]+arrow_indent*Point; //down arrow
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Only note that, according to your conditions, the indicator may draw both buffers on the zero bar.

Files:
 
Karputov Vladimir:

Here's an indicator:

Please note that, according to your conditions, the indicator may draw both buffers at zero bar.

To avoid drawing two buffers, you need to zero out the other buffer when one is full

      if(close[i]>close[i+1])
       {
        down_arr[i] = EMPTY_VALUE;
        up_arr[i]=low[i]-arrow_indent*Point; //up arrow
       }
      if(close[i]<close[i+1])
       {
        up_arr[i] = EMPTY_VALUE;
        down_arr[i]=high[i]+arrow_indent*Point; //down arrow
       }
 
Vasyl Nosal:
Do you have a solution code?

Like many others, I have more than one solution to your question. Because indicators can be different in nuances (the tasks they perform/code/intervals of calculation, in general, many factors).

 
Karputov Vladimir:

Here's an indicator:

just note that according to your conditions, the indicator can draw both buffers at zero bar.

Are you all shitting me or do you really have no idea what the problem is?

Here is your indicator when you change TF on unloaded history.

:)))))))))))))))))))))))))))))))))))))))))))

 
Dina Paches:

Like many others, I have more than one solution to your question. Because indicators can be different in nuances (the tasks they perform/code/intervals of calculation, in general, many factors).

Blah blah blah...
 
Vasyl Nosal:

Are you all bullshitting me or do you really have no idea what the problem is?

Here is your indicator for TF change on unloaded history.

:)))))))))))))))))))))))))))))))))))))))))))

Can you tell me what manipulations you use to achieve such an interesting picture? And I hope you will be more reticent in the future.

Added: I mentioned above that you need to think about the content of the indicator buffers yourself when pumping up the history:

Forum on trading, automated trading systems and trading strategy testing

Changing TFs is a problem

Karputov Vladimir, 2015.12.07 10:09

You have two variables at your disposal: prev_calculated and rates_total. By controlling the history loading (with prev_calculated==0) you have to think about what to do with the indicator buffers - the usual behaviour in such cases is to equate the history loading to the first indicator loading event.

But you ignored it for some reason and didn't want to make edits.

Anyway here is version 1.43 (here are my corrections and corrections fromAlexey Viktorov):

Forum on trading, automated trading systems and strategy tester

TF change - problem

Alexey Viktorov, 2015.12.07 12:52

If you want to avoid drawing two buffers, you have to reset one buffer to zero when it is full.

      if(close[i]>close[i+1])
       {
        down_arr[i] = EMPTY_VALUE;
        up_arr[i]=low[i]-arrow_indent*Point; //up arrow
       }
      if(close[i]<close[i+1])
       {
        up_arr[i] = EMPTY_VALUE;
        down_arr[i]=high[i]+arrow_indent*Point; //down arrow
       }

:

//+------------------------------------------------------------------+
//|                                                    Shabl_ind.mq4 |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "eevviill"
#property link      "http://alievtm.blogspot.com"
#property version   "1.43"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property  indicator_color1 clrLightBlue
#property  indicator_color2 clrRed
#property  indicator_width1 2
#property  indicator_width2 2

input string   arr_set="Arrow settings";
input int      arrow_indent=22;
input int      up_arrow_code=233;
input int      down_arrow_code=234;

double         up_arr[];
double         down_arr[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,up_arr);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,up_arrow_code);
   SetIndexLabel(0,"UP arrow");

   SetIndexBuffer(1,down_arr);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,down_arrow_code);
   SetIndexLabel(1,"DOWN arrow");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Shabl_ind                                                        |
//+------------------------------------------------------------------+
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[])
  {
   int i,limit=0;
//---
   if(rates_total<=20)
      return(0);
//--- last counted bar will be recounted
   if(prev_calculated==0)
     {
      limit=rates_total-1;
      ArrayInitialize(up_arr,EMPTY_VALUE);
      ArrayInitialize(down_arr,EMPTY_VALUE);
     }
   if(prev_calculated>0)
      limit=rates_total-prev_calculated+1;
   Comment("rates_total=",rates_total,"; prev_calculated=",prev_calculated,"; limit=",limit);
//--- Shabl_ind counted in the 1-st buffer
   for(i=0; i<limit; i++)
     {
      if(close[i]>close[i+1])
        {
         down_arr[i]=EMPTY_VALUE;
         up_arr[i]=low[i]-arrow_indent*Point; //up arrow
        }
      if(close[i]<close[i+1])
        {
         up_arr[i]=EMPTY_VALUE;
         down_arr[i]=high[i]+arrow_indent*Point; //down arrow
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
 
Karputov Vladimir:

Could you tell us what manipulations you use to achieve such an interesting picture? And I hope you will be more reticent in the future.

I open a chart of a currency pair I've never opened before. M1, for example. I attach your indicator. I change it to M5.

So I was right? We need to reset the arrow buffers?

 
Vasyl Nosal:

mql4

Nope. Here's with your design on a pair where there was no history.

What is THIS?

if(i>Bars-20) i=Bars-20;

As I thought, the loop is rooted internally. Also, since in the loop body you access the previous bar ([i+1]), you should start calculation not earlier than from the second bar in history (counting from the left). And buffers must be filled at each bar ALL, not just one. Well, or initialize them beforehand in OnInit().

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[])
{
   int nStartBar = rates_total - MathMax(prev_calculated, 2);

   for(int i = nStartBar; i >= 0; i--)
   {
      if(Close[i] > Close[i+1])
      {
         up_arr[i] = Low[i] - arrow_indent * _Point; //up arrow
         down_arr[i] = 0;
      }
         
      if(Close[i] < Close[i+1])
      {
         up_arr[i] = 0;
         down_arr[i] = High[i] + arrow_indent * _Point; //down arrow
      }
   }

   return(rates_total);
}
 
Karputov Vladimir:

Anyway, here's version 1.43 (my corrections andAlexey Viktorov' s corrections here):


:

Vladimir, why are you doing this inside OnCalculate()? I mean the last two lines - re-initialization of arrays. Both buffers are calculated on each bar, it's an unnecessary action.

if(prev_calculated==0)
{
   limit=rates_total-1;
   ArrayInitialize(up_arr,EMPTY_VALUE);
   ArrayInitialize(down_arr,EMPTY_VALUE);
}
 
Sergei Vladimirov:

WHAT IS THIS?

As I thought, the loop is corrupted internally. Besides, if you access the previous bar in the loop body ([i+1]), then the calculation should start not earlier than from the second bar in the history (counting from the left). And buffers must be filled at each bar ALL, not just one. Well, or initialize them beforehand in OnInit().

That's it? It won't glitch?