Function not retrieving values from an array correctly

Function not retrieving values from an array correctly

Manage alerts

Loading saved threads...

Pinto André · External communityPost link
External question — Stack Overflow Stack Exchange Author: Pinto André Original post: https://stackoverflow.com/questions/70316537 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 a function called Crossed1 that I'm trying to implement to collect values into an array. Then from that array I want to find the highest value of the last 5 bars. int Crossed1() { static int CurrentDirection1=0; static int LastDirection1=0; static bool FirstTime1=true; //---- if((BB20DO<BB30DO)&& (CandleCLOSE<BB20DO) && (CandleCLOSEp<BB20DOp) && (RSIi< 30)) CurrentDirection1=1; // line1 above line2 if(BB20DO>BB30DO) CurrentDirection1=2; // line1 below line2 //---- if(FirstTime1==true) // Need to check if this is the first time the function is run { FirstTime1=false; // Change variable to false LastDirection1=CurrentDirection1; // Set new direction return (0); } if(CurrentDirection1!=LastDirection1 && FirstTime1==false) // If not the first time and there is a direction change { LastDirection1=CurrentDirection1; // Set new direction return(CurrentDirection1); // 1 for up, 2 for down } else { return(0); // No direction change } } Arraymax to retrieve the max value int ArrayCalcMax() { int bars = 5; int malookback=bars; double madaily[5]; double dllv; for(int i = 0; i < malookback; i++) madaily[i] = Crossed1(); Print(" array[",i,"] = ",madaily[i]); int maxPos = ArrayMaximum(madaily,malookback,0); // Check the "maxPos" to make sure it is in range as it could be "-1" if it fails Print(" maxPos = ", maxPos ); if( maxPos >= 0 ) { dllv = madaily[maxPos]; // Please note that "dllv" is a local variable that will be discarded as soon as you return Print(" dllv = ", dllv ); } else Print("Something is wrong!"); return(dllv); } I think as the code is currently, I'm only collecting the current value in the array, is that correct? If i try to include the i , it returns the error Wrong parameters count How can I solve this?
Quote
Report
JensB · External communityPost link
External answer — Stack Overflow Stack Exchange Author: JensB Original post: https://stackoverflow.com/a/70316661 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. These lines: for(int i = 0; i < malookback; i++) madaily[i] = Crossed1(); Print(" array[",i,"] = ",madaily[i]); don't do what you think they do. They translate to for(int i = 0; i< malookback; i++) { madaily[i] = Crossed1(); } Print(" array[",i,"] = ",madaily[i]); // Error because i has been destructed When writing loop statements in C++, you have to include curly braces {} around its contents if it contains more than one line of code. Change those lines to for(int i = 0; i < malookback; i++) { madaily[i] = Crossed1(); Print(" array[",i,"] = ",madaily[i]); } and it will work as intended.
Quote
Report
Pinto André · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Pinto André Original post: https://stackoverflow.com/a/70316833 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Thank you JensB. as you said it worked OK. nonetheless the value doesn't store in the next calculation: 17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[0] = 1 0 17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[1] = 0 0 17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[2] = 0 0 17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[3] = 0 0 17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: array[4] = 0 0 17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: maxPos = 0 0 17:10:44.622 2019.08.23 12:20:00 BBReverse V1 EURUSD,H4: dllv = 1 the result for the array [1] should be 1 and it appears the value zero. 17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[0] = 0 0 17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[1] = 0 0 17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[2] = 0 0 17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[3] = 0 0 17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: array[4] = 0 0 17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: maxPos = 0 0 17:10:44.622 2019.08.23 12:40:00 BBReverse V1 EURUSD,H4: dllv = 0 Thanks André
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Pinto André Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/70316537 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 a function called Crossed1 that I'm trying to implement to collect values into an array. Then from that array I want to find the highest value of the last 5 bars. int Crossed1() { static int CurrentDirection1=0; static int LastDirection1=0; static bool FirstTime1=true; //---- if((BB20DO<BB30DO)&& (CandleCLOSE<BB20DO) && (CandleCLOSEp<BB20DOp) && (RSIi< 30)) CurrentDirection1=1; // line1 above line2 if(BB20DO>BB30DO) CurrentDirection1=2; // line1 below line2 //---- if(FirstTime1==true) // Need to check if this is the first time the function is run { FirstTime1=false; // Change variable to false LastDirection1=CurrentDirection1; // Set new direction return (0); } if(CurrentDirection1!=LastDirection1 && FirstTime1==false) // If not the first time and there is a direction change { LastDirection1=CurrentDirection1; // Set new direction return(CurrentDirection1); // 1 for up, 2 for down } else { return(0); // No direction change } } Arraymax to retrieve the max value int ArrayCalcMax() { int bars = 5; int malookback=bars; double madaily[5]; double dllv; for(int i = 0; i < malookback; i++) madaily[i] = Crossed1(); Print(" array[",i,"] = ",madaily[i]); int maxPos = ArrayMaximum(madaily,malookback,0); // Check the "maxPos" to make sure it is in range as it could be "-1" if it fails Print(" maxPos = ", maxPos ); if( maxPos >= 0 ) { dllv = madaily[maxPos]; // Please note that "dllv" is a local variable that will be discarded as soon as you return Print(" dllv = ", dllv ); } else Print("Something is wrong!"); return(dllv); } I think as the code is currently, I'm only collecting the current value in the array, is that correct? If i try to include the i , it returns the error Wrong parameters count How can I solve this?

Cancel quote

Checking account access…
Function not retrieving values from an array correctly | Forex.com.bd