Replies

missyeam
14 Jan 2019, 17:45

perfect now...thanks!


@missyeam

missyeam
14 Jan 2019, 16:49 ( Updated at: 21 Dec 2023, 09:21 )

RE: so after 16k, the next volume should be 10k, not 17k

missyeam said:



 


@missyeam

missyeam
14 Jan 2019, 16:48 ( Updated at: 21 Dec 2023, 09:21 )




@missyeam

missyeam
14 Jan 2019, 16:37 ( Updated at: 21 Dec 2023, 09:21 )


@missyeam

missyeam
14 Jan 2019, 16:03

Thanks @ Panagiotis...i backtested this..it has a little problem...it creates a new position extra...eg. if gross profit is >0 @ 11k, it starts the next trade @ 12k, not from 1k (starting volume)

 

eg. 1k....gross profit= -0.5

      2k...gross profit = -0.76

      3k.. gross profit =  1.2

      4k! (not 1k here)....but since the third trade was successful...i wanted the 4th trade to start @ 1k

 

Thanks


@missyeam

missyeam
14 Jan 2019, 15:14

so this is my ma cross over cbot code...i now want to add 1k units everytime the gross profit from previous close is < 0

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 ClickAlgoSchoolSMA : Robot
    {
        #region User defined parameters

        [Parameter("Instance Name", DefaultValue = "001")]
        public string InstanceName { get; set; }

        [Parameter("Lot Size", DefaultValue = 0.1)]
        public double LotSize { get; set; }

        [Parameter("Source SMA #1")]
        public DataSeries SourceSma1 { get; set; }

        [Parameter("Source SMA #2")]
        public DataSeries SourceSma2 { get; set; }

        [Parameter("Period SMA #1", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma1 { get; set; }

        [Parameter("Period SMA #2", DefaultValue = 20, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma2 { get; set; }

        [Parameter("Calculate OnBar", DefaultValue = false)]
        public bool CalculateOnBar { get; set; }

        #endregion

        #region Indicator declarations

        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }

        #endregion

        #region cTrader events


        protected override void OnStart()
        {
            // construct the indicators
            _sma1 = Indicators.SimpleMovingAverage(SourceSma1, PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(SourceSma2, PeriodsSma2);
        }


        protected override void OnTick()
        {
            if (CalculateOnBar)
            {
                return;
            }

            ManagePositions();
        }


        protected override void OnBar()
        {
            if (!CalculateOnBar)
            {
                return;
            }

            ManagePositions();
        }


        protected override void OnStop()
        {
            // unused
        }

        #endregion

        #region Position management

        private void ManagePositions()
        {
            if (_sma1.Result.LastValue > _sma2.Result.LastValue)
            {

                if (!IsPositionOpenByType(TradeType.Buy))
                {
                    OpenPosition(TradeType.Buy);
                }

                ClosePosition(TradeType.Sell);
            }


            if (_sma1.Result.LastValue < _sma2.Result.LastValue)
            {

                if (!IsPositionOpenByType(TradeType.Sell))
                {
                    OpenPosition(TradeType.Sell);
                }

                ClosePosition(TradeType.Buy);
            }
        }


        private void OpenPosition(TradeType type)
        {

            long volume = Symbol.QuantityToVolume(LotSize);

            // open a new position
            ExecuteMarketOrder(type, this.Symbol, volume, InstanceName, nullnull);
        }


        private void ClosePosition(TradeType type)
        {
            var p = Positions.Find(InstanceName, this.Symbol, type);

            if (p != null)
            {
                ClosePosition(p);
            }
        }

        #endregion

        #region Position Information


        private bool IsPositionOpenByType(TradeType type)
        {
            var p = Positions.FindAll(InstanceName, Symbol, type);

            if (p.Count() >= 1)
            {
                return true;
            }

            return false;
        }

        #endregion
    }
}
 


@missyeam