Simple code needs streamlining

Created at 10 Aug 2016, 19:34
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!
JU

Juddly01

Joined 28.12.2014

Simple code needs streamlining
10 Aug 2016, 19:34


Here we have a nice little sample bot that works well with many different currency pairs and timeframes. The idea was to create a moving average system that would change int parameter, with no user input, according to market conditions.

The system shows potential and has returned a green account balance over the full available backtest, but my coding ability is still in its infancy and even though the idea is sound the code is ugly. This results in a very big strain being placed on the CPU.

If you like the basic concept and current results I would appreciate some help to clean it up and reduce the demand being placed on the CPU. I intend to use this as a sample to build more advance strategies.

A 1 month backtest from today's date on the EURUSD 1M will return a 5.91 profit factor with P1 set to 882.

 

Thank you.

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 MAEvolution : Robot
    {
        [Parameter(DefaultValue = 200)]
        public int P1 { get; set; }



        private int M2;

        private int P2;


        protected override void OnStart()
        {
            P2 = P1;
            M2 = P2;
        }

        protected override void OnBar()
        {


            if (Account.Balance < Account.Equity)
            {
                M2 = P2;
            }

            if (Account.Balance > Account.Equity)
            {
                if (Positions.Find("evobuy") != null && MarketSeries.Median.IsRising())
                {

                    P2 = P2 + 1;
                    M2 = P2;

                }

                if (Positions.Find("evobuy") != null && MarketSeries.Median.IsFalling())
                {

                    P2 = P2 - 1;
                    M2 = P2;
                }

                if (Positions.Find("evosell") != null && MarketSeries.Median.IsFalling())
                {

                    P2 = P2 + 1;
                    M2 = P2;

                }

                if (Positions.Find("evosell") != null && MarketSeries.Median.IsRising())
                {

                    P2 = P2 - 1;
                    M2 = P2;
                }

            }


            if (Positions.Count <= 1 && Indicators.TriangularMovingAverage(MarketSeries.Close, M2).Result.IsFalling())
            {
                if (Positions.Find("evosell") != null)
                {
                    ClosePosition(Positions.Find("evosell"));
                }
                if (Positions.Count == 0)
                    ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "evobuy");
                Print(M2);
            }

            if (Positions.Count <= 1 && Indicators.TriangularMovingAverage(MarketSeries.Close, M2).Result.IsRising())
            {


                if (Positions.Find("evobuy") != null)
                {
                    ClosePosition(Positions.Find("evobuy"));
                }
                if (Positions.Count == 0)
                    ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "evosell");
                Print(M2);
            }



        }


        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 


@Juddly01