Add take profit on SMA Cbot

Created at 05 Aug 2019, 05:44
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!
BR

brunopsirh

Joined 05.08.2019

Add take profit on SMA Cbot
05 Aug 2019, 05:44


Hi guys,

Appreciate if anyone can help with adding Take Profit with this code

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

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 TrendRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

        [Parameter("Slow Periods", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

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

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

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend Robot";

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);

        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);


            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {

                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);

            }

        }
    }
}

 


@brunopsirh
Replies

Symposium
05 Aug 2019, 07:41

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleTrend : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

        [Parameter("Fast Period", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter("Slow Period", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 0.01, MinValue = 0)]
        public int StopLossInPips { get; set; }

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


        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot";

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnTick()
        {

            {
                var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
                var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

                var currentSlowMa = slowMa.Result.Last(0);
                var currentFastMa = fastMa.Result.Last(0);
                var previousSlowMa = slowMa.Result.Last(1);
                var previousFastMa = fastMa.Result.Last(1);

                if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
                {
                    if (shortPosition != null)
                        ClosePosition(shortPosition);
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
                }
                else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
                {
                    if (longPosition != null)
                        ClosePosition(longPosition);
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label, StopLossInPips, TakeProfitInPips);
                }

            }
        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

I have coded a version with a Trailing Stop, Let me know if you want it.? For the code above.. I have added the SL as well... just set it to zero if it's not being used....

Hope this is of help... Cheers


@Symposium