nested if statements

Created at 09 Jul 2017, 20:41
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AM

amosmsiwa

Joined 30.03.2017

nested if statements
09 Jul 2017, 20:41


In trying to keep the programming code as clean as possible, can the CRL indentify these as identical statements or not?

if ((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0))
            {

                if ((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))
                {
                    if ((_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))
                    {
                        Print("BUY...." + this.Symbol);

                    }
                }

 

if ((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0)) &&((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))(_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))

 {
                        Print("BUY...." + this.Symbol);

                    }

 


@amosmsiwa
Replies

amosmsiwa
09 Jul 2017, 22:03 ( Updated at: 21 Dec 2023, 09:20 )


@amosmsiwa

PorkChop_75
13 Aug 2021, 12:32

RE: Did you get this working?

Hi, just wondering if you ever got this working? i too have been trying similar, even combining in one row as per below but no luck so far!
i simply tried combining to individual working samples, but seem to be missing something!

 

if ((_macdHistogram.Histogram.Last(1) > 0 && _macdHistogram.Histogram.Last(2) <= 0) && (_stochasticOscillator.PercentK.HasCrossedAbove(_stochasticOscillator.PercentD, 0) && _stochasticOscillator.PercentK.Last(1) <= 20))
            {
                ClosePositions(TradeType.Sell);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
            else if ((_macdHistogram.Histogram.Last(1) < 0 && _macdHistogram.Histogram.Last(2) >= 0) && (_stochasticOscillator.PercentK.HasCrossedBelow(_stochasticOscillator.PercentD, 0) && _stochasticOscillator.PercentK.Last(1) >= 80))

 

Would be great to get some help on this one.

 

amosmsiwa said:

In trying to keep the programming code as clean as possible, can the CRL indentify these as identical statements or not?

if ((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0))
            {

                if ((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))
                {
                    if ((_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))
                    {
                        Print("BUY...." + this.Symbol);

                    }
                }

 

if ((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0)) &&((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))(_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))

 {
                        Print("BUY...." + this.Symbol);

                    }

 

 


@PorkChop_75

firemyst
30 Aug 2021, 17:54

RE:

amosmsiwa said:

In trying to keep the programming code as clean as possible, can the CRL indentify these as identical statements or not?

if ((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0))
            {

                if ((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))
                {
                    if ((_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))
                    {
                        Print("BUY...." + this.Symbol);

                    }
                }

 

if ((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0)) &&((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))(_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))

 {
                        Print("BUY...." + this.Symbol);

                    }

 

This should work for you:

if (
	((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0))
	&&
	((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))
	&&
	((_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))
   )
	{
		Print("BUY...." + this.Symbol);
        }

 

However, your original code may be better in the long run because it's already formatted for extra flexibility. For instance, if you're trying to understand why certain trades aren't happening, you would probably want to do something like this:

if ((_macdH.Histogram.LastValue > _macdH.Signal.LastValue) && (_macdH.Histogram.LastValue - _macdH.Signal.LastValue > 0))
{
	if ((_stochO.PercentK.LastValue > _stochO.PercentD.LastValue) || (_stochO.PercentK.LastValue > 50))
	{
		if ((_rsi.Result.LastValue > 51) && (_pSar.Result.LastValue < MarketSeries.Close.LastValue))
		{
                	Print("BUY...." + this.Symbol);
		}
		else if (_rsi.Result.LastValue <= 51)
		{
			Print("RSI last value {0} is not > 51", _rsi.Result.LastValue);
		} 
		else if (_pSar.Result.LastValue >= MarketSeries.Close.LastValue)
		{
			Print("PSAR last value {0} >= MarketServers Close LastValue {1}", _pSar.Result.LastValue, MarketSeries.Close.LastValue);
		} 
	}
	else if (_stochO.PercentK.LastValue <= 50)
	{
		Print("Stoch %K last value {0} is <= 50", _stochO.PercentK.LastValue);
	} 
	//
	// ... etc etc etc ...
	//
}
else if ( ... )
{
	//
	// ... etc etc etc ...
	//
}

Then you can review the logs and fully grasp what is or isn't happening and why.


@firemyst