Can I make a while-loop that uses RSI value as the condition?

Can I make a while-loop that uses RSI value as the condition?

Manage alerts

Loading saved threads...

Nak1987 · External communityPost link
External question — Stack Overflow Stack Exchange Author: Nak1987 Original post: https://stackoverflow.com/questions/63555704 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. When I try to run this while -loop in MQL4, it can not seem to update the rsi from within the loop. It always returns the same value of the rsi and thus stays in an eternal loop. I have tried it on an EA as well as a script. I have also tried making rsi a globally defined variable too but still did not work. void OnTick() { double rsi = iRSI(NULL,0,14,PRICE_CLOSE,0); // defining rsi while( rsi < 50 ) { double rsi = iRSI(NULL,0,14,PRICE_CLOSE,0); // update rsi Sleep(10000); // slow down loop Alert("this is from inside while loop rsi is "+rsi); } Alert("While loop exited, condition met"); // now code goes Alert("opening an order now " ); // to place an order }
Quote
Report
user3666197 · External communityPost link
External answer — Stack Overflow Stack Exchange Author: user3666197 Original post: https://stackoverflow.com/a/63588759 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Can I make a while -loop that uses RSI value as the condition? Oh sure you can. Let's move out the colliding elements. Given the code as-is, there are several brutal misconceptions to repair: if a code spends some time "inside" such a loop, there will never be heard a new incoming QUOTE -message from market, so your Close[0] -value will remain "blind" to any such update from Market. if a second double rsi = ... declaration takes place "inside" a scope of a while -loop-constructor, the newer versions ( Builds ) of the MQL4 language will actually "mask" the older ( "outer" ) variable, that is yet still used in the loop-constructor control variables / conditions. Result? You assign "new" values to an "inner"-variable, that bears just by coincidence a same name as the "outer"-variable ( both are named rsi , yet the " inner " keeps-masking the "outer", so the "outer" never gets any update(s), if any such were stored inside the loop into the "inner" ) Solution avoid masking by not declaring any same-name named variables (always, it is a sign of good engineering practice) avoid receiving market- QUOTE -updates by deaf-loop-locking - prefer the non-blocking use of if(){...} instead of while(){...} MQL4 is a responsive-ecosystem, where OnTick() is automatically called whenever a new QUOTE -message arrives from FX-Market, so design your algorithms so as these never block. For your learning-directed inspiration, you may try your next few steps using this template : string MASK = "[%s]_INF:: " // DEF. & INIT... + "This is a QUOTE# %5d " + "Having arrived at [%s]_FX-MarketTIME\n" + "|Ask %16.8f\n" + "|Bid %16.8f\n" + "|Vol %16d\n" + "|RSI %16.8f"; double rsi = EMPTY; // DEF. & INIT... int nTicks = 0; // DEF. & INIT... void OnDeinit( const int aDeinitReason ) { EventKillTimer(); } int OnInit() { EventSetTimer( 1 ); Comment( "------------------------------------------------------\n ", "WAITING for a first QUOTE-arrival from FX-Market\n", "------------------------------------------------------" ); return( 0 ); } void OnTick() { nTicks++; // update COUNTER rsi = iRSI( NULL, 0, 14, PRICE_CLOSE, 0 ); // update rsi Comment( SetupComment() ); // update GUI // -------------------------------- FIRST 50 FX-Market Ticks delivered if ( nTicks < 50 ) return; // -------------------------------- AFTER 50 FX-Market Ticks delivered // -------------------------------- BE WARNED, THIS IS AN AWFUL ANTI-PATTERN while( True ) { rsi = iRSI( NULL, 0, 14, PRICE_CLOSE, 0 ); // update rsi Comment( SetupComment() ); // update GUI Sleep( 10000 ); } // -------------------------------- NEVER GETS HERE Alert( "?" ); } string SetupComment() { return( StringFormat( MASK, TimeToStr( TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS ), nTicks, TimeToStr( TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS ), NormalizeDouble( Ask, 8 ), NormalizeDouble( Bid, 8 ), Volume[0], NormalizeDouble( rsi, 8 ) ) ); } void OnTimer() { Comment( ChartGetString( 0, CHART_COMMENT ), "\n", TimeToStr( TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS ) ); Print( ChartGetString( 0, CHART_COMMENT ) ); }
Quote
Report
Tex · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Tex Original post: https://stackoverflow.com/a/64749150 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Declare rsi only once, outside the loop, as you already have it. To make it work as you intend REMOVE the type double from in front of variable rsi inside the loop.
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Tex Source score (net votes, not local likes): -1 Original post: https://stackoverflow.com/a/64749150 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Declare rsi only once, outside the loop, as you already have it. To make it work as you intend REMOVE the type double from in front of variable rsi inside the loop.

Cancel quote

Checking account access…