Replies

giovanisgarabotto
06 Oct 2020, 17:17

RE:

PanagiotisCharalampous said:

Hi giovanisgarabotto,

For somebody to help you, you need to share your cBot code and explain what do you need to be changed.

Best Regards,

Panagiotis 

Join us on Telegram

grateful for your return follows below the code:

The code is this but I would like you not to open orders while there is an operation in progress. Only make 1 entry when there are no open transactions. Can you help me?

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NStrategy : Robot
    {
        #region Parameters

        [Parameter("Volume", DefaultValue = 1000, Group = "Risk Managemment")]
        public double Volume { get; set; }

        [Parameter("Take Profit", DefaultValue = 10, Group = "Risk Managemment")]
        public double TP { get; set; }

        [Parameter("Stop Loss", DefaultValue = 10, Group = "Risk Managemment")]
        public double SL { get; set; }

        [Parameter("Min AF", DefaultValue = 0.02, Group = "Parabolic SAR")]
        public double MinAF { get; set; }

        [Parameter("Max AF", DefaultValue = 0.2, Group = "Parabolic SAR")]
        public double MaxAF { get; set; }

        [Parameter("Instance", DefaultValue = "Instance")]
        public string Instance { get; set; }

        #endregion Parameters

        #region Private Variables

        private ParabolicSAR _psar;

        #endregion Private Variables

        #region Methods

        protected override void OnStart()
        {
            // We initialize the PSAR Indicator
            _psar = Indicators.ParabolicSAR(MinAF, MaxAF);
        }

        protected override void OnBar()
        {
            // For purchase we make the entry when there is a green bar, a red one and soon
            // after the top(previous) bar breaks in 1 PIP we will have a purchase order.
            // Purchases below PSAR should not be valid
            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0 && _psar.Result.LastValue < Symbol.Bid)
            {
                if (Bars.OpenPrices.Last(2) < Bars.ClosePrices.Last(2) && Bars.OpenPrices.Last(1) > Bars.ClosePrices.Last(1))
                {
                    // if this break does  not occur in a maximum of 2 bars, we discard the entry
                    PlaceStopOrder(TradeType.Buy, Symbol.Name, Volume, Bars.HighPrices.Last(1) + Symbol.PipSize, Instance, SL, TP, Server.Time.Add(Bars.OpenTimes.LastValue - Bars.OpenTimes.Last(2)));
                }
            }
            // For sale, when there is a red bar and a green bar, then if there is a break in the minimum with
            // another red bar(1PIP), we will have a sales order.
            // Sales above PSAR should not be valid
            if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0 && _psar.Result.LastValue > Symbol.Bid)
            {
                if (Bars.OpenPrices.Last(2) > Bars.ClosePrices.Last(2) && Bars.OpenPrices.Last(1) < Bars.ClosePrices.Last(1))
                {
                    // if this break does  not occur in a maximum of 2 bars, we discard the entry
                    PlaceStopOrder(TradeType.Sell, Symbol.Name, Volume, Bars.LowPrices.Last(1) - Symbol.PipSize, Instance, SL, TP, Server.Time.Add(Bars.OpenTimes.LastValue - Bars.OpenTimes.Last(2)));
                }
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }

        #endregion Methods
    }
}


@giovanisgarabotto