Multiple Indicators Logic Help

 
I’m currently working on logic for multiple indicators. 

The desired outcome is that indicators can be enabled and disabled individually. The expert should trade the logic of each individual indicator if only that indicator is enabled.


If multiple indicators are enabled the expert should combine the Logic and only place an order when the conditions for all enabled indicators are met. 


Sounds simple but I’ve been having issues when multiple indicators are enabled. I’ve made different iterations of the code individual conditions work. RSI and Bollinger bands are giving problems and when both are enabled RSI, BB conditions combine. But if I enable EMA1, Or EMA2 or BothEMA. The expert just starts opening orders even when RSI and BB conditions aren’t met. It seems the issue is when Moving Average related logic is enable with another or multiple indicators.


I have provided 2 of the different styles of coding this logic I have tried.


Below is the original attempt to get the desired outcome.

if(((EnableRSI && RSI[0] > RSIUpperLevel) && RSI[1] <= RSIUpperLevel) || ((EnableMA1 && ma1[1] > ma1[0]) && ma1Direction <= 0) 
|| ((EnableMA2 && ma2[1] > ma2[0]) && ma2Direction <= 0) || ((EnableBothMA && ma1[0] > ma2[0] && ma1[1] > ma2[1]) && ma1Direction <= 0 && ma2Direction <= 0) 
|| ((EnableBB &&  CurrentBBClose < bbUpper[0]) && PrevBBClose > bbUpper[1])){ 
         ma1Direction = 1;
         ma2Direction = 1;
         //Sell Order Placeholder
         }
else if(((EnableRSI && RSI[0] < RSILowerLevel) && RSI[1] >= RSILowerLevel) || ((EnableMA1 && ma1[1] < ma1[0]) && ma1Direction >= 0) 
|| ((EnableMA2 && ma2[1] < ma2[0]) && ma2Direction >= 0) || ((EnableBothMA && ma1[0] < ma2[0] && ma1[1] < ma2[1]) && ma1Direction >= 0 && ma2Direction >= 0) 
|| ((EnableBB && CurrentBBClose > bbLower[0]) && PrevBBClose < bbLower[1])){ 
         ma1Direction = -1;
         ma2Direction = -1;
         //Buy Order Placeholder
         } if(!EnableRSI || !EnableMA1 || !EnableMA2 || !EnableBothMA || !EnableBB)return;    

With the above original version. All indicators work when when individually enabled. And when multiple are enabled the RSI and BB work together properly but if I then enable one of the Moving Average logics expert seems to place trade based on MA even when RSI and BB conditions aren’t met. 



Below is another version.

if((EnableRSI && RSI[0] > RSIUpperLevel) && RSI[1] <= RSIUpperLevel){ if ((EnableMA1 && ma1[1] > ma1[0]) && ma1Direction <= 0){ 
if ((EnableMA2 && ma2[1] > ma2[0]) && ma2Direction <= 0){if((EnableBothMA && ma1[0] > ma2[0] && ma1[1] > ma2[1]) && ma1Direction <= 0 && ma2Direction <= 0){ 
if ((EnableBB &&  CurrentBBClose < bbUpper[0]) && PrevBBClose > bbUpper[1]){ 
         ma1Direction = 1;
         ma2Direction = 1;
         //Sell Order Place holder
         }}}}}
else if((EnableRSI && RSI[0] < RSILowerLevel) && RSI[1] >= RSILowerLevel){if((EnableMA1 && ma1[1] < ma1[0]) && ma1Direction >= 0){ 
if((EnableMA2 && ma2[1] < ma2[0]) && ma2Direction >= 0){if((EnableBothMA && ma1[0] < ma2[0] && ma1[1] < ma2[1]) && ma1Direction >= 0 && ma2Direction >= 0) 
{if ((EnableBB && CurrentBBClose > bbLower[0]) && PrevBBClose < bbLower[1]){ 
         ma1Direction = -1;
         ma2Direction = -1;
         //BuyOrder Placeholder
         } }}}}if(!EnableRSI || !EnableMA1 || !EnableMA2 || !EnableBothMA || !EnableBB)return;    
 

with the version above the expert has completely stopped trading even if only one of the indicators are enabled. And of course when multiple are enabled it doesn’t open orders. 


What should happen is let’s say RSI,BB and MA1 are enabled expert should only trade Buy when RSI and BB and MA1 conditions are all met.


Need guidance on what’s wrong as I’m aware there are experts that work on 8+ indicators.



