You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
How about just recalculating the whole indicator when 0? Seems like the best solution to me.
:) That's what I do...
For a heavy indicator with tons of graphics is not the best solution. The solution is simple - to use your own variable instead of prev_calculated, static or global. But it is a crutch and we want something else.
A person using the word "crutch":
For a heavy indicator with tons of graphics - not the best. No, the solution is simple - to use own variable instead of prev_calculated, static or global. But it is a crutch and we want something else.
And if the history has been swapped, it means that there may be new bars that have been missed or not calculated before - i.e. the indicator readings will already be wrong.
If prev_calculated=0, it means that a full recalculation has to be done. All standard indicators are fully recalculated in this case.
It's all clear, but alas, it all doesn't cancel this:
This is all useful, but it cannot be used according to its direct purpose - to show how many 'bars were processed at the previous call' - prev_calculated
Indicators are different; some don't need bars for calculations at all, some need only live ticks, some have limitations on the calculation depth - they don't care what has changed next in the history, some need to track graphical objects, etc. They don't need additional functions to track changes in history attached to prev_calculated, they only need this - 'processed bars on previous call'. They don't need it.
Anyway, Mr. programmers, please don't distract the joiner from 'catching the event'.
What if I simply recalculate the entire indicator at 0? In my opinion, it is the best solution.
I wouldn't say it's the best solution. At least not yet.
I decided to write an indicator that registers the current drawdown on the account, so I don't have to recalculate anything in the history. The first line looked like this at first.
I wouldn't say it's the best solution. At least not yet.
I decided to write an indicator that registers the current drawdown on the account, so I don't have to recalculate anything in the history. At first the first line looked like this.
"... some price that I don't know where it came from... " - that's rubbish of UNINITIALIZED elements of indicator buffer array. What should be done:
Forum on trading, automated trading systems and trading strategy testing
Bugs, bugs, questions
Alexey Viktorov, 2016.10.17 09:58
Do you understand what you wrote?
Better explain how to get rid of rubbish FIRST run of the indicator. Where does this rubbish come from? Shouldn't there be initialization when linking the buffer to the array? Or is it during initialization that space rubbish gets into the array??? Why is there no such rubbish in mql4?
Give me an example how to sift the trash from the normal values without using extraneous additional static or global variables.
Everyone is good enough to quote the documentation.
Nobody owes anybody anything. So there will be rubbish in the indicator buffer, after binding, until you initialise all elements of the array yourself.
Added:
I'm going to create an example now...
//| prev_calculated.mq5 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGray
#property indicator_label1 "prev_calculated"
//---- buffers
double ExtBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,ExtBuffer,INDICATOR_DATA);
//---
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(prev_calculated==0)
{
//--- инициализация элементов индикаторного буфера при первом запуске или при подкачке истории
for(int i=0;i<rates_total;i++)
ExtBuffer[i]=1.01;
return(rates_total);
}
//--- пересчитываем самый правый бар или самый правый бар и один предыдущий при появлении нового бара
int limit=rates_total-prev_calculated+1;
for(int i=rates_total-limit;i<rates_total;i++)
{
//--- визуализация пересчёта самого правого бара
static bool drive=false;
if(!drive)
ExtBuffer[i]=1.03;
else
ExtBuffer[i]=1.02;
drive=!drive;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Nobody owes anybody anything. So there will be rubbish in the indicator buffer, after binding, until you initialise all elements of the array yourself.
Added:
I'm going to create an example now...
//| prev_calculated.mq5 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGray
#property indicator_label1 "prev_calculated"
//---- buffers
double ExtBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,ExtBuffer,INDICATOR_DATA);
//---
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(prev_calculated==0)
{
//--- инициализация элементов индикаторного буфера при первом запуске или при подкачке истории
for(int i=0;i<rates_total;i++)
ExtBuffer[i]=1.01;
return(rates_total);
}
//--- пересчитываем самый правый бар или самый правый бар и один предыдущий при появлении нового бара
int limit=rates_total-prev_calculated+1;
for(int i=rates_total-limit;i<rates_total;i++)
{
//--- визуализация пересчёта самого правого бара
static bool drive=false;
if(!drive)
ExtBuffer[i]=1.03;
else
ExtBuffer[i]=1.02;
drive=!drive;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Amazing ability to answer the wrong question...
Now explain to me what happens if:
1. Counted 100 bars, entered into buffers from 0 to 99 inclusive (let's consider direction as time series) value 1.03
2. Suddenly history is loaded and prev_calculated becomes 0
From which bar it will be 1.03?