Changing from Multiple if Statements to Switch and Case with when statements gives Syntax Errors
Created at 25 May 2020, 08:41
Changing from Multiple if Statements to Switch and Case with when statements gives Syntax Errors
25 May 2020, 08:41
Hi Guys,
I am having issues with changing from the following:
#region Trailing Stop Loss
private void AtrStopLoss()
{
var positionSearch = Positions.FindAll(Label, Symbol.Name);
foreach (var position in positionSearch)
{
if (position.StopLoss == null)
continue;
var positionSize = Math.Round(position.Quantity * (LotClose / 100), 2);
double lotStep = Symbol.VolumeInUnitsToQuantity(Symbol.VolumeInUnitsStep);
double steps = positionSize / lotStep;
if (Math.Floor(steps) < steps)
{
positionSize = Math.Floor(steps) * lotStep;
}
var entryPrice = position.EntryPrice;
var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;
#region 1st ATR Break Even
if (distance >= AtrStop * Symbol.PipSize)
{
if (position.TradeType == TradeType.Buy && StopLossUpdate == 1)
{
if (position.StopLoss <= position.EntryPrice + AtrStop)
{
if (IsBacktesting)
{
ClosePosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), null);
}
else if (!IsBacktesting)
{
ModifyPosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), null);
}
StopLossUpdate = 2;
Print("1st ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR");
}
}
}
if (position.TradeType == TradeType.Sell && StopLossUpdate == 1)
{
if (position.StopLoss >= position.EntryPrice - AtrStop)
{
if (IsBacktesting)
{
ClosePosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips), null);
}
else if (!IsBacktesting)
{
ModifyPosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips), null);
}
StopLossUpdate = 2;
Print("1st ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR");
}
}
}
}
#endregion
#region 2nd ATR Trailing Stop
if (distance >= AtrStop2 * Symbol.PipSize)
{
double tsl = AtrtslMulti / 100;
if (position.TradeType == TradeType.Buy && StopLossUpdate == 2)
{
if (position.StopLoss <= position.EntryPrice + AtrStop2)
{
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * (AtrStop * tsl)), null, HasTrailingStop);
StopLossUpdate = 3;
Print("2nd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR2");
}
}
}
if (position.TradeType == TradeType.Sell && StopLossUpdate == 2)
{
if (position.StopLoss >= position.EntryPrice - AtrStop2)
{
ModifyPosition(position, position.EntryPrice - (Symbol.PipSize * (AtrStop * tsl)), null, HasTrailingStop);
StopLossUpdate = 3;
Print("2nd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR2");
}
}
}
}
#endregion
#region 3rd ATR Trailing Stop
if (!(distance >= AtrStop3 * Symbol.PipSize))
continue;
if (position.TradeType == TradeType.Buy && StopLossUpdate == 3)
{
ModifyPosition(position, position.StopLoss, null, false);
StopLossUpdate = 4;
Print("3rd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR3");
}
}
if (position.TradeType == TradeType.Sell && StopLossUpdate == 3)
{
ModifyPosition(position, position.StopLoss, null, false);
StopLossUpdate = 4;
Print("3rd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR3");
}
}
#endregion
}
}
#endregion
to this:
#region Trailing Stop Loss
private void AtrStopLoss()
{
var positionSearch = Positions.FindAll(Label, Symbol.Name);
foreach (var position in positionSearch)
{
if (position.StopLoss == null)
continue;
var positionSize = Math.Round(position.Quantity * (LotClose / 100), 2);
double lotStep = Symbol.VolumeInUnitsToQuantity(Symbol.VolumeInUnitsStep);
double steps = positionSize / lotStep;
if (Math.Floor(steps) < steps)
{
positionSize = Math.Floor(steps) * lotStep;
}
var entryPrice = position.EntryPrice;
var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;
#region 1st ATR Break Even
if (distance >= AtrStop * Symbol.PipSize)
{
switch (position.TradeType)
{
case TradeType.Buy when StopLossUpdate == 1:
{
if (position.StopLoss <= position.EntryPrice + AtrStop)
{
if (IsBacktesting)
{
ClosePosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), null);
}
else if (!IsBacktesting)
{
ModifyPosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), null);
}
StopLossUpdate = 2;
Print("1st ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR");
}
}
break;
}
case TradeType.Sell when StopLossUpdate == 1:
{
if (position.StopLoss >= position.EntryPrice - AtrStop)
{
if (IsBacktesting)
{
ClosePosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips), null);
}
else if (!IsBacktesting)
{
ModifyPosition(position, Symbol.QuantityToVolumeInUnits(positionSize));
ModifyPosition(position, position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips), null);
}
StopLossUpdate = 2;
Print("1st ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR");
}
}
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
#endregion
#region 2nd ATR Trailing Stop
if (distance >= AtrStop2 * Symbol.PipSize)
{
double tsl = AtrtslMulti / 100;
switch (position.TradeType)
{
case TradeType.Buy when StopLossUpdate == 2:
{
if (position.StopLoss <= position.EntryPrice + AtrStop2)
{
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * (AtrStop * tsl)), null, HasTrailingStop);
StopLossUpdate = 3;
Print("2nd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR2");
}
}
break;
}
case TradeType.Sell when StopLossUpdate == 2:
{
if (position.StopLoss >= position.EntryPrice - AtrStop2)
{
ModifyPosition(position, position.EntryPrice - (Symbol.PipSize * (AtrStop * tsl)), null, HasTrailingStop);
StopLossUpdate = 3;
Print("2nd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR2");
}
}
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
#endregion
#region 3rd ATR Trailing Stop
if (!(distance >= AtrStop3 * Symbol.PipSize))
continue;
switch (position.TradeType)
{
case TradeType.Buy when StopLossUpdate == 3:
{
ModifyPosition(position, position.StopLoss, null, false);
StopLossUpdate = 4;
Print("3rd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR3");
}
break;
}
case TradeType.Sell when StopLossUpdate == 3:
{
ModifyPosition(position, position.StopLoss, null, false);
StopLossUpdate = 4;
Print("3rd ATR Hit");
if (RunningMode != RunningMode.Optimization)
{
Chart.RemoveObject("ATR3");
}
break;
}
default:
throw new ArgumentOutOfRangeException();
}
#endregion
}
}
#endregion
It builds fine with on errors in Visual Studio 2019, hoever when i build the cBot in cTrader, i get 42 syntax errors ranginf from ';"expected and Invalid expression term '=='.
If someone could help with this that would be great.
PanagiotisCharalampous
25 May 2020, 08:47
Hi VertoTrading,
Can you please provide a complete cBot code that builds so that we can check?
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous