Ok thanks for the explanation.
I added the } in the right place and solved the warning. however, the trailing stops and break even stop do not work in my opinion. I don't see them in the
Events registry
> using System.Collections.Generic;
> using System.Reflection.PortableExecutable;
> using cAlgo.API;
> using cAlgo.API.Indicators;
> using cAlgo.API.Internals;
>
> namespace cAlgo
> {
> [Robot(AccessRights = AccessRights.None)]
> public class NewcBot3 : Robot
> {
> #region User defined parameters
>
> [Parameter("Instance Name", DefaultValue = "001")]
> public string InstanceName { get; set; }
>
> [Parameter("Lot size", DefaultValue = 0.01)]
> public double LotSize { get; set; }
>
> [Parameter("Source SMA #1")]
> public DataSeries SourceSma1 { get; set; }
>
> [Parameter("Source SMA #2")]
> public DataSeries SourceSma2 { get; set; }
>
> [Parameter("Source SMA #1", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
> public int PeriodeSma1 { get; set; }
>
> [Parameter("Source SMA #2", DefaultValue = 20, MinValue = 1, MaxValue = 100)]
> public int PeriodeSma2 { get; set; }
>
> [Parameter("Include Trailing Stop",Group = "Protect", DefaultValue = false)]
> public bool IncludeTrailingStop { get; set; }
>
> [Parameter("Trailing Stop Trigger (pips)",Group = "Protect", DefaultValue = 20)]
> public int TrailingStopTrigger { get; set; }
>
> [Parameter("Trailing Stop Step (pips)", Group = "Protect", DefaultValue = 10)]
> public int TrailingStopStep { get; set; }
>
> [Parameter("Calculate OnBar", DefaultValue = false)]
> public bool CalculateOnBar { get; set; }
>
> [Parameter("Include Break-Even", DefaultValue = false)]
> public bool IncludeBreakEven { get; set; }
>
> [Parameter("Break-Even Trigger (pips)", DefaultValue = 10, MinValue = 1)]
> public int BreakEvenPips { get; set; }
>
> [Parameter("Break-Even Extra (pips)", DefaultValue = 2, MinValue = 1)]
> public int BreakEvenExtraPips { get; set; }
>
> [Parameter("Stop Loss ", Group = "Protect", DefaultValue = 100)]
> public int StopLoss { get; set; }
>
> [Parameter("Take Profit", Group = "Protect", DefaultValue = 100)]
> public int TakeProfit { get; set; }
> #endregion
>
> #region Indicator declarations
>
> private SimpleMovingAverage Sma1 { get; set; }
> private SimpleMovingAverage Sma2 { get; set; }
>
> #endregion
>
> #region cTrader events
>
> /// <summary>
> /// This is called when the robot first starts, it is only called once.
> /// </summary>
> protected override void OnStart()
> {
> //construct the indicators
> Sma1 = Indicators.SimpleMovingAverage(SourceSma1, PeriodeSma1);
> Sma2 = Indicators.SimpleMovingAverage(SourceSma2, PeriodeSma2);
> }
>
> /// <summary>
> /// This method is called every time the price change for the symbol
> /// </summary>
> protected override void OnTick()
> {
> if (CalculateOnBar)
> {
> return;
> }
>
> if (IncludeBreakEven)
> {
>
> var allPositionsBySymbol = Positions.FindAll(string.Empty, SymbolName);
> BreakEvenAdjustment(allPositionsBySymbol);
> }
>
> ManagePositions();
>
> }
>
> /// <summary>
> /// This method is called when at every candle (bar) close, when it has formed
> /// </summary>
> protected override void OnBar()
> {
> if (!CalculateOnBar)
> {
> return;
> }
>
> if (IncludeTrailingStop)
> {
> SetTrailingStop();
> }
>
>
> ManagePositions();
>
> }
>
> /// <summary>
> /// This method is called when your robot stops, can be used to clean-up memory resources.
> /// </summary>
> protected override void OnStop()
> {
> // Handle cBot stop here
> // unused
> }
> #endregion
>
> #region Position management
>
> private void ManagePositions()
> {
> if (Sma1.Result.LastValue > Sma2.Result.LastValue)
> {
> //if there is no buy position open, open one and close any sell position that is open
> if (!IsPositionOpenByType(TradeType.Buy))
> {
> OpenPosition(TradeType.Buy);
> }
>
> ClosePosition(TradeType.Sell);
> }
>
> // if a sell position is already open and signal is buy do nothing
> if (Sma1.Result.LastValue < Sma2.Result.LastValue)
> {
> //if there is no sell position open, open one and close any buy position that is open
> if (!IsPositionOpenByType(TradeType.Sell))
> {
> OpenPosition(TradeType.Sell);
> }
>
> ClosePosition(TradeType.Buy);
> }
> }
> /// <summary>
> /// Opens a new long position
> /// </summary>
> /// <param name="type"></param>
> private void OpenPosition(TradeType type)
> {
> //calculate volume from lot size.
>
> #pragma warning disable CS0618 // 'Symbol.QuantityToVolume(double)' is obsolete: 'Use QuantityToVolumeInUnits instead'
> long volume = Symbol.QuantityToVolume(LotSize);
> #pragma warning restore CS0618 // 'Symbol.QuantityToVolume(double)' is obsolete: 'Use QuantityToVolumeInUnits instead'
> // open a new position
> ExecuteMarketOrder(type, SymbolName, volume, InstanceName, StopLoss, TakeProfit);
> }
> /// <summary>
> /// Opens a new long position
> /// </summary>
> /// <param name="type"></param>
> private void ClosePosition(TradeType type)
> {
> var p = Positions.Find(InstanceName, SymbolName, type);
>
> if (p != null)
> {
> ClosePosition(p);
> }
>
> }
> #endregion
>
> #region Position Information
>
> /// <summary>
> ///
> /// </summary>
> /// <param name="type"></param>
> /// <returns></returns>
> private bool IsPositionOpenByType(TradeType type)
> {
> var p = Positions.FindAll(InstanceName, SymbolName, type);
>
> if (p.Length >= 1)
> {
> return true;
> }
> return false;
> }
> #endregion
>
> #region Trailingstop
> private void SetTrailingStop()
> {
> var sellPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Sell);
>
> foreach (Position position in sellPositions)
> {
> double distance = position.EntryPrice - Symbol.Ask;
>
> if (distance < TrailingStopTrigger * Symbol.PipSize)
> continue;
>
> double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
>
> if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
> {
> ModifyPosition(position, newStopLossPrice, position.TakeProfit);
> }
> }
>
> var buyPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Buy);
>
> foreach (Position position in buyPositions)
> {
> double distance = Symbol.Bid - position.EntryPrice;
>
> if (distance < TrailingStopTrigger * Symbol.PipSize)
> continue;
>
> double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
> if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
> {
> ModifyPosition(position, newStopLossPrice, position.TakeProfit);
> }
>
> }
>
> }
> #endregion
>
> #region Break Even
>
> private void BreakEvenAdjustment(IList<Position> allPositions)
> {
>
>
> foreach (Position position in allPositions)
> {
> if (position.StopLoss != null)
> return;
>
> var entryPrice = position.EntryPrice;
> var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;
>
> // move stop loss to break even plus and additional (x) pips
> if (distance >= BreakEvenPips * Symbol.PipSize)
> {
> if (position.TradeType == TradeType.Buy)
> {
> if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
> {
> ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
> Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
> }
> }
> else
> {
> if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
> {
> ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
> Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
> }
> #endregion
>
> }
>
> }
> }
>
> }
> }
> }