get { return Symbol.QuantityToVolumeInUnits(double Quantity); }

Created at 08 Jul 2021, 04:38
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MI

get { return Symbol.QuantityToVolumeInUnits(double Quantity); }
08 Jul 2021, 04:38


Hi All

Cant successfuly built the bot, having issue on the last part of the code

thanks in advance for your inputs and help

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TSBUYORSELL : Robot
    {

        #region Declaration

        public int buyPeriods;
        public int sellPeriods;

        public int noBuyMode;
        public int noSellMode;

        public double BUY1pips = 0;
        public double SELL1pips = 0;

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        //private const string label = "Sample Trend cBot";

        #endregion

        #region Parameter
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 65)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 15)]
        public int FastPeriods { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 60, MinValue = 1)]
        public int TakeProfitInPips { get; set; }

        [Parameter("trigger ", DefaultValue = 20)]
        public int Trigger { get; set; }

        [Parameter("Trailing", DefaultValue = 10)]
        public int Trailing { get; set; }

        #endregion

        protected override void OnStart()
        {

            Positions.Closed += OnPositionsClosed;


            noBuyMode = 0;
            noSellMode = 0;


            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnTick()
        {

            TRAILING();
            {
                var positionBUY1 = Positions.Find("Buy1");
                var positionSELL1 = Positions.Find("Sell1");

                if (noBuyMode > 0)
                {
                    noBuyMode--;
                }

                if (noSellMode > 0)
                {
                    noSellMode--;
                }

                //-- buy && sell periods
                if (positionBUY1 != null)
                {
                    buyPeriods++;

                }

                if (positionSELL1 != null)
                {
                    sellPeriods++;
                }

                var currentSlowMa = slowMa.Result.Last(0);
                var currentFastMa = fastMa.Result.Last(0);
                var previousSlowMa = slowMa.Result.Last(1);
                var previousFastMa = fastMa.Result.Last(1);

         
                if (noSellMode == 0 && positionBUY1 == null && positionSELL1 == null)
                {
                    Open_Sell_Order();
                    Open_Buy_Order();
                    noBuyMode = 1;
                }

     
                if (noBuyMode == 0 && positionBUY1 == null && positionSELL1 == null)
                {
                    Open_Buy_Order();
                    Open_Sell_Order();
                    noSellMode = 1;

                }

            }
        }


        private void Open_Buy_Order()
        {

            ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, "Buy1", StopLossInPips, TakeProfitInPips);

        }


        private void Open_Sell_Order()
        {


            ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, "Sell1", StopLossInPips, TakeProfitInPips);

        }


        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == "Buy1")
            {
                BUY1pips = 0;
            }
            if (position.Label == "Sell1")
            {
                SELL1pips = 0;
            }
        }

        private void TRAILING()
        {
            if (Trailing > 0 && Trigger > 0)
            {

                Position[] positions = Positions.FindAll("Buy1", "Sell1", Symbol);

                foreach (Position position in positions)
                {

                    if (position.TradeType == TradeType.Sell)
                    {

                        double distance = position.EntryPrice - Symbol.Ask;

                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize;

                            if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }

                    else
                    {

                        double distance = Symbol.Bid - position.EntryPrice;

                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize;

                            if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }
                }
            }
        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(double Quantity); }
        }
    
 


@michaelocampo1104@gmail.com
Replies

PanagiotisCharalampous
08 Jul 2021, 08:15

Hi there,

Your code has several issues which are not easy to solve unless somebody knows what are you trying to do. Did you just copy and paste things? The error message indicates that there are brackets missing but if you solve this more will pop up. The errors tell you what is the problem in most of the cases.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous