Questions from a "dummy" - page 235

 
lazarev-d-m:

Changed to this

Seems to be working.

your arrays are not serialized.

that's why it's this.

Buffer[i+1]=Buffer[i]+delta;

peeking into the future


It doesn't recalculate 0 bar - for the same reason - your start - it doesn't start with 0.

 
sergeev:

your arrays are not serialized.

so that's why it's this.

peeking into the future.


It doesn't recalculate 0 bar for the same reason you don't start with 0.

My 0 bar is not calculated according to the formula.

Buffer[0]=price[0]; 

And then based on the price movement, the indicator will catch up with the trend

For example if the price has moved away from the indicator and will not move, then duringthe "period" of candle indicator will catch up the price, this is a kind of alternative to MA for me

 

lazarev-d-m:

it's kind of an alternative to MA for me

looking 1 bar into the future you can safely throw this alternative in the bin
 

My first indicator. I want to scale the symbol from 0 to 100. Am I doing it right? And how to make the current bar not to recalculate (in the tester it jerks, but not on the chart)

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Main"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         MainBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,MainBuffer,INDICATOR_DATA);
   IndicatorSetString(INDICATOR_SHORTNAME,"NormSymbol");
   IndicatorSetInteger(INDICATOR_DIGITS,2);
   ArrayInitialize(MainBuffer, EMPTY_VALUE);

//---
   return(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[])
  {
   
   int first,bar,nLowBar=0,nHighBar=0;
   int nVizCount = (int)ChartGetInteger(0, CHART_VISIBLE_BARS);
   if(prev_calculated==0) // проверка на первый старт расчёта индикатора
     {
      first=0; // стартовый номер для расчёта всех баров
     }
   else
     {
      first=prev_calculated-1; // стартовый номер для расчёта новых баров
     }


//---- основной цикл расчёта индикатора
   for(bar=first; bar<rates_total; bar++)
     {
      nLowBar =ArrayMinimum(low, bar, nVizCount);
      nHighBar=ArrayMaximum(high, bar, nVizCount);
      if(nLowBar>0 && nHighBar>0 && high[nHighBar]-low[nLowBar]!=0)
        {
         MainBuffer[bar]=((close[bar] -low[nLowBar])/(high[nHighBar]-low[nLowBar]))*100;
        }
      else
        {
         MainBuffer[bar]=EMPTY_VALUE;
        }
     }

  
//--- return value of prev_calculated for next call

   return(rates_total);
  }
 
Konstantin83: how to make the current bar not recalculate

Memorise the opening time of the current bar and compare it with the same value of the incoming tick. Only recalculate the value of the current bar when the values do not match.

I would put this in the initialization block:

   nVizCount = (int)ChartGetInteger(0, CHART_VISIBLE_BARS);
   

and the variables would be declared at the global level of the program:

int first,bar,nLowBar=0,nHighBar=0, nVizCount; 
 
Yedelkin:

Memorise the opening time of the current bar and compare it with the same value of the incoming tick. Only recalculate the value of the current bar when the values do not match.

I would put this in the initialisation block:

Thank you. Is this correct in general?
 
Konstantin83: And everything is correct in general?

I haven't noticed anything else. The articles suggest a similar structure. There is a check for division by zero.

Yes, to avoid recalculating, you can also compare prev_calculated and rates_total, as far as I remember.

 
Yedelkin:

I haven't noticed anything else. The articles suggest a similar structure. There is a check for division by zero.

Yes, to avoid recalculating, you can also compare prev_calculated and rates_total, as far as I remember.

Do you know why the indicator does not draw the line after closing the terminal and restarting the terminal? If you switch to another timeframe, it is drawn.
 
Konstantin83: Do you know why after closing the terminal with the indicator and restarting the terminal the indicator does not draw a line? If you switch to another timeframe, it will be drawn.

The terminal needs some time to "get up", synchronise its databases with the server, etc. when starting up. If the indicator is launched at the same time, there may be no data for the indicator calculation. Try the easiest solution: to insert a delay of two or three seconds into OnInit().

In general, it is desirable to print all important information when searching for a problem area. Like

if(nLowBar<0) Print("Ошибка, nLowBar=",nLowBar,", prev_calculated=",prev_calculated,", bar=",bar);
 

Yedelkin:

forward666 : need a visualisation

I haven't found any help for live trading yet - just try opening and closing a position in a demo account.

Here it is: https://www.mql5.com/ru/forum/6343/page96#comment_419028

Reason: