Optimization bug

Created at 19 Feb 2018, 16: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!
irmscher9's avatar

irmscher9

Joined 22.12.2016

Optimization bug
19 Feb 2018, 16:44


Hey

I'm using the following method to make sure cBot doesn't open multiple positions:

var positionBUY1 = Positions.Find("Buy1");
            var positionSELL1 = Positions.Find("Sell1");

            //----- 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;
         

            //-- && HeikenC > -17 
            if (dmsC > 7 && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 100000, "Buy1", 60, 90);  
            }

            //-- && HeikenC < 17
            if (dmsC < -7 && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 100000, "Sell1", 60, 90);
            }

However, sometimes when I running Optimization, I'm getting something like this:

Plenty of similar positions opened simutaneously. Tried backtesting the same bot with same parameters, nothing like that happened. Am I doing something wrong or is it a bug?


@irmscher9
Replies

PanagiotisCharalampous
20 Feb 2018, 12:09

Hi irmscher9,

I tried to reprocude with the given code but could not. Can you post the complete cBot as well as backtesting parameters so that we can reproduce it as well?

Best Regards,

Panagiotis


@PanagiotisCharalampous

irmscher9
20 Feb 2018, 12:22 ( Updated at: 21 Dec 2023, 09:20 )

Sure!

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; }

        [Parameter("Take Profit", DefaultValue = 90, MinValue = 60, Step = 5, MaxValue = 120)]
        public int TP { get; set; }

        [Parameter("dmsC", DefaultValue = 4, MinValue = 3, Step = 1, MaxValue = 15)]
        public int DMSC { get; set; }

        [Parameter("Chaikin Value", DefaultValue = 100, MinValue = 25, Step = 1, MaxValue = 125)]
        public int cknValue { get; set; }

        [Parameter("Donchain Periods", DefaultValue = 20, MinValue = 10, Step = 1, MaxValue = 30)]
        public int dncPeriods { get; set; }


        public DonchianChannel _dnc;

        public DirectionalMovementSystem _dms;

        public ChaikinVolatility _ckn;

        public CommodityChannelIndex _cci;

        public RelativeStrengthIndex _rsi;

        public MacdHistogram _mcd;

        public ExponentialMovingAverage _ema50;

        public int buyPeriods;
        public int sellPeriods;


        protected override void OnStart()
        {

            _dnc = Indicators.DonchianChannel(dncPeriods);

            _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);

        }

        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;

            bool rsi30_20 = _rsi.Result.Minimum(20) <= 30;
            bool rsi70_20 = _rsi.Result.Maximum(20) >= 70;

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


            //-- && HeikenC > -17 
            if (_ckn.Result.LastValue > cknValue && dmsC > DMSC && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 100000, "Buy1", 60, TP);
                //buyPeriods = 0;

            }

            //-- && HeikenC < 17
            if (_ckn.Result.LastValue > cknValue && dmsC < -DMSC && positionBUY1 == null && positionSELL1 == null)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 100000, "Sell1", 60, TP);
                //sellPeriods = 0;

            }

        }

        protected override void OnBar()
        {

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


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

            }

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

            }
        }
    }

}

Parameters:


@irmscher9

PanagiotisCharalampous
20 Feb 2018, 17:00

Hi irmscher9,

It seems there is an issue with historical data for the specific day. We will fix it and inform you. In the meanwhile, you can continue backtesting using Spotware cAlgo.

Best Regards,

Panagiotis


@PanagiotisCharalampous

irmscher9
20 Feb 2018, 17:16

Ok, thanks, a lot.


@irmscher9