Nested For Loop MQL4
Nested For Loop MQL4
Loading saved threads...
gershgorin · External communityPost link
External question — Stack Overflow Stack Exchange
Author: gershgorin
Original post: https://stackoverflow.com/questions/71316227
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I have an array which has the same size as the number of bars in my chart. I want to create a cumulative array that sums them up 200 at a time. I am trying to wrap my head around loops, and I'm not sure what I'm doing wrong.
Array[]
denotes the array that has the same number of bars as the chart.
CumulativeSumArray[]
is the array in which each value is the sum of 200 of the values of
Array[]
. I am attempting to accomplish this task via nested for loops.
Here is a hypothetical situation:
double Array[];
double CumulativeSumArray[];
Length=200;
int i;
int j;
double _CumulativeSumArray;
for(i=Bars-Length-1;i>=0;i--)
{
for(j=i,j>=i+Length;j++)
{
_CumulativeSumArray += Array[j];
}
CumulativeSumArray[i]=_CumulativeSumArray;
}
What I end up getting with this loop, or at least what it looks like, is a cumulative sum of the whole array.
Quote
Report
user3666197 · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: user3666197
Original post: https://stackoverflow.com/a/71317053
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Q :
" not sure what I'm doing wrong. "
A :
( Overhead-costs + Efficiency )
MQL4/5 code-execution eco-system works differently for Script, EA and even way more strange for CustomIndicator :
Setup-
costs
( hopefully not spent more than once )
Update-
costs
( best kept as low as possible, just smart-update per tick )
Maintenance-
costs
( best kept as low as possible, just smart-update per aNewBar )
So, best forget the full-depth tandem-loops straight now :
Be it in
start(){...}
or
OnInit(){...}
, setup the values in
Array[]
and start filling the
CumulativeSumArray[]
... progressively and state-fully :
ArrayCopy( CumulativeSumArray, // destination
Array, // source
0, // start
0, // start
WHOLE_ARRAY ); // scope
for( int fwdBarNUMBER = Bars - 1;
fwdBarNUMBER > Bars - 201;
fwdBarNUMBER--
)
CumulativeSumArray[fwdBarNUMBER] += CumulativeSumArray[fwdBarNUMBER - 1];
for( int fwdBarNUMBER = Bars - 201;
fwdBarNUMBER >= 0;
fwdBarNUMBER--
)
CumulativeSumArray[fwdBarNUMBER] += CumulativeSumArray[fwdBarNUMBER - 1]
- CumulativeSumArray[fwdBarNUMBER - 201];
Given this pre-fill, an
onNewBar()
maintenance is as easy as:
Array[1] = ... update your value on now last known Close[1] & other var. parts
CumulativeSumArray[1] = CumulativeSumArray[ 2] + Array[1]
- CumulativeSumArray[202]; // CumSum update
and
similarly you keep updating the "hot"-end of the running bar inside an
OnTick(){...}
-handler for EA-s or inside an
if ( RefreshRates() ){...}
-POSACK-ed code-block as:
Array[0] = ... update your value on a so far known Close[0]
CumulativeSumArray[0] = CumulativeSumArray[ 1] + Array[0]
- CumulativeSumArray[201];
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: PaulB
Original post: https://stackoverflow.com/a/71322354
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
It should just be a case of getting your logic right for the loops you require. Try the following code
double Array[];
double CumulativeSumArray[];
int Length=200;
int idx=0;
for(int i=Bars-1; i>=0; i-=Length)
{
for(int j=i; j>i-Length; j--) CumulativeSumArray[idx]+=Array[j];
idx++;
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: user3666197 Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/71317053 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Q : " not sure what I'm doing wrong. " A : ( Overhead-costs + Efficiency ) MQL4/5 code-execution eco-system works differently for Script, EA and even way more strange for CustomIndicator : Setup- costs ( hopefully not spent more than once ) Update- costs ( best kept as low as possible, just smart-update per tick ) Maintenance- costs ( best kept as low as possible, just smart-update per aNewBar ) So, best forget the full-depth tandem-loops straight now : Be it in start(){...} or OnInit(){...} , setup the values in Array[] and start filling the CumulativeSumArray[] ... progressively and state-fully : ArrayCopy( CumulativeSumArray, // destination Array, // source 0, // start 0, // start WHOLE_ARRAY ); // scope for( int fwdBarNUMBER = Bars - 1; fwdBarNUMBER > Bars - 201; fwdBarNUMBER-- ) CumulativeSumArray[fwdBarNUMBER] += CumulativeSumArray[fwdBarNUMBER - 1]; for( int fwdBarNUMBER = Bars - 201; fwdBarNUMBER >= 0; fwdBarNUMBER-- ) CumulativeSumArray[fwdBarNUMBER] += CumulativeSumArray[fwdBarNUMBER - 1] - CumulativeSumArray[fwdBarNUMBER - 201]; Given this pre-fill, an onNewBar() maintenance is as easy as: Array[1] = ... update your value on now last known Close[1] & other var. parts CumulativeSumArray[1] = CumulativeSumArray[ 2] + Array[1] - CumulativeSumArray[202]; // CumSum update and similarly you keep updating the "hot"-end of the running bar inside an OnTick(){...} -handler for EA-s or inside an if ( RefreshRates() ){...} -POSACK-ed code-block as: Array[0] = ... update your value on a so far known Close[0] CumulativeSumArray[0] = CumulativeSumArray[ 1] + Array[0] - CumulativeSumArray[201];
Checking account access…