[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 480

 
hoz:

And if I want the last 30 bars for example, how do I do this?

try Bars-30
 
midorum:
try the Bars-30
No, it's not. It would logically indent on the left 30 bars and draw to the 0th bar.
 
SetIndexDrawBegin(0,Bars-30);//первый бар отрисовки индикатора 

I don't know what your logic is, with me this setting draws the last 30 bars of the indicator

Once again, SetIndexDrawBegin() calculates the bars from the left end of the chart

check before you start to argue

 
merkulov.artem:

OK. I will try to outline the essence of the problem, maybe someone will suggest or help implement it in a simpler way.

We are working on a minute chart. Let's take the one-minute bar (high+low/2 - average value of the bar) on the 29/10/12 chart at 00:00 - it will be a reference point.

Then we check the deviation from this price upwards by 10 pips (each correct deviation is +1).

When we reach the 10 point deviation from the starting point - the +1 counter; and we start checking further deviations, but from the point, where we reached the 10 point deviation, and wait for the next 10 point rise in price.

For example, we get:

If (start point) + 10 pips <= price (we use all subsequent prices of bars from the starting point.) We get a counter = counter +1; and we start the starting point already from this point -(start point + 10 pips).

If (start point + 10 pips) +10 pips <= price (all subsequent prices of bars from the reference point are used) we get counter=Counter+1; and we start from this point-(start point + 10 pips) +10 pips.

If(start point + 10 p ips+10 p ips) + 10 pips <= price (we use all subsequent prices of bars from the reference point.) we get counter=Counter+1; and we start from this point -(start point + 10 pips+ 10pips) + 10 pips.

and so on...

Until we reach a counter of 10(counter == 10).

And every new formed1-minute bar (high+low/2 - average value of the bar) we check for this condition and wait until the counter reaches10 (counter == 10). Suppose two days passed, each new bar formed we checked and shifted if the conditions were correct .....

And when we reached counter =10 - we output the message that "Counter == 10". We set the starting point 2 days ahead of the original one set by the user i.e. from29/10/12 at 00:00 to 31/10/12 at 00:00 and repeat the cycle.

Repeat the cycle and so move through the schedule.

I.e. we should take the newly formed bars and draw (high+low/2 - average value of the bar). And check for the conditions and, if they are correct, shift them as described above.

I have tried to implement it through an array, it is very confusing and gives wrong values! Please help me, how to implement this algorithm!


You don't need arrays here at all. Is this even an Expert Advisor, a script, or an indicator? The matter is that the code of the Expert Advisor will be different from that of the indicator for the implementation of this task.

And there is a significant disadvantage here. Look, we have marked the starting point and it is right on the peak of the uptrend. Suppose we do not consider the reading of this candle in further fetching of 10 points - let's call it the starting candle. So, the next candlestick goes downwards and we don't catch either level = starting point + 10 pips: the price goes down. So we may say, that we are stuck for a long time - the price went down and may go up and down anyway - it will not reach the level of the starting point anyway - the bears have pressed it tightly. But let's say we got lucky and after 5,000 candles the price returned to the level of the starting point, and even went 10 pips up. We have wasted a lot of time, but now we can put the price equal to the starting point + 10 points into the variable and wait till the price reaches the level = variable level + 10 points... In short, we don't need all that - judging by your description we are catching the level = starting point + 10*10 = starting point + 100 pips. Once this level has been caught, we move the start date 2 days into the future and calculate a new starting point. This is complicated - there is an easier way.

We set the start date in custom variables (preceded by extern in the code). Declare a variable of datetime type. Suppose it is variable dt_StartDate (start date) and then in the initialization block we assign it the value stored in the custom variable. Then we look for the candlestick that matches this date and calculate the starting point using your formula. However, it is not the starting point, but the starting price level. Declare a variable of double type, e.g. d_StartLevel (starting level) and enter the value of calculated level in the variable. Or it can be even easier - do not declare a variable, but draw a horizontal line through the calculated starting price. Let's call it StartLevel. Now let's draw the second line on the chart at a distance = 100 points up from the starting level - let's call it OtlovLevel. Further on, we don't need any arrays - on every tick we will simply watch if the price is higher than OtlovLevel or not. As soon as it is, we increase the value of dt_StartDate variable by 2 days, calculate the start level again and shift the start line there. After that we again take 100 points up from the start line and drag the OtlovLevel line there. That is all, the task is solved. Any time we can ask for the price at which any line is set and compare it with the current price or with the High price of the current candle.

 
midorum:

I don't know what your logic is, with me this setting draws the last 30 bars of the indicator

Once again, SetIndexDrawBegin() calculates the bars from the left end of the chart

check it before you argue.

So I'm not arguing. Here's a look. Let's take a standard CCI indicator. I replacedCCIPeriod with(Bars- CCIPeriod) in the SetIndexDrawBegin function. I only changed the yellow highlighted in the code...

Here's the code:

//+------------------------------------------------------------------+
//|                                                          CCI.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
//----
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 LightSeaGreen
//---- input parameters
extern int CCIPeriod = 14;
//---- buffers
double CCIBuffer[];
double RelBuffer[];
double DevBuffer[];
double MovBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(1, RelBuffer);
   SetIndexBuffer(2, DevBuffer);
   SetIndexBuffer(3, MovBuffer);
//---- indicator lines
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, CCIBuffer);
//----
   if(CCIPeriod <= 0)
       CCIPeriod = 14;
