Take Profit doesn't work...

Created at 02 May 2018, 19:08
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!
irmscher9's avatar

irmscher9

Joined 22.12.2016

Take Profit doesn't work...
02 May 2018, 19:08


Hey

For some strange reason Take Profit doesn't get triggered in some cases:

https://1drv.ms/u/s!AvJJop5QTya7gYFXETTXtTytprVPjw

NZDJPY 23/08/2015-31/08/2015 tick mode

Here's the cBOT:

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 NewcBot : Robot
    {
        //--TODO:
        //-- 1. Chaikin Volotility, if above 500 - don't trade for X amount of time.

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

        public DonchianChannel _dnc;
        public DonchianChannel _dnc100;
        public AcceleratorOscillator _acc;
        public RainbowOscillator _rbw;

        public DirectionalMovementSystem _dms;

        public ChaikinVolatility _ckn;

        public CommodityChannelIndex _cci;

        public RelativeStrengthIndex _rsi;

        public MacdHistogram _mcd;

        public ExponentialMovingAverage _ema50;

        public int buyPeriods;
        public int sellPeriods;

        public int noBuyMode;
        public int noSellMode;

        public int strUptrend = 0;
        public int strDowntrend = 0;

        public int rbwBUY = 0;
        public int rbwSELL = 0;

        public double BUY1pips = 0;
        public double SELL1pips = 0;


        protected override void OnStart()
        {

            Positions.Closed += OnPositionsClosed;

            _dnc = Indicators.DonchianChannel(20);
            _dnc100 = Indicators.DonchianChannel(100);
            _acc = Indicators.AcceleratorOscillator();
            _rbw = Indicators.RainbowOscillator(SourceSeries, 9, MovingAverageType.Simple);

            _dms = Indicators.DirectionalMovementSystem(14);

            _ckn = Indicators.ChaikinVolatility(14, 10, MovingAverageType.Simple);

            _cci = Indicators.CommodityChannelIndex(20);

            _rsi = Indicators.RelativeStrengthIndex(SourceSeries, 21);

            _mcd = Indicators.MacdHistogram(50, 15, 9);

            _ema50 = Indicators.ExponentialMovingAverage(SourceSeries, 50);

            Print("The current symbol has pip size of: {0}", Symbol.PipSize);

            noBuyMode = 0;
            noSellMode = 0;

        }

        protected override void OnTick()
        {
            //--
            var positionBUY1 = Positions.Find("Buy1");
            var positionSELL1 = Positions.Find("Sell1");

            //-- current price
            double ask = Math.Round(Symbol.Ask, 5);
            double bid = Math.Round(Symbol.Bid, 5);
            double current_price = (ask + bid) / 2;

            //----- dmsC
            double dmsD0 = _dms.DIPlus.Last(0) - _dms.DIMinus.Last(0);
            double dmsD1 = _dms.DIPlus.Last(1) - _dms.DIMinus.Last(1);
            double dmsC = dmsD0 - dmsD1;

            //-- str Trends
            if (_rsi.Result.LastValue >= 75)
            {
                strUptrend = 1;
            }

            if (strUptrend > 0 && _rsi.Result.LastValue < 50)
            {
                strUptrend = 0;
            }

            if (_rsi.Result.LastValue <= 25)
            {
                strDowntrend = 1;
            }

            if (strDowntrend > 0 && _rsi.Result.LastValue > 50)
            {
                strDowntrend = 0;
            }

            //-- bools
            bool rsi25_30 = _rsi.Result.Minimum(30) <= 25;
            bool rsi75_30 = _rsi.Result.Maximum(30) >= 75;

            double C = (current_price - _ema50.Result.LastValue) * 1000;



            //-- && current_price > _dnc.Middle.LastValue 
            double spread = Symbol.Spread * 100;
            double SL = 60;
            if (spread > 10)
                SL = 60 + Math.Round(spread * 1.5);


            //-- 
            double A = (current_price - MarketSeries.Open.LastValue) * 100;
            double rbwT = (A * _rbw.Result.LastValue) * -1;



            //-------------- BUY1 SELL1 
            //--  && _rbw.Result.LastValue < 1.5 && A < 200 && rbwT < 200
            if (_ckn.Result.LastValue > 66 && dmsC > 11 && spread < 800 && noBuyMode == 0 && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 100000, "Buy1", SL, 200);
                noBuyMode = 1;

            }

            //-- && current_price < _dnc.Middle.LastValue && _rbw.Result.LastValue > -1.5 && A > -200 && rbwT > -200
            if (_ckn.Result.LastValue > 66 && dmsC < -11 && spread < 800 && noSellMode == 0 && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 100000, "Sell1", SL, 200);
                noSellMode = 1;

            }

        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == "Buy1")
            {
                BUY1pips = 0;
            }
            if (position.Label == "Sell1")
            {
                SELL1pips = 0;
            }
        }

        protected override void OnBar()
        {

            //-- positions
            //-- find positions
            var positionBUY1 = Positions.Find("Buy1");
            var positionSELL1 = Positions.Find("Sell1");


            if (noBuyMode > 0)
            {
                noBuyMode--;
            }

            if (noSellMode > 0)
            {
                noSellMode--;
            }

            //-- buy && sell periods
            if (positionBUY1 != null)
            {
                buyPeriods++;

            }

            if (positionSELL1 != null)
            {
                sellPeriods++;

            }
        }
    }

}

 

Interestingly, when I remove the  rbwT < 200 and  rbwT > -200 the 200 Take Profit works just fine.

