You need to use prints if the logic is not working as you expected it to. I can't tell what the script is going to do just by looking at the code, you would have to output what its doing and then make any adjustments needed to make it work the way you actually wanted. You have multiple variables being used and price conditions, you likely will need to output those details so you can better understand whats happening.
It also looks like you are setting up the variables in OnStateChange for your indicators. Not all indicators support multi timeframes, you may need to just call the indicator directly inside the BarsInProgress condition to make sure you get a value based on the secondary series.
else if(BarsInProgress == 2)
{
double indicatorValue = TrendMagicModified(Params).BullOrBear[1] ;

Comment