control open trades

Created at 15 Nov 2022, 20:17
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!
AN

andagon

Joined 17.10.2019

control open trades
15 Nov 2022, 20:17


Good afternoon, I am new to c.bots and I am working with a free c.bots called ON BARD SELL. I would like to be able to modify it to control the number of open operations. I found help in this forum to have only one open operation this is the code


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


the problem is that due to my ignorance I don't know where it should be located I would be very grateful if you advise me thanks

 

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 ONBARDSELL : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.001, Step = 111)]
        public double Quantity { get; set; }

        [Parameter("Enable TakeProfit?", Group = "TakeProfit", DefaultValue = true)]
        public bool useTakeProfit { get; set; }

        [Parameter("Take Profit", Group = "TakeProfit", DefaultValue = 6.6, MinValue = 0.1, Step = 0.1)]
        public double takeProfit { get; set; }

        [Parameter("Enable Stop Loss?", Group = "StopLoss", DefaultValue = true)]
        public bool useStopLoss { get; set; }

        [Parameter("Stop Loss", Group = "StopLoss", DefaultValue = 0.1, MinValue = 0.1, Step = 0.1)]
        public double stopLoss { get; set; }

        private MovingAverage tenMinimum;
        private MovingAverage tenMaximum;

        private double volumeInUnits;

        protected override void OnStart()
        {
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            tenMaximum = Indicators.MovingAverage(Bars.LowPrices, 2, MovingAverageType.Simple);
            tenMinimum = Indicators.MovingAverage(Bars.HighPrices, 1, MovingAverageType.Simple);
        }

        protected override void OnBar()
        {
            if (Positions.Count() < 2)
            {
                if (Bars.LastBar.High <= tenMaximum.Result.LastValue && !useTakeProfit)
                {
                    foreach (var position in Positions)
                    {
                        ClosePosition(position);
                    }
                }
            }

            if (Bars.LastBar.Close <= tenMinimum.Result.LastValue)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "TenMov", (useStopLoss ? stopLoss : double.MaxValue), (useTakeProfit ? takeProfit : double.MaxValue));

            }


        }
    }
}

 


@andagon
Replies

PanagiotisChar
16 Nov 2022, 09:08

Hi there,

Try this

            if (Bars.LastBar.Close <= tenMinimum.Result.LastValue)
            {
                if(Positions.Count(x => x.TradeType == TradeType.Sell) == 0)
                     ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "TenMov", (useStopLoss ? stopLoss : double.MaxValue), (useTakeProfit ? takeProfit : double.MaxValue));

            }

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar