Historical Spread

Created at 25 Nov 2013, 23:33
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!
bukk530's avatar

bukk530

Joined 03.11.2013

Historical Spread
25 Nov 2013, 23:33


Is there any way we can get the historical spread in our algos or in backtest?


@bukk530
Replies

Spotware
27 Nov 2013, 16:51

Unfortunately, it is not possible. 


@Spotware

firemyst
05 Jan 2020, 07:58

@Panagiotis / Spotware:

Does this answer still hold true?

I want to retrieve historical spread data in indicator and bot code, and I can't seem to find a way to get historical spread values, or (at the very least) historical bid/ask prices on each historical candle close

Is there a way to do this?

Thank you.


@firemyst

PanagiotisCharalampous
07 Jan 2020, 09:24

RE:

firemyst said:

@Panagiotis / Spotware:

Does this answer still hold true?

I want to retrieve historical spread data in indicator and bot code, and I can't seem to find a way to get historical spread values, or (at the very least) historical bid/ask prices on each historical candle close

Is there a way to do this?

Thank you.

Hi firemyst,

To find the historical spread just check the difference the historical bid/ask prices when running backtesting using tick data.

Best Regards,

Panagiotis 

Join us on Telegram

 

 


@PanagiotisCharalampous

firemyst
07 Jan 2020, 10:59

RE: RE:

PanagiotisCharalampous said:

firemyst said:

@Panagiotis / Spotware:

Does this answer still hold true?

I want to retrieve historical spread data in indicator and bot code, and I can't seem to find a way to get historical spread values, or (at the very least) historical bid/ask prices on each historical candle close

Is there a way to do this?

Thank you.

Hi firemyst,

To find the historical spread just check the difference the historical bid/ask prices when running backtesting using tick data.

Best Regards,

Panagiotis 

Join us on Telegram

 

 

Sorry for the confusion -- I meant is it possible to get this data programmatically? Eg, in cbot code what can I do to get the historical data in real time if it's possible? For example, if I'm on the H1 timeframe, and currently at the 11am candle, if I want to obtain the spread in the cbot code at the close of the 8am candle, how would I do it?

Thank you.


@firemyst

PanagiotisCharalampous
07 Jan 2020, 11:07

Hi firemyst,

The spread value itself is not saved somewhere to be retrieved in a straight forward way. You will need to calculate and save the value yourself using the available tick data.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

YesOrNot2
20 Aug 2024, 02:14

11 Years later… ^^

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SpreadPressure : Indicator
    {
        private Ticks tick;
        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
        }
        public override void Calculate(int index)
        {
            SpredResult[index] = (tick.Last(0).Ask - tick.Last(0).Bid) / Symbol.PipSize;
        }
    }
}

@YesOrNot2

YesOrNot2
20 Aug 2024, 02:39

Spread V2

Better this : 
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class SpreadPressure : Indicator
    {
        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries SpredResult { get; set; }

        private Ticks tick;
        private Bars barstick;

        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
            barstick = MarketData.GetBars(TimeFrame.Tick);

            while (barstick.OpenTimes[0] > Bars.OpenTimes[0])
                barstick.LoadMoreHistory();
        }

        public override void Calculate(int index)
        {
            var indexTf = GetIndexByDate(Bars.OpenTimes.Last(0));
            SpredResult[index] = (tick[indexTf].Ask - tick[indexTf].Bid) / Symbol.PipSize;
        }

        private int GetIndexByDate(DateTime date)
        {
            var bars = MarketData.GetBars(TimeFrame.Tick);
            for (var i = bars.OpenTimes.Count - 1; i >= 0; i--)
            {
                if (bars.OpenTimes[i] <= date)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}

@YesOrNot2

PanagiotisCharalampous
20 Aug 2024, 05:01

RE: Spread V2

YesOrNot2 said: 

Better this : 
 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using cAlgo.API;using cAlgo.API.Collections;using cAlgo.API.Indicators;using cAlgo.API.Internals;namespace cAlgo{    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]    public class SpreadPressure : Indicator    {        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]        public IndicatorDataSeries SpredResult { get; set; }        private Ticks tick;        private Bars barstick;        protected override void Initialize()        {            tick = MarketData.GetTicks();            barstick = MarketData.GetBars(TimeFrame.Tick);            while (barstick.OpenTimes[0] > Bars.OpenTimes[0])                barstick.LoadMoreHistory();        }        public override void Calculate(int index)        {            var indexTf = GetIndexByDate(Bars.OpenTimes.Last(0));            SpredResult[index] = (tick[indexTf].Ask - tick[indexTf].Bid) / Symbol.PipSize;        }        private int GetIndexByDate(DateTime date)        {            var bars = MarketData.GetBars(TimeFrame.Tick);            for (var i = bars.OpenTimes.Count - 1; i >= 0; i--)            {                if (bars.OpenTimes[i] <= date)                {                    return i;                }            }            return -1;        }    }}

You can also use Symbol.Spread


@PanagiotisCharalampous