Kind Regards 

 
Abdul Wahab Mughal:
I’m currently working on logic for multiple indicators. 

The desired outcome is that indicators can be enabled and disabled individually. The expert should trade the logic of each individual indicator if only that indicator is enabled.


If multiple indicators are enabled the expert should combine the Logic and only place an order when the conditions for all enabled indicators are met. 


Sounds simple but I’ve been having issues when multiple indicators are enabled. I’ve made different iterations of the code individual conditions work. RSI and Bollinger bands are giving problems and when both are enabled RSI, BB conditions combine. But if I enable EMA1, Or EMA2 or BothEMA. The expert just starts opening orders even when RSI and BB conditions aren’t met. It seems the issue is when Moving Average related logic is enable with another or multiple indicators.


I have provided 2 of the different styles of coding this logic I have tried.


Below is the original attempt to get the desired outcome.

With the above original version. All indicators work when when individually enabled. And when multiple are enabled the RSI and BB work together properly but if I then enable one of the Moving Average logics expert seems to place trade based on MA even when RSI and BB conditions aren’t met. 



Below is another version.

with the version above the expert has completely stopped trading even if only one of the indicators are enabled. And of course when multiple are enabled it doesn’t open orders. 


What should happen is let’s say RSI,BB and MA1 are enabled expert should only trade Buy when RSI and BB and MA1 conditions are all met.


Need guidance on what’s wrong as I’m aware there are experts that work on 8+ indicators.



Kind Regards 

What happens is on the first version any indicator can fire the signal . 

For the second version , i assume you want all the indicators that are enabled to fire at the same signal   a signal(or be at that state)

If i understand correctyly then you should have a "tally to beat" variable which you initialize at the start of the ea and it counts how many indicators are enabled.

Then you treat each indicator as a "vote" , and in this case you need all the votes to be in for a trade .

 
Lorentzos Roussos #:

What happens is on the first version any indicator can fire the signal . 

For the second version , i assume you want all the indicators that are enabled to fire at the same signal   a signal(or be at that state)

If i understand correctyly then you should have a "tally to beat" variable which you initialize at the start of the ea and it counts how many indicators are enabled.

Then you treat each indicator as a "vote" , and in this case you need all the votes to be in for a trade .

Hello to clarify each individual indicator has its own Boolean. What I want is when multiple indicators are enabled the Expert should open orders when all the enabled conditions are met. So if only 1 indicator is enabled then only that condition needs to be met. If 4 indicators are enabled then the conditions of all 4 indicators needs to be met before an order is issued.


Vice versa for any number of enabled Indicators.


A tally wouldn’t work I don’t think as it can’t exclude the disabled indicators.


I have tried a different way shown below. The expert still doesn’t trade for any number of enable indicators so somewhere is wrong.


            bool buyCondition = true;
            bool sellCondition = true;

            if (EnableRSI)
            {
                buyCondition = buyCondition && (RSI[0] < RSILowerLevel && RSI[1] >= RSILowerLevel);
                sellCondition = sellCondition && (RSI[0] > RSIUpperLevel && RSI[1] <= RSIUpperLevel);
            }

            if (EnableMA1)
            {
                buyCondition = buyCondition && (ma1[1] < ma1[0] && ma1Direction >= 0);
                sellCondition = sellCondition && (ma1[1] > ma1[0] && ma1Direction <= 0);
            }

            if (EnableMA2)
            {
                buyCondition = buyCondition && (ma2[1] < ma2[0] && ma2Direction >= 0);
                sellCondition = sellCondition && (ma2[1] > ma2[0] && ma2Direction <= 0);
            }

            if (EnableBothMA)
            {
                buyCondition = buyCondition && (ma1[0] < ma2[0] && ma1[1] < ma2[1]);
                sellCondition = sellCondition && (ma1[0] > ma2[0] && ma1[1] > ma2[1]);
            }

            if (EnableBB)
            {
                buyCondition = buyCondition && (CurrentBBClose > bbLower[0] && PrevBBClose < bbLower[1]);
                sellCondition = sellCondition && (CurrentBBClose < bbUpper[0] && PrevBBClose > bbUpper[1]);
            }

            if (buyCondition)
            {
                // Place your buy trade here
            }
            else if (sellCondition)
            {
                // Place your sell trade here
            }
        }




Kind Regards 

 
Abdul Wahab Mughal #:

