How can I shorten this code to avoid redundancy?

How can I shorten this code to avoid redundancy?

Manage alerts

Loading saved threads...

Renz Carillo · External communityPost link
External question — Stack Overflow Stack Exchange Author: Renz Carillo Original post: https://stackoverflow.com/questions/66470554 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Here's my code snippet: string trend() { double emaTrend0 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0); double emaTrend1 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,1); double emaTrend2 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,2); double emaTrend3 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,3); double emaTrend4 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,4); double emaTrend5 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,5); double emaTrend6 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,6); double emaTrend7 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,7); double emaTrend8 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,8); double emaTrend9 = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,9); string signal; double HighCandle0 = High[0]; double HighCandle1 = High[1]; double HighCandle2 = High[2]; double HighCandle3 = High[3]; double HighCandle4 = High[4]; double HighCandle5 = High[5]; double HighCandle6 = High[6]; double HighCandle7 = High[7]; double HighCandle8 = High[8]; double HighCandle9 = High[9]; double LowCandle0 = Low[0]; double LowCandle1 = Low[1]; double LowCandle2 = Low[2]; double LowCandle3 = Low[3]; double LowCandle4 = Low[4]; double LowCandle5 = Low[5]; double LowCandle6 = Low[6]; double LowCandle7 = Low[7]; double LowCandle8 = Low[8]; double LowCandle9 = Low[9]; if (emaTrend0 > HighCandle0 && emaTrend1 > HighCandle1 && emaTrend2 > HighCandle2 &&emaTrend3 > HighCandle3 && emaTrend4 > HighCandle4 &&emaTrend5 > HighCandle5 &&emaTrend6 > HighCandle6 &&emaTrend7 > HighCandle7 && emaTrend8 > HighCandle8 && emaTrend9 > HighCandle9 ) signal = "downtrend"; else if (emaTrend0 < LowCandle0 && emaTrend1 < LowCandle1 && emaTrend2 < LowCandle2 &&emaTrend3 < LowCandle3 && emaTrend4 < LowCandle4 &&emaTrend5 < LowCandle5 &&emaTrend6 < LowCandle6 &&emaTrend7 < LowCandle7 && emaTrend8 < LowCandle8 && emaTrend9 < LowCandle9 ) signal = "uptrend"; return signal; } What I wanted to happen is to loop the variable and every condition must be true before producing the signal such as downtrend or uptrend . The code below is my attempt, but it was still no good. My attempt produced a signal of downtrend or uptrend when one of the conditions is true , but what I need is for everything to be true before producing a signal. string trend() { string signal = ""; for (int i = 0; i<=9 ; i++){ double emaTrend = iMA (NULL,0,200,0,MODE_EMA,PRICE_CLOSE,i); double highCandle = High[i]; double lowCandle = Low[i]; if (emaTrend > highCandle){ signal = "downtrend"; } else if (emaTrend < lowCandle){ signal = "uptrend"; } } return signal; } All replies would be deeply appreciated. Thank you!
Quote
Report
Azeem · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Azeem Original post: https://stackoverflow.com/a/66470937 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Here's a possible solution by counting the number of down and up trends and then compare those counts ( not tested ): std::string trend() { const std::size_t max_count = 10; std::size_t count_downtrend = 0; std::size_t count_uptrend = 0; for (std::size_t i = 0; i != max_count; ++i) { const double emaTrend = iMA (NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, i); const double highCandle = High[i]; const double lowCandle = Low[i]; if (emaTrend > highCandle) { ++count_downtrend; } else if (emaTrend < lowCandle) { ++count_uptrend; } } std::string signal; if (count_downtrend == max_count) { signal = "downtrend"; } else if (count_uptrend == max_count) { signal = "uptrend"; } return signal; }
Quote
Report
molbdnilo · External communityPost link
External answer — Stack Overflow Stack Exchange Author: molbdnilo Original post: https://stackoverflow.com/a/66471271 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. This is very similar to the regular "linear search loop", with an early break if the trend is broken: enum Direction { Up, Down, None }; Direction direction(double value, double low, double high) { return value < low ? Down : (value > high ? Up : None); } double emaClose(int i) { return iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,i); } string trend() { const Direction d = direction(emaClose(0), Low[0], High[0]); if (d == None) { return ""; } for (int i = 1; i < 10 ; i++) { if (direction(emaClose(i), Low[i], High[i]) != d) { return ""; } } return d == Up ? "uptrend" : "downtrend"; }
Quote
Report

Post Reply

Checking account access…