rbwT is my coefficient that makes sure the position doesn't get opened too deeply into the trend, just in case the formula is this:

double A = (current_price - MarketSeries.Open.LastValue) * 100;
 double rbwT = (A * _rbw.Result.LastValue) * -1;

but all in all if we remove these two from the BUY and SELL it will just let the position open later and deeper into the bearish candle, which you can see below:

https://1drv.ms/u/s!AvJJop5QTya7gYFY0nk6xQbk5IeAww

Here's the version of the cBOT without rbwT:

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 NewcBot : Robot
    {
        //--TODO:
        //-- 1. Chaikin Volotility, if above 500 - don't trade for X amount of time.

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

        public DonchianChannel _dnc;
        public DonchianChannel _dnc100;
        public AcceleratorOscillator _acc;
        public RainbowOscillator _rbw;

        public DirectionalMovementSystem _dms;

        public ChaikinVolatility _ckn;

        public CommodityChannelIndex _cci;

        public RelativeStrengthIndex _rsi;

        public MacdHistogram _mcd;

        public ExponentialMovingAverage _ema50;

        public int buyPeriods;
        public int sellPeriods;

        public int noBuyMode;
        public int noSellMode;

        public int strUptrend = 0;
        public int strDowntrend = 0;

        public int rbwBUY = 0;
        public int rbwSELL = 0;

        public double BUY1pips = 0;
        public double SELL1pips = 0;


        protected override void OnStart()
        {

            Positions.Closed += OnPositionsClosed;

            _dnc = Indicators.DonchianChannel(20);
            _dnc100 = Indicators.DonchianChannel(100);
            _acc = Indicators.AcceleratorOscillator();
            _rbw = Indicators.RainbowOscillator(SourceSeries, 9, MovingAverageType.Simple);

            _dms = Indicators.DirectionalMovementSystem(14);

            _ckn = Indicators.ChaikinVolatility(14, 10, MovingAverageType.Simple);

            _cci = Indicators.CommodityChannelIndex(20);

            _rsi = Indicators.RelativeStrengthIndex(SourceSeries, 21);

            _mcd = Indicators.MacdHistogram(50, 15, 9);

            _ema50 = Indicators.ExponentialMovingAverage(SourceSeries, 50);

            Print("The current symbol has pip size of: {0}", Symbol.PipSize);

            noBuyMode = 0;
            noSellMode = 0;

        }

        protected override void OnTick()
        {
            //--
            var positionBUY1 = Positions.Find("Buy1");
            var positionSELL1 = Positions.Find("Sell1");

            //-- current price
            double ask = Math.Round(Symbol.Ask, 5);
            double bid = Math.Round(Symbol.Bid, 5);
            double current_price = (ask + bid) / 2;

            //----- dmsC
            double dmsD0 = _dms.DIPlus.Last(0) - _dms.DIMinus.Last(0);
            double dmsD1 = _dms.DIPlus.Last(1) - _dms.DIMinus.Last(1);
            double dmsC = dmsD0 - dmsD1;

            //-- str Trends
            if (_rsi.Result.LastValue >= 75)
            {
                strUptrend = 1;
            }

            if (strUptrend > 0 && _rsi.Result.LastValue < 50)
            {
                strUptrend = 0;
            }

            if (_rsi.Result.LastValue <= 25)
            {
                strDowntrend = 1;
            }

            if (strDowntrend > 0 && _rsi.Result.LastValue > 50)
            {
                strDowntrend = 0;
            }

            //-- bools
            bool rsi25_30 = _rsi.Result.Minimum(30) <= 25;
            bool rsi75_30 = _rsi.Result.Maximum(30) >= 75;

            double C = (current_price - _ema50.Result.LastValue) * 1000;



            //-- && current_price > _dnc.Middle.LastValue 
            double spread = Symbol.Spread * 100;
            double SL = 60;
            if (spread > 10)
                SL = 60 + Math.Round(spread * 1.5);


            //-- 
            double A = (current_price - MarketSeries.Open.LastValue) * 100;
            double rbwT = (A * _rbw.Result.LastValue) * -1;



            //-------------- BUY1 SELL1 
            //-- 
            if (_ckn.Result.LastValue > 66 && dmsC > 11 && spread < 800 && noBuyMode == 0 && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 100000, "Buy1", SL, 200);
                noBuyMode = 1;

            }

            //-- 
            if (_ckn.Result.LastValue > 66 && dmsC < -11 && spread < 800 && noSellMode == 0 && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 100000, "Sell1", SL, 200);
                noSellMode = 1;

            }

        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == "Buy1")
            {
                BUY1pips = 0;
            }
            if (position.Label == "Sell1")
            {
                SELL1pips = 0;
            }
        }

        protected override void OnBar()
        {

            //-- positions
            //-- find positions
            var positionBUY1 = Positions.Find("Buy1");
            var positionSELL1 = Positions.Find("Sell1");


            if (noBuyMode > 0)
            {
                noBuyMode--;
            }

            if (noSellMode > 0)
            {
                noSellMode--;
            }

            //-- buy && sell periods
            if (positionBUY1 != null)
            {
                buyPeriods++;

            }

            if (positionSELL1 != null)
            {
                sellPeriods++;

            }
        }
    }

}

 

So basically to put this short: removing the rbwT makes the SELL position opened up later and so this somehow makes the Take Profit to work as expected.  This is a weird behavour, how come...

My upfront gratitude to anyone who can clarifty this thing for me, thanks! :)


@irmscher9