Bot opens multiple positions

Created at 07 May 2020, 14:21
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!
AM

amirus.stark

Joined 01.05.2020

Bot opens multiple positions
07 May 2020, 14:21


Hi, I'm still learning, and can't fix something, I tried tons of workarounds, the bot opens a position every single bar the conditions are met, but I don't want it to open a long position if there's already a long one.

Here's the code ! 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MACDbot : Robot
    {


        private Position _position;

        private ZeroLagMACD _macd;
        private ATRStops _atr;
        private VWAP _vwap;
        string Label = "MACDbot";

        private bool _isShortPositionOpen;
        private bool _isLongPositionOpen;






        [Parameter(DefaultValue = 10000)]
        public int Volume { get; set; }



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

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







        protected override void OnStart()
        {

            _macd = Indicators.GetIndicator<ZeroLagMACD>(26, 12, 9);
            _atr = Indicators.GetIndicator<ATRStops>(MovingAverageType.Simple, 15, 3.0, true);
            _vwap = Indicators.GetIndicator<VWAP>(0, false);

        }

        protected override void OnBar()
        {
            if (Trade.IsExecuting)
                return;


            var lastIndex = Bars.ClosePrices.Count - 1;
            double close = Bars.ClosePrices[lastIndex - 1];
            
            _isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
            _isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;




            if (_position == null && _macd.MACD.Last(0) > _macd.Signal.Last(0) && close > _atr.Result.Last(0) && close > _vwap.Result.Last(0))
            {

                Buy();

            }
            if (_position == null && _macd.MACD.Last(0) < _macd.Signal.Last(0) && close < _atr.Result.Last(0) && close < _vwap.Result.Last(0))
            {

                Sell();




            }
        }


        private void ClosePosition()
        {
            if (_position != null)
            {
                ClosePosition(_position);
                _position = null;
            }
        }

        private void Buy()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "RavenMKII", StopLoss, TakeProfit);
        }

        private void Sell()
        {
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "RavenMKII", StopLoss, TakeProfit);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
        }

    }
}


@amirus.stark
Replies

PanagiotisCharalampous
07 May 2020, 14:42

Hi amirus.stark,

You can add a condition checking if positions are 0 before opening a position e.g. Positions.Count() == 0.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous