0

I have a bunch of cases in a switch. I need the code to stay in one case at a time until a specific criteria is met, then I want to tell it which case to go to. I currently have the case going to itself, this does not cause an error, but it does cause the program I'm using to continuously load and never execute.

The criteria I need met is the bool statement 'activated', and until it is I would like the code to remain in the case.

There is a lot of code missing so it may not make sense, but I would like to know if there is better way to do this.

Also, before you ask, I am still a beginner and I know everyone hates goto statements. If you can propose an alternate, I will listen.

Thank you wise ones.

    if(CurrentPosition.Value != 0){
            switch(location)
            {

                case 0:
                    if(criteria1 && criteria2)
                    {
                        LimitLongsPT.Send(S1 + ((R1 - S1) * BuffZone));
                        LongStopSA.Send(S1 - StopAmount);
                        ShortStopDeep.Send(R1 + StopAmount);
                        TradeManager.ProcessEvents();
                        if(activated == true)
                        {
                            if(PublicFunctions.DoubleEquals(CurrentPosition.Value, 0))
                            {
                                break;
                            }
                            if(PublicFunctions.DoubleLess(CurrentPosition.Value, 0))
                            {
                                if(PublicFunctions.DoubleGreater(MP[0], S1) && PublicFunctions.DoubleLess(MP[0], R1))
                                {
                                    goto case 15;
                                }
                                if(PublicFunctions.DoubleGreater(MP[0], R1) && PublicFunctions.DoubleLess(MP[0], R2))
                                {
                                    goto case 16;
                                }
                                if(PublicFunctions.DoubleGreater(MP[0], S2) && PublicFunctions.DoubleLess(MP[0], S1))
                                {
                                    goto case 23;
                                }
                            }
                            if(PublicFunctions.DoubleGreater(CurrentPosition.Value, 0))
                            {
                                if(PublicFunctions.DoubleGreater(MP[0], R1) && PublicFunctions.DoubleLess(MP[0], R2))
                                {
                                    goto case 1;
                                }
                            }
                        }
                    }
                    **goto case 0;**
                case 1:
                    if(PublicFunctions.DoubleGreater(CurrentPosition.Value, 0) && PublicFunctions.DoubleGreater(MP[0], R1) && PublicFunctions.DoubleLess(MP[0], R2))
                    {
  • 3
    more or less it seems like you've coded yourself in the corner there; because you switch on one thing and then if...else on the other; considering 'case 23' it also seems that this function is enormous. You need to break down your code and think carefully about each behaviour you have in this code, so that you can encapsulate it. This will help using atomic bits of logic for relevant conditions. – zaitsman Jun 13 '16 at 03:13
  • 5
    The alternative to `goto` in this case is a _state machine_ where the `switch` block is executed many times over time. `goto` is generally a sign of poor design and that you like torturing kittens ;) –  Jun 13 '16 at 03:15
  • 1
    It sounds like the criteria variables change over time here. What you have with case 0: goto case 0 is a loop. have you considered refactoring into a while loop? – Assaf Jun 13 '16 at 03:15
  • Thanks for the replies, I did forget to mention, but Assaf nailed it. In time, the condition will be met, its an ever changing variable I have no control over, and yes the switch has 32 cases in it. I started this using while loops and could not get it to work properly. I will look into this 'state machine'. Thanks again. – Steven Seagull Jun 13 '16 at 17:53

1 Answers1

1

You need to enclose the switch in a while-loop and set "location" to whatever you want next.

 if(CurrentPosition.Value != 0){
         while (location > -1) {
            switch(location)
            {

                case 0:
                    if(criteria1 && criteria2)
                    {
                        LimitLongsPT.Send(S1 + ((R1 - S1) * BuffZone));
                        LongStopSA.Send(S1 - StopAmount);
                        ShortStopDeep.Send(R1 + StopAmount);
                        TradeManager.ProcessEvents();
                        if(activated == true)
                        {
                            if(PublicFunctions.DoubleEquals(CurrentPosition.Value, 0))
                            {
                                location = -1;
                                break;
                            }
                            if(PublicFunctions.DoubleLess(CurrentPosition.Value, 0))
                            {
                                if(PublicFunctions.DoubleGreater(MP[0], S1) && PublicFunctions.DoubleLess(MP[0], R1))
                                {
                                    location = 15;
                                    break;
                                }
                                if(PublicFunctions.DoubleGreater(MP[0], R1) && PublicFunctions.DoubleLess(MP[0], R2))
                                {
                                    location = 16;
                                    break;
                                }
                                if(PublicFunctions.DoubleGreater(MP[0], S2) && PublicFunctions.DoubleLess(MP[0], S1))
                                {
                                    location = 23;
                                    break;
                                }
                            }
                            if(PublicFunctions.DoubleGreater(CurrentPosition.Value, 0))
                            {
                                if(PublicFunctions.DoubleGreater(MP[0], R1) && PublicFunctions.DoubleLess(MP[0], R2))
                                {
                                  location = 1;
                                  break;;
                                }
                            }
                        }
                    }
                    location = 0;
                    break;
                case 1:
                    if(PublicFunctions.DoubleGreater(CurrentPosition.Value, 0) && PublicFunctions.DoubleGreater(MP[0], R1) && PublicFunctions.DoubleLess(MP[0], R2))
                    {

Though personally I would avoid making such a big mess and flatten it out a bit more. Maybe look into a State Machine if that is what you need.

Community
  • 1
  • 1
Tedd Hansen
  • 12,074
  • 14
  • 61
  • 97