Add take profit on SMA

Created at 18 Feb 2019, 18:06
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!
SK

Skypips

Joined 12.02.2019

Add take profit on SMA
18 Feb 2019, 18:06


Hi guys,

Appreciate if anyone can help with adding Take Profit and Stop Loss with this code

Parameters TakeProfit is there but it is not placed in the action of the code.

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class Sma252 : Robot
    {
        private SimpleMovingAverage _sma;

        private Position _position;
        private bool _isShortPositionOpen;
        private bool _isLongPositionOpen;


        [Parameter(DefaultValue = 252)]
        public int Period { get; set; }

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

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

        [Parameter("Take Profit", DefaultValue = 40, MinValue = 0, Step = 10)]
        public double TakeProfit { get; set; }

        protected override void OnStart()
        {
            _sma = Indicators.SimpleMovingAverage(Source, Period);

        }

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

            var lastIndex = MarketSeries.Close.Count - 1;

            _isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
            _isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;

            double close = MarketSeries.Close[lastIndex - 1];
            double lastClose = MarketSeries.Close[lastIndex - 2];
            double sma = _sma.Result[lastIndex - 1];
            double lastSma = _sma.Result[lastIndex - 2];

            if (!_isLongPositionOpen && sma < close && lastSma > lastClose)
            {
                ClosePosition(_position);
                Buy();
            }
            else if (!_isShortPositionOpen && sma > close && lastSma < lastClose)
            {
                ClosePosition(_position);
                Sell();
            }
        }

        /// <summary>
        /// _position holds the latest opened position
        /// </summary>
        /// <param name="openedPosition"></param>
        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
        }

        /// <summary>
        /// Create Buy Order
        /// </summary>
        private void Buy()
        {
            Trade.CreateBuyMarketOrder(Symbol, Volume);
        }

        /// <summary>
        /// Create Sell Order
        /// </summary>
        private void Sell()
        {
            Trade.CreateSellMarketOrder(Symbol, Volume);
        }

        /// <summary>
        /// Close Position
        /// </summary>
        /// <param name="pos"></param>
        private void ClosePosition(Position pos)
        {
            if (pos == null)
                return;
            Trade.Close(pos);

        }

        /// <summary>
        /// Print Error
        /// </summary>
        /// <param name="error"></param>
        protected override void OnError(Error error)
        {
            Print(error.Code.ToString());
        }
    }
}

 


@Skypips
Replies

Skypips
18 Feb 2019, 18:19

I managed it with  ExecuteMarketOrder

Now the issue is the bot creates multiple orders at the same time, how can i avoid that and keep only one order until it is closed?


@Skypips

Shares4UsDevelopment
18 Feb 2019, 18:32

make a global bool  BuyOrder=false;

when doing th execute test it beign false if not execute. 

if execute set BuyOrder to true;


@Shares4UsDevelopment

Skypips
18 Feb 2019, 18:59

Thanks Ton. I have managed it by adding var Position and then  if Position == null then buy or sell happens.

 

thanks a lot


@Skypips