Hello to clarify each individual indicator has its own Boolean. What I want is when multiple indicators are enabled the Expert should open orders when all the enabled conditions are met. So if only 1 indicator is enabled then only that condition needs to be met. If 4 indicators are enabled then the conditions of all 4 indicators needs to be met before an order is issued.


Vice versa for any number of enabled Indicators.


A tally wouldn’t work I don’t think as it can’t exclude the disabled indicators.


I have tried a different way shown below. The expert still doesn’t trade for any number of enable indicators so somewhere is wrong.





Kind Regards 

I see

Are the indicator values called in properly ?

 
Lorentzos Roussos #:

I see

Are the indicator values called in properly ?

Hello, yes they are. I have used print statements to check and Handles and Copy buffer are correct. 

And actually with the original logic code. When each individual indicator is enabled on its own it works where expert trades based on that individual enabled indicators logic. The issue comes when enabling multiple indicators. I’m using RSI, BB, MA1 and MA2. 

If I only use RSI AND BB then expert opens orders when both conditions are met first, as intended. However if I enable one of the MA aswell then expert just starts opening orders even when RSI and BB conditions are not met. 

It seems the expert with the original conditions is trading only based on MA when MA is enabled with the other indicators. What it’s suppose to do is only trade once all enabled indicators conditions are met. And ignores the disabled indicator conditions.


For the second version I thought it would work but expert is not opening orders at all even if only one indicator is enabled or multiple.


I’m thinking it needs some sort of Priority, Verification, Confirmation but I’m not sure how to do that.


Kind Regards 
 
Abdul Wahab Mughal #:
Hello, yes they are. I have used print statements to check and Handles and Copy buffer are correct. 

And actually with the original logic code. When each individual indicator is enabled on its own it works where expert trades based on that individual enabled indicators logic. The issue comes when enabling multiple indicators. I’m using RSI, BB, MA1 and MA2. 

If I only use RSI AND BB then expert opens orders when both conditions are met first, as intended. However if I enable one of the MA aswell then expert just starts opening orders even when RSI and BB conditions are not met. 

It seems the expert with the original conditions is trading only based on MA when MA is enabled with the other indicators. What it’s suppose to do is only trade once all enabled indicators conditions are met. And ignores the disabled indicator conditions.


For the second version I thought it would work but expert is not opening orders at all even if only one indicator is enabled or multiple.


I’m thinking it needs some sort of Priority, Verification, Confirmation but I’m not sure how to do that.


Kind Regards 

If you disable all indicators does it trade ?

What are ma1Direction and ma2Direction ?
 
Lorentzos Roussos #:

If you disable all indicators does it trade ?

What are ma1Direction and ma2Direction ?
For the original conditions code, when all indicators are disabled the expert doesn’t trade, it’s not supposed to so that is okay.


For ma1Direction and ma2Direction, they are how i learned to code for a single moving average expert. Part of coding for a single moving average was using a int global variable that’s = 0 and when sell or buy is fired the variable is changed to 1 for Buy and -1 to Sell. I believe it’s part of determining moving average positive or negative direction. 

It’s just how I learned to code a single MA.
Also can confirm that the Moving average logic works for MA1 and MA2 individually and if both ma1 and ma2 are enabled.


Considering that when I enable multiple indicators. Through process of elimination it seems the issue is with the MA1 and MA2 and BothMAs logic. 

It could be the way I’ve done the logic doesn’t work with multiple indicators.

I’m that case how else would I the Moving Average logics without using the MaDirection variable?

All the above is for the original code.

For the second version I have no idea why it’s completely stopped working.




 
Abdul Wahab Mughal #:
For the original conditions code, when all indicators are disabled the expert doesn’t trade, it’s not supposed to so that is okay.


For ma1Direction and ma2Direction, they are how i learned to code for a single moving average expert. Part of coding for a single moving average was using a int global variable that’s = 0 and when sell or buy is fired the variable is changed to 1 for Buy and -1 to Sell. I believe it’s part of determining moving average positive or negative direction. 

It’s just how I learned to code a single MA.
Also can confirm that the Moving average logic works for MA1 and MA2 individually and if both ma1 and ma2 are enabled.


Considering that when I enable multiple indicators. Through process of elimination it seems the issue is with the MA1 and MA2 and BothMAs logic. 

It could be the way I’ve done the logic doesn’t work with multiple indicators.

I’m that case how else would I the Moving Average logics without using the MaDirection variable?

All the above is for the original code.

For the second version I have no idea why it’s completely stopped working.




Sort of a way to save the last signal ,i see . 

