Reputation: 1
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))
{
Upvotes: 0
Views: 1319
Reputation: 12314
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.
Upvotes: 1