One position per Cbot Label.

Created at 22 Jul 2020, 16:40
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!
RY

ryan.a.blake

Joined 28.01.2019

One position per Cbot Label.
22 Jul 2020, 16:40


Hi, I have searched through the forum and found many ways to try and get the Cbot to create one trade at a time. But none of them appear to work. I feel this could be a very simple method but cant seem to get it right. Currently I use    if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0) - which works well, however this searches for positions throughout the whole account rather than just the Cbot label. So i can then place manual trades and have other robots with different labels, place trades alongside the running cbot.

Can anyone assist.

 

Code below.

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

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

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

        [Parameter("Lot Size", DefaultValue = 0.01)]
        public double lotsize { get; set; }

        [Parameter("SMA Period", DefaultValue = 1, MinValue = 1, MaxValue = 100)]
        public int smaPeriod { get; set; }

        [Parameter("Loss", DefaultValue = 50)]
        public double loss { get; set; }

        [Parameter("TakeProfitPips", DefaultValue = 10.0)]
        public int TP { get; set; }

        [Parameter("StopLossPips", DefaultValue = 10.0, MinValue = 0)]
        public int SL { get; set; }

        [Parameter("Position Label", DefaultValue = "My Label")]
        public string MyLabel { get; set; }

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

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

        private bool _isTrigerred;

        private TMASlope _tma1 { get; set; }



        protected override void OnStart()
        {

            _tma1 = Indicators.GetIndicator<TMASlope>(smaPeriod);


        }
        protected override void OnBar()
        {

            TRAILING();

            if (Trade.IsExecuting)
                return;

            if (_tma1.Sma.Last(1) > 0.0 && _tma1.Sma.Last(2) < 0.0)
              


            if (Positions.Count(x => x.TradeType == TradeType.Buy) == 0)
            {

                double volume = Symbol.QuantityToVolumeInUnits(lotsize);
                ClosePosition(TradeType.Sell);
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, InstanceName, SL, TP);




            }
            if (_tma1.Sma.Last(1) < 0.0 && _tma1.Sma.Last(2) > 0.0)

                if (Positions.Count(x => x.TradeType == TradeType.Sell) == 0)
                {
                    {
                        double volume = Symbol.QuantityToVolumeInUnits(lotsize);
                        ClosePosition(TradeType.Buy);

                        ExecuteMarketOrder(TradeType.Sell, Symbol, volume, InstanceName, SL, TP);


                    }

                }
        }
        private void ClosePosition(TradeType type)
        {
            var p = Positions.Find(InstanceName, Symbol);

            if (p != null)
            {
                ClosePosition(p);
            }

            if (Account.Balance <= loss)
                Stop();



            TRAILING();

        }
        private void TRAILING()
        {
            if (Trailing > 0.0 && Trigger > 0.0)
            {

                Position[] positions = Positions.FindAll(InstanceName, 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);
                            }
                        }
                    }
                }
            }
        }
    }
}


@ryan.a.blake
Replies

PanagiotisCharalampous
22 Jul 2020, 16:42

Hi ryan.a.blake,

Try

    if (Positions.Count(x => x.TradeType == TradeType.Sell && x.Label == InstanceName) == 0)

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

ryan.a.blake
22 Jul 2020, 17:26

RE:

PanagiotisCharalampous said:

Hi ryan.a.blake,

Try

    if (Positions.Count(x => x.TradeType == TradeType.Sell && x.Label == InstanceName) == 0)

Best Regards,

Panagiotis 

Join us on Telegram

 

Works Perfectly!! Thank you very much.


@ryan.a.blake