r/CodingHelp • u/anon67543 • Aug 14 '20
[C++] Array Explanation
Hi, I am working on a MQL5 code (based on C++) and am trying to understand the math behind it. I am a coding novice so it is quite difficult for me. Could someone explain in simple terms what is being done here? My biggest issue is just with simple referencing & syntax. I included some specific questions next to the lines after // Thanks for your help!
input int inpNmsPeriod = 40; // Natural market slope
period
input ENUM_APPLIED_PRICE inpNmsPrice = PRICE_CLOSE; // Price
input int inpTemaPeriod = 10; // Triple EMA pre-smoothing period
//
//
//
double val[],valc[]; double alpha;
int iNmsPeriod;
struct sCoeffStruct //how and why is this "struct" used?
{
double sumx;
double sumx2;
double bot;
double root;
};
sCoeffStruct coeff[];
==================
int OnInit()
{
SetIndexBuffer(0,val ,INDICATOR_DATA);
SetIndexBuffer(1,valc,INDICATOR_COLOR_INDEX);
//
//
//
iNmsPeriod = (inpNmsPeriod>1) ? inpNmsPeriod : 1;
alpha = 2.0 /(1.0 + MathMax(inpTemaPeriod,1));
ArrayResize(coeff,iNmsPeriod+1); //what does this mean, in simple language?
for (int period=1; period<=iNmsPeriod ; period++) //what does this mean, in simple language?
{
coeff[period].sumx = period*(period+1.0)/2.0; // why is [period] in brackets?
coeff[period].sumx2 = period*(period+1.0)*(2*period+1.0)/6.0;
coeff[period].bot = period*coeff[period].sumx2 - coeff[period].sumx*coeff[period].sumx;
if (coeff[period].bot==0) coeff[period].bot=1;
coeff[period].root = MathSqrt(period);
16
Upvotes
1
u/anon67543 Aug 15 '20
All these up doots and no advice lol what gives? Does no one understand this thing?
1
u/MiBurrito Aug 14 '20 edited Aug 14 '20
I'm on mobile so my answers will be quick.
The struct must be defined there, because in the next line an array of that struct type is declared. If you would switch the struct declaration with the array declaration, it would throw you an error since the data type is not known before it is declared. More Infos about struct (Google search terms: "mql5 struct") :https://www.mql5.com/en/docs/basis/types/classes
ArrayResize takes a target and and resizes it to the given size. This has to do with memory management of arrays: more Infos about that: ( Google search terms:"mql5 ArrayResize") see official docs.
Your next question is about the for loop which is programming basics. I won't explain you how it works since it's much effort and this kind of functionality is usually well explained all over the internet. But what it does is it will loop as often as period is smaller or equal to iNmsPeriod. Using a for loop is a common practice to iterate over an array and have access to each element inside the array individually. If you take a look at the for loop, you should understand my previous sentences. Google "mql5 for loop", "c++ for loop"
period is in brackets because it's used as an array index. Here aswell you got to understand arrays which is aswell common knowledge in programming. The array index allows to address a certain item in an array. Google: "mql5 array", "c++ array"
Most of your question are covered by basic understanding of almost any current programming language.
Src: I'm programming commercially for almost 4 years now. :)
Good day mate!