How I save my optimsation runs

Created at 14 Jul 2022, 07:49
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!
TR

triplelorenz

Joined 05.01.2022

How I save my optimsation runs
14 Jul 2022, 07:49


Hi all,

I am always asking for help, so thought I might be able to help someone else for a change. I have been doing some work on improving my use of optimsation runs and apart of that is saving them and analysing etc etc.

so this is how I do it. Please let me know if you have any questions etc, it gives good output but there is some mucking around to get it.

First I installed the cTrader CSV Data Export Tool from ClickAlgo. www.clickalgo.com/data-export-tool - do all the things need to get it to work etc etc.

Then I have put code into the "onstop()" part of my cbot to fetch all the information that I want. This way each time an optimsation run is completed, it sends that information to the spreadsheet.

For the " TradeLogger.Info(string.Concat(InstanceName.ToString(), etc " really long part I used a backtest run and then saved the parameters to a txt file. From there I put it into excel and use a  CONCATENATEMULTIPLE function which I found (www.trumpexcel.com/concatenate-excel-ranges/) to make the list work.

If you send information to individual TradeLogger.Info it seems to get a bit mixed up and I couldnt ganurantee if all the parameters were from the same run, hence the giant line of code to get them all into one big string, then I separate them through the use of CSV. It is still a work in prgoress, so please let me know of any improvements. Currently I am working on system for forward testing and comparing them using some of the techniques from Darwinex's videos on youtube.

Here are the sections from my code.

//// parameters bit

        [Parameter("Show Log FIle", Group = "Trade Logger", DefaultValue = false)]
        public bool ShowLogFile { get; set; }

        [Parameter("Enable Data Logger", Group = "Trade Logger", DefaultValue = false)]
        public bool enableDataLogger { get; set; }

 

/// indicator declaration 

        // Trade logger stuff
        private double peak;
        private List<double> drawdown = new List<double>();
        private List<string> individualtrades = new List<string>();
        private double maxDrawdown;

 

//// onstart section

            //trade logger stuff
            Positions.Closed += PositionsOnClosed;

            TradeLogger.SetLogDir("c:\\Users\\ianlo\\OneDrive\\Documents\\cAlgo\\Backtesting");
            TradeLogger.Extension = "csv";

//// on stop section

 protected override void OnStop()
        {
            if (enableDataLogger)
            {
                double calcs = Account.Balance;
                double maxDD = GetMaxDrawDown();
                int totaltrades = History.Count;
                double winningtrades = winningtradesamount();
                double losingtrades = totaltrades - winningtrades;
                double profitfromwinning = winningtradestotal();
                double Lossfromlosing = losingtradestotal();
                //double profitfactor = (profitfromwinning / winningtrades) / (Lossfromlosing / losingtrades);
                double profitfactor = ((winningtrades / totaltrades) * (profitfromwinning / winningtrades)) / ((losingtrades / totaltrades) * Math.Abs((Lossfromlosing / losingtrades)));


                //int[] InvTrades = Equitycurve(ref cal).ToArray();
                //string InvTradesString = string.Join(".", InvTrades);


                TradeLogger.Info(string.Concat(InstanceName.ToString(), ", ", Volumetype.ToString(), ", ", PerTrade.ToString(), ", ", PeriodsST1.ToString(), ", ", MutliplierST1.ToString(), ", ", PeriodsST2.ToString(), ", ", MutliplierST2.ToString(), ", ", PeriodsST3.ToString(), ", ", MutliplierST3.ToString(), ", ", SLATR.ToString(), ", ", Profitratio.ToString(), ", ", PeriodsRSI.ToString(), ", ", RSIOverSold.ToString(), ", ", RSIOverBought.ToString(), ", ", enableChaikinfilter.ToString(), ", ", enableADXfilter.ToString(), ", ", ADXlow.ToString(), ", ", IncludeBreakEven.ToString(), ", ", BreakEvenPips.ToString(), ", ", BreakEvenExtraPips.ToString(), ", ", Profitratio.ToString(), ", ", calcs.ToString(), ", ", totaltrades.ToString(), ", ", winningtrades.ToString(), ", ", losingtrades.ToString(), ", ", profitfromwinning.ToString(), ", ", Lossfromlosing.ToString(), ", ", maxDD.ToString(), ", ", profitfactor.ToString()));
                //TradeLogger.Info("Individual Trades " + InvTradesString);




            }
        }

 

////// private subs and PositiononClosed section ( I am still working on to make it better etc...)

 

        #region Trade Logger and Get Fitness

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.Label != InstanceName) return;

            Print("Position labeled {0} has closed", position.Label);

            GetMaxDrawDown();
            var stringnetprofit = position.NetProfit;
            Equitycurve(ref stringnetprofit);

        }


        private List<string> Equitycurve(ref double netprofit)
        {


            {
                //List<string> individualtrades = new List<string>(); - add this back at the start
                var stringnetprofit = netprofit.ToString();

                individualtrades.Add(stringnetprofit);

                return individualtrades;



                //string[] InvTrades = individualtrades.ToArray();

                //return InvTrades[];

                // You can convert it back to an array if you would like to
                //int[] terms = termsList.ToArray();
                //string res = string.Join(".", str);
            }

        }







        private double GetMaxDrawDown()
        {
            peak = Math.Max(peak, Account.Balance);

            drawdown.Add((peak - Account.Balance) / peak * 100);
            drawdown.Sort();


            return maxDrawdown = drawdown[drawdown.Count - 1];


        }

        private double winningtradesamount()
        {
            double totalprofit = 0;
            double gainedPips = 0;
            int winningTrades = 0;
            foreach (HistoricalTrade trade in History)
            {
                if (trade.Label == InstanceName)
                {
                    totalprofit += trade.NetProfit;
                    gainedPips += trade.Pips;

                    if (trade.NetProfit > 0)
                        winningTrades++;
                }
            }
            return winningTrades;
        }

        private double winningtradestotal()
        {
            double totalprofit = 0;

            foreach (HistoricalTrade trade in History)
            {
                if (trade.Label == InstanceName)
                {
                    if (trade.NetProfit > 0)
                        totalprofit += trade.NetProfit;
                }
            }
            return totalprofit;
        }

        private double losingtradestotal()
        {
            double totalprofit = 0;

            foreach (HistoricalTrade trade in History)
            {
                if (trade.Label == InstanceName)
                {
                    if (trade.NetProfit < 0)
                        totalprofit += trade.NetProfit;
                }
            }
            return totalprofit;
        }



        private double _fit_winLossRatio(GetFitnessArgs args)
        {

            double calcs = Account.Balance;

            return args.WinningTrades / (args.WinningTrades + args.LosingTrades + 1);
        }

        #endregion


@triplelorenz