//----
   SetIndexDrawBegin(0, Bars - CCIPeriod);
  
//---- name for DataWindow and indicator subwindow label
   short_name="CCI(" + CCIPeriod + ")";
   IndicatorShortName(short_name);
   SetIndexLabel(0, short_name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Commodity Channel Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i, k, counted_bars = IndicatorCounted();
   double price, sum, mul; 
   if(CCIPeriod <= 1)
       return(0);
   if(Bars <= CCIPeriod) 
       return(0);
//---- initial zero
   if(counted_bars < 1)
     {
       for(i = 1; i <= CCIPeriod; i++) 
           CCIBuffer[Bars-i] = 0.0;
       for(i = 1; i <= CCIPeriod; i++) 
           DevBuffer[Bars-i] = 0.0;
       for(i = 1; i <= CCIPeriod; i++) 
           MovBuffer[Bars-i] =0.0;
     }
//---- last counted bar will be recounted
   int limit = Bars - counted_bars;
   if(counted_bars > 0) 
       limit++;
//---- moving average
   for(i = 0; i < limit; i++)
       MovBuffer[i] = iMA(NULL, 0, CCIPeriod, 0, MODE_SMA, PRICE_TYPICAL, i);
//---- standard deviations
   i = Bars - CCIPeriod + 1;
   if(counted_bars > CCIPeriod - 1) 
       i = Bars - counted_bars - 1;
   mul = 0.015 / CCIPeriod;
   while(i >= 0)
     {
       sum = 0.0;
       k = i + CCIPeriod - 1;
       while(k >= i)
        {
          price =(High[k] + Low[k] + Close[k]) / 3;
          sum += MathAbs(price - MovBuffer[i]);
          k--;
        }
       DevBuffer[i] = sum*mul;
       i--;
     }
   i = Bars - CCIPeriod + 1;
   if(counted_bars > CCIPeriod - 1) 
       i = Bars - counted_bars - 1;
   while(i >= 0)
     {
       price = (High[i] + Low[i] + Close[i]) / 3;
       RelBuffer[i] = price - MovBuffer[i];
       i--;
     }
//---- cci counting
   i = Bars - CCIPeriod + 1;
   if(counted_bars > CCIPeriod - 1) 
       i = Bars - counted_bars - 1;
   while(i >= 0)
     {
       if(DevBuffer[i] == 0.0) 
           CCIBuffer[i] = 0.0;
       else 
           CCIBuffer[i] = RelBuffer[i] / DevBuffer[i];
       i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Logically, ifCCIPeriod is 14. ThenSetIndexDrawBegin(0, Bars - CCIPeriod) should draw only 14 last bars. It starts from value (Bar - 14). But this is what I see on the screen

 
hoz:

So I'm not arguing. Here's a look. Take the standard CCI indicator. I replacedCCIPeriod in the SetIndexDrawBegin function with(Bars- CCIPeriod). I only changed the yellow highlighted in the code...

Here's the code:

Logically, ifCCIPeriod is 14. ThenSetIndexDrawBegin(0, Bars - CCIPeriod) should draw only 14 last bars. It starts from value (Bar - 14). But this is what I see on the screen

This is what I see on the screen

at the top the native indicator, below changed to SetIndexDrawBegin(0, Bars-CCIPeriod);

 
midorum, my apologies. My blunder. MetaEditor opened another terminal... It's working now. Thank you!
 
hoz:
midorum, my apologies. My blunder. MetaEditor opened another terminal... It's working now. Thank you!
Yes, that's the problem I'm always struggling with too - 3 terminals, and Editor gets folders mixed up. Better to run it through the button in the terminal and open the file in it manually.
 

Good day people, please give me an answer, how to determine that the 15-minute candle opened, I used a mind-boggling string, but as it turned out, not always the candle opens by time... I think there is a simpler way to find out that the candle opened... but I do not understand it...

if ((TimeMinute(TimeCurrent())==0 && TimeSeconds(TimeCurrent())==0)|| (TimeMinute(TimeCurrent())==15 && TimeSeconds(TimeCurrent())==0) || (TimeMinute(TimeCurrent())==30 && TimeSeconds(TimeCurrent())==0) || (TimeMinute(TimeCurrent())==45 && TimeSeconds(TimeCurrent())==0)

 
stater:

Good day people, please give me an answer, how to determine that the 15-minute candle opened, I used a mind-boggling string, but as it turned out, not always the candle opens by time... I think there is a simpler way to find out that the candle opened... but I do not understand it...

if ((TimeMinute(TimeCurrent())==0 && TimeSeconds(TimeCurrent())==0)|| (TimeMinute(TimeCurrent())==15 && TimeSeconds(TimeCurrent())==0) || (TimeMinute(TimeCurrent())==30 && TimeSeconds(TimeCurrent())==0) || (TimeMinute(TimeCurrent())==45 && TimeSeconds(TimeCurrent())==0)


https://www.mql5.com/ru/forum/131853/page4#504607