So , unless i'm blind , i can't see the issue here . I believe the problem is in another part of the code in conjuction with this method of firing signals 

 
Lorentzos Roussos #:

Sort of a way to save the last signal ,i see . 

So , unless i'm blind , i can't see the issue here . I believe the problem is in another part of the code in conjuction with this method of firing signals 

The problem has to be with the conditions as the indicators work on their own just not together.

The thing is that if only RSI and BB are enabled then the original works both RSI and BB conditions are met first before opening order. It’s only when one or more of the moving averages is also enabled when expert starts opening orders almost straight away even when the RSI and BB aren’t met. 

What I noticed is that the expert will open and buy or sell which is line with the moving average condition but not with the RSI or BB.

I believe the way the moving average is coded, maybe because of the Madirections variables that it is getting priority over the non MA indicators.

What other way would there be to code the MA1 and MA2 each with individual conditions without using The madirection variables that I was originally taught. Maybe that would fix it? 

You’re welcome to try the logic for yourself and see if that will help understand the problem.


Kind Regards 

 
Abdul Wahab Mughal #:
The problem has to be with the conditions as the indicators work on their own just not together.

The thing is that if only RSI and BB are enabled then the original works both RSI and BB conditions are met first before opening order. It’s only when one or more of the moving averages is also enabled when expert starts opening orders almost straight away even when the RSI and BB aren’t met. 

What I noticed is that the expert will open and buy or sell which is line with the moving average condition but not with the RSI or BB.

I believe the way the moving average is coded, maybe because of the Madirections variables that it is getting priority over the non MA indicators.

What other way would there be to code the MA1 and MA2 each with individual conditions without using The madirection variables that I was originally taught. Maybe that would fix it? 

You’re welcome to try the logic for yourself and see if that will help understand the problem.


Kind Regards 

could it be too rare that they fire at the exact same time ? Since there is no flaw in the code above (from what i can see) and there is no flaw in the other parts .

 
Lorentzos Roussos #:

could it be too rare that they fire at the exact same time ? Since there is no flaw in the code above (from what i can see) and there is no flaw in the other parts .

No the problem doesn’t correlate with the conditions combined being too rare.

As what’s happening is let’s say RSI, BB and MA1 are enabled only. The expert after being placed on chart will open trades even when RSI and BB are not met. However I noticed that whether it’s a buy or sell is following the MA1 logic.

So it seems the Moving Average logic is somehow overriding the other indicators logic. Or is being prioritised. It could be due to using MA1Direction variable in the firing block of code.


For the second version. Let’s say only RSI is enabled. The expert doesn’t trade at all. I’ve tried all the indicators individually, I’ve tried different combinations of enabled indicators and I’ve tried all together. No orders are being fired at all.

Even if I just modify the original code like below expert stops trading completely.


if((EnableRSI && RSI[0] > RSIUpperLevel) && RSI[1] <= RSIUpperLevel){ if((EnableMA1 && ma1[1] > ma1[0]) && ma1Direction <= 0) 
|| ((EnableMA2 && ma2[1] > ma2[0]) && ma2Direction <= 0) || ((EnableBothMA && ma1[0] > ma2[0] && ma1[1] > ma2[1]) && ma1Direction <= 0 && ma2Direction <= 0) 
|| ((EnableBB &&  CurrentBBClose < bbUpper[0]) && PrevBBClose > bbUpper[1]){ 
         ma1Direction = 1;
         ma2Direction = 1;
         //Sell Order Placeholder
         }}
else if((EnableRSI && RSI[0] < RSILowerLevel) && RSI[1] >= RSILowerLevel){ if((EnableMA1 && ma1[1] < ma1[0]) && ma1Direction >= 0) 
|| ((EnableMA2 && ma2[1] < ma2[0]) && ma2Direction >= 0) || ((EnableBothMA && ma1[0] < ma2[0] && ma1[1] < ma2[1]) && ma1Direction >= 0 && ma2Direction >= 0) 
|| ((EnableBB && CurrentBBClose > bbLower[0]) && PrevBBClose < bbLower[1]){ 
         ma1Direction = -1;
         ma2Direction = -1;
         //Buy Order Placeholder
         }} if(!EnableRSI || !EnableMA1 || !EnableMA2 || !EnableBothMA || !EnableBB)return;    



Just one change at RSI stops it from working completely.


Okay a separate question if you were trying to use 4 indicators and achieve the same desired function as me How would you code it? Maybe that would be a good starting point to figure the issue out


Kind Regards