Defining Maximum Spread

Created at 12 Apr 2019, 00:40
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!
MR

Mr4x

Joined 12.04.2019

Defining Maximum Spread
12 Apr 2019, 00:40


Hi guys,

I was wondering if somebody could point me in the right direction.

I have a cbot almost complete but there is one feature I haven't been able to figure out yet - defining a maximum spread.

Could somebody please give me an example, in the simplest way, of some code that would prevent a trade from executing if spread exceeds a certain number of pips? Can it be done with a simple "if" statement?

Many thanks


@Mr4x
Replies

2bnnp
12 Apr 2019, 19:43

        [Parameter("Max. spread in Pip", DefaultValue = 2, MinValue = 0, Step = 0.1)]
        public double MaxSpread { get; set; }


        #region spread logic

        /// <summary>
        /// returns true if spread is smaller than max spread
        /// </summary>
        /// <returns></returns>
        bool Spread()
        {
            double spread = Symbol.Spread;
            double size = Symbol.PipSize;
            double maxSpreadPip = MaxSpread * size;
            if (spread < maxSpreadPip)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

add this in the main class, you can then use it like so: 

protected override void OnBar()
        {
            if (TimeWindow() && Spread())
            {
                ManagePositions();
            }
        }

 


@2bnnp