Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 669

 
psyman:

I've read your correspondence arising from my topic, it's all fun :-) but what about the question which caused all this buzz?

Declaring an array viaSetIndexBuffer(1, tmp1) gives me nothing. Of course, I can increase array size in the same loop, but I want to know a simpler and more efficient way.

Show me the code in full - what you've done there, what you wanted and what you've got.

 

I want to watch for volatility. At least open-close to start with, averaging over a period is done using SMAs.


//+------------------------------------------------------------------+
//|                                                        _null.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot OC
#property indicator_label1  "O-C"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrSteelBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         ip1Buf[];

input int ip1=100;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
  
string s_name1;

s_name1="O-C (" + IntegerToString(ip1) + ")";

IndicatorShortName(s_name1);
SetIndexLabel(0, s_name1);


//--- indicator buffers mapping
   SetIndexBuffer(0,ip1Buf);
   
//---
   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[])
  {


int i;
double tmp1[];

SetIndexBuffer(1, tmp1);


      Print("rates_total = ",rates_total);
      for(i=1; i<rates_total-1; i++)
      {
      tmp1[i]=close[i];      
      ip1Buf[i]=iMA(NULL,0,100,0,0,tmp1[i],0);
      
      }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
psyman:

I want to watch for volatility. At least open-close to start with, averaging over a period is done with SMA.


SetIndexBuffer(1, tmp1) why not in OnInit() ?

Why no IndicatorBuffers(2) ?

Why don't you calculate it optimally? There is a full loop on every tick.

Why iMA() and not iMAOnArray() ?

 

=Whyno IndicatorBuffers(2) ?


#property strict doesn't complain about it, that's probably why I didn't write it in.


=WhyiMA() and not iMAOnArray() ?


I don't have enough knowledge and the textbook doesn't say anything about it.

Lack of system knowledge is a serious limitation.


=Full loopon every tick.


I do not understand anything about it, please explain why or give me a link to where it is done.

I have a minimum one hour TF.

 
=На each tick is a full cycle.


I don't understand any of this, please explain it or give me a link to where it is done.

I have a minimum TF of one hour.

At every call to OnCalculate you have the for loop for whizzing through the data from 1 to rates_total, i.e. doing the same job. This, of course, is a bad thing.

 
psyman:

=Whyno IndicatorBuffers(2) ?


#property strict doesn't complain about it, that's probably why I didn't write it in.


=WhyiMA() and not iMAOnArray() ?


I don't have enough knowledge and the textbook doesn't say anything about it.

Lack of system knowledge is a serious limitation.


=Full loopon every tick.


I do not understand anything about it, please explain why or give me a link to where it is done.

I have a minimum one hour TF.

You know, I've attached an indicator template to this thread - somewhere in the middle of it - you can find it and make whatever you want out of it. Look it up. I also wrote that a lot of times people are interested in what's going on, so I decided to make an indicator template and put it in this thread.

 

Forum on trading, automated trading systems & strategy testing

Any questions from newbies on MQL4, help and discussion on algorithms and codes

Andrei Novichkov, 2018.10.17 22:06

At every call to OnCalculate you have for loop for scouring data from 1 to rates_total, i.e. doing the same job. This is certainly a bad thing.


If you say it's bad, tell me how to do it well. Should I move calculations to OnInit?

 

Forum on trading, automated trading systems & strategy testing

Any questions for newbies on MQL4, help and discussion on algorithms and codes

Artyom Trishkin, 2018.10.17 22:10

You know, I directly in this branch - somewhere in the middle of it, attached an indicator template - you can find it, and do directly from it what you want. Look it up. I wrote that a lot of people are interested in how it works and so I decided to make an indicator template and put it in this thread.


Searching for the words "indicator template" and your name doesn't find anything, and wrote here already on the volume of War and Peace.

Any combination of words from the post come to mind.

 
How to create an array of class instances?
Made ClassName* className[], then ArrayResize on it, but does not give invalid pointer access to methods
 
Roman Sharanov:
How to create an array of class instances?
Made ClassName* className[], then ArrayResize on it, but does not give invalid pointer access to methods

There is an example inCArrayObj

Reason: